xtistrm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/xtistrm.h
  3. // Purpose: streaming runtime metadata information (extended class info)
  4. // Author: Stefan Csomor
  5. // Modified by:
  6. // Created: 27/07/03
  7. // Copyright: (c) 2003 Stefan Csomor
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_XTISTRMH__
  11. #define _WX_XTISTRMH__
  12. #include "wx/defs.h"
  13. #if wxUSE_EXTENDED_RTTI
  14. #include "wx/object.h"
  15. const int wxInvalidObjectID = -2;
  16. const int wxNullObjectID = -3;
  17. // Filer contains the interfaces for streaming objects in and out of XML,
  18. // rendering them either to objects in memory, or to code. Note: We
  19. // consider the process of generating code to be one of *depersisting* the
  20. // object from xml, *not* of persisting the object to code from an object
  21. // in memory. This distinction can be confusing, and should be kept
  22. // in mind when looking at the property streamers and callback interfaces
  23. // listed below.
  24. // ----------------------------------------------------------------------------
  25. // wxObjectWriterCallback
  26. //
  27. // This class will be asked during the streaming-out process about every single
  28. // property or object instance. It can veto streaming out by returning false
  29. // or modify the value before it is streamed-out.
  30. // ----------------------------------------------------------------------------
  31. /*
  32. class WXDLLIMPEXP_BASE wxClassInfo;
  33. class WXDLLIMPEXP_BASE wxAnyList;
  34. class WXDLLIMPEXP_BASE wxPropertyInfo;
  35. class WXDLLIMPEXP_BASE wxAny;
  36. class WXDLLIMPEXP_BASE wxHandlerInfo;
  37. */
  38. class WXDLLIMPEXP_BASE wxObjectWriter;
  39. class WXDLLIMPEXP_BASE wxObjectReader;
  40. class WXDLLIMPEXP_BASE wxObjectWriterCallback
  41. {
  42. public:
  43. virtual ~wxObjectWriterCallback() {}
  44. // will be called before an object is written, may veto by returning false
  45. virtual bool BeforeWriteObject( wxObjectWriter *WXUNUSED(writer),
  46. const wxObject *WXUNUSED(object),
  47. const wxClassInfo *WXUNUSED(classInfo),
  48. const wxStringToAnyHashMap &WXUNUSED(metadata))
  49. { return true; }
  50. // will be called after this object has been written, may be
  51. // needed for adjusting stacks
  52. virtual void AfterWriteObject( wxObjectWriter *WXUNUSED(writer),
  53. const wxObject *WXUNUSED(object),
  54. const wxClassInfo *WXUNUSED(classInfo) )
  55. {}
  56. // will be called before a property gets written, may change the value,
  57. // eg replace a concrete wxSize by wxSize( wxDefaultCoord, wxDefaultCoord )
  58. // or veto writing that property at all by returning false
  59. virtual bool BeforeWriteProperty( wxObjectWriter *WXUNUSED(writer),
  60. const wxObject *WXUNUSED(object),
  61. const wxPropertyInfo *WXUNUSED(propInfo),
  62. const wxAny &WXUNUSED(value) )
  63. { return true; }
  64. // will be called before a property gets written, may change the value,
  65. // eg replace a concrete wxSize by wxSize( wxDefaultCoord, wxDefaultCoord )
  66. // or veto writing that property at all by returning false
  67. virtual bool BeforeWriteProperty( wxObjectWriter *WXUNUSED(writer),
  68. const wxObject *WXUNUSED(object),
  69. const wxPropertyInfo *WXUNUSED(propInfo),
  70. const wxAnyList &WXUNUSED(value) )
  71. { return true; }
  72. // will be called after a property has been written out, may be needed
  73. // for adjusting stacks
  74. virtual void AfterWriteProperty( wxObjectWriter *WXUNUSED(writer),
  75. const wxPropertyInfo *WXUNUSED(propInfo) )
  76. {}
  77. // will be called before this delegate gets written
  78. virtual bool BeforeWriteDelegate( wxObjectWriter *WXUNUSED(writer),
  79. const wxObject *WXUNUSED(object),
  80. const wxClassInfo* WXUNUSED(classInfo),
  81. const wxPropertyInfo *WXUNUSED(propInfo),
  82. const wxObject *&WXUNUSED(eventSink),
  83. const wxHandlerInfo* &WXUNUSED(handlerInfo) )
  84. { return true; }
  85. virtual void AfterWriteDelegate( wxObjectWriter *WXUNUSED(writer),
  86. const wxObject *WXUNUSED(object),
  87. const wxClassInfo* WXUNUSED(classInfo),
  88. const wxPropertyInfo *WXUNUSED(propInfo),
  89. const wxObject *&WXUNUSED(eventSink),
  90. const wxHandlerInfo* &WXUNUSED(handlerInfo) )
  91. { }
  92. };
  93. class WXDLLIMPEXP_BASE wxObjectWriterFunctor: public wxObjectFunctor
  94. {
  95. };
  96. class WXDLLIMPEXP_BASE wxObjectWriter: public wxObject
  97. {
  98. friend class wxObjectWriterFunctor;
  99. public:
  100. wxObjectWriter();
  101. virtual ~wxObjectWriter();
  102. // with this call you start writing out a new top-level object
  103. void WriteObject(const wxObject *object, const wxClassInfo *classInfo,
  104. wxObjectWriterCallback *writercallback, const wxString &name,
  105. const wxStringToAnyHashMap &metadata);
  106. // Managing the object identity table a.k.a context
  107. //
  108. // these methods make sure that no object gets written twice,
  109. // because sometimes multiple calls to the WriteObject will be
  110. // made without wanting to have duplicate objects written, the
  111. // object identity table will be reset manually
  112. virtual void ClearObjectContext();
  113. // gets the object Id for a passed in object in the context
  114. int GetObjectID(const wxObject *obj);
  115. // returns true if this object has already been written in this context
  116. bool IsObjectKnown( const wxObject *obj );
  117. //
  118. // streaming callbacks
  119. //
  120. // these callbacks really write out the values in the stream format
  121. // begins writing out a new toplevel entry which has the indicated unique name
  122. virtual void DoBeginWriteTopLevelEntry( const wxString &name ) = 0;
  123. // ends writing out a new toplevel entry which has the indicated unique name
  124. virtual void DoEndWriteTopLevelEntry( const wxString &name ) = 0;
  125. // start of writing an object having the passed in ID
  126. virtual void DoBeginWriteObject(const wxObject *object, const wxClassInfo *classInfo,
  127. int objectID, const wxStringToAnyHashMap &metadata ) = 0;
  128. // end of writing an toplevel object name param is used for unique
  129. // identification within the container
  130. virtual void DoEndWriteObject(const wxObject *object,
  131. const wxClassInfo *classInfo, int objectID ) = 0;
  132. // writes a simple property in the stream format
  133. virtual void DoWriteSimpleType( const wxAny &value ) = 0;
  134. // start of writing a complex property into the stream (
  135. virtual void DoBeginWriteProperty( const wxPropertyInfo *propInfo ) = 0;
  136. // end of writing a complex property into the stream
  137. virtual void DoEndWriteProperty( const wxPropertyInfo *propInfo ) = 0;
  138. virtual void DoBeginWriteElement() = 0;
  139. virtual void DoEndWriteElement() = 0;
  140. // insert an object reference to an already written object
  141. virtual void DoWriteRepeatedObject( int objectID ) = 0;
  142. // insert a null reference
  143. virtual void DoWriteNullObject() = 0;
  144. // writes a delegate in the stream format
  145. virtual void DoWriteDelegate( const wxObject *object, const wxClassInfo* classInfo,
  146. const wxPropertyInfo *propInfo, const wxObject *eventSink,
  147. int sinkObjectID, const wxClassInfo* eventSinkClassInfo,
  148. const wxHandlerInfo* handlerIndo ) = 0;
  149. void WriteObject(const wxObject *object, const wxClassInfo *classInfo,
  150. wxObjectWriterCallback *writercallback, bool isEmbedded, const wxStringToAnyHashMap &metadata );
  151. protected:
  152. struct wxObjectWriterInternal;
  153. wxObjectWriterInternal* m_data;
  154. struct wxObjectWriterInternalPropertiesData;
  155. void WriteAllProperties( const wxObject * obj, const wxClassInfo* ci,
  156. wxObjectWriterCallback *writercallback,
  157. wxObjectWriterInternalPropertiesData * data );
  158. void WriteOneProperty( const wxObject *obj, const wxClassInfo* ci,
  159. const wxPropertyInfo* pi, wxObjectWriterCallback *writercallback,
  160. wxObjectWriterInternalPropertiesData *data );
  161. void FindConnectEntry(const wxEvtHandler * evSource,
  162. const wxEventSourceTypeInfo* dti, const wxObject* &sink,
  163. const wxHandlerInfo *&handler);
  164. };
  165. /*
  166. Streaming callbacks for depersisting XML to code, or running objects
  167. */
  168. class WXDLLIMPEXP_BASE wxObjectReaderCallback;
  169. /*
  170. wxObjectReader handles streaming in a class from a arbitrary format.
  171. While walking through it issues calls out to interfaces to readercallback
  172. the guts from the underlying storage format.
  173. */
  174. class WXDLLIMPEXP_BASE wxObjectReader: public wxObject
  175. {
  176. public:
  177. wxObjectReader();
  178. virtual ~wxObjectReader();
  179. // the only thing wxObjectReader knows about is the class info by object ID
  180. wxClassInfo *GetObjectClassInfo(int objectID);
  181. bool HasObjectClassInfo( int objectID );
  182. void SetObjectClassInfo(int objectID, wxClassInfo* classInfo);
  183. // Reads the component the reader is pointed at from the underlying format.
  184. // The return value is the root object ID, which can
  185. // then be used to ask the depersister about that object
  186. // if there was a problem you will get back wxInvalidObjectID and the current
  187. // error log will carry the problems encoutered
  188. virtual int ReadObject( const wxString &name, wxObjectReaderCallback *readercallback ) = 0;
  189. private:
  190. struct wxObjectReaderInternal;
  191. wxObjectReaderInternal *m_data;
  192. };
  193. // This abstract class matches the allocate-init/create model of creation of objects.
  194. // At runtime, these will create actual instances, and manipulate them.
  195. // When generating code, these will just create statements of C++
  196. // code to create the objects.
  197. class WXDLLIMPEXP_BASE wxObjectReaderCallback
  198. {
  199. public:
  200. virtual ~wxObjectReaderCallback() {}
  201. // allocate the new object on the heap, that object will have the passed in ID
  202. virtual void AllocateObject(int objectID, wxClassInfo *classInfo,
  203. wxStringToAnyHashMap &metadata) = 0;
  204. // initialize the already allocated object having the ID objectID with the Create method
  205. // creation parameters which are objects are having their Ids passed in objectIDValues
  206. // having objectId <> wxInvalidObjectID
  207. virtual void CreateObject(int objectID,
  208. const wxClassInfo *classInfo,
  209. int paramCount,
  210. wxAny *VariantValues,
  211. int *objectIDValues,
  212. const wxClassInfo **objectClassInfos,
  213. wxStringToAnyHashMap &metadata) = 0;
  214. // construct the new object on the heap, that object will have the passed in ID
  215. // (for objects that don't support allocate-create type of creation)
  216. // creation parameters which are objects are having their Ids passed in
  217. // objectIDValues having objectId <> wxInvalidObjectID
  218. virtual void ConstructObject(int objectID,
  219. const wxClassInfo *classInfo,
  220. int paramCount,
  221. wxAny *VariantValues,
  222. int *objectIDValues,
  223. const wxClassInfo **objectClassInfos,
  224. wxStringToAnyHashMap &metadata) = 0;
  225. // destroy the heap-allocated object having the ID objectID, this may be used
  226. // if an object is embedded in another object and set via value semantics,
  227. // so the intermediate object can be destroyed after safely
  228. virtual void DestroyObject(int objectID, wxClassInfo *classInfo) = 0;
  229. // set the corresponding property
  230. virtual void SetProperty(int objectID,
  231. const wxClassInfo *classInfo,
  232. const wxPropertyInfo* propertyInfo,
  233. const wxAny &VariantValue) = 0;
  234. // sets the corresponding property (value is an object)
  235. virtual void SetPropertyAsObject(int objectID,
  236. const wxClassInfo *classInfo,
  237. const wxPropertyInfo* propertyInfo,
  238. int valueObjectId) = 0;
  239. // adds an element to a property collection
  240. virtual void AddToPropertyCollection( int objectID,
  241. const wxClassInfo *classInfo,
  242. const wxPropertyInfo* propertyInfo,
  243. const wxAny &VariantValue) = 0;
  244. // sets the corresponding property (value is an object)
  245. virtual void AddToPropertyCollectionAsObject(int objectID,
  246. const wxClassInfo *classInfo,
  247. const wxPropertyInfo* propertyInfo,
  248. int valueObjectId) = 0;
  249. // sets the corresponding event handler
  250. virtual void SetConnect(int EventSourceObjectID,
  251. const wxClassInfo *EventSourceClassInfo,
  252. const wxPropertyInfo *delegateInfo,
  253. const wxClassInfo *EventSinkClassInfo,
  254. const wxHandlerInfo* handlerInfo,
  255. int EventSinkObjectID ) = 0;
  256. };
  257. /*
  258. wxObjectRuntimeReaderCallback implements the callbacks that will bring back
  259. an object into a life memory instance
  260. */
  261. class WXDLLIMPEXP_BASE wxObjectRuntimeReaderCallback: public wxObjectReaderCallback
  262. {
  263. struct wxObjectRuntimeReaderCallbackInternal;
  264. wxObjectRuntimeReaderCallbackInternal * m_data;
  265. public:
  266. wxObjectRuntimeReaderCallback();
  267. virtual ~wxObjectRuntimeReaderCallback();
  268. // returns the object having the corresponding ID fully constructed
  269. wxObject *GetObject(int objectID);
  270. // allocate the new object on the heap, that object will have the passed in ID
  271. virtual void AllocateObject(int objectID, wxClassInfo *classInfo,
  272. wxStringToAnyHashMap &metadata);
  273. // initialize the already allocated object having the ID objectID with
  274. // the Create method creation parameters which are objects are having
  275. // their Ids passed in objectIDValues having objectId <> wxInvalidObjectID
  276. virtual void CreateObject(int objectID,
  277. const wxClassInfo *classInfo,
  278. int paramCount,
  279. wxAny *VariantValues,
  280. int *objectIDValues,
  281. const wxClassInfo **objectClassInfos,
  282. wxStringToAnyHashMap &metadata
  283. );
  284. // construct the new object on the heap, that object will have the
  285. // passed in ID (for objects that don't support allocate-create type of
  286. // creation) creation parameters which are objects are having their Ids
  287. // passed in objectIDValues having objectId <> wxInvalidObjectID
  288. virtual void ConstructObject(int objectID,
  289. const wxClassInfo *classInfo,
  290. int paramCount,
  291. wxAny *VariantValues,
  292. int *objectIDValues,
  293. const wxClassInfo **objectClassInfos,
  294. wxStringToAnyHashMap &metadata);
  295. // destroy the heap-allocated object having the ID objectID, this may be
  296. // used if an object is embedded in another object and set via value semantics,
  297. // so the intermediate object can be destroyed after safely
  298. virtual void DestroyObject(int objectID, wxClassInfo *classInfo);
  299. // set the corresponding property
  300. virtual void SetProperty(int objectID,
  301. const wxClassInfo *classInfo,
  302. const wxPropertyInfo* propertyInfo,
  303. const wxAny &variantValue);
  304. // sets the corresponding property (value is an object)
  305. virtual void SetPropertyAsObject(int objectId,
  306. const wxClassInfo *classInfo,
  307. const wxPropertyInfo* propertyInfo,
  308. int valueObjectId);
  309. // adds an element to a property collection
  310. virtual void AddToPropertyCollection( int objectID,
  311. const wxClassInfo *classInfo,
  312. const wxPropertyInfo* propertyInfo,
  313. const wxAny &VariantValue);
  314. // sets the corresponding property (value is an object)
  315. virtual void AddToPropertyCollectionAsObject(int objectID,
  316. const wxClassInfo *classInfo,
  317. const wxPropertyInfo* propertyInfo,
  318. int valueObjectId);
  319. // sets the corresponding event handler
  320. virtual void SetConnect(int eventSourceObjectID,
  321. const wxClassInfo *eventSourceClassInfo,
  322. const wxPropertyInfo *delegateInfo,
  323. const wxClassInfo *eventSinkClassInfo,
  324. const wxHandlerInfo* handlerInfo,
  325. int eventSinkObjectID );
  326. };
  327. #endif // wxUSE_EXTENDED_RTTI
  328. #endif