xti.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/xti.h
  3. // Purpose: runtime metadata information (extended class info)
  4. // Author: Stefan Csomor
  5. // Modified by: Francesco Montorsi
  6. // Created: 27/07/03
  7. // Copyright: (c) 1997 Julian Smart
  8. // (c) 2003 Stefan Csomor
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_XTIH__
  12. #define _WX_XTIH__
  13. // We want to support properties, event sources and events sinks through
  14. // explicit declarations, using templates and specialization to make the
  15. // effort as painless as possible.
  16. //
  17. // This means we have the following domains :
  18. //
  19. // - Type Information for categorizing built in types as well as custom types
  20. // this includes information about enums, their values and names
  21. // - Type safe value storage : a kind of wxVariant, called right now wxAny
  22. // which will be merged with wxVariant
  23. // - Property Information and Property Accessors providing access to a class'
  24. // values and exposed event delegates
  25. // - Information about event handlers
  26. // - extended Class Information for accessing all these
  27. // ----------------------------------------------------------------------------
  28. // headers
  29. // ----------------------------------------------------------------------------
  30. #include "wx/defs.h"
  31. #if wxUSE_EXTENDED_RTTI
  32. class WXDLLIMPEXP_FWD_BASE wxAny;
  33. class WXDLLIMPEXP_FWD_BASE wxAnyList;
  34. class WXDLLIMPEXP_FWD_BASE wxObject;
  35. class WXDLLIMPEXP_FWD_BASE wxString;
  36. class WXDLLIMPEXP_FWD_BASE wxClassInfo;
  37. class WXDLLIMPEXP_FWD_BASE wxHashTable;
  38. class WXDLLIMPEXP_FWD_BASE wxObject;
  39. class WXDLLIMPEXP_FWD_BASE wxPluginLibrary;
  40. class WXDLLIMPEXP_FWD_BASE wxHashTable;
  41. class WXDLLIMPEXP_FWD_BASE wxHashTable_Node;
  42. class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap;
  43. class WXDLLIMPEXP_FWD_BASE wxPropertyInfoMap;
  44. class WXDLLIMPEXP_FWD_BASE wxPropertyAccessor;
  45. class WXDLLIMPEXP_FWD_BASE wxObjectAllocatorAndCreator;
  46. class WXDLLIMPEXP_FWD_BASE wxObjectAllocator;
  47. #define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
  48. #include "wx/xtitypes.h"
  49. #include "wx/xtihandler.h"
  50. // ----------------------------------------------------------------------------
  51. // wxClassInfo
  52. // ----------------------------------------------------------------------------
  53. class WXDLLIMPEXP_BASE wxObjectFunctor
  54. {
  55. public:
  56. virtual ~wxObjectFunctor();
  57. // Invoke the actual event handler:
  58. virtual void operator()(const wxObject *) = 0;
  59. };
  60. class WXDLLIMPEXP_FWD_BASE wxPropertyInfo;
  61. class WXDLLIMPEXP_FWD_BASE wxHandlerInfo;
  62. typedef wxObject *(*wxObjectConstructorFn)(void);
  63. typedef wxPropertyInfo *(*wxPropertyInfoFn)(void);
  64. typedef wxHandlerInfo *(*wxHandlerInfoFn)(void);
  65. typedef void (*wxVariantToObjectConverter)( const wxAny &data, wxObjectFunctor* fn );
  66. typedef wxObject* (*wxVariantToObjectPtrConverter) ( const wxAny& data);
  67. typedef wxAny (*wxObjectToVariantConverter)( wxObject* );
  68. WXDLLIMPEXP_BASE wxString wxAnyGetAsString( const wxAny& data);
  69. WXDLLIMPEXP_BASE const wxObject* wxAnyGetAsObjectPtr( const wxAny& data);
  70. class WXDLLIMPEXP_BASE wxObjectWriter;
  71. class WXDLLIMPEXP_BASE wxObjectWriterCallback;
  72. typedef bool (*wxObjectStreamingCallback) ( const wxObject *, wxObjectWriter *, \
  73. wxObjectWriterCallback *, const wxStringToAnyHashMap & );
  74. class WXDLLIMPEXP_BASE wxClassInfo
  75. {
  76. friend class WXDLLIMPEXP_BASE wxPropertyInfo;
  77. friend class /* WXDLLIMPEXP_BASE */ wxHandlerInfo;
  78. friend wxObject *wxCreateDynamicObject(const wxString& name);
  79. public:
  80. wxClassInfo(const wxClassInfo **_Parents,
  81. const wxChar *_UnitName,
  82. const wxChar *_ClassName,
  83. int size,
  84. wxObjectConstructorFn ctor,
  85. wxPropertyInfoFn _Props,
  86. wxHandlerInfoFn _Handlers,
  87. wxObjectAllocatorAndCreator* _Constructor,
  88. const wxChar ** _ConstructorProperties,
  89. const int _ConstructorPropertiesCount,
  90. wxVariantToObjectPtrConverter _PtrConverter1,
  91. wxVariantToObjectConverter _Converter2,
  92. wxObjectToVariantConverter _Converter3,
  93. wxObjectStreamingCallback _streamingCallback = NULL) :
  94. m_className(_ClassName),
  95. m_objectSize(size),
  96. m_objectConstructor(ctor),
  97. m_next(sm_first),
  98. m_firstPropertyFn(_Props),
  99. m_firstHandlerFn(_Handlers),
  100. m_firstProperty(NULL),
  101. m_firstHandler(NULL),
  102. m_firstInited(false),
  103. m_parents(_Parents),
  104. m_unitName(_UnitName),
  105. m_constructor(_Constructor),
  106. m_constructorProperties(_ConstructorProperties),
  107. m_constructorPropertiesCount(_ConstructorPropertiesCount),
  108. m_variantOfPtrToObjectConverter(_PtrConverter1),
  109. m_variantToObjectConverter(_Converter2),
  110. m_objectToVariantConverter(_Converter3),
  111. m_streamingCallback(_streamingCallback)
  112. {
  113. sm_first = this;
  114. Register();
  115. }
  116. wxClassInfo(const wxChar *_UnitName, const wxChar *_ClassName,
  117. const wxClassInfo **_Parents) :
  118. m_className(_ClassName),
  119. m_objectSize(0),
  120. m_objectConstructor(NULL),
  121. m_next(sm_first),
  122. m_firstPropertyFn(NULL),
  123. m_firstHandlerFn(NULL),
  124. m_firstProperty(NULL),
  125. m_firstHandler(NULL),
  126. m_firstInited(true),
  127. m_parents(_Parents),
  128. m_unitName(_UnitName),
  129. m_constructor(NULL),
  130. m_constructorProperties(NULL),
  131. m_constructorPropertiesCount(0),
  132. m_variantOfPtrToObjectConverter(NULL),
  133. m_variantToObjectConverter(NULL),
  134. m_objectToVariantConverter(NULL),
  135. m_streamingCallback(NULL)
  136. {
  137. sm_first = this;
  138. Register();
  139. }
  140. // ctor compatible with old RTTI system
  141. wxClassInfo(const wxChar *_ClassName,
  142. const wxClassInfo *_Parent1,
  143. const wxClassInfo *_Parent2,
  144. int size,
  145. wxObjectConstructorFn ctor) :
  146. m_className(_ClassName),
  147. m_objectSize(size),
  148. m_objectConstructor(ctor),
  149. m_next(sm_first),
  150. m_firstPropertyFn(NULL),
  151. m_firstHandlerFn(NULL),
  152. m_firstProperty(NULL),
  153. m_firstHandler(NULL),
  154. m_firstInited(true),
  155. m_parents(NULL),
  156. m_unitName(NULL),
  157. m_constructor(NULL),
  158. m_constructorProperties(NULL),
  159. m_constructorPropertiesCount(0),
  160. m_variantOfPtrToObjectConverter(NULL),
  161. m_variantToObjectConverter(NULL),
  162. m_objectToVariantConverter(NULL),
  163. m_streamingCallback(NULL)
  164. {
  165. sm_first = this;
  166. m_parents[0] = _Parent1;
  167. m_parents[1] = _Parent2;
  168. m_parents[2] = NULL;
  169. Register();
  170. }
  171. virtual ~wxClassInfo();
  172. // allocates an instance of this class, this object does not have to be
  173. // initialized or fully constructed as this call will be followed by a call to Create
  174. virtual wxObject *AllocateObject() const
  175. { return m_objectConstructor ? (*m_objectConstructor)() : 0; }
  176. // 'old naming' for AllocateObject staying here for backward compatibility
  177. wxObject *CreateObject() const { return AllocateObject(); }
  178. // direct construction call for classes that cannot construct instances via alloc/create
  179. wxObject *ConstructObject(int ParamCount, wxAny *Params) const;
  180. bool NeedsDirectConstruction() const;
  181. const wxChar *GetClassName() const
  182. { return m_className; }
  183. const wxChar *GetBaseClassName1() const
  184. { return m_parents[0] ? m_parents[0]->GetClassName() : NULL; }
  185. const wxChar *GetBaseClassName2() const
  186. { return (m_parents[0] && m_parents[1]) ? m_parents[1]->GetClassName() : NULL; }
  187. const wxClassInfo *GetBaseClass1() const
  188. { return m_parents[0]; }
  189. const wxClassInfo *GetBaseClass2() const
  190. { return m_parents[0] ? m_parents[1] : NULL; }
  191. const wxChar *GetIncludeName() const
  192. { return m_unitName; }
  193. const wxClassInfo **GetParents() const
  194. { return m_parents; }
  195. int GetSize() const
  196. { return m_objectSize; }
  197. bool IsDynamic() const
  198. { return (NULL != m_objectConstructor); }
  199. wxObjectConstructorFn GetConstructor() const
  200. { return m_objectConstructor; }
  201. const wxClassInfo *GetNext() const
  202. { return m_next; }
  203. // statics:
  204. static void CleanUp();
  205. static wxClassInfo *FindClass(const wxString& className);
  206. static const wxClassInfo *GetFirst()
  207. { return sm_first; }
  208. // Climb upwards through inheritance hierarchy.
  209. // Dual inheritance is catered for.
  210. bool IsKindOf(const wxClassInfo *info) const;
  211. wxDECLARE_CLASS_INFO_ITERATORS();
  212. // if there is a callback registered with that class it will be called
  213. // before this object will be written to disk, it can veto streaming out
  214. // this object by returning false, if this class has not registered a
  215. // callback, the search will go up the inheritance tree if no callback has
  216. // been registered true will be returned by default
  217. bool BeforeWriteObject( const wxObject *obj, wxObjectWriter *streamer,
  218. wxObjectWriterCallback *writercallback, const wxStringToAnyHashMap &metadata) const;
  219. // gets the streaming callback from this class or any superclass
  220. wxObjectStreamingCallback GetStreamingCallback() const;
  221. // returns the first property
  222. wxPropertyInfo* GetFirstProperty() const
  223. { EnsureInfosInited(); return m_firstProperty; }
  224. // returns the first handler
  225. wxHandlerInfo* GetFirstHandler() const
  226. { EnsureInfosInited(); return m_firstHandler; }
  227. // Call the Create upon an instance of the class, in the end the object is fully
  228. // initialized
  229. virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
  230. // get number of parameters for constructor
  231. virtual int GetCreateParamCount() const
  232. { return m_constructorPropertiesCount; }
  233. // get n-th constructor parameter
  234. virtual const wxChar* GetCreateParamName(int n) const
  235. { return m_constructorProperties[n]; }
  236. // Runtime access to objects for simple properties (get/set) by property
  237. // name and variant data
  238. virtual void SetProperty (wxObject *object, const wxChar *propertyName,
  239. const wxAny &value) const;
  240. virtual wxAny GetProperty (wxObject *object, const wxChar *propertyName) const;
  241. // Runtime access to objects for collection properties by property name
  242. virtual wxAnyList GetPropertyCollection(wxObject *object,
  243. const wxChar *propertyName) const;
  244. virtual void AddToPropertyCollection(wxObject *object, const wxChar *propertyName,
  245. const wxAny& value) const;
  246. // we must be able to cast variants to wxObject pointers, templates seem
  247. // not to be suitable
  248. void CallOnAny( const wxAny &data, wxObjectFunctor* functor ) const;
  249. wxObject* AnyToObjectPtr( const wxAny &data) const;
  250. wxAny ObjectPtrToAny( wxObject *object ) const;
  251. // find property by name
  252. virtual const wxPropertyInfo *FindPropertyInfo (const wxChar *PropertyName) const;
  253. // find handler by name
  254. virtual const wxHandlerInfo *FindHandlerInfo (const wxChar *handlerName) const;
  255. // find property by name
  256. virtual wxPropertyInfo *FindPropertyInfoInThisClass (const wxChar *PropertyName) const;
  257. // find handler by name
  258. virtual wxHandlerInfo *FindHandlerInfoInThisClass (const wxChar *handlerName) const;
  259. // puts all the properties of this class and its superclasses in the map,
  260. // as long as there is not yet an entry with the same name (overriding mechanism)
  261. void GetProperties( wxPropertyInfoMap &map ) const;
  262. private:
  263. const wxChar *m_className;
  264. int m_objectSize;
  265. wxObjectConstructorFn m_objectConstructor;
  266. // class info object live in a linked list:
  267. // pointers to its head and the next element in it
  268. static wxClassInfo *sm_first;
  269. wxClassInfo *m_next;
  270. static wxHashTable *sm_classTable;
  271. wxPropertyInfoFn m_firstPropertyFn;
  272. wxHandlerInfoFn m_firstHandlerFn;
  273. protected:
  274. void EnsureInfosInited() const
  275. {
  276. if ( !m_firstInited)
  277. {
  278. if ( m_firstPropertyFn != NULL)
  279. m_firstProperty = (*m_firstPropertyFn)();
  280. if ( m_firstHandlerFn != NULL)
  281. m_firstHandler = (*m_firstHandlerFn)();
  282. m_firstInited = true;
  283. }
  284. }
  285. mutable wxPropertyInfo* m_firstProperty;
  286. mutable wxHandlerInfo* m_firstHandler;
  287. private:
  288. mutable bool m_firstInited;
  289. const wxClassInfo** m_parents;
  290. const wxChar* m_unitName;
  291. wxObjectAllocatorAndCreator* m_constructor;
  292. const wxChar ** m_constructorProperties;
  293. const int m_constructorPropertiesCount;
  294. wxVariantToObjectPtrConverter m_variantOfPtrToObjectConverter;
  295. wxVariantToObjectConverter m_variantToObjectConverter;
  296. wxObjectToVariantConverter m_objectToVariantConverter;
  297. wxObjectStreamingCallback m_streamingCallback;
  298. const wxPropertyAccessor *FindAccessor (const wxChar *propertyName) const;
  299. protected:
  300. // registers the class
  301. void Register();
  302. void Unregister();
  303. DECLARE_NO_COPY_CLASS(wxClassInfo)
  304. };
  305. WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name);
  306. // ----------------------------------------------------------------------------
  307. // wxDynamicClassInfo
  308. // ----------------------------------------------------------------------------
  309. // this object leads to having a pure runtime-instantiation
  310. class WXDLLIMPEXP_BASE wxDynamicClassInfo : public wxClassInfo
  311. {
  312. friend class WXDLLIMPEXP_BASE wxDynamicObject;
  313. public:
  314. wxDynamicClassInfo( const wxChar *_UnitName, const wxChar *_ClassName,
  315. const wxClassInfo* superClass );
  316. virtual ~wxDynamicClassInfo();
  317. // constructs a wxDynamicObject with an instance
  318. virtual wxObject *AllocateObject() const;
  319. // Call the Create method for a class
  320. virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
  321. // get number of parameters for constructor
  322. virtual int GetCreateParamCount() const;
  323. // get i-th constructor parameter
  324. virtual const wxChar* GetCreateParamName(int i) const;
  325. // Runtime access to objects by property name, and variant data
  326. virtual void SetProperty (wxObject *object, const wxChar *PropertyName,
  327. const wxAny &Value) const;
  328. virtual wxAny GetProperty (wxObject *object, const wxChar *PropertyName) const;
  329. // adds a property to this class at runtime
  330. void AddProperty( const wxChar *propertyName, const wxTypeInfo* typeInfo );
  331. // removes an existing runtime-property
  332. void RemoveProperty( const wxChar *propertyName );
  333. // renames an existing runtime-property
  334. void RenameProperty( const wxChar *oldPropertyName, const wxChar *newPropertyName );
  335. // as a handler to this class at runtime
  336. void AddHandler( const wxChar *handlerName, wxObjectEventFunction address,
  337. const wxClassInfo* eventClassInfo );
  338. // removes an existing runtime-handler
  339. void RemoveHandler( const wxChar *handlerName );
  340. // renames an existing runtime-handler
  341. void RenameHandler( const wxChar *oldHandlerName, const wxChar *newHandlerName );
  342. private:
  343. struct wxDynamicClassInfoInternal;
  344. wxDynamicClassInfoInternal* m_data;
  345. };
  346. // ----------------------------------------------------------------------------
  347. // wxDECLARE class macros
  348. // ----------------------------------------------------------------------------
  349. #define _DECLARE_DYNAMIC_CLASS(name) \
  350. public: \
  351. static wxClassInfo ms_classInfo; \
  352. static const wxClassInfo* ms_classParents[]; \
  353. static wxPropertyInfo* GetPropertiesStatic(); \
  354. static wxHandlerInfo* GetHandlersStatic(); \
  355. static wxClassInfo *GetClassInfoStatic() \
  356. { return &name::ms_classInfo; } \
  357. virtual wxClassInfo *GetClassInfo() const \
  358. { return &name::ms_classInfo; }
  359. #define wxDECLARE_DYNAMIC_CLASS(name) \
  360. static wxObjectAllocatorAndCreator* ms_constructor; \
  361. static const wxChar * ms_constructorProperties[]; \
  362. static const int ms_constructorPropertiesCount; \
  363. _DECLARE_DYNAMIC_CLASS(name)
  364. #define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
  365. wxDECLARE_NO_ASSIGN_CLASS(name); \
  366. wxDECLARE_DYNAMIC_CLASS(name)
  367. #define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
  368. wxDECLARE_NO_COPY_CLASS(name); \
  369. wxDECLARE_DYNAMIC_CLASS(name)
  370. #define wxDECLARE_CLASS(name) \
  371. wxDECLARE_DYNAMIC_CLASS(name)
  372. #define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
  373. #define wxCLASSINFO(name) (&name::ms_classInfo)
  374. #endif // wxUSE_EXTENDED_RTTI
  375. #endif // _WX_XTIH__