随笔-341  评论-2670  文章-0  trackbacks-0
    跟上一篇文章一样,一样的数据结构,但是添加了对数组、列表和映射的更多的支持。首先是代码,然后是序列化后的XML文件。

    这里的代码展示了如和向object inspector注册自己感兴趣的类和成员。至于成员的类型和容器里面的东西,会自动判断。
  1 #include "..\..\..\..\VL++\Library\Platform\VL_Console.h"
  2 #include "..\..\..\..\VL++\Library\Data\Inspector\VL_ObjectInspector.h"
  3 #include "..\..\..\..\VL++\Library\Data\Inspector\VL_ObjectStrconv.h"
  4 #include "..\..\..\..\VL++\Library\Data\Inspector\VL_ObjectXML.h"
  5 #include "..\..\..\..\VL++\Library\Data\VL_DataAccess.h"
  6 #include "..\..\..\..\VL++\Library\Data\VL_System.h"
  7 
  8 using namespace vl;
  9 using namespace vl::platform;
 10 using namespace vl::inspector;
 11 using namespace vl::stream;
 12 using namespace vl::system;
 13 
 14 class Animal : public VL_Base
 15 {
 16 public:
 17     VInt                Age;
 18     VInt                Weight;
 19 };
 20 
 21 class Mammalian : public Animal
 22 {
 23 public:
 24     VInt                FingerCount;
 25 };
 26 
 27 class Cat : public Mammalian
 28 {
 29 public:
 30     VInt                BodyLength;
 31     VInt                TailLength;
 32 
 33     Cat()
 34     {
 35         Age=10;
 36         Weight=100;
 37         FingerCount=18;
 38         BodyLength=50;
 39         TailLength=30;
 40     }
 41 };
 42 
 43 class Fish : public Animal
 44 {
 45 public:
 46     VDouble                MaxDepth;
 47     VInt                Length;
 48 
 49     Fish()
 50     {
 51         Age=20;
 52         Weight=200;
 53         MaxDepth=3000;
 54         Length=25;
 55     }
 56 };
 57 
 58 class Bird : public Animal
 59 {
 60 public:
 61     VBool                CanFly;
 62     VUnicodeString        Name;
 63 
 64     Bird()
 65     {
 66         Age=30;
 67         Weight=300;
 68         CanFly=true;
 69         Name=L"Jack";
 70     }
 71 };
 72 
 73 class Human : public Mammalian
 74 {
 75 public:
 76     VUnicodeString                                                Name;
 77     VL_AutoPtr<Animal>                                            MainPat;
 78     Bird                                                        MainTransportation;
 79     VL_AutoPtr<Animal>                                            OtherPats[4];
 80     Bird                                                        OtherTranspotations[3];
 81     VL_List<VL_AutoPtr<Animal> , false , Animal*>                OtherPatsList;
 82     VL_List<Bird , false>                                        OtherTranspotationsList;
 83     VL_ListedMap<VUnicodeString , VL_AutoPtr<Animal>>            PatMap;
 84     VL_MultiMap<VUnicodeString , VL_AutoPtr<Animal> , false , VUnicodeString , Animal*>    PatMultiMap;
 85 
 86     Human()
 87     {
 88         Age=16;
 89         Weight=2000;
 90         FingerCount=20;
 91         Name=L"囧Smith";
 92 
 93         Cat* cat=new Cat;
 94         cat->Age=5;
 95         cat->Weight=150;
 96         cat->BodyLength=40;
 97         cat->TailLength=20;
 98         MainPat=cat;
 99 
100         MainTransportation.Name=L"Red Dragon";
101         MainTransportation.Age=3000;
102 
103         OtherPats[0]=new Cat;
104         OtherPats[1]=new Fish;
105         OtherPats[2]=new Bird;
106 
107         for(VInt i=0;i<3;i++)
108         {
109             OtherTranspotations[i].Name=L"Blue Dragon "+VUnicodeString(i+1);
110             OtherTranspotations[i].Age=3000+i*100;
111         }
112 
113         OtherPatsList.Add(new Cat);
114         OtherPatsList.Add(new Fish);
115         OtherPatsList.Add(new Bird);
116 
117         for(VInt i=0;i<3;i++)
118         {
119             OtherTranspotationsList.Add(Bird());
120             OtherTranspotationsList[i].Name=L"Green Dragon "+VUnicodeString(i+1);
121             OtherTranspotationsList[i].Age=3000+i*10;
122         }
123 
124         PatMap.Add(L"Cat",new Cat);
125         PatMap.Add(L"Fish",new Fish);
126         PatMap.Add(L"Bird",new Bird);
127 
128         PatMultiMap.Add(L"A",new Cat);
129         PatMultiMap.Add(L"A",new Fish);
130         PatMultiMap.Add(L"B",new Bird);
131     }
132 };
133 
134 VL_BEGIN_INSPECTOR_DECLARATION
135 
136     VL_BEGIN_BASE_CLASS(Animal)
137         VL_ADD_CLASS_MEMBER(Age)
138         VL_ADD_CLASS_MEMBER(Weight)
139     VL_END_CLASS(Animal)
140 
141     VL_BEGIN_SUB_CLASS(Mammalian,Animal)
142         VL_ADD_CLASS_MEMBER(FingerCount)
143     VL_END_CLASS(Mammalian)
144 
145     VL_BEGIN_SUB_CLASS(Cat,Mammalian)
146         VL_ADD_CLASS_MEMBER(BodyLength)
147         VL_ADD_CLASS_MEMBER(TailLength)
148     VL_END_CLASS(Cat)
149 
150     VL_BEGIN_SUB_CLASS(Fish,Animal)
151         VL_ADD_CLASS_MEMBER(MaxDepth)
152         VL_ADD_CLASS_MEMBER(Length)
153     VL_END_CLASS(Fish)
154 
155     VL_BEGIN_SUB_CLASS(Bird,Animal)
156         VL_ADD_CLASS_MEMBER(CanFly)
157         VL_ADD_CLASS_MEMBER(Name)
158     VL_END_CLASS(Bird)
159 
160     VL_BEGIN_SUB_CLASS(Human,Mammalian)
161         VL_ADD_CLASS_MEMBER(Name)
162         VL_ADD_CLASS_MEMBER(MainPat)
163         VL_ADD_CLASS_MEMBER(MainTransportation)
164         VL_ADD_CLASS_MEMBER(OtherPats)
165         VL_ADD_CLASS_MEMBER(OtherTranspotations)
166         VL_ADD_CLASS_MEMBER(OtherPatsList)
167         VL_ADD_CLASS_MEMBER(OtherTranspotationsList)
168         VL_ADD_CLASS_MEMBER(PatMap)
169         VL_ADD_CLASS_MEMBER(PatMultiMap)
170     VL_END_CLASS(Human)
171 
172     VL_BEGIN_INSPECTOR_MANAGER(AnimalManager)
173         VL_BIND_INSPECTOR(Animal)
174         VL_BIND_INSPECTOR(Mammalian)
175         VL_BIND_INSPECTOR(Cat)
176         VL_BIND_INSPECTOR(Fish)
177         VL_BIND_INSPECTOR(Bird)
178         VL_BIND_INSPECTOR(Human)
179     VL_END_INSPECTOR_MANAGER
180 
181 VL_END_INSPECTOR_DECLARATION
182 
183 void PrintAnimal(VL_InspectorManager::Ptr Manager , Animal* aAnimal)
184 {
185     VL_InspectorConverterManager Converter;
186     Converter.AddNormalConverter(new VL_InspectorReader);
187 
188     VL_ObjectInspector::Ptr Inspector=Manager->GetInspector(VL_InspectorSelector<Animal>::GetID());
189     GetConsole()->WriteLine(Converter.FindToStringConverter(Inspector)->ConvertToString(Inspector,aAnimal));
190 }
191 
192 void PrintType(VL_InspectorManager::Ptr Manager , VL_ObjectType::Ptr Type)
193 {
194     VL_InspectorConverterManager Converter;
195     Converter.AddNormalConverter(new VL_InspectorReader);
196 
197     VL_ObjectInspector::Ptr Inspector=Manager->GetInspector(L"VL_ObjectType");
198     GetConsole()->WriteLine(Converter.FindToStringConverter(Inspector)->ConvertToString(Inspector,Type.Object()));
199 }
200 
201 void WriteHuman(VL_InspectorManager::Ptr Manager , Human& human , VUnicodeString FileName)
202 {
203     VL_XMLDocument XML;
204     XML.GetRootElement()->SetName(L"objectxml");
205     VL_ObjectXML XMLSerializer;
206     XMLSerializer.Write(XML.GetRootElement(),Manager->GetInspector(L"Human"),&human);
207 
208     VL_FileStream XMLFile(FileName,VL_FileStream::vomWrite);
209     XML.Save(&XMLFile,true);
210 }
211 
212 VBool ReadHuman(VL_InspectorManager::Ptr Manager , Human& human , VUnicodeString FileName)
213 {
214     VL_XMLDocument XML;
215     VL_FileStream XMLFile(FileName,VL_FileStream::vomRead);
216     XML.Load(&XMLFile);
217 
218     Human NewHuman;
219     VPointer Data=&NewHuman;
220     try
221     {
222         VL_ObjectXML XMLSerializer;
223         XMLSerializer.Read(Manager,XML.GetRootElement()->GetChildren()[0]->GetElement(),Data);
224         return true;
225     }
226     catch(const VL_XMLError& Error)
227     {
228         GetConsole()->WriteLine(Error.Message);
229         return false;
230     }
231 }
232 
233 void vlmain()
234 {
235     GetConsole()->SetTitle(L"Vczh Library++ 2.0 Type Information Test");
236     GetConsole()->SetTestMemoryLeaks(true);
237     GetConsole()->SetPauseOnExit(true);
238     VUnicodeString WorkPath=VFileName(GetConsole()->GetAppPath()).MakeAbsolute(L"..\\TestFile\\").GetStrW();
239 
240     VL_FileStream LogStream(WorkPath+L"Console.txt",VL_FileStream::vomWrite);
241     VL_StreamWriter LogWriter(vceUtf16,true,&LogStream);
242     GetConsole()->SetLogger(&LogWriter);
243 
244     VL_InspectorManager::Ptr Manager=VL_GET_INSPECTOR_MANAGER(AnimalManager);
245 
246     Cat cat;
247     Fish fish;
248     Bird bird;
249     Human human;
250     PrintAnimal(Manager,&cat);
251     PrintAnimal(Manager,&fish);
252     PrintAnimal(Manager,&bird);
253     PrintAnimal(Manager,&human);
254 
255     for(VInt i=0;i<Manager->GetInspectorCount();i++)
256     {
257         GetConsole()->Write(L"typeof("+Manager->GetInspector(i)->GetID()+L") = ");
258         PrintType(Manager,Manager->GetInspector(i)->GetType());
259     }
260 
261     GetConsole()->WriteLine(L"开始写入");
262     WriteHuman(Manager,human,WorkPath+L"Object.xml");
263     Human NewHuman;
264     GetConsole()->WriteLine(L"开始读取");
265     ReadHuman(Manager,NewHuman,WorkPath+L"Object.xml");
266     GetConsole()->WriteLine(L"开始再次写入");
267     WriteHuman(Manager,NewHuman,WorkPath+L"Object2.xml");
268 
269     VL_FileStream StreamA(WorkPath+L"Object.xml",VL_FileStream::vomRead);
270     VL_FileStream StreamB(WorkPath+L"Object2.xml",VL_FileStream::vomRead);
271     if(ReadText(&StreamA)==ReadText(&StreamB))
272     {
273         GetConsole()->WriteLine(L"序列化成功!");
274     }
275     else
276     {
277         GetConsole()->WriteLine(L"序列化前后不一致。");
278     }
279 }

    于是这里是序列化的结果:
  1 <?xml version="1.0" encoding="gb2312" standalone="no"?>
  2 <objectxml>
  3   <struct typeid="Human">
  4     <member name="MainPat">
  5       <struct typeid="Cat">
  6         <member name="BodyLength">
  7           <basic typeid="VInt32s" value="40"/>
  8         </member>
  9         <member name="TailLength">
 10           <basic typeid="VInt32s" value="20"/>
 11         </member>
 12         <base typeid="Mammalian">
 13           <member name="FingerCount">
 14             <basic typeid="VInt32s" value="18"/>
 15           </member>
 16           <base typeid="Animal">
 17             <member name="Age">
 18               <basic typeid="VInt32s" value="5"/>
 19             </member>
 20             <member name="Weight">
 21               <basic typeid="VInt32s" value="150"/>
 22             </member>
 23           </base>
 24         </base>
 25       </struct>
 26     </member>
 27     <member name="MainTransportation">
 28       <struct typeid="Bird">
 29         <member name="CanFly">
 30           <basic typeid="VBool" value="true"/>
 31         </member>
 32         <member name="Name">
 33           <basic typeid="VUnicodeString" value="Red Dragon"/>
 34         </member>
 35         <base typeid="Animal">
 36           <member name="Age">
 37             <basic typeid="VInt32s" value="3000"/>
 38           </member>
 39           <member name="Weight">
 40             <basic typeid="VInt32s" value="300"/>
 41           </member>
 42         </base>
 43       </struct>
 44     </member>
 45     <member name="Name">
 46       <basic typeid="VUnicodeString" value="囧Smith"/>
 47     </member>
 48     <member name="OtherPats">
 49       <list typeid="VL_AutoPtr&lt;Animal&gt;[4]">
 50         <struct typeid="Cat">
 51           <member name="BodyLength">
 52             <basic typeid="VInt32s" value="50"/>
 53           </member>
 54           <member name="TailLength">
 55             <basic typeid="VInt32s" value="30"/>
 56           </member>
 57           <base typeid="Mammalian">
 58             <member name="FingerCount">
 59               <basic typeid="VInt32s" value="18"/>
 60             </member>
 61             <base typeid="Animal">
 62               <member name="Age">
 63                 <basic typeid="VInt32s" value="10"/>
 64               </member>
 65               <member name="Weight">
 66                 <basic typeid="VInt32s" value="100"/>
 67               </member>
 68             </base>
 69           </base>
 70         </struct>
 71         <struct typeid="Fish">
 72           <member name="Length">
 73             <basic typeid="VInt32s" value="25"/>
 74           </member>
 75           <member name="MaxDepth">
 76             <basic typeid="VDouble" value="3000.0"/>
 77           </member>
 78           <base typeid="Animal">
 79             <member name="Age">
 80               <basic typeid="VInt32s" value="20"/>
 81             </member>
 82             <member name="Weight">
 83               <basic typeid="VInt32s" value="200"/>
 84             </member>
 85           </base>
 86         </struct>
 87         <struct typeid="Bird">
 88           <member name="CanFly">
 89             <basic typeid="VBool" value="true"/>
 90           </member>
 91           <member name="Name">
 92             <basic typeid="VUnicodeString" value="Jack"/>
 93           </member>
 94           <base typeid="Animal">
 95             <member name="Age">
 96               <basic typeid="VInt32s" value="30"/>
 97             </member>
 98             <member name="Weight">
 99               <basic typeid="VInt32s" value="300"/>
100             </member>
101           </base>
102         </struct>
103         <null/>
104       </list>
105     </member>
106     <member name="OtherPatsList">
107       <list typeid="VL_List&lt;VL_AutoPtr&lt;Animal&gt; , false&gt;">
108         <struct typeid="Cat">
109           <member name="BodyLength">
110             <basic typeid="VInt32s" value="50"/>
111           </member>
112           <member name="TailLength">
113             <basic typeid="VInt32s" value="30"/>
114           </member>
115           <base typeid="Mammalian">
116             <member name="FingerCount">
117               <basic typeid="VInt32s" value="18"/>
118             </member>
119             <base typeid="Animal">
120               <member name="Age">
121                 <basic typeid="VInt32s" value="10"/>
122               </member>
123               <member name="Weight">
124                 <basic typeid="VInt32s" value="100"/>
125               </member>
126             </base>
127           </base>
128         </struct>
129         <struct typeid="Fish">
130           <member name="Length">
131             <basic typeid="VInt32s" value="25"/>
132           </member>
133           <member name="MaxDepth">
134             <basic typeid="VDouble" value="3000.0"/>
135           </member>
136           <base typeid="Animal">
137             <member name="Age">
138               <basic typeid="VInt32s" value="20"/>
139             </member>
140             <member name="Weight">
141               <basic typeid="VInt32s" value="200"/>
142             </member>
143           </base>
144         </struct>
145         <struct typeid="Bird">
146           <member name="CanFly">
147             <basic typeid="VBool" value="true"/>
148           </member>
149           <member name="Name">
150             <basic typeid="VUnicodeString" value="Jack"/>
151           </member>
152           <base typeid="Animal">
153             <member name="Age">
154               <basic typeid="VInt32s" value="30"/>
155             </member>
156             <member name="Weight">
157               <basic typeid="VInt32s" value="300"/>
158             </member>
159           </base>
160         </struct>
161       </list>
162     </member>
163     <member name="OtherTranspotations">
164       <list typeid="Bird[3]">
165         <struct typeid="Bird">
166           <member name="CanFly">
167             <basic typeid="VBool" value="true"/>
168           </member>
169           <member name="Name">
170             <basic typeid="VUnicodeString" value="Blue Dragon 1"/>
171           </member>
172           <base typeid="Animal">
173             <member name="Age">
174               <basic typeid="VInt32s" value="3000"/>
175             </member>
176             <member name="Weight">
177               <basic typeid="VInt32s" value="300"/>
178             </member>
179           </base>
180         </struct>
181         <struct typeid="Bird">
182           <member name="CanFly">
183             <basic typeid="VBool" value="true"/>
184           </member>
185           <member name="Name">
186             <basic typeid="VUnicodeString" value="Blue Dragon 2"/>
187           </member>
188           <base typeid="Animal">
189             <member name="Age">
190               <basic typeid="VInt32s" value="3100"/>
191             </member>
192             <member name="Weight">
193               <basic typeid="VInt32s" value="300"/>
194             </member>
195           </base>
196         </struct>
197         <struct typeid="Bird">
198           <member name="CanFly">
199             <basic typeid="VBool" value="true"/>
200           </member>
201           <member name="Name">
202             <basic typeid="VUnicodeString" value="Blue Dragon 3"/>
203           </member>
204           <base typeid="Animal">
205             <member name="Age">
206               <basic typeid="VInt32s" value="3200"/>
207             </member>
208             <member name="Weight">
209               <basic typeid="VInt32s" value="300"/>
210             </member>
211           </base>
212         </struct>
213       </list>
214     </member>
215     <member name="OtherTranspotationsList">
216       <list typeid="VL_List&lt;Bird , false&gt;">
217         <struct typeid="Bird">
218           <member name="CanFly">
219             <basic typeid="VBool" value="true"/>
220           </member>
221           <member name="Name">
222             <basic typeid="VUnicodeString" value="Green Dragon 1"/>
223           </member>
224           <base typeid="Animal">
225             <member name="Age">
226               <basic typeid="VInt32s" value="3000"/>
227             </member>
228             <member name="Weight">
229               <basic typeid="VInt32s" value="300"/>
230             </member>
231           </base>
232         </struct>
233         <struct typeid="Bird">
234           <member name="CanFly">
235             <basic typeid="VBool" value="true"/>
236           </member>
237           <member name="Name">
238             <basic typeid="VUnicodeString" value="Green Dragon 2"/>
239           </member>
240           <base typeid="Animal">
241             <member name="Age">
242               <basic typeid="VInt32s" value="3010"/>
243             </member>
244             <member name="Weight">
245               <basic typeid="VInt32s" value="300"/>
246             </member>
247           </base>
248         </struct>
249         <struct typeid="Bird">
250           <member name="CanFly">
251             <basic typeid="VBool" value="true"/>
252           </member>
253           <member name="Name">
254             <basic typeid="VUnicodeString" value="Green Dragon 3"/>
255           </member>
256           <base typeid="Animal">
257             <member name="Age">
258               <basic typeid="VInt32s" value="3020"/>
259             </member>
260             <member name="Weight">
261               <basic typeid="VInt32s" value="300"/>
262             </member>
263           </base>
264         </struct>
265       </list>
266     </member>
267     <member name="PatMap">
268       <map typeid="VL_ListedMap&lt;VUnicodeString , VL_AutoPtr&lt;Animal&gt;&gt;">
269         <pair>
270           <key>
271             <basic typeid="VUnicodeString" value="Bird"/>
272           </key>
273           <value>
274             <struct typeid="Bird">
275               <member name="CanFly">
276                 <basic typeid="VBool" value="true"/>
277               </member>
278               <member name="Name">
279                 <basic typeid="VUnicodeString" value="Jack"/>
280               </member>
281               <base typeid="Animal">
282                 <member name="Age">
283                   <basic typeid="VInt32s" value="30"/>
284                 </member>
285                 <member name="Weight">
286                   <basic typeid="VInt32s" value="300"/>
287                 </member>
288               </base>
289             </struct>
290           </value>
291         </pair>
292         <pair>
293           <key>
294             <basic typeid="VUnicodeString" value="Cat"/>
295           </key>
296           <value>
297             <struct typeid="Cat">
298               <member name="BodyLength">
299                 <basic typeid="VInt32s" value="50"/>
300               </member>
301               <member name="TailLength">
302                 <basic typeid="VInt32s" value="30"/>
303               </member>
304               <base typeid="Mammalian">
305                 <member name="FingerCount">
306                   <basic typeid="VInt32s" value="18"/>
307                 </member>
308                 <base typeid="Animal">
309                   <member name="Age">
310                     <basic typeid="VInt32s" value="10"/>
311                   </member>
312                   <member name="Weight">
313                     <basic typeid="VInt32s" value="100"/>
314                   </member>
315                 </base>
316               </base>
317             </struct>
318           </value>
319         </pair>
320         <pair>
321           <key>
322             <basic typeid="VUnicodeString" value="Fish"/>
323           </key>
324           <value>
325             <struct typeid="Fish">
326               <member name="Length">
327                 <basic typeid="VInt32s" value="25"/>
328               </member>
329               <member name="MaxDepth">
330                 <basic typeid="VDouble" value="3000.0"/>
331               </member>
332               <base typeid="Animal">
333                 <member name="Age">
334                   <basic typeid="VInt32s" value="20"/>
335                 </member>
336                 <member name="Weight">
337                   <basic typeid="VInt32s" value="200"/>
338                 </member>
339               </base>
340             </struct>
341           </value>
342         </pair>
343       </map>
344     </member>
345     <member name="PatMultiMap">
346       <map typeid="VL_MultiMap&lt;VUnicodeString , VL_AutoPtr&lt;Animal&gt; , false&gt;">
347         <pair>
348           <key>
349             <basic typeid="VUnicodeString" value="A"/>
350           </key>
351           <value>
352             <list typeid="VL_List&lt;VL_AutoPtr&lt;Animal&gt; , false&gt;">
353               <struct typeid="Cat">
354                 <member name="BodyLength">
355                   <basic typeid="VInt32s" value="50"/>
356                 </member>
357                 <member name="TailLength">
358                   <basic typeid="VInt32s" value="30"/>
359                 </member>
360                 <base typeid="Mammalian">
361                   <member name="FingerCount">
362                     <basic typeid="VInt32s" value="18"/>
363                   </member>
364                   <base typeid="Animal">
365                     <member name="Age">
366                       <basic typeid="VInt32s" value="10"/>
367                     </member>
368                     <member name="Weight">
369                       <basic typeid="VInt32s" value="100"/>
370                     </member>
371                   </base>
372                 </base>
373               </struct>
374               <struct typeid="Fish">
375                 <member name="Length">
376                   <basic typeid="VInt32s" value="25"/>
377                 </member>
378                 <member name="MaxDepth">
379                   <basic typeid="VDouble" value="3000.0"/>
380                 </member>
381                 <base typeid="Animal">
382                   <member name="Age">
383                     <basic typeid="VInt32s" value="20"/>
384                   </member>
385                   <member name="Weight">
386                     <basic typeid="VInt32s" value="200"/>
387                   </member>
388                 </base>
389               </struct>
390             </list>
391           </value>
392         </pair>
393         <pair>
394           <key>
395             <basic typeid="VUnicodeString" value="B"/>
396           </key>
397           <value>
398             <list typeid="VL_List&lt;VL_AutoPtr&lt;Animal&gt; , false&gt;">
399               <struct typeid="Bird">
400                 <member name="CanFly">
401                   <basic typeid="VBool" value="true"/>
402                 </member>
403                 <member name="Name">
404                   <basic typeid="VUnicodeString" value="Jack"/>
405                 </member>
406                 <base typeid="Animal">
407                   <member name="Age">
408                     <basic typeid="VInt32s" value="30"/>
409                   </member>
410                   <member name="Weight">
411                     <basic typeid="VInt32s" value="300"/>
412                   </member>
413                 </base>
414               </struct>
415             </list>
416           </value>
417         </pair>
418       </map>
419     </member>
420     <base typeid="Mammalian">
421       <member name="FingerCount">
422         <basic typeid="VInt32s" value="20"/>
423       </member>
424       <base typeid="Animal">
425         <member name="Age">
426           <basic typeid="VInt32s" value="16"/>
427         </member>
428         <member name="Weight">
429           <basic typeid="VInt32s" value="2000"/>
430         </member>
431       </base>
432     </base>
433   </struct>
434 </objectxml>
posted on 2009-06-28 20:12 陈梓瀚(vczh) 阅读(6454) 评论(10)  编辑 收藏 引用 所属分类: C++

评论:
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-28 23:05 | 尹东斐
这实现看起来很有诱惑力 :0

有点不太懂,请指教:

136 VL_BEGIN_BASE_CLASS(Animal)
137 VL_ADD_CLASS_MEMBER(Age)
138 VL_ADD_CLASS_MEMBER(Weight)
139 VL_END_CLASS(Animal)

在VL_ADD_CLASS_MEMBER的时候,没有涉及到成员的类型,那么当读取数据的时候,类型是怎么得到的?

GetConsole()->WriteLine(Converter.FindToStringConverter(Inspector)->ConvertToString(Inspector,Type.Object()));

你在这里用到了Type.Object(),我想问你是不是把所有类型都包装起来了,相当于每个类型都有个get_type()之类的方法,如果你是这么实现的,那就没有问题了。如果不是的话,

197 VL_ObjectInspector::Ptr Inspector=Manager->GetInspector(L"VL_ObjectType");

188 VL_ObjectInspector::Ptr Inspector=Manager->GetInspector(VL_InspectorSelector<Animal>::GetID());

我想这两句的作用应该是一样的,只不过VL_InspectorSelector<Animal>是在宏里面特化的,L"VL_ObjectType"是你为所有“内建”类型写的。

难道Inspector里面包含有每个成员的类型?
  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-28 23:47 | 陈梓瀚(vczh)
@尹东斐
1:Type是智能指针,Type.Object用于获得那个真实的指针,你看错了。
2:VL_ADD_CLASS_MEMBER(Weight)可以得到Weight的类型,这个用模板就可以了。
3:Inspector里面包含了每一个直接或间接注册的类型的所有信息,完全可以只适用Inspector来修改对象里面的内容。这一点是XML Serializer读写的基础。  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-28 23:54 | 陈梓瀚(vczh)
@尹东斐
等我连通过反射调用函数的东西做完之后,会有一个Demo的。暂定为服务器使用http协议将一个C++类挂起来,然后有一个客户端去调用它。就跟现在的web service差不多,当然我并不使用那个协议。  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-29 00:43 | 尹东斐
@陈梓瀚(vczh)

>>VL_ADD_CLASS_MEMBER(Weight)可以得到Weight的类型,这个用模板就可以了。

这个用模板也做不到吧? 除非有typeof之类的操作符,否则拿不到类型的呀。  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-29 00:48 | 尹东斐
比如:
class A
{
public:
int i;
};

VL_BEGIN_INSPECTOR_DECLARATION
VL_BEGIN_BASE_CLASS(A)
VL_ADD_CLASS_MEMBER(i)
VL_END_CLASS(A)
VL_END_INSPECTOR_DECLARATION

A a;

现在我要通过类似 int x = func(a, "i"); 来的到a中i的值,那这个时候,怎么知道i的类型呢?也就是说func的返回值是从哪里来的?

不吝赐教~  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-29 05:06 | 陈梓瀚(vczh)
@尹东斐
有一种东西叫模板函数,函数里面不就拿得到类型了么。这个时候送进模板类去搞特化。  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-29 05:08 | 陈梓瀚(vczh)
@尹东斐
而且Inspector返回的是指针,然后有各种各样的办法来告诉你这个东西是个int,让你用合适的方法去拿。  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-29 19:21 | yindf
@陈梓瀚(vczh)

谢谢  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-29 23:34 | 陈昱(CY)
是不是VL_BEGIN_BASE_CLASS的话,VL_ADD_CLASS_MEMBER里面的内容类型一定要C++的基本类型?这样对所有基本类型都进行特化?  回复  更多评论
  
# re: C++基于类似反射机制的XML序列化和反序列化完成 2009-06-30 08:08 | 陈梓瀚(vczh)
@陈昱(CY)
背后预定义了很多已经存在的基本类型和很多容器等等。你也可以用相同的办法扩展成支持STL的。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理