set_view_base.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 detail/set_view_base.hpp
  9. /// \brief Helper base for the construction of the bimap views types.
  10. #ifndef BOOST_BIMAP_DETAIL_SET_VIEW_BASE_HPP
  11. #define BOOST_BIMAP_DETAIL_SET_VIEW_BASE_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <boost/bimap/relation/member_at.hpp>
  17. #include <boost/bimap/relation/support/data_extractor.hpp>
  18. #include <boost/bimap/detail/modifier_adaptor.hpp>
  19. #include <boost/bimap/detail/set_view_iterator.hpp>
  20. #include <boost/bimap/relation/support/get_pair_functor.hpp>
  21. #include <boost/bimap/relation/detail/to_mutable_relation_functor.hpp>
  22. #include <boost/bimap/relation/mutant_relation.hpp>
  23. #include <boost/bimap/container_adaptor/support/iterator_facade_converters.hpp>
  24. namespace boost {
  25. namespace bimaps {
  26. namespace detail {
  27. template< class Key, class Value, class KeyToBase >
  28. class set_view_key_to_base
  29. {
  30. public:
  31. const Key operator()( const Value & v ) const
  32. {
  33. return keyToBase( v );
  34. }
  35. private:
  36. KeyToBase keyToBase;
  37. };
  38. template< class MutantRelationStorage, class KeyToBase >
  39. class set_view_key_to_base<MutantRelationStorage,MutantRelationStorage,KeyToBase>
  40. {
  41. typedef BOOST_DEDUCED_TYPENAME MutantRelationStorage::non_mutable_storage non_mutable_storage;
  42. public:
  43. const MutantRelationStorage & operator()( const non_mutable_storage & k ) const
  44. {
  45. return ::boost::bimaps::relation::detail::mutate<MutantRelationStorage>(k);
  46. }
  47. const MutantRelationStorage & operator()( const MutantRelationStorage & k ) const
  48. {
  49. return k;
  50. }
  51. };
  52. // The next macro can be converted in a metafunctor to gain code robustness.
  53. /*===========================================================================*/
  54. #define BOOST_BIMAP_SET_VIEW_CONTAINER_ADAPTOR( \
  55. CONTAINER_ADAPTOR, CORE_INDEX, OTHER_ITER, CONST_OTHER_ITER \
  56. ) \
  57. ::boost::bimaps::container_adaptor::CONTAINER_ADAPTOR \
  58. < \
  59. CORE_INDEX, \
  60. ::boost::bimaps::detail:: \
  61. set_view_iterator< \
  62. BOOST_DEDUCED_TYPENAME CORE_INDEX::iterator >, \
  63. ::boost::bimaps::detail:: \
  64. const_set_view_iterator< \
  65. BOOST_DEDUCED_TYPENAME CORE_INDEX::const_iterator >, \
  66. ::boost::bimaps::detail:: \
  67. set_view_iterator< \
  68. BOOST_DEDUCED_TYPENAME CORE_INDEX::OTHER_ITER >, \
  69. ::boost::bimaps::detail:: \
  70. const_set_view_iterator< \
  71. BOOST_DEDUCED_TYPENAME CORE_INDEX::CONST_OTHER_ITER >, \
  72. ::boost::bimaps::container_adaptor::support::iterator_facade_to_base \
  73. < \
  74. ::boost::bimaps::detail:: set_view_iterator< \
  75. BOOST_DEDUCED_TYPENAME CORE_INDEX::iterator>, \
  76. ::boost::bimaps::detail::const_set_view_iterator< \
  77. BOOST_DEDUCED_TYPENAME CORE_INDEX::const_iterator> \
  78. \
  79. >, \
  80. ::boost::mpl::na, \
  81. ::boost::mpl::na, \
  82. ::boost::bimaps::relation::detail:: \
  83. get_mutable_relation_functor< \
  84. BOOST_DEDUCED_TYPENAME CORE_INDEX::value_type >, \
  85. ::boost::bimaps::relation::support:: \
  86. get_above_view_functor< \
  87. BOOST_DEDUCED_TYPENAME CORE_INDEX::value_type >, \
  88. ::boost::bimaps::detail::set_view_key_to_base< \
  89. BOOST_DEDUCED_TYPENAME CORE_INDEX::key_type, \
  90. BOOST_DEDUCED_TYPENAME CORE_INDEX::value_type, \
  91. BOOST_DEDUCED_TYPENAME CORE_INDEX::key_from_value \
  92. > \
  93. >
  94. /*===========================================================================*/
  95. /*===========================================================================*/
  96. #define BOOST_BIMAP_SEQUENCED_SET_VIEW_CONTAINER_ADAPTOR( \
  97. CONTAINER_ADAPTOR, CORE_INDEX, OTHER_ITER, CONST_OTHER_ITER \
  98. ) \
  99. ::boost::bimaps::container_adaptor::CONTAINER_ADAPTOR \
  100. < \
  101. CORE_INDEX, \
  102. ::boost::bimaps::detail:: \
  103. set_view_iterator< \
  104. BOOST_DEDUCED_TYPENAME CORE_INDEX::iterator >, \
  105. ::boost::bimaps::detail:: \
  106. const_set_view_iterator< \
  107. BOOST_DEDUCED_TYPENAME CORE_INDEX::const_iterator >, \
  108. ::boost::bimaps::detail:: \
  109. set_view_iterator< \
  110. BOOST_DEDUCED_TYPENAME CORE_INDEX::OTHER_ITER >, \
  111. ::boost::bimaps::detail:: \
  112. const_set_view_iterator< \
  113. BOOST_DEDUCED_TYPENAME CORE_INDEX::CONST_OTHER_ITER >, \
  114. ::boost::bimaps::container_adaptor::support::iterator_facade_to_base \
  115. < \
  116. ::boost::bimaps::detail:: set_view_iterator< \
  117. BOOST_DEDUCED_TYPENAME CORE_INDEX::iterator>, \
  118. ::boost::bimaps::detail::const_set_view_iterator< \
  119. BOOST_DEDUCED_TYPENAME CORE_INDEX::const_iterator> \
  120. \
  121. >, \
  122. ::boost::mpl::na, \
  123. ::boost::mpl::na, \
  124. ::boost::bimaps::relation::detail:: \
  125. get_mutable_relation_functor< \
  126. BOOST_DEDUCED_TYPENAME CORE_INDEX::value_type >, \
  127. ::boost::bimaps::relation::support:: \
  128. get_above_view_functor< \
  129. BOOST_DEDUCED_TYPENAME CORE_INDEX::value_type > \
  130. >
  131. /*===========================================================================*/
  132. #if defined(BOOST_MSVC)
  133. /*===========================================================================*/
  134. #define BOOST_BIMAP_SET_VIEW_BASE_FRIEND(TYPE,INDEX_TYPE) \
  135. typedef ::boost::bimaps::detail::set_view_base< \
  136. TYPE< INDEX_TYPE >, INDEX_TYPE > template_class_friend; \
  137. friend class template_class_friend;
  138. /*===========================================================================*/
  139. #else
  140. /*===========================================================================*/
  141. #define BOOST_BIMAP_SET_VIEW_BASE_FRIEND(TYPE,INDEX_TYPE) \
  142. friend class ::boost::bimaps::detail::set_view_base< \
  143. TYPE< INDEX_TYPE >, INDEX_TYPE >;
  144. /*===========================================================================*/
  145. #endif
  146. /// \brief Common base for set views.
  147. template< class Derived, class Index >
  148. class set_view_base
  149. {
  150. typedef ::boost::bimaps::container_adaptor::support::
  151. iterator_facade_to_base
  152. <
  153. ::boost::bimaps::detail::
  154. set_view_iterator<BOOST_DEDUCED_TYPENAME Index:: iterator>,
  155. ::boost::bimaps::detail::
  156. const_set_view_iterator<BOOST_DEDUCED_TYPENAME Index::const_iterator>
  157. > iterator_to_base_;
  158. typedef BOOST_DEDUCED_TYPENAME Index::value_type::left_value_type left_type_;
  159. typedef BOOST_DEDUCED_TYPENAME Index::value_type::right_value_type right_type_;
  160. typedef BOOST_DEDUCED_TYPENAME Index::value_type value_type_;
  161. typedef ::boost::bimaps::detail::
  162. set_view_iterator<BOOST_DEDUCED_TYPENAME Index::iterator> iterator_;
  163. public:
  164. bool replace(iterator_ position,
  165. const value_type_ & x)
  166. {
  167. return derived().base().replace(
  168. derived().template functor<iterator_to_base_>()(position),x
  169. );
  170. }
  171. template< class CompatibleLeftType >
  172. bool replace_left(iterator_ position,
  173. const CompatibleLeftType & l)
  174. {
  175. return derived().base().replace(
  176. derived().template functor<iterator_to_base_>()(position),
  177. ::boost::bimaps::relation::detail::copy_with_left_replaced(*position,l)
  178. );
  179. }
  180. template< class CompatibleRightType >
  181. bool replace_right(iterator_ position,
  182. const CompatibleRightType & r)
  183. {
  184. return derived().base().replace(
  185. derived().template functor<iterator_to_base_>()(position),
  186. ::boost::bimaps::relation::detail::copy_with_right_replaced(*position,r)
  187. );
  188. }
  189. /* This function may be provided in the future
  190. template< class Modifier >
  191. bool modify(iterator_ position,
  192. Modifier mod)
  193. {
  194. return derived().base().modify(
  195. derived().template functor<iterator_to_base_>()(position),
  196. ::boost::bimaps::detail::relation_modifier_adaptor
  197. <
  198. Modifier,
  199. BOOST_DEDUCED_TYPENAME Index::value_type,
  200. BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
  201. data_extractor
  202. <
  203. ::boost::bimaps::relation::member_at::left,
  204. BOOST_DEDUCED_TYPENAME Index::value_type
  205. >::type,
  206. BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
  207. data_extractor
  208. <
  209. ::boost::bimaps::relation::member_at::right,
  210. BOOST_DEDUCED_TYPENAME Index::value_type
  211. >::type
  212. >(mod)
  213. );
  214. }
  215. */
  216. /*
  217. template< class Modifier >
  218. bool modify_left(iterator_ position, Modifier mod)
  219. {
  220. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
  221. data_extractor
  222. <
  223. BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::member_at::right,
  224. BOOST_DEDUCED_TYPENAME Index::value_type
  225. >::type left_data_extractor_;
  226. return derived().base().modify(
  227. derived().template functor<iterator_to_base_>()(position),
  228. // this may be replaced later by
  229. // ::boost::bind( mod, ::boost::bind(data_extractor_(),_1) )
  230. ::boost::bimaps::detail::unary_modifier_adaptor
  231. <
  232. Modifier,
  233. BOOST_DEDUCED_TYPENAME Index::value_type,
  234. left_data_extractor_
  235. >(mod)
  236. );
  237. }
  238. template< class Modifier >
  239. bool modify_right(iterator_ position, Modifier mod)
  240. {
  241. typedef BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::support::
  242. data_extractor
  243. <
  244. BOOST_DEDUCED_TYPENAME ::boost::bimaps::relation::member_at::right,
  245. BOOST_DEDUCED_TYPENAME Index::value_type
  246. >::type right_data_extractor_;
  247. return derived().base().modify(
  248. derived().template functor<iterator_to_base_>()(position),
  249. // this may be replaced later by
  250. // ::boost::bind( mod, ::boost::bind(data_extractor_(),_1) )
  251. ::boost::bimaps::detail::unary_modifier_adaptor
  252. <
  253. Modifier,
  254. BOOST_DEDUCED_TYPENAME Index::value_type,
  255. right_data_extractor_
  256. >(mod)
  257. );
  258. }
  259. */
  260. protected:
  261. typedef set_view_base set_view_base_;
  262. private:
  263. // Curiously Recurring Template interface.
  264. Derived& derived()
  265. {
  266. return *static_cast<Derived*>(this);
  267. }
  268. Derived const& derived() const
  269. {
  270. return *static_cast<Derived const*>(this);
  271. }
  272. };
  273. } // namespace detail
  274. } // namespace bimaps
  275. } // namespace boost
  276. #endif // BOOST_BIMAP_DETAIL_SET_VIEW_BASE_HPP