]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/typeinfo.h
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
18 // intended for internal use only at this time and may change in future
21 // The reason why we need this simple RTTI system in addition to the older
22 // wxObject-based one is that the latter does not work in template
33 #define _WX_DECLARE_TYPEINFO_CUSTOM(CLS, IDENTFUNC)
34 #define WX_DECLARE_TYPEINFO_INLINE(CLS)
35 #define WX_DECLARE_TYPEINFO(CLS)
36 #define WX_DEFINE_TYPEINFO(CLS)
37 #define WX_DECLARE_ABSTRACT_TYPEINFO(CLS)
40 // For improved type-safety, let's make the check using class name
41 // comparison. Most modern compilers already do this, but we cannot
42 // rely on all supported compilers to work this well. However, in
43 // cases where we'd know that typeid() would be flawless (as such),
44 // wxTypeId could of course simply be defined as typeid.
47 class wxTypeIdentifier
50 wxTypeIdentifier(const char* className
)
52 m_className
= className
;
55 bool operator==(const wxTypeIdentifier
& other
)
57 return strcmp(m_className
, other
.m_className
) == 0;
60 bool operator!=(const wxTypeIdentifier
& other
)
62 return strcmp(m_className
, other
.m_className
) != 0;
65 const char* m_className
;
68 #define wxTypeId(OBJ) wxTypeIdentifier(typeid(OBJ).name())
70 #else // if !wxNO_RTTI
73 // When C++ RTTI is not available, we will have to make the type comparison
74 // using pointer to a dummy static member function. This will fail if
75 // declared type is used across DLL boundaries, although using
76 // WX_DECLARE_TYPEINFO() and WX_DEFINE_TYPEINFO() pair instead of
77 // WX_DECLARE_TYPEINFO_INLINE() should fix this. However, that approach is
78 // usually not possible when type info needs to be declared for a template
82 typedef void (*wxTypeIdentifier
)();
84 // Use this macro to declare type info with specified static function
85 // IDENTFUNC used as type identifier. Usually you should only use
86 // WX_DECLARE_TYPEINFO() or WX_DECLARE_TYPEINFO_INLINE() however.
87 #define _WX_DECLARE_TYPEINFO_CUSTOM(CLS, IDENTFUNC) \
89 virtual wxTypeIdentifier GetWxTypeId() const \
91 return reinterpret_cast<wxTypeIdentifier> \
95 // Use this macro to declare type info with externally specified
96 // type identifier, defined with WX_DEFINE_TYPEINFO().
97 #define WX_DECLARE_TYPEINFO(CLS) \
99 static CLS sm_wxClassInfo(); \
100 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
102 // Use this macro to implement type identifier function required by
103 // WX_DECLARE_TYPEINFO().
104 // NOTE: CLS is required to have default ctor. If it doesn't
105 // already, you should provide a private dummy one.
106 #define WX_DEFINE_TYPEINFO(CLS) \
107 CLS CLS::sm_wxClassInfo() { return CLS(); }
109 // Use this macro to declare type info fully inline in class.
110 // NOTE: CLS is required to have default ctor. If it doesn't
111 // already, you should provide a private dummy one.
112 #define WX_DECLARE_TYPEINFO_INLINE(CLS) \
114 static CLS sm_wxClassInfo() { return CLS(); } \
115 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
117 #define wxTypeId(OBJ) (OBJ).GetWxTypeId()
119 // Because abstract classes cannot be instantiated, we use
120 // this macro to define pure virtual type interface for them.
121 #define WX_DECLARE_ABSTRACT_TYPEINFO(CLS) \
123 virtual wxTypeIdentifier GetWxTypeId() const = 0;
125 #endif // wxNO_RTTI/!wxNO_RTTI
127 #endif // _WX_TYPEINFO_H_