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"
44 class WXDLLIMPEXP_BASE wxObject
;
45 class WXDLLIMPEXP_BASE wxClassInfo
;
46 class WXDLLIMPEXP_BASE wxHashTable
;
47 class WXDLLIMPEXP_BASE wxObjectRefData
;
48 class WXDLLIMPEXP_BASE wxEvent
;
50 typedef void (wxObject::*wxObjectEventFunction
)(wxEvent
&);
52 // ----------------------------------------------------------------------------
55 // In the header files there would no change from pure c++ code, in the
56 // implementation, an enum would have
57 // to be enumerated eg :
59 // WX_BEGIN_ENUM( wxFlavor )
60 // WX_ENUM_MEMBER( Vanilla )
61 // WX_ENUM_MEMBER( Chocolate )
62 // WX_ENUM_MEMBER( Strawberry )
63 // WX_END_ENUM( wxFlavor )
64 // ----------------------------------------------------------------------------
66 struct WXDLLIMPEXP_BASE wxEnumMemberData
72 class WXDLLIMPEXP_BASE wxEnumData
75 wxEnumData( wxEnumMemberData
* data
) ;
77 // returns true if the member has been found and sets the int value
78 // pointed to accordingly (if ptr != null )
79 // if not found returns false, value left unchanged
80 bool HasEnumMemberValue( const wxChar
*name
, int *value
= NULL
) ;
82 // returns the value of the member, if not found in debug mode an
83 // assert is issued, in release 0 is returned
84 int GetEnumMemberValue(const wxChar
*name
);
86 // returns the name of the enum member having the passed in value
87 // returns an emtpy string if not found
88 const wxChar
*GetEnumMemberName(int value
);
90 // returns the number of members in this enum
91 int GetEnumCount() { return m_count
; }
93 // returns the value of the nth member
94 int GetEnumMemberValueByIndex( int n
) ;
96 // returns the value of the nth member
97 const wxChar
*GetEnumMemberNameByIndex( int n
) ;
99 wxEnumMemberData
*m_members
;
103 #define WX_BEGIN_ENUM( e ) \
104 wxEnumMemberData s_enumDataMembers##e[] = {
106 #define WX_ENUM_MEMBER( v ) { #v, v } ,
108 #define WX_END_ENUM( e ) { NULL , 0 } } ; \
109 wxEnumData s_enumData##e( s_enumDataMembers##e ) ; \
110 wxEnumData *wxGetEnumData(e) { return &s_enumData##e ; } \
111 template<> const wxTypeInfo* wxGetTypeInfo( e * ){ static wxEnumTypeInfo s_typeInfo(wxT_ENUM , &s_enumData##e) ; return &s_typeInfo ; } \
112 template<> void wxStringReadValue(const wxString& s , e &data ) \
114 data = (e) s_enumData##e.GetEnumMemberValue(s) ; \
116 template<> void wxStringWriteValue(wxString &s , const e &data ) \
118 s = s_enumData##e.GetEnumMemberName((int)data) ; \
121 // ----------------------------------------------------------------------------
133 // typedef wxSet<wxFlavor> wxCoupe ;
135 // in the implementation file :
137 // WX_BEGIN_ENUM( wxFlavor )
138 // WX_ENUM_MEMBER( Vanilla )
139 // WX_ENUM_MEMBER( Chocolate )
140 // WX_ENUM_MEMBER( Strawberry )
141 // WX_END_ENUM( wxFlavor )
143 // WX_IMPLEMENT_SET_STREAMING( wxCoupe , wxFlavor )
145 // implementation note : no partial specialization for streaming, but a delegation to a
148 // ----------------------------------------------------------------------------
150 // in order to remove dependancy on string tokenizer
151 void wxSetStringToArray( const wxString
&s
, wxArrayString
&array
) ;
154 void wxSetFromString(const wxString
&s
, wxSet
<e
> &data
)
156 wxEnumData
* edata
= wxGetEnumData((e
) 0) ;
159 wxArrayString array
;
160 wxSetStringToArray( s
, array
) ;
162 for ( int i
= 0 ; i
< array
.Count() ; ++i
)
166 if ( edata
->HasEnumMemberValue( flag
, &ivalue
) )
168 data
.Set( (e
) ivalue
) ;
174 void wxSetToString( wxString
&s
, const wxSet
<e
> &data
)
176 wxEnumData
* edata
= wxGetEnumData((e
) 0) ;
177 int count
= edata
->GetEnumCount() ;
180 for ( i
= 0 ; i
< count
; i
++ )
182 e value
= (e
) edata
->GetEnumMemberValueByIndex(i
) ;
183 if ( data
.Contains( value
) )
185 // this could also be done by the templated calls
188 s
+= edata
->GetEnumMemberNameByIndex(i
) ;
193 // if the wxSet specialization above does not work for all compilers, add this to the WX_IMPLEMENT_SET_STREAMING macro
194 // template<> const wxTypeInfo* wxGetTypeInfo( SetName * ){ static wxEnumTypeInfo s_typeInfo(wxT_SET , &s_enumData##e) ; return &s_typeInfo ; }\
196 #define WX_IMPLEMENT_SET_STREAMING(SetName,e) \
197 template<> void wxStringReadValue(const wxString &s , wxSet<e> &data ) \
199 wxSetFromString( s , data ) ; \
201 template<> void wxStringWriteValue( wxString &s , const wxSet<e> &data ) \
203 wxSetToString( s , data ) ; \
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
213 wxT_VOID
= 0, // unknown type
223 wxT_STRING
, // must be wxString
224 wxT_SET
, // must be wxSet<> template
226 wxT_OBJECT
, // must be a component (pointer !!!)
227 wxT_CUSTOM
, // user defined type (e.g. wxPoint)
228 wxT_DELEGATE
, // for connecting against an event source
229 wxT_LAST_TYPE_KIND
// sentinel for bad data, asserts, debugging
232 class WXDLLIMPEXP_BASE wxTypeInfo
235 wxTypeInfo() : m_kind( wxT_VOID
) {}
236 virtual ~wxTypeInfo() {}
237 wxTypeKind
GetKind() const { return m_kind
; }
242 class WXDLLIMPEXP_BASE wxBuiltInTypeInfo
: public wxTypeInfo
245 wxBuiltInTypeInfo( wxTypeKind kind
) { assert( kind
< wxT_SET
) ; m_kind
= kind
;}
248 class WXDLLIMPEXP_BASE wxCustomTypeInfo
: public wxTypeInfo
251 wxCustomTypeInfo( const wxChar
*typeName
)
252 { m_kind
= wxT_CUSTOM
; m_typeName
= typeName
;}
253 const wxChar
*GetTypeName() const { assert( m_kind
== wxT_CUSTOM
) ; return m_typeName
; }
255 const wxChar
*m_typeName
; // Kind == wxT_CUSTOM
258 class WXDLLIMPEXP_BASE wxEnumTypeInfo
: public wxTypeInfo
261 wxEnumTypeInfo( wxTypeKind kind
, wxEnumData
* enumInfo
)
262 { assert( kind
== wxT_ENUM
|| kind
== wxT_SET
) ; m_kind
= kind
; m_enumInfo
= enumInfo
;}
263 const wxEnumData
* GetEnumData() const { assert( m_kind
== wxT_ENUM
|| m_kind
== wxT_SET
) ; return m_enumInfo
; }
265 wxEnumData
*m_enumInfo
; // Kind == wxT_ENUM or Kind == wxT_SET
268 class WXDLLIMPEXP_BASE wxClassTypeInfo
: public wxTypeInfo
271 wxClassTypeInfo( wxClassInfo
* classInfo
)
272 { m_kind
= wxT_OBJECT
; m_classInfo
= classInfo
;}
273 const wxClassInfo
*GetClassInfo() const { assert( m_kind
== wxT_OBJECT
) ; return m_classInfo
; }
275 wxClassInfo
*m_classInfo
; // Kind == wxT_OBJECT - could be NULL
278 // a delegate is an exposed event source
280 class WXDLLIMPEXP_BASE wxDelegateTypeInfo
: public wxTypeInfo
283 wxDelegateTypeInfo( int eventType
, wxClassInfo
* eventClass
)
284 { m_kind
= wxT_DELEGATE
; m_eventClass
= eventClass
; m_eventType
= eventType
;}
285 const wxClassInfo
*GetEventClass() const { assert( m_kind
== wxT_DELEGATE
) ; return m_eventClass
; }
286 int GetEventType() const { assert( m_kind
== wxT_DELEGATE
) ; return m_eventType
; }
288 const wxClassInfo
*m_eventClass
; // (extended will merge into classinfo)
292 template<typename T
> const wxTypeInfo
* wxGetTypeInfo( T
* ) ;
294 template<typename T
> const wxTypeInfo
* wxGetTypeInfo( wxSet
<T
> * )
296 static wxEnumTypeInfo
s_typeInfo(wxT_SET
, wxGetEnumData((T
) 0) ) ; return &s_typeInfo
;
299 // this macro is for usage with custom, non-object derived classes and structs, wxPoint is such a custom type
301 #define WX_CUSTOM_TYPE_INFO( e ) \
302 template<> const wxTypeInfo* wxGetTypeInfo( e * ){ static wxCustomTypeInfo s_typeInfo(#e) ; return &s_typeInfo ; } \
304 // ----------------------------------------------------------------------------
307 // streaming is defined for xml constructs right now, the aim is to make this
308 // pluggable in the future
309 // ----------------------------------------------------------------------------
311 // convenience function (avoids including xml headers in users code)
314 void wxXmlAddContentToNode( wxXmlNode
* node
, const wxString
& data
) ;
315 wxString
wxXmlGetContentFromNode( wxXmlNode
*node
) ;
317 // templated streaming, every type must have their specialization for these methods
320 void wxStringReadValue( const wxString
&s
, T
&data
) ;
323 void wxStringWriteValue( wxString
&s
, const T
&data
) ;
325 // for simple types this default implementation is ok, composited structures will have to
326 // loop through their properties
329 void wxXmlReadValue( wxXmlNode
*node
, T
&data
)
331 wxStringReadValue
<T
>( wxXmlGetContentFromNode( node
) , data
) ;
335 void wxXmlWriteValue( wxXmlNode
*node
, const T
&data
)
338 wxStringWriteValue
<T
>( s
, data
) ;
339 wxXmlAddContentToNode( node
,s
) ;
342 // ----------------------------------------------------------------------------
343 // wxxVariant as typesafe data holder
344 // ----------------------------------------------------------------------------
346 class WXDLLIMPEXP_BASE wxxVariantData
349 virtual ~wxxVariantData() {}
351 // return a heap allocated duplicate
352 virtual wxxVariantData
* Clone() const = 0 ;
354 // returns the type info of the contentc
355 virtual const wxTypeInfo
* GetTypeInfo() const = 0 ;
357 // write the value into an xml node
358 virtual void Write( wxXmlNode
* node
) const = 0 ;
360 // read the value from the xml node
361 virtual void Read( wxXmlNode
* node
) = 0 ;
363 // write the value into a string
364 virtual void Write( wxString
&s
) const = 0 ;
366 // read the value from a string
367 virtual void Read( const wxString
&s
) = 0 ;
370 template<typename T
> class WXDLLIMPEXP_BASE wxxVariantDataT
: public wxxVariantData
373 wxxVariantDataT(T d
) : m_data(d
) {}
374 virtual ~wxxVariantDataT() {}
376 // get a copy of the stored data
377 T
Get() const { return m_data
; }
380 void Set(T d
) { m_data
= d
; }
382 // return a heap allocated duplicate
383 virtual wxxVariantData
* Clone() const { return new wxxVariantDataT
<T
>( Get() ) ; }
385 // returns the type info of the contentc
386 virtual const wxTypeInfo
* GetTypeInfo() const { return wxGetTypeInfo( (T
*) NULL
) ; }
388 // write the value into an xml node
389 virtual void Write( wxXmlNode
* node
) const { wxXmlWriteValue( node
, m_data
) ; }
391 // read the value from the xml node
392 virtual void Read( wxXmlNode
* node
) { wxXmlReadValue( node
, m_data
) ; }
394 // write the value into a string
395 virtual void Write( wxString
&s
) const { wxStringWriteValue( s
, m_data
) ; }
397 // read the value from a string
398 virtual void Read( const wxString
&s
) { wxStringReadValue( s
, m_data
) ; }
404 class WXDLLIMPEXP_BASE wxxVariant
407 wxxVariant() { m_data
= NULL
; }
408 wxxVariant( wxxVariantData
* data
, const wxString
& name
= wxT("") ) : m_data(data
) , m_name(name
) {}
409 wxxVariant( const wxxVariant
&d
) { if ( d
.m_data
) m_data
= d
.m_data
->Clone() ; else m_data
= NULL
; m_name
= d
.m_name
; }
411 template<typename T
> wxxVariant( T data
, const wxString
& name
= wxT("") ) :
412 m_data(new wxxVariantDataT
<T
>(data
) ), m_name(name
) {}
413 ~wxxVariant() { delete m_data
; }
415 // get a copy of the stored data
416 template<typename T
> T
Get() const
418 wxxVariantDataT
<T
> *dataptr
= dynamic_cast<wxxVariantDataT
<T
>*> (m_data
) ;
420 return dataptr
->Get() ;
424 template<typename T
> Set(T data
) const
427 m_data
= new wxxVariantDataT
<T
>(data
) ;
430 wxxVariant
& operator=(const wxxVariant
&d
)
432 m_data
= d
.m_data
->Clone() ;
437 // gets the stored data casted to a wxObject* , returning NULL if cast is not possible
438 wxObject
* GetAsObject() const ;
440 // write the value into an xml node
441 void Write( wxXmlNode
* node
) const { m_data
->Write( node
) ; }
443 // read the value from the xml node
444 void Read( wxXmlNode
* node
) { m_data
->Read( node
) ; }
446 // write the value into a string
447 void Write( wxString
&s
) const { m_data
->Write( s
) ; }
449 // read the value from a string
450 void Read( const wxString
&s
) { m_data
->Read( s
) ; }
452 // returns this value as string
453 wxString
GetAsString() const
460 void SetFromString( const wxString
&s
)
465 wxxVariantData
* m_data
;
469 // ----------------------------------------------------------------------------
472 // wxPropertyInfo is used to inquire of the property by name. It doesn't
473 // provide access to the property, only information about it. If you
474 // want access, look at wxPropertyAccessor.
475 // ----------------------------------------------------------------------------
477 class WXDLLIMPEXP_BASE wxPropertyAccessor
480 virtual void SetProperty(wxObject
*object
, const wxxVariant
&value
) const = 0 ;
481 virtual wxxVariant
GetProperty(wxObject
*object
) const = 0 ;
482 virtual bool HasSetter() const = 0 ;
483 virtual bool HasGetter() const = 0 ;
484 const wxChar
* GetGetterName() const { return m_setterName
; }
485 const wxChar
* GetSetterName() const { return m_getterName
; }
486 virtual wxxVariant
ReadValue( wxXmlNode
* node
) const = 0 ;
487 virtual void WriteValue( wxXmlNode
* node
, wxObject
*o
) const = 0 ;
488 virtual wxxVariant
ReadValue( const wxString
&value
) const = 0 ;
489 virtual void WriteValue( wxString
& value
, wxObject
*o
) const = 0 ;
491 const wxChar
*m_setterName
;
492 const wxChar
*m_getterName
;
496 void wxXmlReadValue( wxXmlNode
*node
, T
&data
) ;
499 void wxXmlWriteValue( wxXmlNode
*node
, const T
&data
) ;
501 template<class Klass
, typename T
>
502 class WXDLLIMPEXP_BASE wxPropertyAccessorT
: public wxPropertyAccessor
505 typedef void (Klass::*setter_t
)(T value
);
506 typedef void (Klass::*setter_ref_t
)(const T
& value
);
507 typedef T (Klass::*getter_t
)() const;
508 typedef const T
& (Klass::*getter_ref_t
)() const;
510 wxPropertyAccessorT(setter_t setter
, getter_t getter
, const wxChar
*g
, const wxChar
*s
)
511 : m_setter(setter
), m_setter_ref(NULL
), m_getter(getter
) ,m_getter_ref(NULL
) {m_setterName
= s
;m_getterName
=g
;}
512 wxPropertyAccessorT(int WXUNUSED(zero
), getter_t getter
, const wxChar
*g
, const wxChar
*s
)
513 : m_setter(NULL
), m_setter_ref(NULL
), m_getter(getter
) ,m_getter_ref(NULL
) {m_setterName
= s
;m_getterName
=g
;}
514 wxPropertyAccessorT(setter_ref_t setter
, getter_t getter
, const wxChar
*g
, const wxChar
*s
)
515 : m_setter(NULL
), m_setter_ref(setter
), m_getter(getter
) , m_getter_ref(NULL
){m_setterName
= s
;m_getterName
=g
;}
516 wxPropertyAccessorT(setter_ref_t setter
, getter_ref_t getter
, const wxChar
*g
, const wxChar
*s
)
517 : m_setter(NULL
), m_setter_ref(setter
), m_getter(NULL
) , m_getter_ref(getter
){m_setterName
= s
;m_getterName
=g
;}
518 wxPropertyAccessorT(setter_t setter
, getter_ref_t getter
, const wxChar
*g
, const wxChar
*s
)
519 : m_setter(NULL
), m_setter(setter
), m_getter(NULL
) , m_getter_ref(getter
){m_setterName
= s
;m_getterName
=g
;}
520 wxPropertyAccessorT(int WXUNUSED(zero
), getter_ref_t getter
, const wxChar
*g
, const wxChar
*s
)
521 : m_setter(NULL
), m_setter(NULL
), m_getter(NULL
) , m_getter_ref(getter
){m_setterName
= s
;m_getterName
=g
;}
523 // returns true if this accessor has a setter
524 bool HasSetter() const { return m_setter
!= NULL
|| m_setter_ref
!= NULL
; }
526 // return true if this accessor has a getter
527 bool HasGetter() const { return m_getter
!= NULL
|| m_getter_ref
!= NULL
; }
529 // set the property this accessor is responsible for in an object
530 void SetProperty(wxObject
*o
, const wxxVariant
&v
) const
532 Klass
*obj
= dynamic_cast<Klass
*>(o
);
533 T value
= v
.Get
<T
>();
535 (obj
->*(m_setter
))(value
);
537 (obj
->*(m_setter_ref
))(value
);
540 // gets the property this accessor is responsible for from an object
541 wxxVariant
GetProperty(wxObject
*o
) const
543 return wxxVariant( (wxxVariantData
* ) DoGetProperty( o
) ) ;
546 // write the property this accessor is responsible for from an object into
548 void WriteValue( wxXmlNode
* node
, wxObject
*o
) const
550 DoGetProperty( o
)->Write( node
) ;
553 // write the property this accessor is responsible for from an object into
555 void WriteValue( wxString
& s
, wxObject
*o
) const
557 DoGetProperty( o
)->Write( s
) ;
560 // read a wxxVariant having the correct type for the property this accessor
561 // is responsible for from an xml node
562 wxxVariant
ReadValue( wxXmlNode
* node
) const
565 wxXmlReadValue( node
, data
) ;
566 return wxxVariant( data
) ;
569 // read a wxxVariant having the correct type for the property this accessor
570 // is responsible for from a string
571 wxxVariant
ReadValue( const wxString
&value
) const
574 wxStringReadValue( value
, data
) ;
575 return wxxVariant( data
) ;
579 wxxVariantDataT
<T
>* DoGetProperty(wxObject
*o
) const
581 Klass
*obj
= dynamic_cast<Klass
*>(o
);
583 return new wxxVariantDataT
<T
>( (obj
->*(m_getter
))() ) ;
585 return new wxxVariantDataT
<T
>( (obj
->*(m_getter_ref
))() ) ;
589 setter_ref_t m_setter_ref
;
591 getter_ref_t m_getter_ref
;
594 class WXDLLIMPEXP_BASE wxPropertyInfo
597 wxPropertyInfo( wxPropertyInfo
* &iter
, const wxChar
*name
, const wxChar
*typeName
, const wxTypeInfo
* typeInfo
, wxPropertyAccessor
*accessor
, wxxVariant dv
) :
598 m_name( name
) , m_typeName(typeName
) , m_typeInfo( typeInfo
) , m_accessor( accessor
) , m_defaultValue( dv
)
605 wxPropertyInfo
* i
= iter
;
612 // return the name of this property
613 const wxChar
* GetName() const { return m_name
; }
615 // return the typename of this property
616 const wxChar
* GetTypeName() const { return m_typeName
; }
618 // return the type info of this property
619 const wxTypeInfo
* GetTypeInfo() const { return m_typeInfo
; }
621 // return the accessor for this property
622 wxPropertyAccessor
* GetAccessor() const { return m_accessor
; }
624 // returns NULL if this is the last property of this class
625 wxPropertyInfo
* GetNext() const { return m_next
; }
627 // returns the default value of this property, its kind may be wxT_VOID if it is not valid
628 wxxVariant
GetDefaultValue() const { return m_defaultValue
; }
630 const wxChar
* m_name
;
631 const wxChar
* m_typeName
;
632 const wxTypeInfo
* m_typeInfo
;
633 wxPropertyAccessor
* m_accessor
;
634 wxxVariant m_defaultValue
;
635 // string representation of the default value
636 // to be assigned by the designer to the property
637 // when the component is dropped on the container.
638 wxPropertyInfo
* m_next
;
641 #define WX_BEGIN_PROPERTIES_TABLE(theClass) \
642 const wxPropertyInfo *theClass::GetPropertiesStatic() \
644 typedef theClass class_t; \
645 static wxPropertyInfo* first = NULL ;
647 #define WX_END_PROPERTIES_TABLE() \
650 #define WX_PROPERTY( name , type , setter , getter ,defaultValue ) \
651 static wxPropertyAccessorT<class_t , type> _accessor##name( setter , getter , #setter , #getter ) ; \
652 static wxPropertyInfo _propertyInfo##name( first , #name , #type , wxGetTypeInfo( (type*) NULL ) ,&_accessor##name , wxxVariant(defaultValue) ) ;
654 #define WX_DELEGATE( name , eventType , eventClass ) \
655 static wxDelegateTypeInfo _typeInfo##name( eventType , CLASSINFO( eventClass ) ) ; \
656 static wxPropertyInfo _propertyInfo##name( first , #name , NULL , &_typeInfo##name , NULL , wxxVariant() ) ; \
658 // ----------------------------------------------------------------------------
661 // this is describing an event sink
662 // ----------------------------------------------------------------------------
667 wxHandlerInfo( wxHandlerInfo
* &iter
, const wxChar
*name
, wxObjectEventFunction address
, const wxClassInfo
* eventClassInfo
) :
668 m_name( name
) , m_eventClassInfo( eventClassInfo
) , m_eventFunction( address
)
675 wxHandlerInfo
* i
= iter
;
683 // get the name of the handler method
684 const wxChar
* GetName() const { return m_name
; }
686 // return the class info of the event
687 const wxClassInfo
* GetEventClassInfo() const { return m_eventClassInfo
; }
689 // get the handler function pointer
690 wxObjectEventFunction
GetEventFunction() const { return m_eventFunction
; }
692 // returns NULL if this is the last handler of this class
693 wxHandlerInfo
* GetNext() const { return m_next
; }
695 wxObjectEventFunction m_eventFunction
;
696 const wxChar
* m_name
;
697 const wxClassInfo
* m_eventClassInfo
;
698 wxHandlerInfo
* m_next
;
701 #define WX_HANDLER(name,eventClassType) \
702 static wxHandlerInfo _handlerInfo##name( first , #name , (wxObjectEventFunction) (wxEventFunction) &name , CLASSINFO( eventClassType ) ) ;
704 #define WX_BEGIN_HANDLERS_TABLE(theClass) \
705 const wxHandlerInfo *theClass::GetHandlersStatic() \
707 typedef theClass class_t; \
708 static wxHandlerInfo* first = NULL ;
710 #define WX_END_HANDLERS_TABLE() \
713 // ----------------------------------------------------------------------------
714 // Constructor Bridges
716 // allow to set up constructors with params during runtime
717 // ----------------------------------------------------------------------------
719 class WXDLLIMPEXP_BASE wxConstructorBridge
722 virtual void Create(wxObject
*o
, wxxVariant
*args
) = 0;
725 // Creator Bridges for all Numbers of Params
729 template<typename Class
>
730 struct wxConstructorBridge_0
: public wxConstructorBridge
732 void Create(wxObject
*o
, wxxVariant
*)
734 Class
*obj
= dynamic_cast<Class
*>(o
);
739 struct wxConstructorBridge_Dummy
: public wxConstructorBridge
741 void Create(wxObject
*, wxxVariant
*)
746 #define WX_CONSTRUCTOR_0(klass) \
747 wxConstructorBridge_0<klass> constructor##klass ; \
748 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
749 const wxChar *klass::sm_constructorProperties##klass[] = { NULL } ; \
750 const int klass::sm_constructorPropertiesCount##klass = 0 ;
752 #define WX_CONSTRUCTOR_DUMMY(klass) \
753 wxConstructorBridge_Dummy constructor##klass ; \
754 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
755 const wxChar *klass::sm_constructorProperties##klass[] = { NULL } ; \
756 const int klass::sm_constructorPropertiesCount##klass = 0 ;
760 template<typename Class
, typename T0
>
761 struct wxConstructorBridge_1
: public wxConstructorBridge
763 void Create(wxObject
*o
, wxxVariant
*args
)
765 Class
*obj
= dynamic_cast<Class
*>(o
);
772 #define WX_CONSTRUCTOR_1(klass,t0,v0) \
773 wxConstructorBridge_1<klass,t0> constructor##klass ; \
774 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
775 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 } ; \
776 const int klass::sm_constructorPropertiesCount##klass = 1 ;
780 template<typename Class
,
781 typename T0
, typename T1
>
782 struct wxConstructorBridge_2
: public wxConstructorBridge
784 void Create(wxObject
*o
, wxxVariant
*args
)
786 Class
*obj
= dynamic_cast<Class
*>(o
);
794 #define WX_CONSTRUCTOR_2(klass,t0,v0,t1,v1) \
795 wxConstructorBridge_2<klass,t0,t1> constructor##klass ; \
796 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
797 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 } ; \
798 const int klass::sm_constructorPropertiesCount##klass = 2;
802 template<typename Class
,
803 typename T0
, typename T1
, typename T2
>
804 struct wxConstructorBridge_3
: public wxConstructorBridge
806 void Create(wxObject
*o
, wxxVariant
*args
)
808 Class
*obj
= dynamic_cast<Class
*>(o
);
817 #define WX_CONSTRUCTOR_3(klass,t0,v0,t1,v1,t2,v2) \
818 wxConstructorBridge_3<klass,t0,t1,t2> constructor##klass ; \
819 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
820 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 , #v2 } ; \
821 const int klass::sm_constructorPropertiesCount##klass = 3 ;
825 template<typename Class
,
826 typename T0
, typename T1
, typename T2
, typename T3
>
827 struct wxConstructorBridge_4
: public wxConstructorBridge
829 void Create(wxObject
*o
, wxxVariant
*args
)
831 Class
*obj
= dynamic_cast<Class
*>(o
);
841 #define WX_CONSTRUCTOR_4(klass,t0,v0,t1,v1,t2,v2,t3,v3) \
842 wxConstructorBridge_4<klass,t0,t1,t2,t3> constructor##klass ; \
843 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
844 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 , #v2 , #v3 } ; \
845 const int klass::sm_constructorPropertiesCount##klass = 4 ;
849 template<typename Class
,
850 typename T0
, typename T1
, typename T2
, typename T3
, typename T4
>
851 struct wxConstructorBridge_5
: public wxConstructorBridge
853 void Create(wxObject
*o
, wxxVariant
*args
)
855 Class
*obj
= dynamic_cast<Class
*>(o
);
866 #define WX_CONSTRUCTOR_5(klass,t0,v0,t1,v1,t2,v2,t3,v3,t4,v4) \
867 wxConstructorBridge_5<klass,t0,t1,t2,t3,t4> constructor##klass ; \
868 wxConstructorBridge* klass::sm_constructor##klass = &constructor##klass ; \
869 const wxChar *klass::sm_constructorProperties##klass[] = { #v0 , #v1 , #v2 , #v3 , #v4 } ; \
870 const int klass::sm_constructorPropertiesCount##klass = 5;
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 typedef wxObject
*(*wxObjectConstructorFn
)(void);
877 typedef wxObject
* (*wxVariantToObjectConverter
)( const wxxVariant
&data
) ;
878 typedef wxxVariant (*wxObjectToVariantConverter
)( wxObject
* ) ;
880 class WXDLLIMPEXP_BASE wxClassInfo
883 wxClassInfo(const wxClassInfo
**_Parents
,
884 const wxChar
*_UnitName
,
885 const wxChar
*_ClassName
,
887 wxObjectConstructorFn ctor
,
888 const wxPropertyInfo
*_Props
,
889 const wxHandlerInfo
*_Handlers
,
890 wxConstructorBridge
* _Constructor
,
891 const wxChar
** _ConstructorProperties
,
892 const int _ConstructorPropertiesCount
,
893 wxVariantToObjectConverter _Converter1
,
894 wxObjectToVariantConverter _Converter2
895 ) : m_parents(_Parents
) , m_unitName(_UnitName
) ,m_className(_ClassName
),
896 m_objectSize(size
), m_objectConstructor(ctor
) , m_firstProperty(_Props
) , m_firstHandler(_Handlers
) , m_constructor( _Constructor
) ,
897 m_constructorProperties(_ConstructorProperties
) , m_constructorPropertiesCount(_ConstructorPropertiesCount
),
898 m_variantToObjectConverter( _Converter1
) , m_objectToVariantConverter( _Converter2
) , m_next(sm_first
)
901 Register( m_className
, this ) ;
906 wxObject
*CreateObject() { return m_objectConstructor
? (*m_objectConstructor
)() : 0; }
908 const wxChar
*GetClassName() const { return m_className
; }
909 const wxClassInfo
**GetParents() const { return m_parents
; }
910 int GetSize() const { return m_objectSize
; }
912 wxObjectConstructorFn
GetConstructor() const { return m_objectConstructor
; }
913 static const wxClassInfo
*GetFirst() { return sm_first
; }
914 const wxClassInfo
*GetNext() const { return m_next
; }
915 static wxClassInfo
*FindClass(const wxChar
*className
);
917 // Climb upwards through inheritance hierarchy.
918 // Dual inheritance is catered for.
920 bool IsKindOf(const wxClassInfo
*info
) const
927 for ( int i
= 0 ; m_parents
[i
] ; ++ i
)
929 if ( m_parents
[i
]->IsKindOf( info
) )
936 // Initializes parent pointers and hash table for fast searching.
937 // this is going to be removed by Register/Unregister calls
938 // in Constructor / Destructor together with making the hash map private
940 static void InitializeClasses();
942 // Cleans up hash table used for fast searching.
944 static void CleanUpClasses();
946 // returns the first property
947 const wxPropertyInfo
* GetFirstProperty() const { return m_firstProperty
; }
949 // returns the first handler
950 const wxHandlerInfo
* GetFirstHandler() const { return m_firstHandler
; }
952 // Call the Create method for a class
953 virtual void Create (wxObject
*object
, int ParamCount
, wxxVariant
*Params
)
955 wxASSERT( ParamCount
== m_constructorPropertiesCount
) ;
956 m_constructor
->Create( object
, Params
) ;
959 // get number of parameters for constructor
960 virtual int GetCreateParamCount() const { return m_constructorPropertiesCount
; }
962 // get i-th constructor parameter
963 virtual const wxChar
* GetCreateParamName(int i
) const { return m_constructorProperties
[i
] ; }
965 // Runtime access to objects by property name, and variant data
966 virtual void SetProperty (wxObject
*object
, const wxChar
*PropertyName
, const wxxVariant
&Value
);
967 virtual wxxVariant
GetProperty (wxObject
*object
, const wxChar
*PropertyName
);
969 // we must be able to cast variants to wxObject pointers, templates seem not to be suitable
970 wxObject
* VariantToInstance( const wxxVariant
&data
) const { return m_variantToObjectConverter( data
) ; }
971 wxxVariant
InstanceToVariant( wxObject
*object
) const { return m_objectToVariantConverter( object
) ; }
973 // find property by name
974 virtual const wxPropertyInfo
*FindPropertyInfo (const wxChar
*PropertyName
) const ;
976 // find handler by name
977 virtual const wxHandlerInfo
*FindHandlerInfo (const wxChar
*PropertyName
) const ;
980 const wxChar
*m_className
;
982 wxObjectConstructorFn m_objectConstructor
;
984 // class info object live in a linked list:
985 // pointers to its head and the next element in it
987 static wxClassInfo
*sm_first
;
990 // FIXME: this should be private (currently used directly by way too
992 static wxHashTable
*sm_classTable
;
995 const wxClassInfo
** m_parents
;
996 const wxPropertyInfo
* m_firstProperty
;
997 const wxHandlerInfo
* m_firstHandler
;
998 const wxChar
* m_unitName
;
1000 wxConstructorBridge
* m_constructor
;
1001 const wxChar
** m_constructorProperties
;
1002 const int m_constructorPropertiesCount
;
1003 wxVariantToObjectConverter m_variantToObjectConverter
;
1004 wxObjectToVariantConverter m_objectToVariantConverter
;
1006 const wxPropertyAccessor
*FindAccessor (const wxChar
*propertyName
);
1008 // registers the class
1009 static void Register(const wxChar
*name
, wxClassInfo
*info
);
1011 static void Unregister(const wxChar
*name
);
1013 // InitializeClasses() helper
1014 static wxClassInfo
*GetBaseByName(const wxChar
*name
);
1016 DECLARE_NO_COPY_CLASS(wxClassInfo
)
1019 WXDLLIMPEXP_BASE wxObject
*wxCreateDynamicObject(const wxChar
*name
);
1021 // ----------------------------------------------------------------------------
1022 // Dynamic class macros
1023 // ----------------------------------------------------------------------------
1025 #define _DECLARE_DYNAMIC_CLASS(name) \
1027 static wxClassInfo sm_class##name; \
1028 static const wxClassInfo* sm_classParents##name[] ; \
1029 static const wxPropertyInfo* GetPropertiesStatic() ; \
1030 static const wxHandlerInfo* GetHandlersStatic() ; \
1031 virtual wxClassInfo *GetClassInfo() const \
1032 { return &name::sm_class##name; }
1034 #define DECLARE_DYNAMIC_CLASS(name) \
1035 _DECLARE_DYNAMIC_CLASS(name) \
1036 static wxConstructorBridge* sm_constructor##name ; \
1037 static const wxChar * sm_constructorProperties##name[] ; \
1038 static const int sm_constructorPropertiesCount##name ;
1040 #define DECLARE_DYNAMIC_CLASS_NO_ASSIGN(name) \
1041 DECLARE_NO_ASSIGN_CLASS(name) \
1042 DECLARE_DYNAMIC_CLASS(name)
1044 #define DECLARE_DYNAMIC_CLASS_NO_COPY(name) \
1045 DECLARE_NO_COPY_CLASS(name) \
1046 DECLARE_DYNAMIC_CLASS(name)
1048 #define DECLARE_ABSTRACT_CLASS(name) _DECLARE_DYNAMIC_CLASS(name)
1049 #define DECLARE_CLASS(name) DECLARE_DYNAMIC_CLASS(name)
1051 // -----------------------------------
1052 // for concrete classes
1053 // -----------------------------------
1055 // Single inheritance with one base class
1057 #define _IMPLEMENT_DYNAMIC_CLASS(name, basename, unit) \
1058 wxObject* wxConstructorFor##name() \
1059 { return new name; } \
1060 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,NULL } ; \
1061 wxObject* wxVariantToObjectConverter##name ( const wxxVariant &data ) { return data.Get<name*>() ; } \
1062 wxxVariant wxObjectToVariantConverter##name ( wxObject *data ) { return wxxVariant( dynamic_cast<name*> (data) ) ; } \
1063 wxClassInfo name::sm_class##name(sm_classParents##name , wxT(unit) , wxT(#name), \
1064 (int) sizeof(name), \
1065 (wxObjectConstructorFn) wxConstructorFor##name , \
1066 name::GetPropertiesStatic(),name::GetHandlersStatic(),name::sm_constructor##name , name::sm_constructorProperties##name , \
1067 name::sm_constructorPropertiesCount##name , wxVariantToObjectConverter##name , wxObjectToVariantConverter##name); \
1068 template<> void wxStringReadValue(const wxString & , name * & ){assert(0) ;}\
1069 template<> void wxStringWriteValue(wxString & , name* const & ){assert(0) ;}\
1070 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1072 #define IMPLEMENT_DYNAMIC_CLASS( name , basename ) \
1073 _IMPLEMENT_DYNAMIC_CLASS( name , basename , "" ) \
1074 const wxPropertyInfo *name::GetPropertiesStatic() { return (wxPropertyInfo*) NULL ; } \
1075 const wxHandlerInfo *name::GetHandlersStatic() { return (wxHandlerInfo*) NULL ; } \
1076 WX_CONSTRUCTOR_DUMMY( name )
1078 #define IMPLEMENT_DYNAMIC_CLASS_XTI( name , basename , unit ) \
1079 _IMPLEMENT_DYNAMIC_CLASS( name , basename , unit )
1081 // this is for classes that do not derive from wxobject, there are no creators for these
1083 #define IMPLEMENT_DYNAMIC_CLASS_NO_WXOBJECT_NO_BASE_XTI( name , unit ) \
1084 const wxClassInfo* name::sm_classParents##name[] = { NULL } ; \
1085 wxClassInfo name::sm_class##name(sm_classParents##name , wxT("") , wxT(#name), \
1086 (int) sizeof(name), \
1087 (wxObjectConstructorFn) 0 , \
1088 name::GetPropertiesStatic(),name::GetHandlersStatic(),0 , 0 , \
1090 template<> void wxStringReadValue(const wxString & , name * & ){assert(0) ;}\
1091 template<> void wxStringWriteValue(wxString & , name* const & ){assert(0) ;}\
1092 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1094 // this is for subclasses that still do not derive from wxobject
1096 #define IMPLEMENT_DYNAMIC_CLASS_NO_WXOBJECT_XTI( name , basename, unit ) \
1097 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,NULL } ; \
1098 wxClassInfo name::sm_class##name(sm_classParents##name , wxT("") , wxT(#name), \
1099 (int) sizeof(name), \
1100 (wxObjectConstructorFn) 0 , \
1101 name::GetPropertiesStatic(),name::GetHandlersStatic(),0 , 0 , \
1103 template<> void wxStringReadValue(const wxString & , name * & ){assert(0) ;}\
1104 template<> void wxStringWriteValue(wxString & , name* const & ){assert(0) ;}\
1105 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1107 // Multiple inheritance with two base classes
1109 #define _IMPLEMENT_DYNAMIC_CLASS2(name, basename, basename2, unit) \
1110 wxObject* wxConstructorFor##name() \
1111 { return new name; } \
1112 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,&basename2::sm_class##basename2 , NULL } ; \
1113 wxObject* wxVariantToObjectConverter##name ( const wxxVariant &data ) { return data.Get<name*>() ; } \
1114 wxxVariant wxObjectToVariantConverter##name ( wxObject *data ) { return wxxVariant( dynamic_cast<name*> (data) ) ; } \
1115 wxClassInfo name::sm_class##name(sm_classParents##name , wxT(unit) , wxT(#name), \
1116 (int) sizeof(name), \
1117 (wxObjectConstructorFn) wxConstructorFor##name , \
1118 name::GetPropertiesStatic(),name::GetHandlersStatic(),name::sm_constructor##name , name::sm_constructorProperties##name , \
1119 name::sm_constructorPropertiesCount##name , wxVariantToObjectConverter##name , wxObjectToVariantConverter##name); \
1120 template<> void wxStringReadValue(const wxString & , name * & ){assert(0) ;}\
1121 template<> void wxStringWriteValue(wxString & , name* const & ){assert(0) ;}\
1122 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1124 #define IMPLEMENT_DYNAMIC_CLASS2( name , basename , basename2) \
1125 _IMPLEMENT_DYNAMIC_CLASS2( name , basename , basename2 , "") \
1126 const wxPropertyInfo *name::GetPropertiesStatic() { return (wxPropertyInfo*) NULL ; } \
1127 const wxHandlerInfo *name::GetHandlersStatic() { return (wxHandlerInfo*) NULL ; } \
1128 WX_CONSTRUCTOR_DUMMY( name )
1130 #define IMPLEMENT_DYNAMIC_CLASS2_XTI( name , basename , basename2, unit) \
1131 _IMPLEMENT_DYNAMIC_CLASS2( name , basename , basename2 , unit)
1133 // -----------------------------------
1134 // for abstract classes
1135 // -----------------------------------
1137 // Single inheritance with one base class
1139 #define _IMPLEMENT_ABSTRACT_CLASS(name, basename) \
1140 const wxClassInfo* name::sm_classParents##name[] = { &basename::sm_class##basename ,NULL } ; \
1141 wxObject* wxVariantToObjectConverter##name ( const wxxVariant &data ) { return data.Get<name*>() ; } \
1142 wxxVariant wxObjectToVariantConverter##name ( wxObject *data ) { return wxxVariant( dynamic_cast<name*> (data) ) ; } \
1143 wxClassInfo name::sm_class##name(sm_classParents##name , wxT("") , wxT(#name), \
1144 (int) sizeof(name), \
1145 (wxObjectConstructorFn) 0 , \
1146 name::GetPropertiesStatic(),name::GetHandlersStatic(),0 , 0 , \
1147 0 , wxVariantToObjectConverter##name , wxObjectToVariantConverter##name); \
1148 template<> void wxStringReadValue(const wxString & , name * & ){assert(0) ;}\
1149 template<> void wxStringWriteValue(wxString & , name* const & ){assert(0) ;}\
1150 template<> const wxTypeInfo* wxGetTypeInfo( name ** ){ static wxClassTypeInfo s_typeInfo(&name::sm_class##name) ; return &s_typeInfo ; }
1152 #define IMPLEMENT_ABSTRACT_CLASS( name , basename ) \
1153 _IMPLEMENT_ABSTRACT_CLASS( name , basename ) \
1154 const wxHandlerInfo *name::GetHandlersStatic() { return (wxHandlerInfo*) NULL ; } \
1155 const wxPropertyInfo *name::GetPropertiesStatic() { return (wxPropertyInfo*) NULL ; }
1157 // Multiple inheritance with two base classes
1159 #define IMPLEMENT_ABSTRACT_CLASS2(name, basename1, basename2) \
1160 wxClassInfo name::sm_class##name(wxT(#name), wxT(#basename1), \
1161 wxT(#basename2), (int) sizeof(name), \
1162 (wxObjectConstructorFn) 0);
1164 #define IMPLEMENT_CLASS IMPLEMENT_ABSTRACT_CLASS
1165 #define IMPLEMENT_CLASS2 IMPLEMENT_ABSTRACT_CLASS2