1 // Boost.Bimap 2 // 3 // Copyright (c) 2006-2007 Matias Capeletto 4 // 5 // Distributed under the Boost Software License, Version 1.0. 6 // (See accompanying file LICENSE_1_0.txt or copy at 7 // http://www.boost.org/LICENSE_1_0.txt) 8 9 /// \file relation/detail/mutant.hpp 10 /// \brief Mutate functions to extract views of mutant classes. 11 12 #ifndef BOOST_BIMAP_RELATION_DETAIL_MUTANT_HPP 13 #define BOOST_BIMAP_RELATION_DETAIL_MUTANT_HPP 14 15 #if defined(_MSC_VER) 16 #pragma once 17 #endif 18 19 #include <boost/config.hpp> 20 21 #include <boost/bimap/detail/debug/static_error.hpp> 22 #include <boost/mpl/contains.hpp> 23 #include <boost/mpl/assert.hpp> 24 #include <boost/static_assert.hpp> 25 #include <boost/type_traits/is_const.hpp> 26 #include <boost/utility/addressof.hpp> 27 #include <boost/mpl/not.hpp> 28 #include <boost/utility/enable_if.hpp> 29 30 namespace boost { 31 namespace bimaps { 32 namespace relation { 33 34 /// \brief Relation details, mutant idiom and symmetrical metafunctions builders. 35 36 namespace detail { 37 38 //@{ 39 /// \brief Converts a mutant class to a view with zero overhead. 40 /** 41 42 This function is a safe wrapper around reinterpret_cast. It checks at 43 compile time that the desired view is supported by the mutant class. 44 See also mutant, can_mutate_in. 45 \ingroup mutant_group 46 **/ 47 48 49 template< class View, class Type > 50 BOOST_DEDUCED_TYPENAME enable_if< mpl::not_< is_const< Type > >, 51 52 View& 53 mutate(Type & m)54>::type mutate( Type & m ) 55 { 56 BOOST_MPL_ASSERT(( 57 ::boost::mpl::contains<BOOST_DEDUCED_TYPENAME Type::mutant_views,View> 58 )); 59 return *reinterpret_cast< View* >(boost::addressof(m)); 60 } 61 62 template< class View, class Type > 63 BOOST_DEDUCED_TYPENAME enable_if< is_const< Type >, 64 65 const View& 66 mutate(Type & m)67>::type mutate( Type & m ) 68 { 69 BOOST_MPL_ASSERT(( 70 ::boost::mpl::contains<BOOST_DEDUCED_TYPENAME Type::mutant_views,View> 71 )); 72 return *reinterpret_cast< const View* >(boost::addressof(m)); 73 } 74 75 //@} 76 77 } // namespace detail 78 } // namespace relation 79 } // namespace bimaps 80 } // namespace boost 81 82 #endif // BOOST_BIMAP_RELATION_DETAIL_MUTANT_HPP 83 84