1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTypeId implementation
4 // Author: Jaakko Salli
7 // Copyright: (c) wxWidgets Team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_TYPEINFO_H_
12 #define _WX_TYPEINFO_H_
15 // This file defines wxTypeId macro that should be used internally in
16 // wxWidgets instead of typeid(), for compatibility with builds that do
17 // not implement C++ RTTI. Also, type defining macros in this file are also
18 // intended for internal use only at this time and may change in future
29 #define _WX_DECLARE_TYPEINFO_CUSTOM(CLS, IDENTFUNC)
30 #define WX_DECLARE_TYPEINFO_INLINE(CLS)
31 #define WX_DECLARE_TYPEINFO(CLS)
32 #define WX_DEFINE_TYPEINFO(CLS)
33 #define WX_DECLARE_ABSTRACT_TYPEINFO(CLS)
36 // For improved type-safety, let's make the check using class name
37 // comparison. Most modern compilers already do this, but we cannot
38 // rely on all supported compilers to work this well. However, in
39 // cases where we'd know that typeid() would be flawless (as such),
40 // wxTypeId could of course simply be defined as typeid.
46 wxTypeInfo(const char* className
)
48 m_className
= className
;
51 bool operator==(const wxTypeInfo
& other
)
53 return strcmp(m_className
, other
.m_className
) == 0;
56 bool operator!=(const wxTypeInfo
& other
)
58 return strcmp(m_className
, other
.m_className
) != 0;
61 const char* m_className
;
64 #define wxTypeId(OBJ) wxTypeInfo(typeid(OBJ).name())
66 #else // if !wxNO_RTTI
69 // When C++ RTTI is not available, we will have to make the type comparison
70 // using pointer to a dummy static member function. This will fail if
71 // declared type is used across DLL boundaries, although using
72 // WX_DECLARE_TYPEINFO() and WX_DEFINE_TYPEINFO() pair instead of
73 // WX_DECLARE_TYPEINFO_INLINE() should fix this. However, that approach is
74 // usually not possible when type info needs to be declared for a template
78 typedef void (*wxTypeIdentifier
)();
80 // Use this macro to declare type info with specified static function
81 // IDENTFUNC used as type identifier. Usually you should only use
82 // WX_DECLARE_TYPEINFO() or WX_DECLARE_TYPEINFO_INLINE() however.
83 #define _WX_DECLARE_TYPEINFO_CUSTOM(CLS, IDENTFUNC) \
85 virtual wxTypeIdentifier GetWxTypeId() const \
87 return reinterpret_cast<wxTypeIdentifier> \
91 // Use this macro to declare type info with externally specified
92 // type identifier, defined with WX_DEFINE_TYPEINFO().
93 #define WX_DECLARE_TYPEINFO(CLS) \
95 static CLS sm_wxClassInfo(); \
96 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
98 // Use this macro to implement type identifier function required by
99 // WX_DECLARE_TYPEINFO().
100 // NOTE: CLS is required to have default ctor. If it doesn't
101 // already, you should provide a private dummy one.
102 #define WX_DEFINE_TYPEINFO(CLS) \
103 CLS CLS::sm_wxClassInfo() { return CLS(); }
105 // Use this macro to declare type info fully inline in class.
106 // NOTE: CLS is required to have default ctor. If it doesn't
107 // already, you should provide a private dummy one.
108 #define WX_DECLARE_TYPEINFO_INLINE(CLS) \
110 static CLS sm_wxClassInfo() { return CLS(); } \
111 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
113 #define wxTypeId(OBJ) (OBJ).GetWxTypeId()
115 // Because abstract classes cannot be instantiated, we use
116 // this macro to define pure virtual type interface for them.
117 #define WX_DECLARE_ABSTRACT_TYPEINFO(CLS) \
119 virtual wxTypeIdentifier GetWxTypeId() const = 0;
121 #endif // wxNO_RTTI/!wxNO_RTTI
123 #endif // _WX_TYPEINFO_H_