反射(二)

通过上一节的例子,在反射里加入数据成员,属性等,实现更加复杂的对象。

1.实现

  1 #define DATAPROP(n) {&self::n,#n,0}
  2 #define ATTRPROP(n) {&self::get##n,&self::set##n,#n,0}
  3 #define FUNCPROP(n) {&self::n,#n,0}
  4     class ObjectClass : public Object
  5     {
  6         DECL_RTTI(ObjectClass, Object);
  7         DECL_TPLT(ObjectClass);
  8     private:
  9         static  const ClassProperty& GetPropertys()
 10         {
 11             const static ClassProperty::BaseProperty bp[] = { &base::GetPropertys,vNull };
 12             const static DataProperty dp[] =
 13             {
 14                 DATAPROP(intValue),
 15                 DATAPROP(blValue),
 16                 DATAPROP(sintValue),
 17                 DATAPROP(floatValue),
 18                 DataProperty()
 19             };
 20             const static AttrProperty ap[] = 
 21             { 
 22                 ATTRPROP(Int),
 23                 AttrProperty()
 24             };
 25             const static FuncProperty fp[] = { FuncProperty() };
 26             const static ClassProperty prop(bp, dp, ap, fp);
 27             return prop;
 28         }
 29     private:
 30         int  getInt()const { return intValue; }
 31         void setInt(int i) { intValue = i; }
 32     public:
 33         int intValue;
 34         bool blValue;
 35         sint sintValue;
 36         f32  floatValue;
 37     };
 38     class ClassWithProperty : public Object
 39     {
 40         DECL_RTTI(ClassWithProperty, Object);
 41         DECL_TPLT(ClassWithProperty);
 42     public:
 43         static  const ClassProperty& GetPropertys()
 44         {
 45             const static ClassProperty::BaseProperty bp[] = { &base::GetPropertys,vNull };
 46             const static DataProperty dp[] = 
 47             { 
 48                 DATAPROP(rawString),
 49                 DATAPROP(intValue),
 50                 DATAPROP(blValue),
 51                 DATAPROP(sintValue),
 52                 DATAPROP(floatValue),
 53                 DATAPROP(strValue),
 54                 DATAPROP(objClassPointer),
 55                 DATAPROP(objClassValue),
 56                 DataProperty() 
 57             };
 58             const static AttrProperty ap[] = 
 59             {
 60                 ATTRPROP(Int),
 61                 ATTRPROP(Bool),
 62                 ATTRPROP(Float),
 63                 ATTRPROP(String),
 64                 AttrProperty() 
 65             };
 66             const static FuncProperty fp[] = 
 67             { 
 68                 {&self::getValString,"ValString",0},
 69                 {&self::getPtrString,"PtrString",0},
 70                 {&self::getRefString,"RefString",0},
 71                 FuncProperty() 
 72             };
 73             const static ClassProperty prop(bp,dp,ap,fp);
 74             return prop;
 75         }
 76     public: void displayProperty(int depth,const ClassProperty* props)
 77     {
 78         if (props == nullptr)return;
 79             const DataProperty* dp = props->getDataProperty();
 80             while (dp->type != Type::vNone)
 81             {
 82                 int n = depth;
 83                 while (n-- >= 0)LOG_P("  ");
 84                 LOG_P("%s %s\n", dp->type.getName(),dp->desc);
 85                 const Type currType = dp->type;
 86                 if (currType != Type::vNone)
 87                 {
 88                     displayProperty(depth + 1, currType.getPropertys());
 89                 }
 90                 const Type* baseTypes = dp->type.getBaseTypes();
 91                 while (*baseTypes != vNull)
 92                 {
 93                     displayProperty(depth + 1,baseTypes->getPropertys());
 94                     baseTypes++;
 95                 }
 96                 dp++;
 97             }
 98 
 99             const AttrProperty* ap = props->getAttrProperty();
100             while (ap->type != Type::vNone)
101             {
102                 int n = depth;
103                 while (n-- >= 0)LOG_P(" ");
104                 LOG_P("%s %s\n", ap->type.getName(), ap->desc);
105                 const Type* baseTypes = ap->type.getBaseTypes();
106                 ap++;
107             }
108 
109             const FuncProperty* fp = props->getFuncProperty();
110             while (fp->type != Type::vNone) 
111             {
112                 int n = depth;
113                 while (n-- >= 0)LOG_P(" ");
114                 LOG_P("%s %s\n", fp->type.getName(), fp->desc);
115                 const Type* baseTypes = fp->type.getBaseTypes();
116                 fp++;
117             }
118         }
119     public:
120         int  getInt()const { return intValue; }
121         void setInt(int i) { intValue = i; }
122         bool getBool()const { return blValue; }
123         void setBool(bool b) { blValue = b; }
124         sint getLong()const { return sintValue; }
125         void setLong(sint l) { sintValue = l; }
126         f32  getFloat()const { return floatValue; }
127         void setFloat(f32 f) { floatValue = f; }
128         void setString(AString s) { strValue = s; }
129         AString getString()const { return strValue; }
130     public:
131         const AString& getRefString(const AString& i)
132         {
133             LOG_P("RefString:%s\n", i.getRawString());
134             static AString as = AtomString("atomString"); 
135             return as;
136         }
137         const AString  getValString(const AString  i)
138         {
139             LOG_P("ValString:%s\n", i.getRawString());
140             return AtomString("atomString"); 
141         }
142         const AString* getPtrString(const AString* i)
143         { 
144             LOG_P("PtrString:%s\n", i->getRawString());
145             static AString as = AtomString("atomString"); return &as; 
146         }
147     private:
148         char* rawString;
149         AString strValue;
150         int intValue;
151         bool blValue;
152         sint sintValue;
153         f32  floatValue;
154         ObjectClass* objClassPointer;
155         ObjectClass  objClassValue;
156     };

 

 

2.使用

 1         ClassWithProperty ct;
 2         ct.displayProperty(0, &ct.getProperty());
 3         ct.DataMember["blValue"] = "true";
 4         bool bl = ct.DataMember["blValue"];
 5         std::cout << bl << std::endl;
 6         ct.DataMember["objClassValue"]["intValue"] = "12";
 7         int rf = ct.DataMember["objClassValue"]["intValue"];
 8         std::cout << rf << std::endl;
 9         Object& subObj = ct.DataMember["objClassValue"];
10         subObj.DataMember["intValue"] = 13.5f;
11         unsigned int rf2 = ct.DataMember["objClassValue"]["intValue"];
12         std::cout << rf2 << std::endl;
13         u32 iv = ct.DataMember["objClassValue"]["intValue"];
14         std::cout << iv << std::endl;
15         float rfu8 = ct.DataMember["objClassValue"]["intValue"];
16         std::cout << rfu8 << std::endl;
17         double rfud = ct.DataMember["objClassValue"]["intValue"];
18         std::cout << rfud << std::endl;
19 
20         FuncProperty fp = ct.getProperty().getFuncProperty("ValString");
21         AString s = fp.invoke<AString, AString>(&ct,"value");
22         LOG_P("ValString ret:%s\n", s.getRawString());
23 
24         fp = ct.getProperty().getFuncProperty("RefString");
25         AString& rs = fp.invoke<AString&>(&ct, &AString("reff"));
26         LOG_P("RefString ret:%s\n", rs.getRawString());
27 
28         fp = ct.getProperty().getFuncProperty("PtrString");
29         AString* ps = fp.invoke<AString*, AString*>(&ct, &AString("pointer"));
30         LOG_P("PtrString ret:%s\n", ps->getRawString());

3.结果

   

 

posted @ 2016-07-07 23:45  goooon  阅读(146)  评论(0编辑  收藏  举报