]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xti.h
synchronize GTK2 minimum version in docs
[wxWidgets.git] / include / wx / xti.h
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 // RCS-ID: $Id$
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_XTIH__
14 #define _WX_XTIH__
15
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.
19 //
20 // This means we have the following domains :
21 //
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
30
31 // ----------------------------------------------------------------------------
32 // headers
33 // ----------------------------------------------------------------------------
34
35 #include "wx/defs.h"
36
37 #if wxUSE_EXTENDED_RTTI
38
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;
49
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;
55
56
57 #define wx_dynamic_cast(t, x) dynamic_cast<t>(x)
58
59 #include "wx/xtitypes.h"
60 #include "wx/xtihandler.h"
61
62 // ----------------------------------------------------------------------------
63 // wxClassInfo
64 // ----------------------------------------------------------------------------
65
66 class WXDLLIMPEXP_BASE wxObjectFunctor
67 {
68 public:
69 virtual ~wxObjectFunctor();
70
71 // Invoke the actual event handler:
72 virtual void operator()(const wxObject *) = 0;
73 };
74
75 class WXDLLIMPEXP_FWD_BASE wxPropertyInfo;
76 class WXDLLIMPEXP_FWD_BASE wxHandlerInfo;
77
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* );
84
85 WXDLLIMPEXP_BASE wxString wxAnyGetAsString( const wxAny& data);
86 WXDLLIMPEXP_BASE const wxObject* wxAnyGetAsObjectPtr( const wxAny& data);
87
88 class WXDLLIMPEXP_BASE wxObjectWriter;
89 class WXDLLIMPEXP_BASE wxObjectWriterCallback;
90
91 typedef bool (*wxObjectStreamingCallback) ( const wxObject *, wxObjectWriter *, \
92 wxObjectWriterCallback *, const wxStringToAnyHashMap & );
93
94
95
96 class WXDLLIMPEXP_BASE wxClassInfo
97 {
98 friend class WXDLLIMPEXP_BASE wxPropertyInfo;
99 friend class /* WXDLLIMPEXP_BASE */ wxHandlerInfo;
100 friend wxObject *wxCreateDynamicObject(const wxString& name);
101
102 public:
103 wxClassInfo(const wxClassInfo **_Parents,
104 const wxChar *_UnitName,
105 const wxChar *_ClassName,
106 int size,
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),
118 m_objectSize(size),
119 m_objectConstructor(ctor),
120 m_next(sm_first),
121 m_firstPropertyFn(_Props),
122 m_firstHandlerFn(_Handlers),
123 m_firstProperty(NULL),
124 m_firstHandler(NULL),
125 m_firstInited(false),
126 m_parents(_Parents),
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)
135 {
136 sm_first = this;
137 Register();
138 }
139
140 wxClassInfo(const wxChar *_UnitName, const wxChar *_ClassName,
141 const wxClassInfo **_Parents) :
142 m_className(_ClassName),
143 m_objectSize(0),
144 m_objectConstructor(NULL),
145 m_next(sm_first),
146 m_firstPropertyFn(NULL),
147 m_firstHandlerFn(NULL),
148 m_firstProperty(NULL),
149 m_firstHandler(NULL),
150 m_firstInited(true),
151 m_parents(_Parents),
152 m_unitName(_UnitName),
153 m_constructor(NULL),
154 m_constructorProperties(NULL),
155 m_constructorPropertiesCount(0),
156 m_variantOfPtrToObjectConverter(NULL),
157 m_variantToObjectConverter(NULL),
158 m_objectToVariantConverter(NULL),
159 m_streamingCallback(NULL)
160 {
161 sm_first = this;
162 Register();
163 }
164
165 // ctor compatible with old RTTI system
166 wxClassInfo(const wxChar *_ClassName,
167 const wxClassInfo *_Parent1,
168 const wxClassInfo *_Parent2,
169 int size,
170 wxObjectConstructorFn ctor) :
171 m_className(_ClassName),
172 m_objectSize(size),
173 m_objectConstructor(ctor),
174 m_next(sm_first),
175 m_firstPropertyFn(NULL),
176 m_firstHandlerFn(NULL),
177 m_firstProperty(NULL),
178 m_firstHandler(NULL),
179 m_firstInited(true),
180 m_parents(NULL),
181 m_unitName(NULL),
182 m_constructor(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)
189 {
190 sm_first = this;
191 m_parents[0] = _Parent1;
192 m_parents[1] = _Parent2;
193 m_parents[2] = NULL;
194 Register();
195 }
196
197 virtual ~wxClassInfo();
198
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; }
203
204 // 'old naming' for AllocateObject staying here for backward compatibility
205 wxObject *CreateObject() const { return AllocateObject(); }
206
207 // direct construction call for classes that cannot construct instances via alloc/create
208 wxObject *ConstructObject(int ParamCount, wxAny *Params) const;
209
210 bool NeedsDirectConstruction() const;
211
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; }
218
219 const wxClassInfo *GetBaseClass1() const
220 { return m_parents[0]; }
221 const wxClassInfo *GetBaseClass2() const
222 { return m_parents[0] ? m_parents[1] : NULL; }
223
224 const wxChar *GetIncludeName() const
225 { return m_unitName; }
226 const wxClassInfo **GetParents() const
227 { return m_parents; }
228 int GetSize() const
229 { return m_objectSize; }
230 bool IsDynamic() const
231 { return (NULL != m_objectConstructor); }
232
233 wxObjectConstructorFn GetConstructor() const
234 { return m_objectConstructor; }
235 const wxClassInfo *GetNext() const
236 { return m_next; }
237
238 // statics:
239
240 static void CleanUp();
241 static wxClassInfo *FindClass(const wxString& className);
242 static const wxClassInfo *GetFirst()
243 { return sm_first; }
244
245
246 // Climb upwards through inheritance hierarchy.
247 // Dual inheritance is catered for.
248
249 bool IsKindOf(const wxClassInfo *info) const;
250
251 wxDECLARE_CLASS_INFO_ITERATORS();
252
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;
260
261 // gets the streaming callback from this class or any superclass
262 wxObjectStreamingCallback GetStreamingCallback() const;
263
264 // returns the first property
265 wxPropertyInfo* GetFirstProperty() const
266 { EnsureInfosInited(); return m_firstProperty; }
267
268 // returns the first handler
269 wxHandlerInfo* GetFirstHandler() const
270 { EnsureInfosInited(); return m_firstHandler; }
271
272 // Call the Create upon an instance of the class, in the end the object is fully
273 // initialized
274 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
275
276 // get number of parameters for constructor
277 virtual int GetCreateParamCount() const
278 { return m_constructorPropertiesCount; }
279
280 // get n-th constructor parameter
281 virtual const wxChar* GetCreateParamName(int n) const
282 { return m_constructorProperties[n]; }
283
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;
289
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;
295
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;
299
300 wxObject* AnyToObjectPtr( const wxAny &data) const;
301
302 wxAny ObjectPtrToAny( wxObject *object ) const;
303
304 // find property by name
305 virtual const wxPropertyInfo *FindPropertyInfo (const wxChar *PropertyName) const;
306
307 // find handler by name
308 virtual const wxHandlerInfo *FindHandlerInfo (const wxChar *handlerName) const;
309
310 // find property by name
311 virtual wxPropertyInfo *FindPropertyInfoInThisClass (const wxChar *PropertyName) const;
312
313 // find handler by name
314 virtual wxHandlerInfo *FindHandlerInfoInThisClass (const wxChar *handlerName) const;
315
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;
319
320 private:
321 const wxChar *m_className;
322 int m_objectSize;
323 wxObjectConstructorFn m_objectConstructor;
324
325 // class info object live in a linked list:
326 // pointers to its head and the next element in it
327
328 static wxClassInfo *sm_first;
329 wxClassInfo *m_next;
330
331 static wxHashTable *sm_classTable;
332
333 wxPropertyInfoFn m_firstPropertyFn;
334 wxHandlerInfoFn m_firstHandlerFn;
335
336
337 protected:
338 void EnsureInfosInited() const
339 {
340 if ( !m_firstInited)
341 {
342 if ( m_firstPropertyFn != NULL)
343 m_firstProperty = (*m_firstPropertyFn)();
344 if ( m_firstHandlerFn != NULL)
345 m_firstHandler = (*m_firstHandlerFn)();
346 m_firstInited = true;
347 }
348 }
349 mutable wxPropertyInfo* m_firstProperty;
350 mutable wxHandlerInfo* m_firstHandler;
351
352 private:
353 mutable bool m_firstInited;
354
355 const wxClassInfo** m_parents;
356 const wxChar* m_unitName;
357
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;
365
366 const wxPropertyAccessor *FindAccessor (const wxChar *propertyName) const;
367
368 protected:
369 // registers the class
370 void Register();
371 void Unregister();
372
373 DECLARE_NO_COPY_CLASS(wxClassInfo)
374 };
375
376 WXDLLIMPEXP_BASE wxObject *wxCreateDynamicObject(const wxString& name);
377
378 // ----------------------------------------------------------------------------
379 // wxDynamicClassInfo
380 // ----------------------------------------------------------------------------
381
382 // this object leads to having a pure runtime-instantiation
383
384 class WXDLLIMPEXP_BASE wxDynamicClassInfo : public wxClassInfo
385 {
386 friend class WXDLLIMPEXP_BASE wxDynamicObject;
387
388 public:
389 wxDynamicClassInfo( const wxChar *_UnitName, const wxChar *_ClassName,
390 const wxClassInfo* superClass );
391 virtual ~wxDynamicClassInfo();
392
393 // constructs a wxDynamicObject with an instance
394 virtual wxObject *AllocateObject() const;
395
396 // Call the Create method for a class
397 virtual bool Create (wxObject *object, int ParamCount, wxAny *Params) const;
398
399 // get number of parameters for constructor
400 virtual int GetCreateParamCount() const;
401
402 // get i-th constructor parameter
403 virtual const wxChar* GetCreateParamName(int i) const;
404
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;
409
410 // adds a property to this class at runtime
411 void AddProperty( const wxChar *propertyName, const wxTypeInfo* typeInfo );
412
413 // removes an existing runtime-property
414 void RemoveProperty( const wxChar *propertyName );
415
416 // renames an existing runtime-property
417 void RenameProperty( const wxChar *oldPropertyName, const wxChar *newPropertyName );
418
419 // as a handler to this class at runtime
420 void AddHandler( const wxChar *handlerName, wxObjectEventFunction address,
421 const wxClassInfo* eventClassInfo );
422
423 // removes an existing runtime-handler
424 void RemoveHandler( const wxChar *handlerName );
425
426 // renames an existing runtime-handler
427 void RenameHandler( const wxChar *oldHandlerName, const wxChar *newHandlerName );
428
429 private:
430 struct wxDynamicClassInfoInternal;
431 wxDynamicClassInfoInternal* m_data;
432 };
433
434 // ----------------------------------------------------------------------------
435 // wxDECLARE class macros
436 // ----------------------------------------------------------------------------
437
438 #define _DECLARE_DYNAMIC_CLASS(name) \
439 public: \
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; }
448
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)
454
455 #define wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
456 wxDECLARE_NO_ASSIGN_CLASS(name); \
457 wxDECLARE_DYNAMIC_CLASS(name)
458
459 #define wxDECLARE_DYNAMIC_CLASS_NO_COPY(name) \
460 wxDECLARE_NO_COPY_CLASS(name); \
461 wxDECLARE_DYNAMIC_CLASS(name)
462
463 #define wxDECLARE_CLASS(name) \
464 wxDECLARE_DYNAMIC_CLASS(name)
465
466 #define wxDECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
467 #define wxCLASSINFO(name) (&name::ms_classInfo)
468
469 #endif // wxUSE_EXTENDED_RTTI
470 #endif // _WX_XTIH__