Board index » cppbuilder » Streaming a TPersistent object
Dan
CBuilder Developer |
Streaming a TPersistent object2006-03-07 02:33:28 AM cppbuilder18 Hello all, In my application I've created classes inherited from TPersistent in order to use the streaming mechanism to load/store their objects. I've also created a wapper component, TSerialize which looks like this: class TSerialize: public TComponent { private: AnsiString objType; TPersistent* objZ; AnsiString __fastcall GetObjType() { if (objZ) return objZ->ClassName(); else return ""; } TPersistent* __fastcall GetObj() { if (!objZ) objZ = createObject(objType); return objZ; } public: __fastcall TSerialize(TComponent* AOwner):TComponent(AOwner) { objZ = NULL; } __published: __property AnsiString objectA={read=GetObjType,write=objType,stored=true}; __property TPersistent* objectZ={read=GetObj,write=objZ}; }; In GetObj there's a call to createObject which is a factory class for creating dynamic objects based on their classname (My TPersistent objects). Now for my problem - My objects keep a list of their children, much like a TComponent (please please don't ask me why I'm not simply using a TComponent and making my life much easier), I'd like to save those children as well and all with one call to WriteComponent(MyRootComponent). My children property is defined as "public: TMyPersistentItem* children[int]={...}". I've used DefineProperty(children,readChild,writeChild,childrenCount) and the functions themselves look like this: void __fastcall WriteChild(TWriter* writer) { writer->WriteListBegin(); TSerialize* serialize; for (int i=0;i<childrenCount;i++) { serialize = new TSerialize(NULL); serialize->objectZ = child[i]; serialize->objectA = child[i]->ClassName(); writer->WriteComponent(serialize); delete serialize; } writer->WriteListEnd(); } void __fastcall ReadChild(TReader* reader) { reader->ReadListBegin(); TSerialize* serialize; while (!reader->EndOfList()) { serialize = (TSerialize*)reader->ReadComponent(NULL); addChild((TMyPersistentItem*)serialize->objectZ); } reader->ReadListEnd(); } The problem is that the children are written to the stream like this: (I'm putting this in a textual way although the data is obviously binary) objectZ.objectA = "TMyPersistentItem" objectZ.objectZ.Property1 = property1value objectZ.objectZ.Property2 = property2value objectZ.objectZ.Property3 = property3value etc... although in my opinion it should be written as: objectA = "TMyPersistentItem" objectZ.Property1 = property1value objectZ.Property2 = property2value objectZ.Property3 = property3value If I use the same code logic only using DefineBinaryProperty instead of DefineProperty then it works perfectly, keeping the children levels etc... but I need to use DefineProperty in my code and it just doesn't work. I hope I've made myself clear enough and not forgotten any useful detail. If you're still reading this I hope that you can help me understand the problem and how to fix it. Thank you very much Dan |