bbtkRTTI.h

Go to the documentation of this file.
00001 /*=========================================================================                                                                               
00002   Program:   bbtk
00003   Module:    $RCSfile: bbtkRTTI.h,v $
00004   Language:  C++
00005   Date:      $Date: 2008/10/17 08:18:14 $
00006   Version:   $Revision: 1.6 $
00007 =========================================================================*/
00008 
00009 /* ---------------------------------------------------------------------
00010 
00011 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
00012 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
00013 *
00014 *  This software is governed by the CeCILL-B license under French law and 
00015 *  abiding by the rules of distribution of free software. You can  use, 
00016 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
00017 *  license as circulated by CEA, CNRS and INRIA at the following URL 
00018 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
00019 *  or in the file LICENSE.txt.
00020 *
00021 *  As a counterpart to the access to the source code and  rights to copy,
00022 *  modify and redistribute granted by the license, users are provided only
00023 *  with a limited warranty  and the software's author,  the holder of the
00024 *  economic rights,  and the successive licensors  have only  limited
00025 *  liability. 
00026 *
00027 *  The fact that you are presently reading this means that you have had
00028 *  knowledge of the CeCILL-B license and that you accept its terms.
00029 * ------------------------------------------------------------------------ */                                                                         
00030 
00036 #ifndef __BBTKRTTI_H_INCLUDED__
00037 #define __BBTKRTTI_H_INCLUDED__
00038 
00039 #include "bbtkSystem.h"
00040 #include <vector>
00041 
00042 //-----------------------------------------------------------------------------
00043 // RRTI type_info.name() demangling
00044 // For type_info.name() demangling (gcc >= 3.1, see http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html)
00045 // Test for GCC > 3.1.0 //
00046 #if __GNUC__ > 3 ||                                     \
00047   (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 ||              \
00048                      (__GNUC_MINOR__ == 1 &&            \
00049                       __GNUC_PATCHLEVEL__ > 0)))
00050 #include <cxxabi.h>
00051 #include <stdlib.h>
00052 namespace bbtk
00053 {
00054   inline std::string demangle_type_name(const char* name) 
00055   {
00056     int  status;
00057     char* dem = abi::__cxa_demangle(name, 0, 0, &status);
00058     std::string demangled(dem);
00059     free(dem);
00060     if (!status) return demangled;
00061     return name;
00062   }
00063 
00064 }
00065 
00066 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
00067 
00068 //==========================================================================
00069 #elif defined(_WIN32)
00070 // WIN32
00071 //#include "Windows.h"
00072 //#include "stdafx.h"
00073 #include <windows.h>
00074 #include <imagehlp.h>
00075 // include the right library in the linker stage
00076 #pragma comment( lib, "imagehlp.lib" )
00077 
00078 namespace bbtk
00079 {
00080 
00081   
00082   
00083   
00084   inline std::string demangle_type_name(const char* name) 
00085   {
00086     char demangled[513]; 
00087     if (UnDecorateSymbolName(name, demangled, 
00088                              sizeof(name), UNDNAME_COMPLETE))
00089       {
00090                   return name; //demangled;
00091       }
00092     else 
00093       {
00094         return name;
00095       }
00096   }
00097 }
00098 #define BBTK_DEMANGLE_TYPE_NAME(NAME) bbtk::demangle_type_name(NAME)
00099 
00100 #else 
00101 // OTHER
00102 #define BBTK_DEMANGLE_TYPE_NAME(NAME) NAME
00103 #endif 
00104 
00105 #define BBTK_GET_CURRENT_OBJECT_NAME \
00106   BBTK_DEMANGLE_TYPE_NAME(typeid(*this).name())
00107 
00108 #define BBTK_GET_TYPE_NAME(A)           \
00109   BBTK_DEMANGLE_TYPE_NAME(typeid(A).name())
00110 //-----------------------------------------------------------------------------
00111 
00112 
00113 namespace bbtk 
00114 {
00116   template <class T>
00117   inline std::string TypeName() 
00118   { return BBTK_DEMANGLE_TYPE_NAME(typeid(T).name()); }
00120   template <class T>
00121   inline std::string TypeName(const T& t) 
00122   { return BBTK_DEMANGLE_TYPE_NAME(typeid(t).name()); }
00126   template <> 
00127   inline std::string TypeName<std::type_info>(const std::type_info& t)
00128   { return BBTK_DEMANGLE_TYPE_NAME(t.name()); }
00129 
00131   template <class T>
00132   inline std::string HumanTypeName() 
00133   { return TypeName<T>(); }
00135   template <class T>
00136   inline std::string HumanTypeName(const T& t) 
00137   { return TypeName(t); }
00141   template <> 
00142   inline std::string HumanTypeName<std::type_info>(const std::type_info& t)
00143   { return TypeName<std::type_info>(t); }
00147 #define BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(TYPE,NAME)                 \
00148   template <> inline std::string HumanTypeName< TYPE >()                \
00149   { return NAME; }                                                      \
00150     template <> inline std::string HumanTypeName< TYPE >(const TYPE&)   \
00151     { return NAME; }    
00152   
00153   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(std::string,"String");
00154   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed char,"Char");
00155   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed short,"Short");
00156   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(signed int,"Int");
00157   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned char,"UChar");
00158   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned short,"UShort");
00159   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(unsigned int,"UInt");
00160   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(float,"Float");
00161   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(double,"Double");
00162   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(bool,"Bool");
00163   BBTK_DEFINE_HUMAN_READABLE_TYPE_NAME(long,"Long");
00164 
00165   // Human readable strings for std::vector
00166 #define BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(TYPE)               \
00167   template <> inline std::string HumanTypeName< std::vector<TYPE> >()   \
00168   { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;}                             \
00169     template <> inline std::string HumanTypeName< std::vector<TYPE> >   \
00170         (const std::vector<TYPE>&) \
00171   { std::string t("Vector"); t += HumanTypeName<TYPE>(); return t;}     
00172 
00173   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int8_t);
00174   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint8_t);
00175   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int16_t);
00176   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint16_t);
00177   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(int32_t);
00178   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(uint32_t);
00179   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(long);
00180   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(float);
00181   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(double);
00182   BBTK_DEFINE_HUMAN_READABLE_VECTOR_TYPE_NAME(std::string);
00183 
00185   typedef const std::type_info& TypeInfo;
00186 
00187 
00188   BBTK_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
00189                                   const std::type_info& source_type,
00190                                   void*  source_pointer
00191                                   );
00192  BBTK_EXPORT void*  run_time_up_or_down_cast( const std::type_info& target_type,
00193                                   const std::type_info& source_type,
00194                                   const void*  source_pointer
00195                                   );
00196 
00197 
00198 }
00199 
00200 #endif
00201 

Generated on Wed Nov 12 11:37:08 2008 for BBTK by  doxygen 1.5.6