home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-30 | 5.5 KB | 241 lines | [TEXT/CWIE] |
- /*
- property is © 1996 Boxes Objects Links Design Pty Ltd. All Rights Reserved.
- You use this software at your own risk, etc. Permission is given to Timothy C.
- Delaney to use property. Permission is given for all others to use property if
- acknowledgement of this copyright is given in publically-released software.
- Acknowledgement should consist of a statement equivalent to "Sections of this
- program are © Boxes Objects Links Design Pty Ltd", visible in an "About..." box
- or splash screen.
- */
-
- #include "property.h"
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- T &field )
- : mObject(theObject)
- {
- mGetAccess = internalAccess_None;
- mSetAccess = internalAccess_None;
-
- mGet.mGetField = NULL;
- mSet.mSetField = NULL;
-
- if (access == propertyAccess_ReadOnly)
- {
- mGetAccess = internalAccess_Field;
- mGet.mGetField = &field;
- }
- else if (access == propertyAccess_WriteOnly)
- {
- mSetAccess = internalAccess_Field;
- mSet.mSetField = &field;
- }
-
- MatchAccess_();
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- void (*getFunc) (void * const, T & ) )
- : mObject(theObject),
- mGetAccess(internalAccess_Func), mSetAccess(internalAccess_None)
- {
- MatchAccess_();
-
- mGet.mGetFunc = getFunc;
- mSet.mSetField = NULL;
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- void (*setFunc) (void * const, const T & ) )
- : mObject(theObject),
- mGetAccess(internalAccess_None), mSetAccess(internalAccess_Func)
- {
- MatchAccess_();
-
- mGet.mGetField = NULL;
- mSet.mSetFunc = setFunc;
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- T &getField,
- T &setField )
- : mObject(theObject),
- mGetAccess(internalAccess_Field), mSetAccess(internalAccess_Field)
- {
- MatchAccess_();
-
- mGet.mGetField = &getField;
- mSet.mSetField = &setField;
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- void (*getFunc) (void * const, T & ),
- T &setField )
- : mObject(theObject),
- mGetAccess(internalAccess_Func), mSetAccess(internalAccess_Field)
- {
- MatchAccess_();
-
- mGet.mGetFunc = getFunc;
- mSet.mSetField = &setField;
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- T &getField,
- void (*setFunc) (void * const, const T&) )
- : mObject(theObject),
- mGetAccess(internalAccess_Field), mSetAccess(internalAccess_Func)
- {
- MatchAccess_();
-
- mGet.mGetField = &getField;
- mSet.mSetFunc = setFunc;
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::property (void * const theObject,
- void (*getFunc) (void * const, T&),
- void (*setFunc) (void * const, const T&) )
- : mObject(theObject),
- mGetAccess(internalAccess_Func), mSetAccess(internalAccess_Func)
- {
- MatchAccess_();
-
- mGet.mGetFunc = getFunc;
- mSet.mSetFunc = setFunc;
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::~property (void )
- {
- }
-
- template <EPropertyAccess access, class T>
- inline bool property<access, T>::Readable (void )
- {
- return mGetAccess != internalAccess_None;
- }
-
- template <EPropertyAccess access, class T>
- inline bool property<access, T>::Writeable (void )
- {
- return mSetAccess != internalAccess_None;
- }
-
- template <EPropertyAccess access, class T>
- inline bool property<access, T>::ReadFunc (void )
- {
- return mGetAccess == internalAccess_Func;
- }
-
- template <EPropertyAccess access, class T>
- inline bool property<access, T>::WriteFunc (void )
- {
- return mSetAccess == internalAccess_Func;
- }
-
- template <EPropertyAccess access, class T>
- inline void property<access, T>::ThrowAccess_ (void ) throw (EPropertyAccess)
- {
- throw access;
- }
-
- template <EPropertyAccess access, class T>
- inline void property<access, T>::MatchAccess_ (void ) throw (EPropertyAccess)
- {
- if (access == propertyAccess_ReadWrite && (!Readable() || !Writeable()))
- {
- ThrowAccess_();
- }
- else if (access == propertyAccess_ReadOnly && Writeable())
- {
- ThrowAccess_();
- }
- else if (access == propertyAccess_WriteOnly && Readable())
- {
- ThrowAccess_();
- }
- }
-
- template <EPropertyAccess access, class T>
- property<access, T>::operator T (void )
- {
- if (Readable())
- {
- if (ReadFunc())
- {
- T tempField;
-
- mGet.mGetFunc(mObject, tempField);
- return tempField;
- }
- else
- {
- return *mGet.mGetField;
- }
- }
- else
- {
- ThrowAccess_();
- return *mGet.mGetField; // shut up warning about missing return value
- }
- }
-
- template <EPropertyAccess access, class T>
- property & property<access, T>::operator= (const T &newValue )
- {
- if (Writeable())
- {
- if (WriteFunc())
- {
- mSet.mSetFunc(mObject, newValue);
- }
- else
- {
- *mSet.mSetField = newValue;
- }
- }
- else
- {
- ThrowAccess_();
- }
-
- return *this;
- }
-
- template <EPropertyAccess access, class T>
- property & property<access, T>::operator= (property<access, T> newProperty )
- {
- return (*this = (T) newProperty);
- }
-
- template <EPropertyAccess access, class T>
- property & property<access, T>::operator+= (const T &newValue )
- {
- return (*this = *this + newValue);
- }
-
- template <EPropertyAccess access, class T>
- property & property<access, T>::operator-= (const T &newValue )
- {
- return (*this = *this - newValue);
- }
-
- template <EPropertyAccess access, class T>
- property & property<access, T>::operator*= (const T &newValue )
- {
- return (*this = *this * newValue);
- }
-
- template <EPropertyAccess access, class T>
- property & property<access, T>::operator/= (const T &newValue )
- {
- return (*this = *this / newValue);
- }
-