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