Chris@16: #ifndef BOOST_SERIALIZATION_SMART_CAST_HPP Chris@16: #define BOOST_SERIALIZATION_SMART_CAST_HPP Chris@16: Chris@16: // MS compatible compilers support #pragma once Chris@101: #if defined(_MSC_VER) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 Chris@16: // smart_cast.hpp: Chris@16: Chris@16: // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Chris@16: // Use, modification and distribution is subject to the Boost Software Chris@16: // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/serialization for updates, documentation, and revision history. Chris@16: Chris@16: // casting of pointers and references. Chris@16: Chris@16: // In casting between different C++ classes, there are a number of Chris@16: // rules that have to be kept in mind in deciding whether to use Chris@16: // static_cast or dynamic_cast. Chris@16: Chris@16: // a) dynamic casting can only be applied when one of the types is polymorphic Chris@16: // Otherwise static_cast must be used. Chris@16: // b) only dynamic casting can do runtime error checking Chris@16: // use of static_cast is generally un checked even when compiled for debug Chris@16: // c) static_cast would be considered faster than dynamic_cast. Chris@16: Chris@16: // If casting is applied to a template parameter, there is no apriori way Chris@16: // to know which of the two casting methods will be permitted or convenient. Chris@16: Chris@16: // smart_cast uses C++ type_traits, and program debug mode to select the Chris@16: // most convenient cast to use. Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include // NULL Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace serialization { Chris@16: namespace smart_cast_impl { Chris@16: Chris@16: template Chris@16: struct reference { Chris@16: Chris@16: struct polymorphic { Chris@16: Chris@16: struct linear { Chris@16: template Chris@16: static T cast(U & u){ Chris@16: return static_cast< T >(u); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct cross { Chris@16: template Chris@16: static T cast(U & u){ Chris@16: return dynamic_cast< T >(u); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: static T cast(U & u){ Chris@16: // if we're in debug mode Chris@16: #if ! defined(NDEBUG) \ Chris@16: || defined(__BORLANDC__) && (__BORLANDC__ <= 0x560) \ Chris@16: || defined(__MWERKS__) Chris@16: // do a checked dynamic cast Chris@16: return cross::cast(u); Chris@16: #else Chris@16: // borland 5.51 chokes here so we can't use it Chris@16: // note: if remove_reference isn't function for these types Chris@16: // cross casting will be selected this will work but will Chris@16: // not be the most efficient method. This will conflict with Chris@16: // the original smart_cast motivation. Chris@101: typedef typename mpl::eval_if< Chris@101: typename mpl::and_< Chris@16: mpl::not_::type, Chris@16: U Chris@16: > >, Chris@16: mpl::not_::type Chris@16: > > Chris@16: >, Chris@16: // borland chokes w/o full qualification here Chris@16: mpl::identity, Chris@16: mpl::identity Chris@16: >::type typex; Chris@16: // typex works around gcc 2.95 issue Chris@16: return typex::cast(u); Chris@16: #endif Chris@16: } Chris@16: }; Chris@16: Chris@16: struct non_polymorphic { Chris@16: template Chris@16: static T cast(U & u){ Chris@16: return static_cast< T >(u); Chris@16: } Chris@16: }; Chris@16: template Chris@16: static T cast(U & u){ Chris@16: #if defined(__BORLANDC__) Chris@16: return mpl::eval_if< Chris@16: boost::is_polymorphic, Chris@16: mpl::identity, Chris@16: mpl::identity Chris@16: >::type::cast(u); Chris@16: #else Chris@101: typedef typename mpl::eval_if< Chris@16: boost::is_polymorphic, Chris@16: mpl::identity, Chris@16: mpl::identity Chris@16: >::type typex; Chris@16: return typex::cast(u); Chris@16: #endif Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct pointer { Chris@16: Chris@16: struct polymorphic { Chris@16: // unfortunately, this below fails to work for virtual base Chris@16: // classes. need has_virtual_base to do this. Chris@16: // Subject for further study Chris@16: #if 0 Chris@16: struct linear { Chris@16: template Chris@16: static T cast(U * u){ Chris@16: return static_cast< T >(u); Chris@16: } Chris@16: }; Chris@16: Chris@16: struct cross { Chris@16: template Chris@16: static T cast(U * u){ Chris@16: T tmp = dynamic_cast< T >(u); Chris@16: #ifndef NDEBUG Chris@16: if ( tmp == 0 ) throw_exception(std::bad_cast()); Chris@16: #endif Chris@16: return tmp; Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: static T cast(U * u){ Chris@16: // if we're in debug mode Chris@101: #if 0 //! defined(NDEBUG) || defined(__BORLANDC__) && (__BORLANDC__ <= 0x560) Chris@16: // do a checked dynamic cast Chris@16: return cross::cast(u); Chris@16: #else Chris@16: // borland 5.51 chokes here so we can't use it Chris@16: // note: if remove_pointer isn't function for these types Chris@16: // cross casting will be selected this will work but will Chris@16: // not be the most efficient method. This will conflict with Chris@16: // the original smart_cast motivation. Chris@16: typedef Chris@101: typename mpl::eval_if< Chris@101: typename mpl::and_< Chris@16: mpl::not_::type, Chris@16: U Chris@16: > >, Chris@16: mpl::not_::type Chris@16: > > Chris@16: >, Chris@16: // borland chokes w/o full qualification here Chris@16: mpl::identity, Chris@16: mpl::identity Chris@16: >::type typex; Chris@16: return typex::cast(u); Chris@16: #endif Chris@16: } Chris@16: #else Chris@16: template Chris@16: static T cast(U * u){ Chris@16: T tmp = dynamic_cast< T >(u); Chris@16: #ifndef NDEBUG Chris@16: if ( tmp == 0 ) throw_exception(std::bad_cast()); Chris@16: #endif Chris@16: return tmp; Chris@16: } Chris@16: #endif Chris@16: }; Chris@16: Chris@16: struct non_polymorphic { Chris@16: template Chris@16: static T cast(U * u){ Chris@16: return static_cast< T >(u); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: static T cast(U * u){ Chris@16: #if defined(__BORLANDC__) Chris@16: return mpl::eval_if< Chris@16: boost::is_polymorphic, Chris@16: mpl::identity, Chris@16: mpl::identity Chris@16: >::type::cast(u); Chris@16: #else Chris@101: typedef typename mpl::eval_if< Chris@16: boost::is_polymorphic, Chris@16: mpl::identity, Chris@16: mpl::identity Chris@16: >::type typex; Chris@16: return typex::cast(u); Chris@16: #endif Chris@16: } Chris@16: Chris@16: }; Chris@16: Chris@16: template Chris@16: struct void_pointer { Chris@16: template Chris@16: static TPtr cast(UPtr uptr){ Chris@16: return static_cast(uptr); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct error { Chris@16: // if we get here, its because we are using one argument in the Chris@16: // cast on a system which doesn't support partial template Chris@16: // specialization Chris@16: template Chris@16: static T cast(U u){ Chris@16: BOOST_STATIC_ASSERT(sizeof(T)==0); Chris@16: return * static_cast(NULL); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // smart_cast_impl Chris@16: Chris@16: // this implements: Chris@16: // smart_cast(Source * s) Chris@16: // smart_cast(s) Chris@16: // note that it will fail with Chris@16: // smart_cast(s) Chris@16: template Chris@16: T smart_cast(U u) { Chris@16: typedef Chris@101: typename mpl::eval_if< Chris@101: typename mpl::or_< Chris@16: boost::is_same, Chris@16: boost::is_same, Chris@16: boost::is_same, Chris@16: boost::is_same Chris@16: >, Chris@16: mpl::identity >, Chris@16: // else Chris@101: typename mpl::eval_if, Chris@16: mpl::identity >, Chris@16: // else Chris@101: typename mpl::eval_if, Chris@16: mpl::identity >, Chris@16: // else Chris@16: mpl::identity Chris@16: > Chris@16: > Chris@16: > Chris@16: >::type typex; Chris@16: return typex::cast(u); Chris@16: } Chris@16: Chris@16: // this implements: Chris@16: // smart_cast_reference(Source & s) Chris@16: template Chris@16: T smart_cast_reference(U & u) { Chris@16: return smart_cast_impl::reference< T >::cast(u); Chris@16: } Chris@16: Chris@16: } // namespace serialization Chris@16: } // namespace boost Chris@16: Chris@16: #endif // BOOST_SERIALIZATION_SMART_CAST_HPP