00001 #ifndef toast_illegal_cast_hpp_INCLUDED
00002 #define toast_illegal_cast_hpp_INCLUDED
00003
00004 #include <boost/type_traits.hpp>
00005
00012 #define TOAST_BAD_CAST_AT_RUNTIME_INTERNAL 0
00013
00014 #if TOAST_BAD_CAST_AT_RUNTIME == 1
00015 #undef TOAST_BAD_CAST_AT_RUNTIME_INTERNAL
00016 #define TOAST_BAD_CAST_AT_RUNTIME_INTERNAL 1
00017 #endif
00018
00019 #define TOAST_BAD_CAST_COMPILETIME(condition) \
00020 typedef typename BAD_CAST< condition >::result_type invalid_cast_test
00021
00022 #define TOAST_BAD_CAST_RUNTIME(condition) \
00023 if(!(condition)) throw std::bad_cast();
00024
00025 #define TOAST_BAD_CAST \
00026 BOOST_PP_IIF(TOAST_BAD_CAST_AT_RUNTIME_INTERNAL, \
00027 TOAST_BAD_CAST_RUNTIME, \
00028 TOAST_BAD_CAST_COMPILETIME)
00029
00030 namespace toast {
00031
00032 template <bool>
00033 struct BAD_CAST;
00034
00035 template <>
00036 struct BAD_CAST<true> { typedef int result_type; };
00037
00055 template <typename T, typename V>
00056 T illegal_cast(V v)
00057 {
00058 TOAST_BAD_CAST
00059 ((::boost::is_function<typename ::boost::remove_pointer<V>::type>::value
00060 && ::boost::is_same<T, void *>::value));
00061 return (T)(v);
00062 }
00063
00066 template <>
00067 void* illegal_cast<void *, bool>(bool b)
00068 {
00069 return (void *)(b);
00070 }
00071
00072 }
00073
00074 #endif // toast_illegal_cast_hpp_INCLUDED