]> Creatis software - bbtk.git/blob - kernel/src/bbtkRTTI.h
#3202 BBTK Feature New Normal - fast algorithm for ImageBoundaries box
[bbtk.git] / kernel / src / bbtkRTTI.h
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbtkRTTI.h,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.7 $
34 =========================================================================*/
35
36
37
38 /**
39  *\file
40  *\brief RTTI tools (system dependent).
41  */
42
43 #ifndef __BBTKRTTI_H_INCLUDED__
44 #define __BBTKRTTI_H_INCLUDED__
45
46 #include "bbtkSystem.h"
47 #include <vector>
48
49 //-----------------------------------------------------------------------------
50 // RRTI type_info.name() demangling
51 // For type_info.name() demangling (gcc >= 3.1, see http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html)
52 // Test for GCC > 3.1.0 //
53 #if __GNUC__ > 3 ||                                     \
54   (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 ||              \
55                      (__GNUC_MINOR__ == 1 &&            \
56                       __GNUC_PATCHLEVEL__ > 0)))
57 #include <cxxabi.h>
58 #include <stdlib.h>
59 namespace bbtk
60 {
61   inline std::string demangle_type_name(const char* name) 
62   {
63     int  status;
64     char* dem = abi::__cxa_demangle(name, 0, 0, &status);
65     std::string demangled(dem);
66     free(dem);
67     if (!status) return demangled;
68     return name;
69   }
70
71 }
72
73 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
74
75 //==========================================================================
76 #elif defined(_WIN32)
77 // WIN32
78 //#include "Windows.h"
79 //#include "stdafx.h"
80 #include <windows.h>
81 #include <imagehlp.h>
82 // include the right library in the linker stage
83 #pragma comment( lib, "imagehlp.lib" )
84
85 namespace bbtk
86 {
87
88   
89   
90   
91   inline std::string demangle_type_name(const char* name) 
92   {
93     char demangled[513]; 
94     if (UnDecorateSymbolName(name, demangled, 
95                              sizeof(name), UNDNAME_COMPLETE))
96       {
97                   return name; //demangled;
98       }
99     else 
100       {
101         return name;
102       }
103   }
104 }
105 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
106
107 #else 
108 // OTHER
109 #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME
110 #endif 
111
112 #define BBTK_GET_CURRENT_OBJECT_NAME \
113   BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name())
114
115 #define BBTK_GET_TYPE_NAME(A)           \
116   BBTK_DEMANGLE_TYPE_NAME(typeid(A).name())
117 //-----------------------------------------------------------------------------
118
119
120 namespace bbtk 
121 {
122   /// Template method which returns the name of a type
123   template <class T>
124   inline std::string TypeName() 
125   { return BBTK_DEMANGLE_TYPE_NAME(typeid(T).name()); }
126   /// Template method which returns the name of the type of a variable
127   template <class T>
128   inline std::string TypeName(const T& t) 
129   { return BBTK_DEMANGLE_TYPE_NAME(typeid(t).name()); }
130   /// Specialisation of TypeName when the type passed is already a type_info :
131   /// The user does not want to know the type of the type_info class but of 
132   /// the class whose type_info is passed !
133   template <> 
134   inline std::string TypeName<std::type_info>(const std::type_info& t)
135   { return BBTK_DEMANGLE_TYPE_NAME(t.name()); }
136
137   /// Template method which returns the human readable name of a type
138   template <class T>
139   inline std::string HumanTypeName() 
140   { return TypeName<T>(); }
141   /// Template method which returns the human readable name of the type of a variable
142   template <class T>
143   inline std::string HumanTypeName(const T& t) 
144   { return TypeName(t); }
145   /// Specialisation of TypeName when the type passed is already a type_info :
146   /// The user does not want to know the type of the type_info class but of 
147   /// the class whose type_info is passed !
148   template <> 
149   inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
150   { return TypeName<std::type_info>(t); }
151   /// Macro to specialise the template function TypeName for certain types 
152   /// (typically highly template types, such as STL types) 
153   /// in order to return a **really** human readable string
154 #define BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME)                 \
155   template <> inline std::string HumanTypeName< TYPE >()                \
156   { return NAME; }                                                      \
157     template <> inline std::string HumanTypeName< TYPE >(const TYPE&)   \
158     { return NAME; }    
159   
160   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String");
161   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char");
162   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short");
163   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int");
164   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar");
165   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort");
166   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt");
167   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float");
168   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double");
169   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool");
170   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long");
171
172   // Human readable strings for std::vector
173 #define BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE)               \
174   template <> inline std::string HumanTypeName< std::vector<TYPE> >()   \
175   { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;}                             \
176     template <> inline std::string HumanTypeName< std::vector<TYPE> >   \
177         (const std::vector<TYPE>&) \
178   { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;}     
179
180   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t);
181   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t);
182   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t);
183   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t);
184   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t);
185   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t);
186   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long);
187   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float);
188   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double);
189   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string);
190
191 /// 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)) 
192   typedef const std::type_info& TypeInfo;
193
194
195   BBTK_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
196                                   const std::type_info& source_type,
197                                   void*  source_pointer
198                                   );
199  BBTK_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
200                                   const std::type_info& source_type,
201                                   const void*  source_pointer
202                                   );
203
204
205 }
206
207 #endif
208