1*4882a593Smuzhiyun //-- Property.hpp -- 2*4882a593Smuzhiyun 3*4882a593Smuzhiyun /*-------------------------------------------------------------------------- 4*4882a593Smuzhiyun Class Library 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun Copyrights Emad Barsoum (ebarsoum@msn.com) 2003. All rights reserved. 7*4882a593Smuzhiyun ________________________________________________________________ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun PROJECT : General 11*4882a593Smuzhiyun MODULE : property 12*4882a593Smuzhiyun FILENAME : Property.hpp 13*4882a593Smuzhiyun BUILD : 1 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun History of Modifications: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun Date(dd/mm/yyyy)Person Description 18*4882a593Smuzhiyun ---- ------ ----------- 19*4882a593Smuzhiyun 25/03/2003 Emad Barsoum Initial design and coding 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun CLASS NAME: property 22*4882a593Smuzhiyun VERSION: 1.0 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun DESCRIPTION: 25*4882a593Smuzhiyun This class try to simulate property for C++, using template technique. 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun LICENSE: 28*4882a593Smuzhiyun You are free to change or modify or redistribute the code, just keep the header. 29*4882a593Smuzhiyun And you can use this class in any application you want without any warranty. 30*4882a593Smuzhiyun */ 31*4882a593Smuzhiyun #include <assert.h> 32*4882a593Smuzhiyun #include <stdlib.h> 33*4882a593Smuzhiyun #if !defined INC_PROPERTY_HPP 34*4882a593Smuzhiyun #define INC_PROPERTY_HPP 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun #define READ_ONLY 1 37*4882a593Smuzhiyun #define WRITE_ONLY 2 38*4882a593Smuzhiyun #define READ_WRITE 3 39*4882a593Smuzhiyun 40*4882a593Smuzhiyun template<typename Container, typename ValueType, int nPropType> 41*4882a593Smuzhiyun class property 42*4882a593Smuzhiyun { 43*4882a593Smuzhiyun public: property()44*4882a593Smuzhiyun property() 45*4882a593Smuzhiyun { 46*4882a593Smuzhiyun m_cObject = NULL; 47*4882a593Smuzhiyun Set = NULL; 48*4882a593Smuzhiyun Get = NULL; 49*4882a593Smuzhiyun } 50*4882a593Smuzhiyun //-- This to set a pointer to the class that contain the property -- setContainer(Container * cObject)51*4882a593Smuzhiyun void setContainer(Container* cObject) 52*4882a593Smuzhiyun { 53*4882a593Smuzhiyun m_cObject = cObject; 54*4882a593Smuzhiyun } 55*4882a593Smuzhiyun //-- Set the set member function that will change the value -- setter(void (Container::* pSet)(ValueType value))56*4882a593Smuzhiyun void setter(void (Container::*pSet)(ValueType value)) 57*4882a593Smuzhiyun { 58*4882a593Smuzhiyun if((nPropType == WRITE_ONLY) || (nPropType == READ_WRITE)) 59*4882a593Smuzhiyun Set = pSet; 60*4882a593Smuzhiyun else 61*4882a593Smuzhiyun Set = NULL; 62*4882a593Smuzhiyun } 63*4882a593Smuzhiyun //-- Set the get member function that will retrieve the value -- getter(ValueType (Container::* pGet)())64*4882a593Smuzhiyun void getter(ValueType (Container::*pGet)()) 65*4882a593Smuzhiyun { 66*4882a593Smuzhiyun if((nPropType == READ_ONLY) || (nPropType == READ_WRITE)) 67*4882a593Smuzhiyun Get = pGet; 68*4882a593Smuzhiyun else 69*4882a593Smuzhiyun Get = NULL; 70*4882a593Smuzhiyun } 71*4882a593Smuzhiyun //-- Overload the '=' sign to set the value using the set member -- operator =(const ValueType & value)72*4882a593Smuzhiyun ValueType operator =(const ValueType& value) 73*4882a593Smuzhiyun { 74*4882a593Smuzhiyun assert(m_cObject != NULL); 75*4882a593Smuzhiyun assert(Set != NULL); 76*4882a593Smuzhiyun (m_cObject->*Set)(value); 77*4882a593Smuzhiyun return value; 78*4882a593Smuzhiyun } 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun //-- To make possible to cast the property class to the internal type -- operator ValueType()81*4882a593Smuzhiyun operator ValueType() 82*4882a593Smuzhiyun { 83*4882a593Smuzhiyun assert(m_cObject != NULL); 84*4882a593Smuzhiyun assert(Get != NULL); 85*4882a593Smuzhiyun return (m_cObject->*Get)(); 86*4882a593Smuzhiyun } 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun private: 89*4882a593Smuzhiyun Container* m_cObject;//-- Pointer to the module that contain the property -- 90*4882a593Smuzhiyun void (Container::*Set)(ValueType value);//-- Pointer to set member function -- 91*4882a593Smuzhiyun ValueType (Container::*Get)();//-- Pointer to get member function -- 92*4882a593Smuzhiyun }; 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun #endif