1 /*=========================================================================
3 Module: $RCSfile: bbtkRTTI.h,v $
5 Date: $Date: 2008/10/17 08:18:14 $
6 Version: $Revision: 1.6 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
14 * This software is governed by the CeCILL-B license under French law and
15 * abiding by the rules of distribution of free software. You can use,
16 * modify and/ or redistribute the software under the terms of the CeCILL-B
17 * license as circulated by CEA, CNRS and INRIA at the following URL
18 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
19 * or in the file LICENSE.txt.
21 * As a counterpart to the access to the source code and rights to copy,
22 * modify and redistribute granted by the license, users are provided only
23 * with a limited warranty and the software's author, the holder of the
24 * economic rights, and the successive licensors have only limited
27 * The fact that you are presently reading this means that you have had
28 * knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */
33 *\brief RTTI tools (system dependent).
36 #ifndef __BBTKRTTI_H_INCLUDED__
37 #define __BBTKRTTI_H_INCLUDED__
39 #include "bbtkSystem.h"
42 //-----------------------------------------------------------------------------
43 // RRTI type_info.name() demangling
44 // For type_info.name() demangling (gcc >= 3.1, see http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html)
45 // Test for GCC > 3.1.0 //
47 (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 || \
48 (__GNUC_MINOR__ == 1 && \
49 __GNUC_PATCHLEVEL__ > 0)))
54 inline std::string demangle_type_name(const char* name)
57 char* dem = abi::__cxa_demangle(name, 0, 0, &status);
58 std::string demangled(dem);
60 if (!status) return demangled;
66 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
68 //==========================================================================
71 //#include "Windows.h"
75 // include the right library in the linker stage
76 #pragma comment( lib, "imagehlp.lib" )
84 inline std::string demangle_type_name(const char* name)
87 if (UnDecorateSymbolName(name, demangled,
88 sizeof(name), UNDNAME_COMPLETE))
90 return name; //demangled;
98 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
102 #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME
105 #define BBTK_GET_CURRENT_OBJECT_NAME \
106 BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name())
108 #define BBTK_GET_TYPE_NAME(A) \
109 BBTK_DEMANGLE_TYPE_NAME(typeid(A).name())
110 //-----------------------------------------------------------------------------
115 /// Template method which returns the name of a type
117 inline std::string TypeName()
118 { return BBTK_DEMANGLE_TYPE_NAME(typeid(T).name()); }
119 /// Template method which returns the name of the type of a variable
121 inline std::string TypeName(const T& t)
122 { return BBTK_DEMANGLE_TYPE_NAME(typeid(t).name()); }
123 /// Specialisation of TypeName when the type passed is already a type_info :
124 /// The user does not want to know the type of the type_info class but of
125 /// the class whose type_info is passed !
127 inline std::string TypeName<std::type_info>(const std::type_info& t)
128 { return BBTK_DEMANGLE_TYPE_NAME(t.name()); }
130 /// Template method which returns the human readable name of a type
132 inline std::string HumanTypeName()
133 { return TypeName<T>(); }
134 /// Template method which returns the human readable name of the type of a variable
136 inline std::string HumanTypeName(const T& t)
137 { return TypeName(t); }
138 /// Specialisation of TypeName when the type passed is already a type_info :
139 /// The user does not want to know the type of the type_info class but of
140 /// the class whose type_info is passed !
142 inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
143 { return TypeName<std::type_info>(t); }
144 /// Macro to specialise the template function TypeName for certain types
145 /// (typically highly template types, such as STL types)
146 /// in order to return a **really** human readable string
147 #define BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME) \
148 template <> inline std::string HumanTypeName< TYPE >() \
150 template <> inline std::string HumanTypeName< TYPE >(const TYPE&) \
153 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String");
154 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char");
155 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short");
156 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int");
157 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar");
158 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort");
159 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt");
160 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float");
161 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double");
162 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool");
163 BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long");
165 // Human readable strings for std::vector
166 #define BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE) \
167 template <> inline std::string HumanTypeName< std::vector<TYPE> >() \
168 { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;} \
169 template <> inline std::string HumanTypeName< std::vector<TYPE> > \
170 (const std::vector<TYPE>&) \
171 { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;}
173 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t);
174 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t);
175 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t);
176 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t);
177 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t);
178 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t);
179 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long);
180 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float);
181 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double);
182 BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string);
184 /// The bbtk::TypeInfo type is a const ref on std::type_info (which can only be manipulated as such (because typeid returns const std::type_info& and type_info has all constructors private))
185 typedef const std::type_info& TypeInfo;
188 BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type,
189 const std::type_info& source_type,
192 BBTK_EXPORT void* run_time_up_or_down_cast( const std::type_info& target_type,
193 const std::type_info& source_type,
194 const void* source_pointer