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