1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: runtime metadata information (extended class info)
4 // Author: Stefan Csomor
5 // Modified by: Francesco Montorsi
7 // Copyright: (c) 1997 Julian Smart
8 // (c) 2003 Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 // We want to support properties, event sources and events sinks through
16 // explicit declarations, using templates and specialization to make the
17 // effort as painless as possible.
19 // This means we have the following domains :
21 // - Type Information for categorizing built in types as well as custom types
22 // this includes information about enums, their values and names
23 // - Type safe value storage : a kind of wxVariant, called right now wxAny
24 // which will be merged with wxVariant
25 // - Property Information and Property Accessors providing access to a class'
26 // values and exposed event delegates
27 // - Information about event handlers
28 // - extended Class Information for accessing all these
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
36 #if wxUSE_EXTENDED_RTTI
38 class WXDLLIMPEXP_FWD_BASE wxAny
;
39 class WXDLLIMPEXP_FWD_BASE wxAnyList
;
40 class WXDLLIMPEXP_FWD_BASE wxObject
;
41 class WXDLLIMPEXP_FWD_BASE wxString
;
42 class WXDLLIMPEXP_FWD_BASE wxClassInfo
;
43 class WXDLLIMPEXP_FWD_BASE wxHashTable
;
44 class WXDLLIMPEXP_FWD_BASE wxObject
;
45 class WXDLLIMPEXP_FWD_BASE wxPluginLibrary
;
46 class WXDLLIMPEXP_FWD_BASE wxHashTable
;
47 class WXDLLIMPEXP_FWD_BASE wxHashTable_Node
;
49 class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap
;
50 class WXDLLIMPEXP_FWD_BASE wxPropertyInfoMap
;
51 class WXDLLIMPEXP_FWD_BASE wxPropertyAccessor
;
52 class WXDLLIMPEXP_FWD_BASE wxObjectAllocatorAndCreator
;
53 class WXDLLIMPEXP_FWD_BASE wxObjectAllocator
;
56 #define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
58 #include "wx/xtitypes.h"
59 #include "wx/xtihandler.h"
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 class WXDLLIMPEXP_BASE wxObjectFunctor
68 virtual ~wxObjectFunctor();
70 // Invoke the actual event handler:
71 virtual void operator()(const wxObject
*) = 0;
74 class WXDLLIMPEXP_FWD_BASE wxPropertyInfo
;
75 class WXDLLIMPEXP_FWD_BASE wxHandlerInfo
;
77 typedef wxObject
*(*wxObjectConstructorFn
)(void);
78 typedef wxPropertyInfo
*(*wxPropertyInfoFn
)(void);
79 typedef wxHandlerInfo
*(*wxHandlerInfoFn
)(void);
80 typedef void (*wxVariantToObjectConverter
)( const wxAny
&data
, wxObjectFunctor
* fn
);
81 typedef wxObject
* (*wxVariantToObjectPtrConverter
) ( const wxAny
& data
);
82 typedef wxAny (*wxObjectToVariantConverter
)( wxObject
* );
84 WXDLLIMPEXP_BASE wxString
wxAnyGetAsString( const wxAny
& data
);
85 WXDLLIMPEXP_BASE
const wxObject
* wxAnyGetAsObjectPtr( const wxAny
& data
);
87 class WXDLLIMPEXP_BASE wxObjectWriter
;
88 class WXDLLIMPEXP_BASE wxObjectWriterCallback
;
90 typedef bool (*wxObjectStreamingCallback
) ( const wxObject
*, wxObjectWriter
*, \
91 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& );
95 class WXDLLIMPEXP_BASE wxClassInfo
97 friend class WXDLLIMPEXP_BASE wxPropertyInfo
;
98 friend class /* WXDLLIMPEXP_BASE */ wxHandlerInfo
;
99 friend wxObject
*wxCreateDynamicObject(const wxString
& name
);
102 wxClassInfo(const wxClassInfo
**_Parents
,
103 const wxChar
*_UnitName
,
104 const wxChar
*_ClassName
,
106 wxObjectConstructorFn ctor
,
107 wxPropertyInfoFn _Props
,
108 wxHandlerInfoFn _Handlers
,
109 wxObjectAllocatorAndCreator
* _Constructor
,
110 const wxChar
** _ConstructorProperties
,
111 const int _ConstructorPropertiesCount
,
112 wxVariantToObjectPtrConverter _PtrConverter1
,
113 wxVariantToObjectConverter _Converter2
,
114 wxObjectToVariantConverter _Converter3
,
115 wxObjectStreamingCallback _streamingCallback
= NULL
) :
116 m_className(_ClassName
),
118 m_objectConstructor(ctor
),
120 m_firstPropertyFn(_Props
),
121 m_firstHandlerFn(_Handlers
),
122 m_firstProperty(NULL
),
123 m_firstHandler(NULL
),
124 m_firstInited(false),
126 m_unitName(_UnitName
),
127 m_constructor(_Constructor
),
128 m_constructorProperties(_ConstructorProperties
),
129 m_constructorPropertiesCount(_ConstructorPropertiesCount
),
130 m_variantOfPtrToObjectConverter(_PtrConverter1
),
131 m_variantToObjectConverter(_Converter2
),
132 m_objectToVariantConverter(_Converter3
),
133 m_streamingCallback(_streamingCallback
)
139 wxClassInfo(const wxChar
*_UnitName
, const wxChar
*_ClassName
,
140 const wxClassInfo
**_Parents
) :
141 m_className(_ClassName
),
143 m_objectConstructor(NULL
),
145 m_firstPropertyFn(NULL
),
146 m_firstHandlerFn(NULL
),
147 m_firstProperty(NULL
),
148 m_firstHandler(NULL
),
151 m_unitName(_UnitName
),
153 m_constructorProperties(NULL
),
154 m_constructorPropertiesCount(0),
155 m_variantOfPtrToObjectConverter(NULL
),
156 m_variantToObjectConverter(NULL
),
157 m_objectToVariantConverter(NULL
),
158 m_streamingCallback(NULL
)
164 // ctor compatible with old RTTI system
165 wxClassInfo(const wxChar
*_ClassName
,
166 const wxClassInfo
*_Parent1
,
167 const wxClassInfo
*_Parent2
,
169 wxObjectConstructorFn ctor
) :
170 m_className(_ClassName
),
172 m_objectConstructor(ctor
),
174 m_firstPropertyFn(NULL
),
175 m_firstHandlerFn(NULL
),
176 m_firstProperty(NULL
),
177 m_firstHandler(NULL
),
182 m_constructorProperties(NULL
),
183 m_constructorPropertiesCount(0),
184 m_variantOfPtrToObjectConverter(NULL
),
185 m_variantToObjectConverter(NULL
),
186 m_objectToVariantConverter(NULL
),
187 m_streamingCallback(NULL
)
190 m_parents
[0] = _Parent1
;
191 m_parents
[1] = _Parent2
;
196 virtual ~wxClassInfo();
198 // allocates an instance of this class, this object does not have to be
199 // initialized or fully constructed as this call will be followed by a call to Create
200 virtual wxObject
*AllocateObject() const
201 { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
203 // 'old naming' for AllocateObject staying here for backward compatibility
204 wxObject
*CreateObject() const { return AllocateObject(); }
206 // direct construction call for classes that cannot construct instances via alloc/create
207 wxObject
*ConstructObject(int ParamCount
, wxAny
*Params
) const;
209 bool NeedsDirectConstruction() const;
211 const wxChar
*GetClassName() const
212 { return m_className
; }
213 const wxChar
*GetBaseClassName1() const
214 { return m_parents
[0] ? m_parents
[0]->GetClassName() : NULL
; }
215 const wxChar
*GetBaseClassName2() const
216 { return (m_parents
[0] && m_parents
[1]) ? m_parents
[1]->GetClassName() : NULL
; }
218 const wxClassInfo
*GetBaseClass1() const
219 { return m_parents
[0]; }
220 const wxClassInfo
*GetBaseClass2() const
221 { return m_parents
[0] ? m_parents
[1] : NULL
; }
223 const wxChar
*GetIncludeName() const
224 { return m_unitName
; }
225 const wxClassInfo
**GetParents() const
226 { return m_parents
; }
228 { return m_objectSize
; }
229 bool IsDynamic() const
230 { return (NULL
!= m_objectConstructor
); }
232 wxObjectConstructorFn
GetConstructor() const
233 { return m_objectConstructor
; }
234 const wxClassInfo
*GetNext() const
239 static void CleanUp();
240 static wxClassInfo
*FindClass(const wxString
& className
);
241 static const wxClassInfo
*GetFirst()
245 // Climb upwards through inheritance hierarchy.
246 // Dual inheritance is catered for.
248 bool IsKindOf(const wxClassInfo
*info
) const;
250 wxDECLARE_CLASS_INFO_ITERATORS();
252 // if there is a callback registered with that class it will be called
253 // before this object will be written to disk, it can veto streaming out
254 // this object by returning false, if this class has not registered a
255 // callback, the search will go up the inheritance tree if no callback has
256 // been registered true will be returned by default
257 bool BeforeWriteObject( const wxObject
*obj
, wxObjectWriter
*streamer
,
258 wxObjectWriterCallback
*writercallback
, const wxStringToAnyHashMap
&metadata
) const;
260 // gets the streaming callback from this class or any superclass
261 wxObjectStreamingCallback
GetStreamingCallback() const;
263 // returns the first property
264 wxPropertyInfo
* GetFirstProperty() const
265 { EnsureInfosInited(); return m_firstProperty
; }
267 // returns the first handler
268 wxHandlerInfo
* GetFirstHandler() const
269 { EnsureInfosInited(); return m_firstHandler
; }
271 // Call the Create upon an instance of the class, in the end the object is fully
273 virtual bool Create (wxObject
*object
, int ParamCount
, wxAny
*Params
) const;
275 // get number of parameters for constructor
276 virtual int GetCreateParamCount() const
277 { return m_constructorPropertiesCount
; }
279 // get n-th constructor parameter
280 virtual const wxChar
* GetCreateParamName(int n
) const
281 { return m_constructorProperties
[n
]; }
283 // Runtime access to objects for simple properties (get/set) by property
284 // name and variant data
285 virtual void SetProperty (wxObject
*object
, const wxChar
*propertyName
,
286 const wxAny
&value
) const;
287 virtual wxAny
GetProperty (wxObject
*object
, const wxChar
*propertyName
) const;
289 // Runtime access to objects for collection properties by property name
290 virtual wxAnyList
GetPropertyCollection(wxObject
*object
,
291 const wxChar
*propertyName
) const;
292 virtual void AddToPropertyCollection(wxObject
*object
, const wxChar
*propertyName
,
293 const wxAny
& value
) const;
295 // we must be able to cast variants to wxObject pointers, templates seem
296 // not to be suitable
297 void CallOnAny( const wxAny
&data
, wxObjectFunctor
* functor
) const;
299 wxObject
* AnyToObjectPtr( const wxAny
&data
) const;
301 wxAny
ObjectPtrToAny( wxObject
*object
) const;
303 // find property by name
304 virtual const wxPropertyInfo
*FindPropertyInfo (const wxChar
*PropertyName
) const;
306 // find handler by name
307 virtual const wxHandlerInfo
*FindHandlerInfo (const wxChar
*handlerName
) const;
309 // find property by name
310 virtual wxPropertyInfo
*FindPropertyInfoInThisClass (const wxChar
*PropertyName
) const;
312 // find handler by name
313 virtual wxHandlerInfo
*FindHandlerInfoInThisClass (const wxChar
*handlerName
) const;
315 // puts all the properties of this class and its superclasses in the map,
316 // as long as there is not yet an entry with the same name (overriding mechanism)
317 void GetProperties( wxPropertyInfoMap
&map
) const;
320 const wxChar
*m_className
;
322 wxObjectConstructorFn m_objectConstructor
;
324 // class info object live in a linked list:
325 // pointers to its head and the next element in it
327 static wxClassInfo
*sm_first
;
330 static wxHashTable
*sm_classTable
;
332 wxPropertyInfoFn m_firstPropertyFn
;
333 wxHandlerInfoFn m_firstHandlerFn
;
337 void EnsureInfosInited() const
341 if ( m_firstPropertyFn
!= NULL
)
342 m_firstProperty
= (*m_firstPropertyFn
)();
343 if ( m_firstHandlerFn
!= NULL
)
344 m_firstHandler
= (*m_firstHandlerFn
)();
345 m_firstInited
= true;
348 mutable wxPropertyInfo
* m_firstProperty
;
349 mutable wxHandlerInfo
* m_firstHandler
;
352 mutable bool m_firstInited
;
354 const wxClassInfo
** m_parents
;
355 const wxChar
* m_unitName
;
357 wxObjectAllocatorAndCreator
* m_constructor
;
358 const wxChar
** m_constructorProperties
;
359 const int m_constructorPropertiesCount
;
360 wxVariantToObjectPtrConverter m_variantOfPtrToObjectConverter
;
361 wxVariantToObjectConverter m_variantToObjectConverter
;
362 wxObjectToVariantConverter m_objectToVariantConverter
;
363 wxObjectStreamingCallback m_streamingCallback
;
365 const wxPropertyAccessor
*FindAccessor (const wxChar
*propertyName
) const;
368 // registers the class
372 DECLARE_NO_COPY_CLASS(wxClassInfo
)
375 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxString
& name
);
377 // ----------------------------------------------------------------------------
378 // wxDynamicClassInfo
379 // ----------------------------------------------------------------------------
381 // this object leads to having a pure runtime-instantiation
383 class WXDLLIMPEXP_BASE wxDynamicClassInfo
: public wxClassInfo
385 friend class WXDLLIMPEXP_BASE wxDynamicObject
;
388 wxDynamicClassInfo( const wxChar
*_UnitName
, const wxChar
*_ClassName
,
389 const wxClassInfo
* superClass
);
390 virtual ~wxDynamicClassInfo();
392 // constructs a wxDynamicObject with an instance
393 virtual wxObject
*AllocateObject() const;
395 // Call the Create method for a class
396 virtual bool Create (wxObject
*object
, int ParamCount
, wxAny
*Params
) const;
398 // get number of parameters for constructor
399 virtual int GetCreateParamCount() const;
401 // get i-th constructor parameter
402 virtual const wxChar
* GetCreateParamName(int i
) const;
404 // Runtime access to objects by property name, and variant data
405 virtual void SetProperty (wxObject
*object
, const wxChar
*PropertyName
,
406 const wxAny
&Value
) const;
407 virtual wxAny
GetProperty (wxObject
*object
, const wxChar
*PropertyName
) const;
409 // adds a property to this class at runtime
410 void AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
);
412 // removes an existing runtime-property
413 void RemoveProperty( const wxChar
*propertyName
);
415 // renames an existing runtime-property
416 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
);
418 // as a handler to this class at runtime
419 void AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
,
420 const wxClassInfo
* eventClassInfo
);
422 // removes an existing runtime-handler
423 void RemoveHandler( const wxChar
*handlerName
);
425 // renames an existing runtime-handler
426 void RenameHandler( const wxChar
*oldHandlerName
, const wxChar
*newHandlerName
);
429 struct wxDynamicClassInfoInternal
;
430 wxDynamicClassInfoInternal
* m_data
;
433 // ----------------------------------------------------------------------------
434 // wxDECLARE class macros
435 // ----------------------------------------------------------------------------
437 #define _DECLARE_DYNAMIC_CLASS(name) \
439 static wxClassInfo ms_classInfo; \
440 static const wxClassInfo* ms_classParents[]; \
441 static wxPropertyInfo* GetPropertiesStatic(); \
442 static wxHandlerInfo* GetHandlersStatic(); \
443 static wxClassInfo *GetClassInfoStatic() \
444 { return &name::ms_classInfo; } \
445 virtual wxClassInfo *GetClassInfo() const \
446 { return &name::ms_classInfo; }
448 #define wxDECLARE_DYNAMIC_CLASS(name) \
449 static wxObjectAllocatorAndCreator* ms_constructor; \
450 static const wxChar * ms_constructorProperties[]; \
451 static const int ms_constructorPropertiesCount; \
452 _DECLARE_DYNAMIC_CLASS(name)
454 #define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
455 wxDECLARE_NO_ASSIGN_CLASS(name); \
456 wxDECLARE_DYNAMIC_CLASS(name)
458 #define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
459 wxDECLARE_NO_COPY_CLASS(name); \
460 wxDECLARE_DYNAMIC_CLASS(name)
462 #define wxDECLARE_CLASS(name) \
463 wxDECLARE_DYNAMIC_CLASS(name)
465 #define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
466 #define wxCLASSINFO(name) (&name::ms_classInfo)
468 #endif // wxUSE_EXTENDED_RTTI