00001
00002
00003
00004
00005 #ifndef BALL_COMMON_RTTI_H
00006 #define BALL_COMMON_RTTI_H
00007
00008 #ifndef BALL_CONFIG_CONFIG_H
00009 # include <BALL/CONFIG/config.h>
00010 #endif
00011
00012 #ifndef BALL_COMMON_H
00013 # include <BALL/common.h>
00014 #endif
00015
00016 #include <string>
00017 #include <typeinfo>
00018 using std::string;
00019
00020 namespace BALL
00021 {
00022
00023 # ifdef __GNUC__
00024
00025
00026 namespace GNUDemangling
00027 {
00028 BALL_EXPORT string demangle(string s);
00029 }
00030 # endif
00031
00042 BALL_EXPORT string streamClassName(const std::type_info& t);
00043
00066 namespace RTTI
00067 {
00068
00074 template <typename T>
00075 const T& getDefault()
00076 {
00077 static T t;
00078 return t;
00079 }
00080
00086 template <typename T>
00087 void* getNew()
00088 {
00089 return static_cast<void*>(new T);
00090 }
00091
00096 template <typename T>
00097 const char* getName()
00098 {
00099 return typeid(T).name();
00100 }
00101
00104 template <typename T>
00105 void* getClassID()
00106 {
00107 static char dummy;
00108 return (void*)&dummy;
00109 }
00110
00121 template <typename T>
00122 const char* getStreamName()
00123 {
00124
00125
00126
00127 if ((typeid(T) == typeid(Size))
00128 || (typeid(T) == typeid(Position))
00129 || (typeid(T) == typeid(HashIndex))
00130 || (typeid(T) == typeid(Property))
00131 || (typeid(T) == typeid(Handle)))
00132 {
00133 return "BALL::Size";
00134 }
00135 if ((typeid(T) == typeid(Index))
00136 || (typeid(T) == typeid(ErrorCode))
00137 || (typeid(T) == typeid(Distance)))
00138 {
00139 return "BALL::Index";
00140 }
00141 if (typeid(T) == typeid(::std::string))
00142 {
00143 return "::std::string";
00144 }
00145 if (typeid(T) == typeid(LongSize))
00146 {
00147 return "BALL::LongSize";
00148 }
00149 if (typeid(T) == typeid(bool))
00150 {
00151 return "bool";
00152 }
00153 if (typeid(T) == typeid(float))
00154 {
00155 return "float";
00156 }
00157 if (typeid(T) == typeid(char))
00158 {
00159 return "char";
00160 }
00161 if (typeid(T) == typeid(unsigned char))
00162 {
00163 return "unsigned_char";
00164 }
00165 if (typeid(T) == typeid(double))
00166 {
00167 return "double";
00168 }
00169 static string s("");
00170 static bool is_set = false;
00171
00172 if (!is_set)
00173 {
00174 is_set = true;
00175 s = streamClassName(typeid(T));
00176 }
00177
00178 return s.c_str();
00179 }
00180
00191 template <typename T, typename U>
00192 bool isKindOf(const U& u)
00193 {
00194 return (0 != dynamic_cast<const T*>(&u));
00195 }
00196
00214 template <typename T, typename U>
00215 T* castTo(const U& u)
00216 {
00217 return const_cast<T*>(dynamic_cast<const T*>(&u));
00218 }
00219
00225 template <typename T, typename U>
00226 bool isInstanceOf(const U& u)
00227 {
00228 return (typeid(u) == typeid(T));
00229 }
00230
00231 }
00232 }
00233
00234 #endif // BALL_COMMON_RTTI_H