bbtkRTTI.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "bbtkRTTI.h"
00033 #include "bbtkMessageManager.h"
00034
00035
00036
00037 namespace bbtk
00038 {
00039
00040
00041
00042
00043 struct Dummy {
00044 virtual ~Dummy() {}
00045 };
00046
00047
00048 void* run_time_up_or_down_cast( const std::type_info& target_type,
00049 const std::type_info& source_type,
00050 const void* source_pointer
00051 )
00052 {
00053 return run_time_up_or_down_cast(target_type,
00054 source_type,
00055 const_cast<void*>(source_pointer));
00056 }
00057
00058 void* run_time_up_or_down_cast( const std::type_info& target_type,
00059 const std::type_info& source_type,
00060 void* source_pointer
00061 )
00062 {
00063 bbtkDebugMessage("Data",5,
00064 "run_time_up_or_down_cast : Casting pointer to '"
00065 << TypeName(typeid(*(Dummy*)source_pointer))
00066 << "' from " << TypeName(source_type)
00067 << " to " << TypeName(target_type) << std::endl);
00068
00069 void* target_pointer = 0;
00070 #if __GNUC__ > 3 || \
00071 (__GNUC__ == 3 && (__GNUC_MINOR__ > 1 || \
00072 (__GNUC_MINOR__ == 1 && \
00073 __GNUC_PATCHLEVEL__ > 0)))
00074
00075 const abi::__class_type_info* targetTI =
00076 (const abi::__class_type_info *)( &(target_type));
00077
00078 bbtkDebugMessage("Data",7," * source = "<<source_pointer<<std::endl);
00079
00080 void* tmp = source_pointer;
00081 if (source_type.__do_upcast(targetTI,&tmp))
00082 {
00083 target_pointer = tmp;
00084 }
00085 else
00086 {
00087 bbtkDebugMessage("Data",7,
00088 " * upcast failed : trying dynamic down cast"
00089 <<std::endl);
00090 const abi::__class_type_info* sourceTI =
00091 (const abi::__class_type_info *)( &(source_type));
00092
00093
00094 target_pointer = abi::__dynamic_cast(source_pointer,
00095 sourceTI,
00096 targetTI,
00097 -1);
00098 }
00099
00100 bbtkDebugMessage("Data",7," * target = "<<target_pointer<<std::endl);
00101
00102 #else
00103 bbtkWarning("run_time_up_or_down_cast not impl. on Win : to do");
00104
00105 #endif
00106 return target_pointer;
00107
00108 }
00109
00110 }
00111