1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: runtime metadata information (extended class info)
4 // Author: Stefan Csomor
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2003 Stefan Csomor
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
16 #if defined(__GNUG__) && !defined(__APPLE__)
17 #pragma interface "xti.h"
20 // We want to support properties, event sources and events sinks through
21 // explicit declarations, using templates and specialization to make the
22 // effort as painless as possible.
24 // This means we have the following domains :
26 // - Type Information for categorizing built in types as well as custom types
27 // this includes information about enums, their values and names
28 // - Type safe value storage : a kind of wxVariant, called right now wxxVariant
29 // which will be merged with wxVariant
30 // - Property Information and Property Accessors providing access to a class'
31 // values and exposed event delegates
32 // - Information about event handlers
33 // - extended Class Information for accessing all these
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
40 #include "wx/memory.h"
42 #include "wx/string.h"
43 #include "wx/arrstr.h"
45 class WXDLLIMPEXP_BASE wxObject
;
46 class WXDLLIMPEXP_BASE wxClassInfo
;
47 class WXDLLIMPEXP_BASE wxHashTable
;
48 class WXDLLIMPEXP_BASE wxObjectRefData
;
49 class WXDLLIMPEXP_BASE wxEvent
;
51 typedef void (wxObject::*wxObjectEventFunction
)(wxEvent
&);
53 // ----------------------------------------------------------------------------
56 // In the header files there would no change from pure c++ code, in the
57 // implementation, an enum would have
58 // to be enumerated eg :
60 // WX_BEGIN_ENUM( wxFlavor )
61 // WX_ENUM_MEMBER( Vanilla )
62 // WX_ENUM_MEMBER( Chocolate )
63 // WX_ENUM_MEMBER( Strawberry )
64 // WX_END_ENUM( wxFlavor )
65 // ----------------------------------------------------------------------------
67 struct WXDLLIMPEXP_BASE wxEnumMemberData
73 class WXDLLIMPEXP_BASE wxEnumData
76 wxEnumData( wxEnumMemberData
* data
) ;
78 // returns true if the member has been found and sets the int value
79 // pointed to accordingly (if ptr != null )
80 // if not found returns false, value left unchanged
81 bool HasEnumMemberValue( const wxChar
*name
, int *value
= NULL
) ;
83 // returns the value of the member, if not found in debug mode an
84 // assert is issued, in release 0 is returned
85 int GetEnumMemberValue(const wxChar
*name
);
87 // returns the name of the enum member having the passed in value
88 // returns an emtpy string if not found
89 const wxChar
*GetEnumMemberName(int value
);
91 // returns the number of members in this enum
92 int GetEnumCount() { return m_count
; }
94 // returns the value of the nth member
95 int GetEnumMemberValueByIndex( int n
) ;
97 // returns the value of the nth member
98 const wxChar
*GetEnumMemberNameByIndex( int n
) ;
100 wxEnumMemberData
*m_members
;
104 #define WX_BEGIN_ENUM( e ) \
105 wxEnumMemberData s_enumDataMembers##e[] = {
107 #define WX_ENUM_MEMBER( v ) { #v, v } ,
109 #define WX_END_ENUM( e ) { NULL , 0 } } ; \
110 wxEnumData s_enumData##e( s_enumDataMembers##e ) ; \
111 wxEnumData *wxGetEnumData(e) { return &s_enumData##e ; } \
112 template<> const wxTypeInfo* wxGetTypeInfo( e * ){ static wxEnumTypeInfo s_typeInfo(wxT_ENUM , &s_enumData##e) ; return &s_typeInfo ; } \
113 template<> void wxStringReadValue(const wxString& s , e &data ) \
115 data = (e) s_enumData##e.GetEnumMemberValue(s) ; \
117 template<> void wxStringWriteValue(wxString &s , const e &data ) \
119 s = s_enumData##e.GetEnumMemberName((int)data) ; \
122 // ----------------------------------------------------------------------------
134 // typedef wxSet<wxFlavor> wxCoupe ;
136 // in the implementation file :
138 // WX_BEGIN_ENUM( wxFlavor )
139 // WX_ENUM_MEMBER( Vanilla )
140 // WX_ENUM_MEMBER( Chocolate )
141 // WX_ENUM_MEMBER( Strawberry )
142 // WX_END_ENUM( wxFlavor )
144 // WX_IMPLEMENT_SET_STREAMING( wxCoupe , wxFlavor )
146 // implementation note : no partial specialization for streaming, but a delegation to a
149 // ----------------------------------------------------------------------------
151 // in order to remove dependancy on string tokenizer
152 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
) ;
155 void wxSetFromString(const wxString
&s
, wxSet
<e
> &data
)
157 wxEnumData
* edata
= wxGetEnumData((e
) 0) ;
160 wxArrayString array
;
161 wxSetStringToArray( s
, array
) ;
163 for ( int i
= 0 ; i
< array
.Count() ; ++i
)
167 if ( edata
->HasEnumMemberValue( flag
, &ivalue
) )
169 data
.Set( (e
) ivalue
) ;
175 void wxSetToString( wxString
&s
, const wxSet
<e
> &data
)
177 wxEnumData
* edata
= wxGetEnumData((e
) 0) ;
178 int count
= edata
->GetEnumCount() ;
181 for ( i
= 0 ; i
< count
; i
++ )
183 e value
= (e
) edata
->GetEnumMemberValueByIndex(i
) ;
184 if ( data
.Contains( value
) )
186 // this could also be done by the templated calls
189 s
+= edata
->GetEnumMemberNameByIndex(i
) ;
194 // if the wxSet specialization above does not work for all compilers, add this to the WX_IMPLEMENT_SET_STREAMING macro
195 // template<> const wxTypeInfo* wxGetTypeInfo( SetName * ){ static wxEnumTypeInfo s_typeInfo(wxT_SET , &s_enumData##e) ; return &s_typeInfo ; }
197 #define WX_IMPLEMENT_SET_STREAMING(SetName,e) \
198 template<> void wxStringReadValue(const wxString &s , wxSet<e> &data ) \
200 wxSetFromString( s , data ) ; \
202 template<> void wxStringWriteValue( wxString &s , const wxSet<e> &data ) \
204 wxSetToString( s , data ) ; \
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
214 wxT_VOID
= 0, // unknown type
224 wxT_STRING
, // must be wxString
225 wxT_SET
, // must be wxSet<> template
227 wxT_OBJECT
, // must be a component (pointer !!!)
228 wxT_CUSTOM
, // user defined type (e.g. wxPoint)
229 wxT_DELEGATE
, // for connecting against an event source
230 wxT_LAST_TYPE_KIND
// sentinel for bad data, asserts, debugging
233 class WXDLLIMPEXP_BASE wxTypeInfo
236 wxTypeInfo() : m_kind( wxT_VOID
) {}
237 virtual ~wxTypeInfo() {}
238 wxTypeKind
GetKind() const { return m_kind
; }
243 class WXDLLIMPEXP_BASE wxBuiltInTypeInfo
: public wxTypeInfo
246 wxBuiltInTypeInfo( wxTypeKind kind
) { wxASSERT_MSG( kind
< wxT_SET
, wxT("Illegal Kind for Base Type") ) ; m_kind
= kind
;}
249 class WXDLLIMPEXP_BASE wxCustomTypeInfo
: public wxTypeInfo
252 wxCustomTypeInfo( const wxChar
*typeName
)
253 { m_kind
= wxT_CUSTOM
; m_typeName
= typeName
;}
254 const wxChar
*GetTypeName() const { return m_typeName
; }
256 const wxChar
*m_typeName
; // Kind == wxT_CUSTOM
259 class WXDLLIMPEXP_BASE wxEnumTypeInfo
: public wxTypeInfo
262 wxEnumTypeInfo( wxTypeKind kind
, wxEnumData
* enumInfo
)
263 { wxASSERT_MSG( kind
== wxT_ENUM
|| kind
== wxT_SET
, wxT("Illegal Kind for Enum Type")) ; m_kind
= kind
; m_enumInfo
= enumInfo
;}
264 const wxEnumData
* GetEnumData() const { return m_enumInfo
; }
266 wxEnumData
*m_enumInfo
; // Kind == wxT_ENUM or Kind == wxT_SET
269 class WXDLLIMPEXP_BASE wxClassTypeInfo
: public wxTypeInfo
272 wxClassTypeInfo( wxClassInfo
* classInfo
)
273 { m_kind
= wxT_OBJECT
; m_classInfo
= classInfo
;}
274 const wxClassInfo
*GetClassInfo() const { return m_classInfo
; }
276 wxClassInfo
*m_classInfo
; // Kind == wxT_OBJECT - could be NULL
279 // a delegate is an exposed event source
281 class WXDLLIMPEXP_BASE wxDelegateTypeInfo
: public wxTypeInfo
284 wxDelegateTypeInfo( int eventType
, wxClassInfo
* eventClass
)
285 { m_kind
= wxT_DELEGATE
; m_eventClass
= eventClass
; m_eventType
= eventType
;}
286 const wxClassInfo
*GetEventClass() const { assert( m_kind
== wxT_DELEGATE
) ; return m_eventClass
; }
287 int GetEventType() const { return m_eventType
; }
289 const wxClassInfo
*m_eventClass
; // (extended will merge into classinfo)
293 template<typename T
> const wxTypeInfo
* wxGetTypeInfo( T
* ) ;
295 template<typename T
> const wxTypeInfo
* wxGetTypeInfo( wxSet
<T
> * )
297 static wxEnumTypeInfo
s_typeInfo(wxT_SET
, wxGetEnumData((T
) 0) ) ; return &s_typeInfo
;
300 // this macro is for usage with custom, non-object derived classes and structs, wxPoint is such a custom type
302 #define WX_CUSTOM_TYPE_INFO( e ) \
303 template<> const wxTypeInfo* wxGetTypeInfo( e * ){ static wxCustomTypeInfo s_typeInfo(#e) ; return &s_typeInfo ; } \
305 // ----------------------------------------------------------------------------
308 // streaming is defined for xml constructs right now, the aim is to make this
309 // pluggable in the future
310 // ----------------------------------------------------------------------------
312 // convenience function (avoids including xml headers in users code)
315 void wxXmlAddContentToNode( wxXmlNode
* node
, const wxString
& data
) ;
316 wxString
wxXmlGetContentFromNode( wxXmlNode
*node
) ;
318 // templated streaming, every type must have their specialization for these methods
321 void wxStringReadValue( const wxString
&s
, T
&data
) ;
324 void wxStringWriteValue( wxString
&s
, const T
&data
) ;
326 // for simple types this default implementation is ok, composited structures will have to
327 // loop through their properties
330 void wxXmlReadValue( wxXmlNode
*node
, T
&data
)
332 wxStringReadValue
<T
>( wxXmlGetContentFromNode( node
) , data
) ;
336 void wxXmlWriteValue( wxXmlNode
*node
, const T
&data
)
339 wxStringWriteValue
<T
>( s
, data
) ;
340 wxXmlAddContentToNode( node
,s
) ;
343 // ----------------------------------------------------------------------------
344 // wxxVariant as typesafe data holder
345 // ----------------------------------------------------------------------------
347 class WXDLLIMPEXP_BASE wxxVariantData
350 virtual ~wxxVariantData() {}
352 // return a heap allocated duplicate
353 virtual wxxVariantData
* Clone() const = 0 ;
355 // returns the type info of the contentc
356 virtual const wxTypeInfo
* GetTypeInfo() const = 0 ;
358 // write the value into an xml node
359 virtual void Write( wxXmlNode
* node
) const = 0 ;
361 // read the value from the xml node
362 virtual void Read( wxXmlNode
* node
) = 0 ;
364 // write the value into a string
365 virtual void Write( wxString
&s
) const = 0 ;
367 // read the value from a string
368 virtual void Read( const wxString
&s
) = 0 ;
371 template<typename T
> class WXDLLIMPEXP_BASE wxxVariantDataT
: public wxxVariantData
374 wxxVariantDataT(T d
) : m_data(d
) {}
375 virtual ~wxxVariantDataT() {}
377 // get a copy of the stored data
378 T
Get() const { return m_data
; }
381 void Set(T d
) { m_data
= d
; }
383 // return a heap allocated duplicate
384 virtual wxxVariantData
* Clone() const { return new wxxVariantDataT
<T
>( Get() ) ; }
386 // returns the type info of the contentc
387 virtual const wxTypeInfo
* GetTypeInfo() const { return wxGetTypeInfo( (T
*) NULL
) ; }
389 // write the value into an xml node
390 virtual void Write( wxXmlNode
* node
) const { wxXmlWriteValue( node
, m_data
) ; }
392 // read the value from the xml node
393 virtual void Read( wxXmlNode
* node
) { wxXmlReadValue( node
, m_data
) ; }
395 // write the value into a string
396 virtual void Write( wxString
&s
) const { wxStringWriteValue( s
, m_data
) ; }
398 // read the value from a string
399 virtual void Read( const wxString
&s
) { wxStringReadValue( s
, m_data
) ; }
405 class WXDLLIMPEXP_BASE wxxVariant
408 wxxVariant() { m_data
= NULL
; }
409 wxxVariant( wxxVariantData
* data
, const wxString
& name
= wxT("") ) : m_data(data
) , m_name(name
) {}
410 wxxVariant( const wxxVariant
&d
) { if ( d
.m_data
) m_data
= d
.m_data
->Clone() ; else m_data
= NULL
; m_name
= d
.m_name
; }
412 template<typename T
> wxxVariant( T data
, const wxString
& name
= wxT("") ) :
413 m_data(new wxxVariantDataT
<T
>(data
) ), m_name(name
) {}
414 ~wxxVariant() { delete m_data
; }
416 // get a copy of the stored data
417 template<typename T
> T
Get() const
419 wxxVariantDataT
<T
> *dataptr
= dynamic_cast<wxxVariantDataT
<T
>*> (m_data
) ;
420 wxASSERT_MSG( dataptr
, "Cast not possible" ) ;
421 return dataptr
->Get() ;
425 template<typename T
> void Set(T data
) const
428 m_data
= new wxxVariantDataT
<T
>(data
) ;
431 wxxVariant
& operator=(const wxxVariant
&d
)
433 m_data
= d
.m_data
->Clone() ;
438 // gets the stored data casted to a wxObject* , returning NULL if cast is not possible
439 wxObject
* GetAsObject() const ;
441 // write the value into an xml node
442 void Write( wxXmlNode
* node
) const { m_data
->Write( node
) ; }
444 // read the value from the xml node
445 void Read( wxXmlNode
* node
) { m_data
->Read( node
) ; }
447 // write the value into a string
448 void Write( wxString
&s
) const { m_data
->Write( s
) ; }
450 // read the value from a string
451 void Read( const wxString
&s
) { m_data
->Read( s
) ; }
453 // returns this value as string
454 wxString
GetAsString() const
461 void SetFromString( const wxString
&s
)
466 wxxVariantData
* m_data
;
470 // ----------------------------------------------------------------------------
473 // wxPropertyInfo is used to inquire of the property by name. It doesn't
474 // provide access to the property, only information about it. If you
475 // want access, look at wxPropertyAccessor.
476 // ----------------------------------------------------------------------------
478 class WXDLLIMPEXP_BASE wxPropertyAccessor
482 class SetAndGetByRef
;
484 virtual void SetProperty(wxObject
*object
, const wxxVariant
&value
) const = 0 ;
485 virtual wxxVariant
GetProperty(wxObject
*object
) const = 0 ;
486 virtual bool HasSetter() const = 0 ;
487 virtual bool HasGetter() const = 0 ;
488 const wxChar
* GetGetterName() const { return m_setterName
; }
489 const wxChar
* GetSetterName() const { return m_getterName
; }
490 virtual wxxVariant
ReadValue( wxXmlNode
* node
) const = 0 ;
491 virtual void WriteValue( wxXmlNode
* node
, wxObject
*o
) const = 0 ;
492 virtual wxxVariant
ReadValue( const wxString
&value
) const = 0 ;
493 virtual void WriteValue( wxString
& value
, wxObject
*o
) const = 0 ;
495 const wxChar
*m_setterName
;
496 const wxChar
*m_getterName
;
500 void wxXmlReadValue( wxXmlNode
*node
, T
&data
) ;
503 void wxXmlWriteValue( wxXmlNode
*node
, const T
&data
) ;
505 template<class Klass
, typename T
>
506 class WXDLLIMPEXP_BASE wxPropertyAccessorT
: public wxPropertyAccessor
510 typedef void (Klass::*setter_t
)(T value
);
511 typedef void (Klass::*setter_ref_t
)(const T
& value
);
512 typedef T (Klass::*getter_t
)() const;
513 typedef const T
& (Klass::*getter_ref_t
)() const;
515 wxPropertyAccessorT(setter_t setter
, getter_t getter
, const wxChar
*g
, const wxChar
*s
)
516 : m_setter(setter
), m_setter_ref(NULL
), m_getter(getter
) ,m_getter_ref(NULL
) {m_setterName
= s
;m_getterName
=g
;}
518 wxPropertyAccessorT( getter_t getter
, const wxChar
*g
)
519 : m_setter(NULL
), m_setter_ref(NULL
), m_getter(getter
) ,m_getter_ref(NULL
) {m_setterName
= "";m_getterName
=g
;}
521 wxPropertyAccessorT(SetByRef
*, setter_ref_t setter
, getter_t getter
, const wxChar
*g
, const wxChar
*s
)
522 : m_setter(NULL
), m_setter_ref(setter
), m_getter(getter
) , m_getter_ref(NULL
){m_setterName
= s
;m_getterName
=g
;}
524 // wxPropertyAccessorT(setter_ref_t setter, getter_t getter, const wxChar *g, const wxChar *s)
525 // : m_setter(NULL), m_setter_ref(setter), m_getter(getter) , m_getter_ref(NULL){m_setterName = s;m_getterName=g ;}
527 wxPropertyAccessorT(SetAndGetByRef
*, setter_ref_t setter
, getter_ref_t getter
, const wxChar
*g
, const wxChar
*s
)
528 : m_setter(NULL
), m_setter_ref(setter
), m_getter(NULL
) , m_getter_ref(getter
){m_setterName
= s
;m_getterName
=g
;}
530 // wxPropertyAccessorT(setter_ref_t setter, getter_ref_t getter, const wxChar *g, const wxChar *s)
531 // : m_setter(NULL), m_setter_ref(setter), m_getter(NULL) , m_getter_ref(getter){m_setterName = s;m_getterName=g ;}
533 // wxPropertyAccessorT(setter_t setter, getter_ref_t getter, const wxChar *g, const wxChar *s)
534 // : m_setter(NULL), m_setter(setter), m_getter(NULL) , m_getter_ref(getter){m_setterName = s;m_getterName=g ;}
536 wxPropertyAccessorT(GetByRef
*, setter_t setter
, getter_ref_t getter
, const wxChar
*g
, const wxChar
*s
)
537 : m_setter(NULL
), m_setter(setter
), m_getter(NULL
) , m_getter_ref(getter
){m_setterName
= s
;m_getterName
=g
;}
539 // wxPropertyAccessorT( getter_ref_t getter, const wxChar *g)
540 // : m_setter(NULL), m_setter(NULL), m_getter(NULL) , m_getter_ref(getter){m_setterName = "";m_getterName=g ;}
542 // returns true if this accessor has a setter
543 bool HasSetter() const { return m_setter
!= NULL
|| m_setter_ref
!= NULL
; }
545 // return true if this accessor has a getter
546 bool HasGetter() const { return m_getter
!= NULL
|| m_getter_ref
!= NULL
; }
548 // set the property this accessor is responsible for in an object
549 void SetProperty(wxObject
*o
, const wxxVariant
&v
) const
551 Klass
*obj
= dynamic_cast<Klass
*>(o
);
552 T value
= v
.Get
<T
>();
554 (obj
->*(m_setter
))(value
);
556 (obj
->*(m_setter_ref
))(value
);
559 // gets the property this accessor is responsible for from an object
560 wxxVariant
GetProperty(wxObject
*o
) const
562 return wxxVariant( (wxxVariantData
* ) DoGetProperty( o
) ) ;
565 // write the property this accessor is responsible for from an object into
567 void WriteValue( wxXmlNode
* node
, wxObject
*o
) const
569 DoGetProperty( o
)->Write( node
) ;
572 // write the property this accessor is responsible for from an object into
574 void WriteValue( wxString
& s
, wxObject
*o
) const
576 DoGetProperty( o
)->Write( s
) ;
579 // read a wxxVariant having the correct type for the property this accessor
580 // is responsible for from an xml node
581 wxxVariant
ReadValue( wxXmlNode
* node
) const
584 wxXmlReadValue( node
, data
) ;
585 return wxxVariant( data
) ;
588 // read a wxxVariant having the correct type for the property this accessor
589 // is responsible for from a string
590 wxxVariant
ReadValue( const wxString
&value
) const
593 wxStringReadValue( value
, data
) ;
594 return wxxVariant( data
) ;
598 wxxVariantDataT
<T
>* DoGetProperty(wxObject
*o
) const
600 Klass
*obj
= dynamic_cast<Klass
*>(o
);
602 return new wxxVariantDataT
<T
>( (obj
->*(m_getter
))() ) ;
604 return new wxxVariantDataT
<T
>( (obj
->*(m_getter_ref
))() ) ;
608 setter_ref_t m_setter_ref
;
610 getter_ref_t m_getter_ref
;
613 class WXDLLIMPEXP_BASE wxPropertyInfo
616 wxPropertyInfo( wxPropertyInfo
* &iter
, const wxChar
*name
, const wxChar
*typeName
, const wxTypeInfo
* typeInfo
, wxPropertyAccessor
*accessor
, wxxVariant dv
) :
617 m_name( name
) , m_typeName(typeName
) , m_typeInfo( typeInfo
) , m_accessor( accessor
) , m_defaultValue( dv
)
624 wxPropertyInfo
* i
= iter
;
631 // return the name of this property
632 const wxChar
* GetName() const { return m_name
; }
634 // return the typename of this property
635 const wxChar
* GetTypeName() const { return m_typeName
; }
637 // return the type info of this property
638 const wxTypeInfo
* GetTypeInfo() const { return m_typeInfo
; }
640 // return the accessor for this property
641 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
643 // returns NULL if this is the last property of this class
644 wxPropertyInfo
* GetNext() const { return m_next
; }
646 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
647 wxxVariant
GetDefaultValue() const { return m_defaultValue
; }
649 const wxChar
* m_name
;
650 const wxChar
* m_typeName
;
651 const wxTypeInfo
* m_typeInfo
;
652 wxPropertyAccessor
* m_accessor
;
653 wxxVariant m_defaultValue
;
654 // string representation of the default value
655 // to be assigned by the designer to the property
656 // when the component is dropped on the container.
657 wxPropertyInfo
* m_next
;
660 #define WX_BEGIN_PROPERTIES_TABLE(theClass) \
661 const wxPropertyInfo *theClass::GetPropertiesStatic() \
663 typedef theClass class_t; \
664 static wxPropertyInfo* first = NULL ;
666 #define WX_END_PROPERTIES_TABLE() \
669 #define WX_PROPERTY( name , type , setter , getter ,defaultValue ) \
670 static wxPropertyAccessorT<class_t , type> _accessor##name( &setter , &getter , #setter , #getter ) ; \
671 static wxPropertyInfo _propertyInfo##name( first , #name , #type , wxGetTypeInfo( (type*) NULL ) ,&_accessor##name , wxxVariant(defaultValue) ) ;
673 #define WX_PROPERTY_SET_BY_REF( name , type , setter , getter ,defaultValue ) \
674 static wxPropertyAccessorT<class_t , type> _accessor##name( (wxPropertyAccessor::SetByRef*)NULL, &setter , &getter , #setter , #getter ) ; \
675 static wxPropertyInfo _propertyInfo##name( first , #name , #type , wxGetTypeInfo( (type*) NULL ) ,&_accessor##name , wxxVariant(defaultValue) ) ;
677 #define WX_READONLY_PROPERTY( name , type , getter ,defaultValue ) \
678 static wxPropertyAccessorT<class_t , type> _accessor##name( &getter , #getter ) ; \
679 static wxPropertyInfo _propertyInfo##name( first , #name , #type , wxGetTypeInfo( (type*) NULL ) ,&_accessor##name , wxxVariant(defaultValue) ) ;
681 #define WX_DELEGATE( name , eventType , eventClass ) \
682 static wxDelegateTypeInfo _typeInfo##name( eventType , CLASSINFO( eventClass ) ) ; \
683 static wxPropertyInfo _propertyInfo##name( first , #name , NULL , &_typeInfo##name , NULL , wxxVariant() ) ; \
685 // ----------------------------------------------------------------------------
688 // this is describing an event sink
689 // ----------------------------------------------------------------------------
694 wxHandlerInfo( wxHandlerInfo
* &iter
, const wxChar
*name
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
) :
695 m_eventFunction( address
) , m_name( name
) , m_eventClassInfo( eventClassInfo
)
702 wxHandlerInfo
* i
= iter
;
710 // get the name of the handler method
711 const wxChar
* GetName() const { return m_name
; }
713 // return the class info of the event
714 const wxClassInfo
* GetEventClassInfo() const { return m_eventClassInfo
; }
716 // get the handler function pointer
717 wxObjectEventFunction
GetEventFunction() const { return m_eventFunction
; }
719 // returns NULL if this is the last handler of this class
720 wxHandlerInfo
* GetNext() const { return m_next
; }
722 wxObjectEventFunction m_eventFunction
;
723 const wxChar
* m_name
;
724 const wxClassInfo
* m_eventClassInfo
;
725 wxHandlerInfo
* m_next
;
728 #define WX_HANDLER(name,eventClassType) \
729 static wxHandlerInfo _handlerInfo##name( first , #name , (wxObjectEventFunction) (wxEventFunction) &name , CLASSINFO( eventClassType ) ) ;
731 #define WX_BEGIN_HANDLERS_TABLE(theClass) \
732 const wxHandlerInfo *theClass::GetHandlersStatic() \
734 typedef theClass class_t; \
735 static wxHandlerInfo* first = NULL ;
737 #define WX_END_HANDLERS_TABLE() \
740 // ----------------------------------------------------------------------------
741 // Constructor Bridges
743 // allow to set up constructors with params during runtime
744 // ----------------------------------------------------------------------------
746 class WXDLLIMPEXP_BASE wxConstructorBridge
749 virtual void Create(wxObject
*o
, wxxVariant
*args
) = 0;
752 // Creator Bridges for all Numbers of Params
756 template<typename Class
>
757 struct wxConstructorBridge_0
: public wxConstructorBridge
759 void Create(wxObject
*o
, wxxVariant
*)
761 Class
*obj
= dynamic_cast<Class
*>(o
);
766 struct wxConstructorBridge_Dummy
: public wxConstructorBridge
768 void Create(wxObject
*, wxxVariant
*)
773 #define WX_CONSTRUCTOR_0(klass) \
774 wxConstructorBridge_0<klass> constructor##klass ; \
775 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
776 const wxChar *klass::sm_constructorProperties##klass[] = { NULL } ; \
777 const int klass::sm_constructorPropertiesCount##klass = 0 ;
779 #define WX_CONSTRUCTOR_DUMMY(klass) \
780 wxConstructorBridge_Dummy constructor##klass ; \
781 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
782 const wxChar *klass::sm_constructorProperties##klass[] = { NULL } ; \
783 const int klass::sm_constructorPropertiesCount##klass = 0 ;
787 template<typename Class
, typename T0
>
788 struct wxConstructorBridge_1
: public wxConstructorBridge
790 void Create(wxObject
*o
, wxxVariant
*args
)
792 Class
*obj
= dynamic_cast<Class
*>(o
);
799 #define WX_CONSTRUCTOR_1(klass,t0,v0) \
800 wxConstructorBridge_1<klass,t0> constructor##klass ; \
801 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
802 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 } ; \
803 const int klass::sm_constructorPropertiesCount##klass = 1 ;
807 template<typename Class
,
808 typename T0
, typename T1
>
809 struct wxConstructorBridge_2
: public wxConstructorBridge
811 void Create(wxObject
*o
, wxxVariant
*args
)
813 Class
*obj
= dynamic_cast<Class
*>(o
);
821 #define WX_CONSTRUCTOR_2(klass,t0,v0,t1,v1) \
822 wxConstructorBridge_2<klass,t0,t1> constructor##klass ; \
823 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
824 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 } ; \
825 const int klass::sm_constructorPropertiesCount##klass = 2;
829 template<typename Class
,
830 typename T0
, typename T1
, typename T2
>
831 struct wxConstructorBridge_3
: public wxConstructorBridge
833 void Create(wxObject
*o
, wxxVariant
*args
)
835 Class
*obj
= dynamic_cast<Class
*>(o
);
844 #define WX_CONSTRUCTOR_3(klass,t0,v0,t1,v1,t2,v2) \
845 wxConstructorBridge_3<klass,t0,t1,t2> constructor##klass ; \
846 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
847 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 , #v2 } ; \
848 const int klass::sm_constructorPropertiesCount##klass = 3 ;
852 template<typename Class
,
853 typename T0
, typename T1
, typename T2
, typename T3
>
854 struct wxConstructorBridge_4
: public wxConstructorBridge
856 void Create(wxObject
*o
, wxxVariant
*args
)
858 Class
*obj
= dynamic_cast<Class
*>(o
);
868 #define WX_CONSTRUCTOR_4(klass,t0,v0,t1,v1,t2,v2,t3,v3) \
869 wxConstructorBridge_4<klass,t0,t1,t2,t3> constructor##klass ; \
870 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
871 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 , #v2 , #v3 } ; \
872 const int klass::sm_constructorPropertiesCount##klass = 4 ;
876 template<typename Class
,
877 typename T0
, typename T1
, typename T2
, typename T3
, typename T4
>
878 struct wxConstructorBridge_5
: public wxConstructorBridge
880 void Create(wxObject
*o
, wxxVariant
*args
)
882 Class
*obj
= dynamic_cast<Class
*>(o
);
893 #define WX_CONSTRUCTOR_5(klass,t0,v0,t1,v1,t2,v2,t3,v3,t4,v4) \
894 wxConstructorBridge_5<klass,t0,t1,t2,t3,t4> constructor##klass ; \
895 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
896 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 , #v2 , #v3 , #v4 } ; \
897 const int klass::sm_constructorPropertiesCount##klass = 5;
899 // ----------------------------------------------------------------------------
901 // ----------------------------------------------------------------------------
903 typedef wxObject
*(*wxObjectConstructorFn
)(void);
904 typedef wxObject
* (*wxVariantToObjectConverter
)( const wxxVariant
&data
) ;
905 typedef wxxVariant (*wxObjectToVariantConverter
)( wxObject
* ) ;
907 class WXDLLIMPEXP_BASE wxClassInfo
910 wxClassInfo(const wxClassInfo
**_Parents
,
911 const wxChar
*_UnitName
,
912 const wxChar
*_ClassName
,
914 wxObjectConstructorFn ctor
,
915 const wxPropertyInfo
*_Props
,
916 const wxHandlerInfo
*_Handlers
,
917 wxConstructorBridge
* _Constructor
,
918 const wxChar
** _ConstructorProperties
,
919 const int _ConstructorPropertiesCount
,
920 wxVariantToObjectConverter _Converter1
,
921 wxObjectToVariantConverter _Converter2
922 ) : m_className(_ClassName
), m_objectSize(size
),
923 m_objectConstructor(ctor
), m_next(sm_first
),
924 m_parents(_Parents
), m_firstProperty(_Props
),
925 m_firstHandler(_Handlers
), m_unitName(_UnitName
),
926 m_constructor( _Constructor
),
927 m_constructorProperties(_ConstructorProperties
),
928 m_constructorPropertiesCount(_ConstructorPropertiesCount
),
929 m_variantToObjectConverter( _Converter1
),
930 m_objectToVariantConverter( _Converter2
)
933 Register( m_className
, this ) ;
936 virtual ~wxClassInfo() ;
938 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
940 const wxChar
*GetClassName() const { return m_className
; }
941 const wxClassInfo
**GetParents() const { return m_parents
; }
942 int GetSize() const { return m_objectSize
; }
944 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
945 static const wxClassInfo
*GetFirst() { return sm_first
; }
946 const wxClassInfo
*GetNext() const { return m_next
; }
947 static wxClassInfo
*FindClass(const wxChar
*className
);
949 // Climb upwards through inheritance hierarchy.
950 // Dual inheritance is catered for.
952 bool IsKindOf(const wxClassInfo
*info
) const
959 for ( int i
= 0 ; m_parents
[i
] ; ++ i
)
961 if ( m_parents
[i
]->IsKindOf( info
) )
968 // Initializes parent pointers and hash table for fast searching.
969 // this is going to be removed by Register/Unregister calls
970 // in Constructor / Destructor together with making the hash map private
972 static void InitializeClasses();
974 // Cleans up hash table used for fast searching.
976 static void CleanUpClasses();
978 // returns the first property
979 const wxPropertyInfo
* GetFirstProperty() const { return m_firstProperty
; }
981 // returns the first handler
982 const wxHandlerInfo
* GetFirstHandler() const { return m_firstHandler
; }
984 // Call the Create method for a class
985 virtual void Create (wxObject
*object
, int ParamCount
, wxxVariant
*Params
)
987 wxASSERT_MSG( ParamCount
== m_constructorPropertiesCount
, wxT("Illegal Parameter Count for Create Method")) ;
988 m_constructor
->Create( object
, Params
) ;
991 // get number of parameters for constructor
992 virtual int GetCreateParamCount() const { return m_constructorPropertiesCount
; }
994 // get i-th constructor parameter
995 virtual const wxChar
* GetCreateParamName(int i
) const { return m_constructorProperties
[i
] ; }
997 // Runtime access to objects by property name, and variant data
998 virtual void SetProperty (wxObject
*object
, const wxChar
*PropertyName
, const wxxVariant
&Value
);
999 virtual wxxVariant
GetProperty (wxObject
*object
, const wxChar
*PropertyName
);
1001 // we must be able to cast variants to wxObject pointers, templates seem not to be suitable
1002 wxObject
* VariantToInstance( const wxxVariant
&data
) const { return m_variantToObjectConverter( data
) ; }
1003 wxxVariant
InstanceToVariant( wxObject
*object
) const { return m_objectToVariantConverter( object
) ; }
1005 // find property by name
1006 virtual const wxPropertyInfo
*FindPropertyInfo (const wxChar
*PropertyName
) const ;
1008 // find handler by name
1009 virtual const wxHandlerInfo
*FindHandlerInfo (const wxChar
*PropertyName
) const ;
1012 const wxChar
*m_className
;
1014 wxObjectConstructorFn m_objectConstructor
;
1016 // class info object live in a linked list:
1017 // pointers to its head and the next element in it
1019 static wxClassInfo
*sm_first
;
1020 wxClassInfo
*m_next
;
1022 // FIXME: this should be private (currently used directly by way too
1024 static wxHashTable
*sm_classTable
;
1027 const wxClassInfo
** m_parents
;
1028 const wxPropertyInfo
* m_firstProperty
;
1029 const wxHandlerInfo
* m_firstHandler
;
1030 const wxChar
* m_unitName
;
1032 wxConstructorBridge
* m_constructor
;
1033 const wxChar
** m_constructorProperties
;
1034 const int m_constructorPropertiesCount
;
1035 wxVariantToObjectConverter m_variantToObjectConverter
;
1036 wxObjectToVariantConverter m_objectToVariantConverter
;
1038 const wxPropertyAccessor
*FindAccessor (const wxChar
*propertyName
);
1040 // registers the class
1041 static void Register(const wxChar
*name
, wxClassInfo
*info
);
1043 static void Unregister(const wxChar
*name
);
1045 // InitializeClasses() helper
1046 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
1048 DECLARE_NO_COPY_CLASS(wxClassInfo
)
1051 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
1053 // ----------------------------------------------------------------------------
1054 // Dynamic class macros
1055 // ----------------------------------------------------------------------------
1057 #define _DECLARE_DYNAMIC_CLASS(name) \
1059 static wxClassInfo sm_class##name; \
1060 static const wxClassInfo* sm_classParents##name[] ; \
1061 static const wxPropertyInfo* GetPropertiesStatic() ; \
1062 static const wxHandlerInfo* GetHandlersStatic() ; \
1063 virtual wxClassInfo *GetClassInfo() const \
1064 { return &name::sm_class##name; }
1066 #define DECLARE_DYNAMIC_CLASS(name) \
1067 _DECLARE_DYNAMIC_CLASS(name) \
1068 static wxConstructorBridge* sm_constructor##name ; \
1069 static const wxChar * sm_constructorProperties##name[] ; \
1070 static const int sm_constructorPropertiesCount##name ;
1072 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
1073 DECLARE_NO_ASSIGN_CLASS(name) \
1074 DECLARE_DYNAMIC_CLASS(name)
1076 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
1077 DECLARE_NO_COPY_CLASS(name) \
1078 DECLARE_DYNAMIC_CLASS(name)
1080 #define DECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
1081 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
1083 // -----------------------------------
1084 // for concrete classes
1085 // -----------------------------------
1087 // Single inheritance with one base class
1089 #define _IMPLEMENT_DYNAMIC_CLASS(name, basename, unit) \
1090 wxObject* wxConstructorFor##name() \
1091 { return new name; } \
1092 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,NULL } ; \
1093 wxObject* wxVariantToObjectConverter##name ( const wxxVariant &data ) { return data.Get<name*>() ; } \
1094 wxxVariant wxObjectToVariantConverter##name ( wxObject *data ) { return wxxVariant( dynamic_cast<name*> (data) ) ; } \
1095 wxClassInfo name::sm_class##name(sm_classParents##name , wxT(unit) , wxT(#name), \
1096 (int) sizeof(name), \
1097 (wxObjectConstructorFn) wxConstructorFor##name , \
1098 name::GetPropertiesStatic(),name::GetHandlersStatic(),name::sm_constructor##name , name::sm_constructorProperties##name , \
1099 name::sm_constructorPropertiesCount##name , wxVariantToObjectConverter##name , wxObjectToVariantConverter##name); \
1100 template<> void wxStringReadValue(const wxString & , name * & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") ) ;}\
1101 template<> void wxStringWriteValue(wxString & , name* const & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1102 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1104 #define IMPLEMENT_DYNAMIC_CLASS( name , basename ) \
1105 _IMPLEMENT_DYNAMIC_CLASS( name , basename , "" ) \
1106 const wxPropertyInfo *name::GetPropertiesStatic() { return (wxPropertyInfo*) NULL ; } \
1107 const wxHandlerInfo *name::GetHandlersStatic() { return (wxHandlerInfo*) NULL ; } \
1108 WX_CONSTRUCTOR_DUMMY( name )
1110 #define IMPLEMENT_DYNAMIC_CLASS_XTI( name , basename , unit ) \
1111 _IMPLEMENT_DYNAMIC_CLASS( name , basename , unit )
1113 // this is for classes that do not derive from wxobject, there are no creators for these
1115 #define IMPLEMENT_DYNAMIC_CLASS_NO_WXOBJECT_NO_BASE_XTI( name , unit ) \
1116 const wxClassInfo* name::sm_classParents##name[] = { NULL } ; \
1117 wxClassInfo name::sm_class##name(sm_classParents##name , wxT("") , wxT(#name), \
1118 (int) sizeof(name), \
1119 (wxObjectConstructorFn) 0 , \
1120 name::GetPropertiesStatic(),name::GetHandlersStatic(),0 , 0 , \
1122 template<> void wxStringReadValue(const wxString & , name * & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1123 template<> void wxStringWriteValue(wxString & , name* const & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1124 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1126 // this is for subclasses that still do not derive from wxobject
1128 #define IMPLEMENT_DYNAMIC_CLASS_NO_WXOBJECT_XTI( name , basename, unit ) \
1129 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,NULL } ; \
1130 wxClassInfo name::sm_class##name(sm_classParents##name , wxT("") , wxT(#name), \
1131 (int) sizeof(name), \
1132 (wxObjectConstructorFn) 0 , \
1133 name::GetPropertiesStatic(),name::GetHandlersStatic(),0 , 0 , \
1135 template<> void wxStringReadValue(const wxString & , name * & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1136 template<> void wxStringWriteValue(wxString & , name* const & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1137 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1139 // Multiple inheritance with two base classes
1141 #define _IMPLEMENT_DYNAMIC_CLASS2(name, basename, basename2, unit) \
1142 wxObject* wxConstructorFor##name() \
1143 { return new name; } \
1144 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,&basename2::sm_class##basename2 , NULL } ; \
1145 wxObject* wxVariantToObjectConverter##name ( const wxxVariant &data ) { return data.Get<name*>() ; } \
1146 wxxVariant wxObjectToVariantConverter##name ( wxObject *data ) { return wxxVariant( dynamic_cast<name*> (data) ) ; } \
1147 wxClassInfo name::sm_class##name(sm_classParents##name , wxT(unit) , wxT(#name), \
1148 (int) sizeof(name), \
1149 (wxObjectConstructorFn) wxConstructorFor##name , \
1150 name::GetPropertiesStatic(),name::GetHandlersStatic(),name::sm_constructor##name , name::sm_constructorProperties##name , \
1151 name::sm_constructorPropertiesCount##name , wxVariantToObjectConverter##name , wxObjectToVariantConverter##name); \
1152 template<> void wxStringReadValue(const wxString & , name * & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1153 template<> void wxStringWriteValue(wxString & , name* const & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1154 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1156 #define IMPLEMENT_DYNAMIC_CLASS2( name , basename , basename2) \
1157 _IMPLEMENT_DYNAMIC_CLASS2( name , basename , basename2 , "") \
1158 const wxPropertyInfo *name::GetPropertiesStatic() { return (wxPropertyInfo*) NULL ; } \
1159 const wxHandlerInfo *name::GetHandlersStatic() { return (wxHandlerInfo*) NULL ; } \
1160 WX_CONSTRUCTOR_DUMMY( name )
1162 #define IMPLEMENT_DYNAMIC_CLASS2_XTI( name , basename , basename2, unit) \
1163 _IMPLEMENT_DYNAMIC_CLASS2( name , basename , basename2 , unit)
1165 // -----------------------------------
1166 // for abstract classes
1167 // -----------------------------------
1169 // Single inheritance with one base class
1171 #define _IMPLEMENT_ABSTRACT_CLASS(name, basename) \
1172 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,NULL } ; \
1173 wxObject* wxVariantToObjectConverter##name ( const wxxVariant &data ) { return data.Get<name*>() ; } \
1174 wxxVariant wxObjectToVariantConverter##name ( wxObject *data ) { return wxxVariant( dynamic_cast<name*> (data) ) ; } \
1175 wxClassInfo name::sm_class##name(sm_classParents##name , wxT("") , wxT(#name), \
1176 (int) sizeof(name), \
1177 (wxObjectConstructorFn) 0 , \
1178 name::GetPropertiesStatic(),name::GetHandlersStatic(),0 , 0 , \
1179 0 , wxVariantToObjectConverter##name , wxObjectToVariantConverter##name); \
1180 template<> void wxStringReadValue(const wxString & , name * & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1181 template<> void wxStringWriteValue(wxString & , name* const & ){wxASSERT_MSG( 0 , wxT("Illegal Spezialication Called") );}\
1182 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1184 #define IMPLEMENT_ABSTRACT_CLASS( name , basename ) \
1185 _IMPLEMENT_ABSTRACT_CLASS( name , basename ) \
1186 const wxHandlerInfo *name::GetHandlersStatic() { return (wxHandlerInfo*) NULL ; } \
1187 const wxPropertyInfo *name::GetPropertiesStatic() { return (wxPropertyInfo*) NULL ; }
1189 // Multiple inheritance with two base classes
1191 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
1192 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
1193 wxT(#basename2), (int) sizeof(name), \
1194 (wxObjectConstructorFn) 0);
1196 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
1197 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2