00001
00002
00011 #ifndef ATHCONTAINERS_TOOLS_DVLCAST_H
00012 #define ATHCONTAINERS_TOOLS_DVLCAST_H
00013
00014
00015 #include <limits>
00016 #include <limits.h>
00017 #include <typeinfo>
00018 #include <cstdlib>
00019 #include <cstddef>
00020
00021
00022 namespace DataModel_detail {
00023
00024
00025 #ifdef __GCCXML__
00026
00027 template <class DVL>
00028 struct DVLCast
00029 {
00030 typedef typename DVL::base_value_type T;
00031 template <class U> static T* cast (U* b);
00032 template <class U> static const T* cast (const U* b);
00033 };
00034 #else
00035
00054 template <class DVL, bool has_virtual=DVL::has_virtual> struct DVLCast {};
00055
00056
00060 template <class DVL>
00061 struct DVLCast<DVL, false>
00062 {
00063 typedef typename DVL::base_value_type T;
00064
00065
00070 template <class U>
00071 static T* cast (U* b)
00072 {
00073 return static_cast<T*> (b);
00074 }
00075
00076
00081 template <class U>
00082 static const T* cast (const U* b)
00083 {
00084 return static_cast<const T*> (b);
00085 }
00086 };
00087
00088
00092 template <class DVL>
00093 struct DVLCast<DVL, true>
00094 {
00095 typedef typename DVL::base_value_type T;
00096
00097
00102 template <class U>
00103 static T* cast (U* b)
00104 {
00105
00106
00107
00108
00109 if (!b)
00110 return 0;
00111 if (typeid(*b) == typeid(T)) {
00112 static ptrdiff_t offs = LONG_MAX;
00113 if (offs == LONG_MAX) {
00114 T* ret = dynamic_cast<T*> (b);
00115 offs = reinterpret_cast<char*>(ret) - reinterpret_cast<char*>(b);
00116 return ret;
00117 }
00118 return reinterpret_cast<T*> (reinterpret_cast<char*>(b) + offs);
00119 }
00120 else
00121 return dynamic_cast<T*> (b);
00122 }
00123
00124
00129 template <class U>
00130 static const T* cast (const U* b)
00131 {
00132
00133 if (!b)
00134 return 0;
00135 if (typeid(*b) == typeid(T)) {
00136 static ptrdiff_t offs = LONG_MAX;
00137 if (offs == LONG_MAX) {
00138 T* ret = dynamic_cast<const T*> (b);
00139 offs = reinterpret_cast<char*>(ret) - reinterpret_cast<char*>(b);
00140 return ret;
00141 }
00142 return reinterpret_cast<const T*> (reinterpret_cast<char*>(b) + offs);
00143 }
00144 else
00145 return dynamic_cast<const T*> (b);
00146 }
00147 };
00148 #endif // not __GCCXML__
00149
00150
00151 }
00152
00153
00154 #endif // not ATHCONTAINERS_TOOLS_DVLCAST_H