]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/typeinfo.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTypeId implementation
4 // Author: Jaakko Salli
6 // Copyright: (c) wxWidgets Team
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_TYPEINFO_H_
11 #define _WX_TYPEINFO_H_
14 // This file defines wxTypeId macro that should be used internally in
15 // wxWidgets instead of typeid(), for compatibility with builds that do
16 // not implement C++ RTTI. Also, type defining macros in this file are
17 // intended for internal use only at this time and may change in future
20 // The reason why we need this simple RTTI system in addition to the older
21 // wxObject-based one is that the latter does not work in template
30 // Let's trust that Visual C++ versions 9.0 and later implement C++
31 // RTTI well enough, so we can use it and work around harmless memory
32 // leaks reported by the static run-time libraries.
34 #if wxCHECK_VISUALC_VERSION(9)
35 #define wxTRUST_CPP_RTTI 1
37 #define wxTRUST_CPP_RTTI 0
43 #define _WX_DECLARE_TYPEINFO_CUSTOM(CLS, IDENTFUNC)
44 #define WX_DECLARE_TYPEINFO_INLINE(CLS)
45 #define WX_DECLARE_TYPEINFO(CLS)
46 #define WX_DEFINE_TYPEINFO(CLS)
47 #define WX_DECLARE_ABSTRACT_TYPEINFO(CLS)
51 #define wxTypeId typeid
53 #else /* !wxTRUST_CPP_RTTI */
56 // For improved type-safety, let's make the check using class name
57 // comparison. Most modern compilers already do this, but we cannot
58 // rely on all supported compilers to work this well. However, in
59 // cases where we'd know that typeid() would be flawless (as such),
60 // wxTypeId could of course simply be defined as typeid.
63 class wxTypeIdentifier
66 wxTypeIdentifier(const char* className
)
68 m_className
= className
;
71 bool operator==(const wxTypeIdentifier
& other
)
73 return strcmp(m_className
, other
.m_className
) == 0;
76 bool operator!=(const wxTypeIdentifier
& other
)
78 return strcmp(m_className
, other
.m_className
) != 0;
81 const char* m_className
;
84 #define wxTypeId(OBJ) wxTypeIdentifier(typeid(OBJ).name())
86 #endif /* wxTRUST_CPP_RTTI/!wxTRUST_CPP_RTTI */
88 #else // if !wxNO_RTTI
90 #define wxTRUST_CPP_RTTI 0
93 // When C++ RTTI is not available, we will have to make the type comparison
94 // using pointer to a dummy static member function. This will fail if
95 // declared type is used across DLL boundaries, although using
96 // WX_DECLARE_TYPEINFO() and WX_DEFINE_TYPEINFO() pair instead of
97 // WX_DECLARE_TYPEINFO_INLINE() should fix this. However, that approach is
98 // usually not possible when type info needs to be declared for a template
102 typedef void (*wxTypeIdentifier
)();
104 // Use this macro to declare type info with specified static function
105 // IDENTFUNC used as type identifier. Usually you should only use
106 // WX_DECLARE_TYPEINFO() or WX_DECLARE_TYPEINFO_INLINE() however.
107 #define _WX_DECLARE_TYPEINFO_CUSTOM(CLS, IDENTFUNC) \
109 virtual wxTypeIdentifier GetWxTypeId() const \
111 return reinterpret_cast<wxTypeIdentifier> \
115 // Use this macro to declare type info with externally specified
116 // type identifier, defined with WX_DEFINE_TYPEINFO().
117 #define WX_DECLARE_TYPEINFO(CLS) \
119 static CLS sm_wxClassInfo(); \
120 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
122 // Use this macro to implement type identifier function required by
123 // WX_DECLARE_TYPEINFO().
124 // NOTE: CLS is required to have default ctor. If it doesn't
125 // already, you should provide a private dummy one.
126 #define WX_DEFINE_TYPEINFO(CLS) \
127 CLS CLS::sm_wxClassInfo() { return CLS(); }
129 // Use this macro to declare type info fully inline in class.
130 // NOTE: CLS is required to have default ctor. If it doesn't
131 // already, you should provide a private dummy one.
132 #define WX_DECLARE_TYPEINFO_INLINE(CLS) \
134 static CLS sm_wxClassInfo() { return CLS(); } \
135 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
137 #define wxTypeId(OBJ) (OBJ).GetWxTypeId()
139 // Because abstract classes cannot be instantiated, we use
140 // this macro to define pure virtual type interface for them.
141 #define WX_DECLARE_ABSTRACT_TYPEINFO(CLS) \
143 virtual wxTypeIdentifier GetWxTypeId() const = 0;
145 #endif // wxNO_RTTI/!wxNO_RTTI
147 #endif // _WX_TYPEINFO_H_