mutant.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /// \file relation/detail/mutant.hpp
  9. /// \brief Mutate functions to extract views of mutant classes.
  10. #ifndef BOOST_BIMAP_RELATION_DETAIL_MUTANT_HPP
  11. #define BOOST_BIMAP_RELATION_DETAIL_MUTANT_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <boost/bimap/detail/debug/static_error.hpp>
  17. #include <boost/mpl/contains.hpp>
  18. #include <boost/mpl/assert.hpp>
  19. #include <boost/static_assert.hpp>
  20. #include <boost/type_traits/is_const.hpp>
  21. #include <boost/utility/addressof.hpp>
  22. #include <boost/mpl/not.hpp>
  23. #include <boost/utility/enable_if.hpp>
  24. namespace boost {
  25. namespace bimaps {
  26. namespace relation {
  27. /// \brief Relation details, mutant idiom and symmetrical metafunctions builders.
  28. namespace detail {
  29. //@{
  30. /// \brief Converts a mutant class to a view with zero overhead.
  31. /**
  32. This function is a safe wrapper around reinterpret_cast. It checks at
  33. compile time that the desired view is supported by the mutant class.
  34. See also mutant, can_mutate_in.
  35. \ingroup mutant_group
  36. **/
  37. template< class View, class Type >
  38. BOOST_DEDUCED_TYPENAME enable_if< mpl::not_< is_const< Type > >,
  39. View&
  40. >::type mutate( Type & m )
  41. {
  42. BOOST_MPL_ASSERT((
  43. ::boost::mpl::contains<BOOST_DEDUCED_TYPENAME Type::mutant_views,View>
  44. ));
  45. return *reinterpret_cast< View* >(boost::addressof(m));
  46. }
  47. template< class View, class Type >
  48. BOOST_DEDUCED_TYPENAME enable_if< is_const< Type >,
  49. const View&
  50. >::type mutate( Type & m )
  51. {
  52. BOOST_MPL_ASSERT((
  53. ::boost::mpl::contains<BOOST_DEDUCED_TYPENAME Type::mutant_views,View>
  54. ));
  55. return *reinterpret_cast< const View* >(boost::addressof(m));
  56. }
  57. //@}
  58. } // namespace detail
  59. } // namespace relation
  60. } // namespace bimaps
  61. } // namespace boost
  62. #endif // BOOST_BIMAP_RELATION_DETAIL_MUTANT_HPP