首页 > 其他 > 详细

反射(二)

时间:2016-07-08 00:04:49      阅读:312      评论:0      收藏:0      [点我收藏+]

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

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

2.使用

 1         ClassWithProperty ct;
 2         ct.DataMember["blValue"] = "true";
 3         ct.DataMember["objClassValue"]["intValue"] = "12";
 4         Object& subObj = ct.DataMember["objClassValue"];
 5         subObj.DataMember["intValue"] = 13.5f;
 6         s32 rf = ct.DataMember["objClassValue"]["intValue"];
 7         AString desc = ct.DataMember["objClassValue"]["intValue"];
 8         f32 rfu8 = ct.DataMember["objClassValue"]["intValue"];
 9         f64 rfud = ct.DataMember["objClassValue"]["intValue"];
10 
11         FuncProperty fp = ct.getProperty().getFuncProperty("ValString");
12         AString s = fp.invoke<AString, AString>(&ct,"value");
13         LOG_I("ValString ret:%s", s.getRawString());
14 
15         fp = ct.getProperty().getFuncProperty("RefString");
16         AString& rs = fp.invoke<AString&>(&ct, &AString("reff"));
17         LOG_I("RefString ret:%s", rs.getRawString());
18 
19         fp = ct.getProperty().getFuncProperty("PtrString");
20         AString* ps = fp.invoke<AString*, AString*>(&ct, &AString("pointer"));
21         LOG_I("PtrString ret:%s", ps->getRawString());

 

反射(二)

原文:http://www.cnblogs.com/goooon/p/5651975.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!