Added typeinfo.h which implements wxTypeId, using C++ RTTI if available. wxAny and...
[wxWidgets.git] / include / wx / typeinfo.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/typeinfo.h
3 // Purpose: wxTypeId implementation
4 // Author: Jaakko Salli
5 // Created: 2009-11-19
6 // RCS-ID: $Id$
7 // Copyright: (c) wxWidgets Team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_TYPEINFO_H_
12 #define _WX_TYPEINFO_H_
13
14 //
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
19 // versions.
20 //
21
22 #include "wx/defs.h"
23
24 #ifndef wxNO_RTTI
25
26 #include <typeinfo>
27 #include <string.h>
28
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)
34
35 //
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.
41 //
42
43 class wxTypeInfo
44 {
45 public:
46 wxTypeInfo(const char* className)
47 {
48 m_className = className;
49 }
50
51 bool operator==(const wxTypeInfo& other)
52 {
53 return strcmp(m_className, other.m_className) == 0;
54 }
55
56 bool operator!=(const wxTypeInfo& other)
57 {
58 return strcmp(m_className, other.m_className) != 0;
59 }
60 private:
61 const char* m_className;
62 };
63
64 #define wxTypeId(OBJ) wxTypeInfo(typeid(OBJ).name())
65
66 #else // if !wxNO_RTTI
67
68 //
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
75 // class.
76 //
77
78 typedef void (*wxTypeIdentifier)();
79
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) \
84 public: \
85 virtual wxTypeIdentifier GetWxTypeId() const \
86 { \
87 return reinterpret_cast<wxTypeIdentifier> \
88 (&IDENTFUNC); \
89 }
90
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) \
94 private: \
95 static CLS sm_wxClassInfo(); \
96 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
97
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(); }
104
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) \
109 private: \
110 static CLS sm_wxClassInfo() { return CLS(); } \
111 _WX_DECLARE_TYPEINFO_CUSTOM(CLS, sm_wxClassInfo)
112
113 #define wxTypeId(OBJ) (OBJ).GetWxTypeId()
114
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) \
118 public: \
119 virtual wxTypeIdentifier GetWxTypeId() const = 0;
120
121 #endif // wxNO_RTTI/!wxNO_RTTI
122
123 #endif // _WX_TYPEINFO_H_