1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: runtime metadata information (extended class info)
4 // Author: Stefan Csomor
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
16 // We want to support properties, event sources and events sinks through
17 // explicit declarations, using templates and specialization to make the
18 // effort as painless as possible.
20 // This means we have the following domains :
22 // - Type Information for categorizing built in types as well as custom types
23 // this includes information about enums, their values and names
24 // - Type safe value storage : a kind of wxVariant, called right now wxAny
25 // which will be merged with wxVariant
26 // - Property Information and Property Accessors providing access to a class'
27 // values and exposed event delegates
28 // - Information about event handlers
29 // - extended Class Information for accessing all these
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
37 #if wxUSE_EXTENDED_RTTI
39 class WXDLLIMPEXP_FWD_BASE wxAny
;
40 class WXDLLIMPEXP_FWD_BASE wxAnyList
;
41 class WXDLLIMPEXP_FWD_BASE wxObject
;
42 class WXDLLIMPEXP_FWD_BASE wxString
;
43 class WXDLLIMPEXP_FWD_BASE wxClassInfo
;
44 class WXDLLIMPEXP_FWD_BASE wxHashTable
;
45 class WXDLLIMPEXP_FWD_BASE wxObject
;
46 class WXDLLIMPEXP_FWD_BASE wxPluginLibrary
;
47 class WXDLLIMPEXP_FWD_BASE wxHashTable
;
48 class WXDLLIMPEXP_FWD_BASE wxHashTable_Node
;
50 class WXDLLIMPEXP_FWD_BASE wxStringToAnyHashMap
;
51 class WXDLLIMPEXP_FWD_BASE wxPropertyInfoMap
;
52 class WXDLLIMPEXP_FWD_BASE wxPropertyAccessor
;
53 class WXDLLIMPEXP_FWD_BASE wxObjectAllocatorAndCreator
;
54 class WXDLLIMPEXP_FWD_BASE wxObjectAllocator
;
57 #define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
59 #include "wx/xtitypes.h"
60 #include "wx/xtihandler.h"
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class WXDLLIMPEXP_BASE wxObjectFunctor
69 virtual ~wxObjectFunctor();
71 // Invoke the actual event handler:
72 virtual void operator()(const wxObject
*) = 0;
75 class WXDLLIMPEXP_FWD_BASE wxPropertyInfo
;
76 class WXDLLIMPEXP_FWD_BASE wxHandlerInfo
;
78 typedef wxObject
*(*wxObjectConstructorFn
)(void);
79 typedef wxPropertyInfo
*(*wxPropertyInfoFn
)(void);
80 typedef wxHandlerInfo
*(*wxHandlerInfoFn
)(void);
81 typedef void (*wxVariantToObjectConverter
)( const wxAny
&data
, wxObjectFunctor
* fn
);
82 typedef wxObject
* (*wxVariantToObjectPtrConverter
) ( const wxAny
& data
);
83 typedef wxAny (*wxObjectToVariantConverter
)( wxObject
* );
85 WXDLLIMPEXP_BASE wxString
wxAnyGetAsString( const wxAny
& data
);
86 WXDLLIMPEXP_BASE
const wxObject
* wxAnyGetAsObjectPtr( const wxAny
& data
);
88 class WXDLLIMPEXP_BASE wxObjectWriter
;
89 class WXDLLIMPEXP_BASE wxObjectWriterCallback
;
91 typedef bool (*wxObjectStreamingCallback
) ( const wxObject
*, wxObjectWriter
*, \
92 wxObjectWriterCallback
*, const wxStringToAnyHashMap
& );
96 class WXDLLIMPEXP_BASE wxClassInfo
98 friend class WXDLLIMPEXP_BASE wxPropertyInfo
;
99 friend class WXDLLIMPEXP_BASE wxHandlerInfo
;
100 friend wxObject
*wxCreateDynamicObject(const wxString
& name
);
103 wxClassInfo(const wxClassInfo
**_Parents
,
104 const wxChar
*_UnitName
,
105 const wxChar
*_ClassName
,
107 wxObjectConstructorFn ctor
,
108 wxPropertyInfoFn _Props
,
109 wxHandlerInfoFn _Handlers
,
110 wxObjectAllocatorAndCreator
* _Constructor
,
111 const wxChar
** _ConstructorProperties
,
112 const int _ConstructorPropertiesCount
,
113 wxVariantToObjectPtrConverter _PtrConverter1
,
114 wxVariantToObjectConverter _Converter2
,
115 wxObjectToVariantConverter _Converter3
,
116 wxObjectStreamingCallback _streamingCallback
= NULL
) :
117 m_className(_ClassName
),
119 m_objectConstructor(ctor
),
121 m_firstPropertyFn(_Props
),
122 m_firstHandlerFn(_Handlers
),
123 m_firstProperty(NULL
),
124 m_firstHandler(NULL
),
125 m_firstInited(false),
127 m_unitName(_UnitName
),
128 m_constructor(_Constructor
),
129 m_constructorProperties(_ConstructorProperties
),
130 m_constructorPropertiesCount(_ConstructorPropertiesCount
),
131 m_variantOfPtrToObjectConverter(_PtrConverter1
),
132 m_variantToObjectConverter(_Converter2
),
133 m_objectToVariantConverter(_Converter3
),
134 m_streamingCallback(_streamingCallback
)
140 wxClassInfo(const wxChar
*_UnitName
, const wxChar
*_ClassName
,
141 const wxClassInfo
**_Parents
) :
142 m_className(_ClassName
),
144 m_objectConstructor(NULL
),
146 m_firstPropertyFn(NULL
),
147 m_firstHandlerFn(NULL
),
148 m_firstProperty(NULL
),
149 m_firstHandler(NULL
),
152 m_unitName(_UnitName
),
154 m_constructorProperties(NULL
),
155 m_constructorPropertiesCount(0),
156 m_variantOfPtrToObjectConverter(NULL
),
157 m_variantToObjectConverter(NULL
),
158 m_objectToVariantConverter(NULL
),
159 m_streamingCallback(NULL
)
165 // ctor compatible with old RTTI system
166 wxClassInfo(const wxChar
*_ClassName
,
167 const wxClassInfo
*_Parent1
,
168 const wxClassInfo
*_Parent2
,
170 wxObjectConstructorFn ctor
) :
171 m_className(_ClassName
),
173 m_objectConstructor(ctor
),
175 m_firstPropertyFn(NULL
),
176 m_firstHandlerFn(NULL
),
177 m_firstProperty(NULL
),
178 m_firstHandler(NULL
),
183 m_constructorProperties(NULL
),
184 m_constructorPropertiesCount(0),
185 m_variantOfPtrToObjectConverter(NULL
),
186 m_variantToObjectConverter(NULL
),
187 m_objectToVariantConverter(NULL
),
188 m_streamingCallback(NULL
)
191 m_parents
[0] = _Parent1
;
192 m_parents
[1] = _Parent2
;
197 virtual ~wxClassInfo();
199 // allocates an instance of this class, this object does not have to be
200 // initialized or fully constructed as this call will be followed by a call to Create
201 virtual wxObject
*AllocateObject() const
202 { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
204 // 'old naming' for AllocateObject staying here for backward compatibility
205 wxObject
*CreateObject() const { return AllocateObject(); }
207 // direct construction call for classes that cannot construct instances via alloc/create
208 wxObject
*ConstructObject(int ParamCount
, wxAny
*Params
) const;
210 bool NeedsDirectConstruction() const;
212 const wxChar
*GetClassName() const
213 { return m_className
; }
214 const wxChar
*GetBaseClassName1() const
215 { return m_parents
[0] ? m_parents
[0]->GetClassName() : NULL
; }
216 const wxChar
*GetBaseClassName2() const
217 { return (m_parents
[0] && m_parents
[1]) ? m_parents
[1]->GetClassName() : NULL
; }
219 const wxClassInfo
*GetBaseClass1() const
220 { return m_parents
[0]; }
221 const wxClassInfo
*GetBaseClass2() const
222 { return m_parents
[0] ? m_parents
[1] : NULL
; }
224 const wxChar
*GetIncludeName() const
225 { return m_unitName
; }
226 const wxClassInfo
**GetParents() const
227 { return m_parents
; }
229 { return m_objectSize
; }
230 bool IsDynamic() const
231 { return (NULL
!= m_objectConstructor
); }
233 wxObjectConstructorFn
GetConstructor() const
234 { return m_objectConstructor
; }
235 const wxClassInfo
*GetNext() const
240 static void CleanUp();
241 static wxClassInfo
*FindClass(const wxString
& className
);
242 static const wxClassInfo
*GetFirst()
246 // Climb upwards through inheritance hierarchy.
247 // Dual inheritance is catered for.
249 bool IsKindOf(const wxClassInfo
*info
) const;
251 wxDECLARE_CLASS_INFO_ITERATORS();
253 // if there is a callback registered with that class it will be called
254 // before this object will be written to disk, it can veto streaming out
255 // this object by returning false, if this class has not registered a
256 // callback, the search will go up the inheritance tree if no callback has
257 // been registered true will be returned by default
258 bool BeforeWriteObject( const wxObject
*obj
, wxObjectWriter
*streamer
,
259 wxObjectWriterCallback
*writercallback
, const wxStringToAnyHashMap
&metadata
) const;
261 // gets the streaming callback from this class or any superclass
262 wxObjectStreamingCallback
GetStreamingCallback() const;
264 // returns the first property
265 wxPropertyInfo
* GetFirstProperty() const
266 { EnsureInfosInited(); return m_firstProperty
; }
268 // returns the first handler
269 wxHandlerInfo
* GetFirstHandler() const
270 { EnsureInfosInited(); return m_firstHandler
; }
272 // Call the Create upon an instance of the class, in the end the object is fully
274 virtual bool Create (wxObject
*object
, int ParamCount
, wxAny
*Params
) const;
276 // get number of parameters for constructor
277 virtual int GetCreateParamCount() const
278 { return m_constructorPropertiesCount
; }
280 // get n-th constructor parameter
281 virtual const wxChar
* GetCreateParamName(int n
) const
282 { return m_constructorProperties
[n
]; }
284 // Runtime access to objects for simple properties (get/set) by property
285 // name and variant data
286 virtual void SetProperty (wxObject
*object
, const wxChar
*propertyName
,
287 const wxAny
&value
) const;
288 virtual wxAny
GetProperty (wxObject
*object
, const wxChar
*propertyName
) const;
290 // Runtime access to objects for collection properties by property name
291 virtual wxAnyList
GetPropertyCollection(wxObject
*object
,
292 const wxChar
*propertyName
) const;
293 virtual void AddToPropertyCollection(wxObject
*object
, const wxChar
*propertyName
,
294 const wxAny
& value
) const;
296 // we must be able to cast variants to wxObject pointers, templates seem
297 // not to be suitable
298 void CallOnAny( const wxAny
&data
, wxObjectFunctor
* functor
) const;
300 wxObject
* AnyToObjectPtr( const wxAny
&data
) const;
302 wxAny
ObjectPtrToAny( wxObject
*object
) const;
304 // find property by name
305 virtual const wxPropertyInfo
*FindPropertyInfo (const wxChar
*PropertyName
) const;
307 // find handler by name
308 virtual const wxHandlerInfo
*FindHandlerInfo (const wxChar
*handlerName
) const;
310 // find property by name
311 virtual wxPropertyInfo
*FindPropertyInfoInThisClass (const wxChar
*PropertyName
) const;
313 // find handler by name
314 virtual wxHandlerInfo
*FindHandlerInfoInThisClass (const wxChar
*handlerName
) const;
316 // puts all the properties of this class and its superclasses in the map,
317 // as long as there is not yet an entry with the same name (overriding mechanism)
318 void GetProperties( wxPropertyInfoMap
&map
) const;
321 const wxChar
*m_className
;
323 wxObjectConstructorFn m_objectConstructor
;
325 // class info object live in a linked list:
326 // pointers to its head and the next element in it
328 static wxClassInfo
*sm_first
;
331 static wxHashTable
*sm_classTable
;
333 wxPropertyInfoFn m_firstPropertyFn
;
334 wxHandlerInfoFn m_firstHandlerFn
;
336 mutable bool m_firstInited
;
339 void EnsureInfosInited() const
343 if ( m_firstPropertyFn
!= NULL
)
344 m_firstProperty
= (*m_firstPropertyFn
)();
345 if ( m_firstHandlerFn
!= NULL
)
346 m_firstHandler
= (*m_firstHandlerFn
)();
347 m_firstInited
= true;
350 mutable wxPropertyInfo
* m_firstProperty
;
351 mutable wxHandlerInfo
* m_firstHandler
;
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