// Boost.Bimap // // Copyright (c) 2006-2007 Matias Capeletto // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) /// \file relation/mutant_relation.hpp /// \brief Defines the mutant_relation class #ifndef BOOST_BIMAP_RELATION_MUTANT_RELATION_HPP #define BOOST_BIMAP_RELATION_MUTANT_RELATION_HPP #if defined(_MSC_VER) #pragma once #endif #include #include #include #include #ifndef BOOST_BIMAP_DISABLE_SERIALIZATION #include #endif // BOOST_BIMAP_DISABLE_SERIALIZATION #include #include // Boost.Bimap #include #include #include #include #include #include #include #include #include namespace lslboost { namespace bimaps { namespace relation { namespace detail { // This class is included so structured_pair and mutant_relation share // exactly the same class layout template< class LeftType, class RightType, bool force_mutable > class relation_storage : public symmetrical_base { typedef symmetrical_base base_; typedef relation_storage storage_; public: typedef relation_storage non_mutable_storage; typedef ::lslboost::mpl::vector2 < relation_storage< LeftType, RightType, true >, relation_storage< LeftType, RightType, false > > mutant_views; //@{ /// data BOOST_DEDUCED_TYPENAME base_::left_value_type left; BOOST_DEDUCED_TYPENAME base_::right_value_type right; //@} relation_storage() {} relation_storage(BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::left_value_type >::param_type l, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::right_value_type >::param_type r) : left(l), right(r) {} BOOST_DEDUCED_TYPENAME base_:: left_value_type & get_left() { return left; } const BOOST_DEDUCED_TYPENAME base_:: left_value_type & get_left()const { return left; } BOOST_DEDUCED_TYPENAME base_::right_value_type & get_right() { return right; } const BOOST_DEDUCED_TYPENAME base_::right_value_type & get_right()const { return right; } }; template< class TA, class TB, class Info, bool force_mutable > class relation_info_hook : public ::lslboost::bimaps::relation::detail::relation_storage { typedef ::lslboost::bimaps::relation::detail:: relation_storage base_; typedef BOOST_DEDUCED_TYPENAME ::lslboost::bimaps::tags::support:: default_tagged::type tagged_info_type; public: typedef BOOST_DEDUCED_TYPENAME tagged_info_type::value_type info_type; typedef BOOST_DEDUCED_TYPENAME tagged_info_type::tag info_tag; info_type info; protected: relation_info_hook() {} relation_info_hook( BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::left_value_type >::param_type l, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::right_value_type >::param_type r, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< info_type >::param_type i = info_type() ) : base_(l,r), info(i) {} template< class Relation > relation_info_hook( const Relation & rel ) : base_(rel.left,rel.right), info(rel.info) {} template< class Relation > void change_to( const Relation & rel ) { base_::left = rel.left ; base_::right = rel.right; info = rel.info ; } #ifndef BOOST_BIMAP_DISABLE_SERIALIZATION template< class Archive > void serialize(Archive & ar, const unsigned int) { ar & ::lslboost::serialization::make_nvp("left" , base_::left ); ar & ::lslboost::serialization::make_nvp("right", base_::right); ar & ::lslboost::serialization::make_nvp("info" , info ); } #endif // BOOST_BIMAP_DISABLE_SERIALIZATION }; template< class TA, class TB, bool force_mutable> class relation_info_hook : public ::lslboost::bimaps::relation::detail::relation_storage { typedef ::lslboost::bimaps::relation::detail:: relation_storage base_; public: typedef ::lslboost::mpl::na info_type; typedef member_at::info info_tag; protected: relation_info_hook() {} relation_info_hook( BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::left_value_type >::param_type l, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::right_value_type >::param_type r) : base_(l,r) {} template< class Relation > relation_info_hook( const Relation & rel ) : base_(rel.left,rel.right) {} template< class Relation > void change_to( const Relation & rel ) { base_::left = rel.left ; base_::right = rel.right; } #ifndef BOOST_BIMAP_DISABLE_SERIALIZATION template< class Archive > void serialize(Archive & ar, const unsigned int) { ar & ::lslboost::serialization::make_nvp("left" , base_::left ); ar & ::lslboost::serialization::make_nvp("right", base_::right); } #endif // BOOST_BIMAP_DISABLE_SERIALIZATION }; } // namespace detail /// \brief Abstraction of a related pair of values, that extends the std::pair class. /** The mutant_relation is a mutant class. A mutant class can mutate with zero overhead in other classes that are called views. Each view has to be StorageCompatible with the base class of the mutant. Note that all the views have the following storage structure: \verbatim __________ | | | TA | |__________| | | | TB | |__________| \endverbatim See also select_relation, standard_relation. \ingroup relation_group **/ template< class TA, class TB, class Info = ::lslboost::mpl::na, bool force_mutable = false > class mutant_relation : public ::lslboost::bimaps::relation::detail:: relation_info_hook { typedef ::lslboost::bimaps::relation::detail:: relation_info_hook base_; public: // We have to know the type of the base where the types are // defined because Boost.MultiIndex requires it. typedef ::lslboost::bimaps::relation::detail:: relation_storage storage_base; /// Above view, non mutable view of the relation typedef mutant_relation above_view; //@{ /// A signature compatible std::pair that is a view of the relation. typedef structured_pair< TA, TB, Info, normal_layout > left_pair; typedef structured_pair< TB, TA, Info, mirror_layout > right_pair; //@} typedef ::lslboost::mpl::vector4 < left_pair, right_pair, mutant_relation< TA, TB, Info, true >, mutant_relation< TA, TB, Info, false > > mutant_views; mutant_relation() {} mutant_relation(BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_:: left_value_type >::param_type l, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::right_value_type >::param_type r) : base_(l,r) {} mutant_relation(BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_:: left_value_type >::param_type l, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::right_value_type >::param_type r, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME base_::info_type >::param_type i) : base_(l,r,i) {} mutant_relation(const mutant_relation & rel) : base_(rel) {} mutant_relation(const mutant_relation & rel) : base_(rel) {} // Operators template< bool FM > mutant_relation& operator=(const mutant_relation & rel) { base_::change_to(rel); return *this; } // The following functions are redundant if you only consider this class. // They are included to make easier the construction of the get and the // pair_by metafunction. Remember that not all compiler supports the mutant // idiom. left_pair & get_left_pair() { return ::lslboost::bimaps::relation::detail::mutate(*this); } const left_pair & get_left_pair() const { return ::lslboost::bimaps::relation::detail::mutate(*this); } right_pair & get_right_pair() { return ::lslboost::bimaps::relation::detail::mutate(*this); } const right_pair & get_right_pair() const { return ::lslboost::bimaps::relation::detail::mutate(*this); } above_view & get_view() { return ::lslboost::bimaps::relation::detail::mutate(*this); } const above_view & get_view() const { return ::lslboost::bimaps::relation::detail::mutate(*this); } template< class Tag > const BOOST_DEDUCED_TYPENAME ::lslboost::bimaps::relation::support:: result_of::get::type get() const { return ::lslboost::bimaps::relation::support::get(*this); } template< class Tag > BOOST_DEDUCED_TYPENAME ::lslboost::bimaps::relation::support:: result_of::get::type get() { return ::lslboost::bimaps::relation::support::get(*this); } #ifndef BOOST_BIMAP_DISABLE_SERIALIZATION private: friend class ::lslboost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { base_::serialize(ar,version); } #endif // BOOST_BIMAP_DISABLE_SERIALIZATION }; // hash value template< class FirstType, class SecondType, bool FM > std::size_t hash_value(const detail::relation_storage & r) { std::size_t seed = 0; ::lslboost::hash_combine(seed, r. left ); ::lslboost::hash_combine(seed, r.right ); return seed; } // mutant_relation - mutant_relation template< class FirstType, class SecondType, bool FM1, bool FM2 > bool operator==(const detail::relation_storage & a, const detail::relation_storage & b) { return ( ( a.left == b.left ) && ( a.right == b.right ) ); } template< class FirstType, class SecondType, bool FM1, bool FM2 > bool operator!=(const detail::relation_storage & a, const detail::relation_storage & b) { return ! ( a == b ); } template< class FirstType, class SecondType, bool FM1, bool FM2 > bool operator<(const detail::relation_storage & a, const detail::relation_storage & b) { return ( ( a.left < b.left ) || (( a.left == b.left ) && ( a.right < b.right ))); } template< class FirstType, class SecondType, bool FM1, bool FM2 > bool operator<=(const detail::relation_storage & a, const detail::relation_storage & b) { return ( ( a.left < b.left ) || (( a.left == b.left ) && ( a.right <= b.right ))); } template< class FirstType, class SecondType, bool FM1, bool FM2 > bool operator>(const detail::relation_storage & a, const detail::relation_storage & b) { return ( ( a.left > b.left ) || (( a.left == b.left ) && ( a.right > b.right ))); } template< class FirstType, class SecondType, bool FM1, bool FM2 > bool operator>=(const detail::relation_storage & a, const detail::relation_storage & b) { return ( ( a.left > b.left ) || (( a.left == b.left ) && ( a.right >= b.right ))); } namespace detail { template< class TA, class TB, class Info, bool force_mutable> mutant_relation copy_with_left_replaced(mutant_relation const& rel, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME mutant_relation::left_value_type> ::param_type l) { return mutant_relation(l,rel.right,rel.info); } template< class TA, class TB, bool force_mutable> mutant_relation copy_with_left_replaced(mutant_relation const& rel, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME mutant_relation::left_value_type> ::param_type l) { return mutant_relation(l,rel.right); } template< class TA, class TB, class Info, bool force_mutable> mutant_relation copy_with_right_replaced(mutant_relation const& rel, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME mutant_relation::right_value_type> ::param_type r) { return mutant_relation(rel.left,r,rel.info); } template< class TA, class TB, bool force_mutable> mutant_relation copy_with_right_replaced(mutant_relation const& rel, BOOST_DEDUCED_TYPENAME ::lslboost::call_traits< BOOST_DEDUCED_TYPENAME mutant_relation::right_value_type> ::param_type r) { return mutant_relation(rel.left,r); } } // namespace detail } // namespace relation } // namespace bimaps } // namespace lslboost #endif // BOOST_BIMAP_RELATION_MUTANT_RELATION_HPP