BALL 1.5.0
rtti.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_COMMON_RTTI_H
6#define BALL_COMMON_RTTI_H
7
8#ifndef BALL_CONFIG_CONFIG_H
9# include <BALL/CONFIG/config.h>
10#endif
11
12#ifndef BALL_COMMON_H
13# include <BALL/common.h>
14#endif
15
16#include <string>
17#include <typeinfo>
18using std::string;
19
20namespace BALL
21{
22
23# ifdef __GNUC__
24 // EGCS produces a nearly unreadable name mangling that requires
25 // further interpretation
26 namespace GNUDemangling
27 {
28 BALL_EXPORT string demangle(string s);
29 }
30# endif
31
42 BALL_EXPORT string streamClassName(const std::type_info& t);
43
66 namespace RTTI
67 {
68
74 template <typename T>
75 const T& getDefault()
76 {
77 static T t;
78 return t;
79 }
80
86 template <typename T>
87 void* getNew()
88 {
89 return static_cast<void*>(new T);
90 }
91
96 template <typename T>
97 const char* getName()
98 {
99 return typeid(T).name();
100 }
101
104 template <typename T>
106 {
107 static char dummy;
108 return (void*)&dummy;
109 }
110
121 template <typename T>
122 const char* getStreamName()
123 {
124 // define portable names for the portable
125 // types (some platforms use Size, some unsigned int,
126 // SUN CC even unsigned for the Size type)
127 if ((typeid(T) == typeid(Size))
128 || (typeid(T) == typeid(Position))
129 || (typeid(T) == typeid(HashIndex))
130 || (typeid(T) == typeid(Property))
131 || (typeid(T) == typeid(Handle)))
132 {
133 return "BALL::Size";
134 }
135 if ((typeid(T) == typeid(Index))
136 || (typeid(T) == typeid(ErrorCode))
137 || (typeid(T) == typeid(Distance)))
138 {
139 return "BALL::Index";
140 }
141 if (typeid(T) == typeid(::std::string))
142 {
143 return "::std::string";
144 }
145 if (typeid(T) == typeid(LongSize))
146 {
147 return "BALL::LongSize";
148 }
149 if (typeid(T) == typeid(bool))
150 {
151 return "bool";
152 }
153 if (typeid(T) == typeid(float))
154 {
155 return "float";
156 }
157 if (typeid(T) == typeid(char))
158 {
159 return "char";
160 }
161 if (typeid(T) == typeid(unsigned char))
162 {
163 return "unsigned_char";
164 }
165 if (typeid(T) == typeid(double))
166 {
167 return "double";
168 }
169 static string s("");
170 static bool is_set = false;
171
172 if (!is_set)
173 {
174 is_set = true;
175 s = streamClassName(typeid(T));
176 }
177
178 return s.c_str();
179 }
180
191 template <typename T, typename U>
192 bool isKindOf(const U *u)
193 {
194 return (0 != dynamic_cast<const T*>(u));
195 }
196
214 template <typename T, typename U>
215 T* castTo(const U& u)
216 {
217 return const_cast<T*>(dynamic_cast<const T*>(&u));
218 }
219
225 template <typename T, typename U>
226 bool isInstanceOf(const U& u)
227 {
228 return (typeid(u) == typeid(T));
229 }
230
231 } // namespace RTTI
232} // namespace BALL
233
234#endif // BALL_COMMON_RTTI_H
BALL_EXPORT string streamClassName(const std::type_info &t)
Definition: constants.h:13
BALL_ULONG64_TYPE LongSize
bool isInstanceOf(const U &u)
Definition: rtti.h:226
const T & getDefault()
Definition: rtti.h:75
const char * getStreamName()
Definition: rtti.h:122
bool isKindOf(const U *u)
Definition: rtti.h:192
void * getNew()
Definition: rtti.h:87
T * castTo(const U &u)
Definition: rtti.h:215
const char * getName()
Definition: rtti.h:97
void * getClassID()
Definition: rtti.h:105
#define BALL_EXPORT
Definition: COMMON/global.h:50