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
;
338 void EnsureInfosInited() const
342 if ( m_firstPropertyFn
!= NULL
)
343 m_firstProperty
= (*m_firstPropertyFn
)();
344 if ( m_firstHandlerFn
!= NULL
)
345 m_firstHandler
= (*m_firstHandlerFn
)();
346 m_firstInited
= true;
349 mutable wxPropertyInfo
* m_firstProperty
;
350 mutable wxHandlerInfo
* m_firstHandler
;
353 mutable bool m_firstInited
;
355 const wxClassInfo
** m_parents
;
356 const wxChar
* m_unitName
;
358 wxObjectAllocatorAndCreator
* m_constructor
;
359 const wxChar
** m_constructorProperties
;
360 const int m_constructorPropertiesCount
;
361 wxVariantToObjectPtrConverter m_variantOfPtrToObjectConverter
;
362 wxVariantToObjectConverter m_variantToObjectConverter
;
363 wxObjectToVariantConverter m_objectToVariantConverter
;
364 wxObjectStreamingCallback m_streamingCallback
;
366 const wxPropertyAccessor
*FindAccessor (const wxChar
*propertyName
) const;
369 // registers the class
373 DECLARE_NO_COPY_CLASS(wxClassInfo
)
376 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxString
& name
);
378 // ----------------------------------------------------------------------------
379 // wxDynamicClassInfo
380 // ----------------------------------------------------------------------------
382 // this object leads to having a pure runtime-instantiation
384 class WXDLLIMPEXP_BASE wxDynamicClassInfo
: public wxClassInfo
386 friend class WXDLLIMPEXP_BASE wxDynamicObject
;
389 wxDynamicClassInfo( const wxChar
*_UnitName
, const wxChar
*_ClassName
,
390 const wxClassInfo
* superClass
);
391 virtual ~wxDynamicClassInfo();
393 // constructs a wxDynamicObject with an instance
394 virtual wxObject
*AllocateObject() const;
396 // Call the Create method for a class
397 virtual bool Create (wxObject
*object
, int ParamCount
, wxAny
*Params
) const;
399 // get number of parameters for constructor
400 virtual int GetCreateParamCount() const;
402 // get i-th constructor parameter
403 virtual const wxChar
* GetCreateParamName(int i
) const;
405 // Runtime access to objects by property name, and variant data
406 virtual void SetProperty (wxObject
*object
, const wxChar
*PropertyName
,
407 const wxAny
&Value
) const;
408 virtual wxAny
GetProperty (wxObject
*object
, const wxChar
*PropertyName
) const;
410 // adds a property to this class at runtime
411 void AddProperty( const wxChar
*propertyName
, const wxTypeInfo
* typeInfo
);
413 // removes an existing runtime-property
414 void RemoveProperty( const wxChar
*propertyName
);
416 // renames an existing runtime-property
417 void RenameProperty( const wxChar
*oldPropertyName
, const wxChar
*newPropertyName
);
419 // as a handler to this class at runtime
420 void AddHandler( const wxChar
*handlerName
, wxObjectEventFunction address
,
421 const wxClassInfo
* eventClassInfo
);
423 // removes an existing runtime-handler
424 void RemoveHandler( const wxChar
*handlerName
);
426 // renames an existing runtime-handler
427 void RenameHandler( const wxChar
*oldHandlerName
, const wxChar
*newHandlerName
);
430 struct wxDynamicClassInfoInternal
;
431 wxDynamicClassInfoInternal
* m_data
;
434 // ----------------------------------------------------------------------------
435 // wxDECLARE class macros
436 // ----------------------------------------------------------------------------
438 #define _DECLARE_DYNAMIC_CLASS(name) \
440 static wxClassInfo ms_classInfo; \
441 static const wxClassInfo* ms_classParents[]; \
442 static wxPropertyInfo* GetPropertiesStatic(); \
443 static wxHandlerInfo* GetHandlersStatic(); \
444 static wxClassInfo *GetClassInfoStatic() \
445 { return &name::ms_classInfo; } \
446 virtual wxClassInfo *GetClassInfo() const \
447 { return &name::ms_classInfo; }
449 #define wxDECLARE_DYNAMIC_CLASS(name) \
450 static wxObjectAllocatorAndCreator* ms_constructor; \
451 static const wxChar * ms_constructorProperties[]; \
452 static const int ms_constructorPropertiesCount; \
453 _DECLARE_DYNAMIC_CLASS(name)
455 #define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
456 wxDECLARE_NO_ASSIGN_CLASS(name); \
457 wxDECLARE_DYNAMIC_CLASS(name)
459 #define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
460 wxDECLARE_NO_COPY_CLASS(name); \
461 wxDECLARE_DYNAMIC_CLASS(name)
463 #define wxDECLARE_CLASS(name) \
464 wxDECLARE_DYNAMIC_CLASS(name)
466 #define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
467 #define wxCLASSINFO(name) (&name::ms_classInfo)
469 #endif // wxUSE_EXTENDED_RTTI