-#include "wx/memory.h"
-#include "wx/flags.h"
-#include "wx/string.h"
-#include "wx/arrstr.h"
-#include "wx/hashmap.h"
-
-#include <typeinfo>
-
-// we will move this later to defs.h
-
-#if !wxCHECK_GCC_VERSION( 3 , 4 )
-# define wxUSE_MEMBER_TEMPLATES 0
-#endif
-
-#ifdef _MSC_VER
-# if _MSC_VER <= 1200
-# define wxUSE_MEMBER_TEMPLATES 0
-# endif
-#endif
-
-#ifndef wxUSE_MEMBER_TEMPLATES
-#define wxUSE_MEMBER_TEMPLATES 1
-#endif
-
-#if wxUSE_MEMBER_TEMPLATES
-#define WX_TEMPLATED_MEMBER_CALL( method , type ) method<type>()
-#define WX_TEMPLATED_MEMBER_FIX( type )
-#else
-#define WX_TEMPLATED_MEMBER_CALL( method , type ) method((type*)NULL)
-#define WX_TEMPLATED_MEMBER_FIX( type ) type* =NULL
-#endif
-
-class WXDLLIMPEXP_BASE wxObject;
-class WXDLLIMPEXP_BASE wxClassInfo;
-class WXDLLIMPEXP_BASE wxDynamicClassInfo;
-class WXDLLIMPEXP_BASE wxHashTable;
-class WXDLLIMPEXP_BASE wxObjectRefData;
-class WXDLLIMPEXP_BASE wxEvent;
-
-typedef void (wxObject::*wxObjectEventFunction)(wxEvent&);
-
-// ----------------------------------------------------------------------------
-// Enum Support
-//
-// In the header files there would no change from pure c++ code, in the
-// implementation, an enum would have
-// to be enumerated eg :
-//
-// WX_BEGIN_ENUM( wxFlavor )
-// WX_ENUM_MEMBER( Vanilla )
-// WX_ENUM_MEMBER( Chocolate )
-// WX_ENUM_MEMBER( Strawberry )
-// WX_END_ENUM( wxFlavor )
-// ----------------------------------------------------------------------------
-
-struct WXDLLIMPEXP_BASE wxEnumMemberData
-{
- const wxChar* m_name;
- int m_value;
-};
-
-class WXDLLIMPEXP_BASE wxEnumData
-{
-public :
- wxEnumData( wxEnumMemberData* data ) ;
-
- // returns true if the member has been found and sets the int value
- // pointed to accordingly (if ptr != null )
- // if not found returns false, value left unchanged
- bool HasEnumMemberValue( const wxChar *name , int *value = NULL ) const ;
-
- // returns the value of the member, if not found in debug mode an
- // assert is issued, in release 0 is returned
- int GetEnumMemberValue(const wxChar *name ) const ;
-
- // returns the name of the enum member having the passed in value
- // returns an emtpy string if not found
- const wxChar *GetEnumMemberName(int value) const ;
-
- // returns the number of members in this enum
- int GetEnumCount() const { return m_count ; }
-
- // returns the value of the nth member
- int GetEnumMemberValueByIndex( int n ) const ;
-
- // returns the value of the nth member
- const wxChar *GetEnumMemberNameByIndex( int n ) const ;
-private :
- wxEnumMemberData *m_members;
- int m_count ;
-};
-
-#define WX_BEGIN_ENUM( e ) \
- wxEnumMemberData s_enumDataMembers##e[] = {
-
-#define WX_ENUM_MEMBER( v ) { wxT(#v), v } ,
-
-#define WX_END_ENUM( e ) { NULL , 0 } } ; \
- wxEnumData s_enumData##e( s_enumDataMembers##e ) ; \
- wxEnumData *wxGetEnumData(e) { return &s_enumData##e ; } \
- template<> void wxStringReadValue(const wxString& s , e &data ) \
-{ \
- data = (e) s_enumData##e.GetEnumMemberValue(s) ; \
-} \
- template<> void wxStringWriteValue(wxString &s , const e &data ) \
-{ \
- s = s_enumData##e.GetEnumMemberName((int)data) ; \
-} \
- void FromLong##e( long data , wxxVariant& result ) { result = wxxVariant((e)data) ;} \
- void ToLong##e( const wxxVariant& data , long &result ) { result = (long) data.Get<e>() ;} \
- wxEnumTypeInfo s_typeInfo##e(wxT_ENUM , &s_enumData##e , &wxToStringConverter<e> , &wxFromStringConverter<e> , &ToLong##e , &FromLong##e , typeid(e).name() ) ;
-
-// ----------------------------------------------------------------------------
-// Set Support
-//
-// in the header :
-//
-// enum wxFlavor
-// {
-// Vanilla,
-// Chocolate,
-// Strawberry,
-// };
-//
-// typedef wxBitset<wxFlavor> wxCoupe ;
-//
-// in the implementation file :
-//
-// WX_BEGIN_ENUM( wxFlavor )
-// WX_ENUM_MEMBER( Vanilla )
-// WX_ENUM_MEMBER( Chocolate )
-// WX_ENUM_MEMBER( Strawberry )
-// WX_END_ENUM( wxFlavor )
-//
-// WX_IMPLEMENT_SET_STREAMING( wxCoupe , wxFlavor )
-//
-// implementation note : no partial specialization for streaming, but a delegation to a
-// different class
-//
-// ----------------------------------------------------------------------------
-
-// in order to remove dependancy on string tokenizer
-void wxSetStringToArray( const wxString &s , wxArrayString &array ) ;
-
-template<typename e>
-void wxSetFromString(const wxString &s , wxBitset<e> &data )
-{
- wxEnumData* edata = wxGetEnumData((e) 0) ;
- data.reset() ;
-
- wxArrayString array ;
- wxSetStringToArray( s , array ) ;
- wxString flag;
- for ( int i = 0 ; i < array.Count() ; ++i )
- {
- flag = array[i] ;
- int ivalue ;
- if ( edata->HasEnumMemberValue( flag , &ivalue ) )
- {
- data.set( (e) ivalue ) ;
- }
- }
-}
-
-template<typename e>
-void wxSetToString( wxString &s , const wxBitset<e> &data )
-{
- wxEnumData* edata = wxGetEnumData((e) 0) ;
- int count = edata->GetEnumCount() ;
- int i ;
- s.Clear() ;
- for ( i = 0 ; i < count ; i++ )
- {
- e value = (e) edata->GetEnumMemberValueByIndex(i) ;
- if ( data.test( value ) )
- {
- // this could also be done by the templated calls
- if ( !s.IsEmpty() )
- s +=wxT("|") ;
- s += edata->GetEnumMemberNameByIndex(i) ;
- }
- }
-}
-
-#define WX_IMPLEMENT_SET_STREAMING(SetName,e) \
- template<> void wxStringReadValue(const wxString &s , wxBitset<e> &data ) \
-{ \
- wxSetFromString( s , data ) ; \
-} \
- template<> void wxStringWriteValue( wxString &s , const wxBitset<e> &data ) \
-{ \
- wxSetToString( s , data ) ; \
-} \
- void FromLong##SetName( long data , wxxVariant& result ) { result = wxxVariant(SetName((unsigned long)data)) ;} \
- void ToLong##SetName( const wxxVariant& data , long &result ) { result = (long) data.Get<SetName>().to_ulong() ;} \
- wxEnumTypeInfo s_typeInfo##SetName(wxT_SET , &s_enumData##e , &wxToStringConverter<SetName> , &wxFromStringConverter<SetName> , &ToLong##SetName , &FromLong##SetName, typeid(SetName).name() ) ; \
-}
-
-template<typename e>
-void wxFlagsFromString(const wxString &s , e &data )
-{
- wxEnumData* edata = wxGetEnumData((e*) 0) ;
- data.m_data = 0 ;
-
- wxArrayString array ;
- wxSetStringToArray( s , array ) ;
- wxString flag;
- for ( size_t i = 0 ; i < array.Count() ; ++i )
- {
- flag = array[i] ;
- int ivalue ;
- if ( edata->HasEnumMemberValue( flag , &ivalue ) )
- {
- data.m_data |= ivalue ;
- }
- }
-}
-
-template<typename e>
-void wxFlagsToString( wxString &s , const e& data )
-{
- wxEnumData* edata = wxGetEnumData((e*) 0) ;
- int count = edata->GetEnumCount() ;
- int i ;
- s.Clear() ;
- long dataValue = data.m_data ;
- for ( i = 0 ; i < count ; i++ )
- {
- int value = edata->GetEnumMemberValueByIndex(i) ;
- // make this to allow for multi-bit constants to work
- if ( value && ( dataValue & value ) == value )
- {
- // clear the flags we just set
- dataValue &= ~value ;
- // this could also be done by the templated calls
- if ( !s.IsEmpty() )
- s +=wxT("|") ;
- s += edata->GetEnumMemberNameByIndex(i) ;
- }
- }
-}
-
-#define WX_BEGIN_FLAGS( e ) \
- wxEnumMemberData s_enumDataMembers##e[] = {
-
-#define WX_FLAGS_MEMBER( v ) { wxT(#v), v } ,
-
-#define WX_END_FLAGS( e ) { NULL , 0 } } ; \
- wxEnumData s_enumData##e( s_enumDataMembers##e ) ; \
- wxEnumData *wxGetEnumData(e*) { return &s_enumData##e ; } \
- template<> void wxStringReadValue(const wxString &s , e &data ) \
-{ \
- wxFlagsFromString<e>( s , data ) ; \
-} \
- template<> void wxStringWriteValue( wxString &s , const e& data ) \
-{ \
- wxFlagsToString<e>( s , data ) ; \
-} \
- void FromLong##e( long data , wxxVariant& result ) { result = wxxVariant(e(data)) ;} \
- void ToLong##e( const wxxVariant& data , long &result ) { result = (long) data.Get<e>().m_data ;} \
- wxEnumTypeInfo s_typeInfo##e(wxT_SET , &s_enumData##e , &wxToStringConverter<e> , &wxFromStringConverter<e> , &ToLong##e , &FromLong##e, typeid(e).name() ) ;
-// ----------------------------------------------------------------------------
-// Type Information
-// ----------------------------------------------------------------------------
-//
-//
-// All data exposed by the RTTI is characterized using the following classes.
-// The first characterization is done by wxTypeKind. All enums up to and including
-// wxT_CUSTOM represent so called simple types. These cannot be divided any further.
-// They can be converted to and from wxStrings, that's all.
-
-
-enum wxTypeKind
-{
- wxT_VOID = 0, // unknown type
- wxT_BOOL,
- wxT_CHAR,
- wxT_UCHAR,
- wxT_INT,
- wxT_UINT,
- wxT_LONG,
- wxT_ULONG,
- wxT_FLOAT,
- wxT_DOUBLE,
- wxT_STRING, // must be wxString
- wxT_SET, // must be wxBitset<> template
- wxT_ENUM,
- wxT_CUSTOM, // user defined type (e.g. wxPoint)
-
- wxT_LAST_SIMPLE_TYPE_KIND = wxT_CUSTOM ,
-
- wxT_OBJECT_PTR, // object reference
- wxT_OBJECT , // embedded object
- wxT_COLLECTION , // collection
-
- wxT_DELEGATE , // for connecting against an event source
-
- wxT_LAST_TYPE_KIND = wxT_DELEGATE // sentinel for bad data, asserts, debugging
-};
-
-class WXDLLIMPEXP_BASE wxxVariant ;
-class WXDLLIMPEXP_BASE wxTypeInfo ;
-
-WX_DECLARE_STRING_HASH_MAP_WITH_DECL( wxTypeInfo* , wxTypeInfoMap , class WXDLLIMPEXP_BASE ) ;
-
-class WXDLLIMPEXP_BASE wxTypeInfo
-{
-public :
- typedef void (*converterToString_t)( const wxxVariant& data , wxString &result ) ;
- typedef void (*converterFromString_t)( const wxString& data , wxxVariant &result ) ;
-
- wxTypeInfo(wxTypeKind kind,
- converterToString_t to = NULL, converterFromString_t from = NULL,
- const wxString &name = wxEmptyString):
- m_toString(to), m_fromString(from), m_kind(kind), m_name(name)
- {
- Register();
- }
-#if wxUSE_UNICODE
- wxTypeInfo(wxTypeKind kind,
- converterToString_t to = NULL, converterFromString_t from = NULL,
- const char *name = ""):
- m_toString(to), m_fromString(from), m_kind(kind), m_name(wxString::FromAscii(name))
- {
- Register();
- }
-#endif
-
- virtual ~wxTypeInfo()
- {
- Unregister() ;
- }
-
- // return the kind of this type (wxT_... constants)
- wxTypeKind GetKind() const { return m_kind ; }
-
- // returns the unique name of this type
- const wxString& GetTypeName() const { return m_name ; }
-
- // is this type a delegate type
- bool IsDelegateType() const { return m_kind == wxT_DELEGATE ; }
-
- // is this type a custom type
- bool IsCustomType() const { return m_kind == wxT_CUSTOM ; }
-
- // is this type an object type
- bool IsObjectType() const { return m_kind == wxT_OBJECT || m_kind == wxT_OBJECT_PTR ; }
-
- // can the content of this type be converted to and from strings ?
- bool HasStringConverters() const { return m_toString != NULL && m_fromString != NULL ; }
-
- // convert a wxxVariant holding data of this type into a string
- void ConvertToString( const wxxVariant& data , wxString &result ) const
-
- { wxASSERT_MSG( m_toString , wxT("String conversions not supported") ) ; (*m_toString)( data , result ) ; }
-
- // convert a string into a wxxVariant holding the corresponding data in this type
- void ConvertFromString( const wxString& data , wxxVariant &result ) const
- { wxASSERT_MSG( m_fromString , wxT("String conversions not supported") ) ; (*m_fromString)( data , result ) ; }
-
-#if wxUSE_UNICODE
- static wxTypeInfo *FindType(const char *typeName) { return FindType( wxString::FromAscii(typeName) ) ; }
-#endif
- static wxTypeInfo *FindType(const wxChar *typeName);
-
-private :
-
- void Register();
- void Unregister();
-
- converterToString_t m_toString ;
- converterFromString_t m_fromString ;
-
- static wxTypeInfoMap* sm_typeTable ;
-
- wxTypeKind m_kind;
- wxString m_name;
-};
-
-class WXDLLIMPEXP_BASE wxBuiltInTypeInfo : public wxTypeInfo
-{
-public :
- wxBuiltInTypeInfo( wxTypeKind kind , converterToString_t to = NULL , converterFromString_t from = NULL , const wxString &name = wxEmptyString ) :
- wxTypeInfo( kind , to , from , name )
- { wxASSERT_MSG( GetKind() < wxT_SET , wxT("Illegal Kind for Base Type") ) ; }
-#if wxUSE_UNICODE
- wxBuiltInTypeInfo( wxTypeKind kind , converterToString_t to , converterFromString_t from , const char *name ) :
- wxTypeInfo( kind , to , from , name )
- { wxASSERT_MSG( GetKind() < wxT_SET , wxT("Illegal Kind for Base Type") ) ; }
-#endif
-} ;
-
-class WXDLLIMPEXP_BASE wxCustomTypeInfo : public wxTypeInfo
-{
-public :
- wxCustomTypeInfo( const wxString &name , converterToString_t to , converterFromString_t from ) :
- wxTypeInfo( wxT_CUSTOM , to , from , name )
- {}
-#if wxUSE_UNICODE
- wxCustomTypeInfo( const char *name , converterToString_t to , converterFromString_t from ) :
- wxTypeInfo( wxT_CUSTOM , to , from , name )
- {}
-#endif
-} ;
-
-class WXDLLIMPEXP_BASE wxEnumTypeInfo : public wxTypeInfo
-{
-public :
- typedef void (*converterToLong_t)( const wxxVariant& data , long &result ) ;
- typedef void (*converterFromLong_t)( long data , wxxVariant &result ) ;
-
- wxEnumTypeInfo( wxTypeKind kind , wxEnumData* enumInfo , converterToString_t to , converterFromString_t from ,
- converterToLong_t toLong , converterFromLong_t fromLong , const wxString &name ) :
- wxTypeInfo( kind , to , from , name ) , m_toLong( toLong ) , m_fromLong( fromLong )
- { wxASSERT_MSG( kind == wxT_ENUM || kind == wxT_SET , wxT("Illegal Kind for Enum Type")) ; m_enumInfo = enumInfo ;}
-
-#if wxUSE_UNICODE
- wxEnumTypeInfo( wxTypeKind kind , wxEnumData* enumInfo , converterToString_t to , converterFromString_t from ,
- converterToLong_t toLong , converterFromLong_t fromLong , const char * name ) :
- wxTypeInfo( kind , to , from , name ) , m_toLong( toLong ) , m_fromLong( fromLong )
- { wxASSERT_MSG( kind == wxT_ENUM || kind == wxT_SET , wxT("Illegal Kind for Enum Type")) ; m_enumInfo = enumInfo ;}
-#endif
- const wxEnumData* GetEnumData() const { return m_enumInfo ; }
-
- // convert a wxxVariant holding data of this type into a long
- void ConvertToLong( const wxxVariant& data , long &result ) const
-
- { wxASSERT_MSG( m_toLong , wxT("Long conversions not supported") ) ; (*m_toLong)( data , result ) ; }
-
- // convert a long into a wxxVariant holding the corresponding data in this type
- void ConvertFromLong( long data , wxxVariant &result ) const
- { wxASSERT_MSG( m_fromLong , wxT("Long conversions not supported") ) ; (*m_fromLong)( data , result ) ; }
-
-private :
- converterToLong_t m_toLong ;
- converterFromLong_t m_fromLong ;
-
- wxEnumData *m_enumInfo; // Kind == wxT_ENUM or Kind == wxT_SET
-} ;
-
-class WXDLLIMPEXP_BASE wxClassTypeInfo : public wxTypeInfo
-{
-public :
- wxClassTypeInfo( wxTypeKind kind , wxClassInfo* classInfo , converterToString_t to = NULL , converterFromString_t from = NULL , const wxString &name = wxEmptyString) ;
-#if wxUSE_UNICODE
- wxClassTypeInfo( wxTypeKind kind , wxClassInfo* classInfo , converterToString_t to , converterFromString_t from , const char *name ) ;
-#endif
- const wxClassInfo *GetClassInfo() const { return m_classInfo ; }
-private :
- wxClassInfo *m_classInfo; // Kind == wxT_OBJECT - could be NULL
-} ;
-
-class WXDLLIMPEXP_BASE wxCollectionTypeInfo : public wxTypeInfo
-{
-public :
- wxCollectionTypeInfo( const wxString &elementName , converterToString_t to , converterFromString_t from , const wxString &name) :
- wxTypeInfo( wxT_COLLECTION , to , from , name )
- { m_elementTypeName = elementName ; m_elementType = NULL ;}
-#if wxUSE_UNICODE
- wxCollectionTypeInfo( const char *elementName , converterToString_t to , converterFromString_t from , const char *name ) :
- wxTypeInfo( wxT_COLLECTION , to , from , name )
- { m_elementTypeName = wxString::FromAscii( elementName ) ; m_elementType = NULL ;}
-#endif
- const wxTypeInfo* GetElementType() const
- {
- if ( m_elementType == NULL )
- m_elementType = wxTypeInfo::FindType( m_elementTypeName ) ;
- return m_elementType ; }
-private :
- mutable wxTypeInfo * m_elementType ;
- wxString m_elementTypeName ;
-} ;
-
-// a delegate is an exposed event source
-
-class WXDLLIMPEXP_BASE wxDelegateTypeInfo : public wxTypeInfo
-{
-public :
- wxDelegateTypeInfo( int eventType , wxClassInfo* eventClass , converterToString_t to = NULL , converterFromString_t from = NULL ) ;
- wxDelegateTypeInfo( int eventType , int lastEventType, wxClassInfo* eventClass , converterToString_t to = NULL , converterFromString_t from = NULL ) ;
- int GetEventType() const { return m_eventType ; }
- int GetLastEventType() const { return m_lastEventType ; }
- const wxClassInfo* GetEventClass() const { return m_eventClass ; }
-private :
- const wxClassInfo *m_eventClass; // (extended will merge into classinfo)
- int m_eventType ;
- int m_lastEventType ;
-} ;
-
-template<typename T> const wxTypeInfo* wxGetTypeInfo( T * ) { return wxTypeInfo::FindType(typeid(T).name()) ; }
-
-// this macro is for usage with custom, non-object derived classes and structs, wxPoint is such a custom type
-
-#define WX_CUSTOM_TYPE_INFO( e , toString , fromString ) \
- wxCustomTypeInfo s_typeInfo##e(typeid(e).name() , &toString , &fromString) ;
-
-#define WX_COLLECTION_TYPE_INFO( element , collection ) \
- wxCollectionTypeInfo s_typeInfo##collection( typeid(element).name() , NULL , NULL , typeid(collection).name() ) ;
-
-// sometimes a compiler invents specializations that are nowhere called, use this macro to satisfy the refs
-
-#define WX_ILLEGAL_TYPE_SPECIALIZATION( a )
-/*
- template<> const wxTypeInfo* wxGetTypeInfo( a * ) { assert(0) ; \
- static wxBuiltInTypeInfo s_typeInfo( wxT_VOID ) ; return &s_typeInfo ; } \
- template<> void wxStringReadValue(const wxString & , a & ) { assert(0) ; }\
- template<> void wxStringWriteValue(wxString & , a const & ) { assert(0) ; }
-*/
-
-// ----------------------------------------------------------------------------
-// wxxVariant as typesafe data holder
-// ----------------------------------------------------------------------------
-
-class WXDLLIMPEXP_BASE wxxVariantData
-{
-public:
- virtual ~wxxVariantData() {}
-
- // return a heap allocated duplicate
- virtual wxxVariantData* Clone() const = 0 ;
-
- // returns the type info of the contentc
- virtual const wxTypeInfo* GetTypeInfo() const = 0 ;
-} ;
-
-template<typename T> class wxxVariantDataT : public wxxVariantData
-{
-public:
- wxxVariantDataT(const T& d) : m_data(d) {}
- virtual ~wxxVariantDataT() {}
-
- // get a ref to the stored data
- T & Get() { return m_data; }
-
- // get a const ref to the stored data
- const T & Get() const { return m_data; }
-
- // set the data
- void Set(const T& d) { m_data = d; }
-
- // return a heap allocated duplicate
- virtual wxxVariantData* Clone() const { return new wxxVariantDataT<T>( Get() ) ; }
-
- // returns the type info of the contentc
- virtual const wxTypeInfo* GetTypeInfo() const { return wxGetTypeInfo( (T*) NULL ) ; }
-
-private:
- T m_data;
-};
-
-class WXDLLIMPEXP_BASE wxxVariant
-{
-public :
- wxxVariant() { m_data = NULL ; }
- wxxVariant( wxxVariantData* data , const wxString& name = wxT("") ) : m_data(data) , m_name(name) {}
- wxxVariant( const wxxVariant &d ) { if ( d.m_data ) m_data = d.m_data->Clone() ; else m_data = NULL ; m_name = d.m_name ; }
-
- template<typename T> wxxVariant( const T& data , const wxString& name = wxT("") ) :
- m_data(new wxxVariantDataT<T>(data) ), m_name(name) {}
-
- ~wxxVariant() { delete m_data ; }
-
- // get a ref to the stored data
- template<typename T> T& Get(WX_TEMPLATED_MEMBER_FIX(T))
- {
- wxxVariantDataT<T> *dataptr = dynamic_cast<wxxVariantDataT<T>*> (m_data) ;
- wxASSERT_MSG( dataptr , wxT("Cast not possible") ) ;
- return dataptr->Get() ;
- }
-
- // get a ref to the stored data
- template<typename T> const T& Get(WX_TEMPLATED_MEMBER_FIX(T)) const
- {
- const wxxVariantDataT<T> *dataptr = dynamic_cast<const wxxVariantDataT<T>*> (m_data) ;
- wxASSERT_MSG( dataptr , wxT("Cast not possible") ) ;
- return dataptr->Get() ;
- }