]> Creatis software - bbtk.git/blob - kernel/src/bbtkRTTI.h
249b937b86e5bee82801140dd524a3001aaa9588
[bbtk.git] / kernel / src / bbtkRTTI.h
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkRTTI.h,v $
4   Language:  C++
5   Date:      $Date: 2008/10/17 08:18:14 $
6   Version:   $Revision: 1.6 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
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.
20 *
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
25 *  liability. 
26 *
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 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *\file
33  *\brief RTTI tools (system dependent).
34  */
35
36 #ifndef __BBTKRTTI_H_INCLUDED__
37 #define __BBTKRTTI_H_INCLUDED__
38
39 #include "bbtkSystem.h"
40 #include <vector>
41
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 //
46 #if __GNUC__ > 3 ||                                     \
47   (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 ||              \
48                      (__GNUC_MINOR__ == 1 &&            \
49                       __GNUC_PATCHLEVEL__ > 0)))
50 #include <cxxabi.h>
51 #include <stdlib.h>
52 namespace bbtk
53 {
54   inline std::string demangle_type_name(const char* name) 
55   {
56     int  status;
57     char* dem = abi::__cxa_demangle(name, 0, 0, &status);
58     std::string demangled(dem);
59     free(dem);
60     if (!status) return demangled;
61     return name;
62   }
63
64 }
65
66 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
67
68 //==========================================================================
69 #elif defined(_WIN32)
70 // WIN32
71 //#include "Windows.h"
72 //#include "stdafx.h"
73 #include <windows.h>
74 #include <imagehlp.h>
75 // include the right library in the linker stage
76 #pragma comment( lib, "imagehlp.lib" )
77
78 namespace bbtk
79 {
80
81   
82   
83   
84   inline std::string demangle_type_name(const char* name) 
85   {
86     char demangled[513]; 
87     if (UnDecorateSymbolName(name, demangled, 
88                              sizeof(name), UNDNAME_COMPLETE))
89       {
90                   return name; //demangled;
91       }
92     else 
93       {
94         return name;
95       }
96   }
97 }
98 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
99
100 #else 
101 // OTHER
102 #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME
103 #endif 
104
105 #define BBTK_GET_CURRENT_OBJECT_NAME \
106   BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name())
107
108 #define BBTK_GET_TYPE_NAME(A)           \
109   BBTK_DEMANGLE_TYPE_NAME(typeid(A).name())
110 //-----------------------------------------------------------------------------
111
112
113 namespace bbtk 
114 {
115   /// Template method which returns the name of a type
116   template <class T>
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
120   template <class T>
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 !
126   template <> 
127   inline std::string TypeName<std::type_info>(const std::type_info& t)
128   { return BBTK_DEMANGLE_TYPE_NAME(t.name()); }
129
130   /// Template method which returns the human readable name of a type
131   template <class T>
132   inline std::string HumanTypeName() 
133   { return TypeName<T>(); }
134   /// Template method which returns the human readable name of the type of a variable
135   template <class T>
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 !
141   template <> 
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 >()                \
149   { return NAME; }                                                      \
150     template <> inline std::string HumanTypeName< TYPE >(const TYPE&)   \
151     { return NAME; }    
152   
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");
164
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;}     
172
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);
183
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;
186
187
188   BBTK_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
189                                   const std::type_info& source_type,
190                                   void*  source_pointer
191                                   );
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
195                                   );
196
197
198 }
199
200 #endif
201