sequence_container_adaptor.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 container_adaptor/sequence_container_adaptor.hpp
  9. /// \brief Container adaptor to build a type that is compliant to the concept of a weak associative container.
  10. #ifndef BOOST_BIMAP_CONTAINER_ADAPTOR_SEQUENCE_CONTAINER_ADAPTOR_HPP
  11. #define BOOST_BIMAP_CONTAINER_ADAPTOR_SEQUENCE_CONTAINER_ADAPTOR_HPP
  12. #if defined(_MSC_VER)
  13. #pragma once
  14. #endif
  15. #include <boost/config.hpp>
  16. #include <utility>
  17. #include <boost/mpl/if.hpp>
  18. #include <boost/mpl/vector.hpp>
  19. #include <boost/mpl/aux_/na.hpp>
  20. #include <boost/bimap/container_adaptor/detail/identity_converters.hpp>
  21. #include <boost/bimap/container_adaptor/container_adaptor.hpp>
  22. #include <boost/call_traits.hpp>
  23. #include <boost/operators.hpp>
  24. namespace boost {
  25. namespace bimaps {
  26. namespace container_adaptor {
  27. #ifndef BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES
  28. template
  29. <
  30. class Base, class Iterator, class ConstIterator,
  31. class ReverseIterator, class ConstReverseIterator,
  32. class IteratorToBaseConverter, class IteratorFromBaseConverter,
  33. class ReverseIteratorFromBaseConverter,
  34. class ValueToBaseConverter, class ValueFromBaseConverter,
  35. class FunctorsFromDerivedClasses
  36. >
  37. struct sequence_container_adaptor_base
  38. {
  39. typedef container_adaptor
  40. <
  41. Base, Iterator, ConstIterator,
  42. IteratorToBaseConverter, IteratorFromBaseConverter,
  43. ValueToBaseConverter, ValueFromBaseConverter,
  44. BOOST_DEDUCED_TYPENAME mpl::push_front<
  45. FunctorsFromDerivedClasses,
  46. BOOST_DEDUCED_TYPENAME mpl::if_<
  47. ::boost::mpl::is_na<ReverseIteratorFromBaseConverter>,
  48. // {
  49. detail::iterator_from_base_identity
  50. <
  51. BOOST_DEDUCED_TYPENAME Base::reverse_iterator,
  52. ReverseIterator,
  53. BOOST_DEDUCED_TYPENAME Base::const_reverse_iterator,
  54. ConstReverseIterator
  55. >,
  56. // }
  57. // else
  58. // {
  59. ReverseIteratorFromBaseConverter
  60. // }
  61. >::type
  62. >::type
  63. > type;
  64. };
  65. #endif // BOOST_BIMAP_DOXYGEN_WILL_NOT_PROCESS_THE_FOLLOWING_LINES
  66. /// \brief Container adaptor to build a type that is compliant to the concept of a sequence container.
  67. template
  68. <
  69. class Base,
  70. class Iterator,
  71. class ConstIterator,
  72. class ReverseIterator,
  73. class ConstReverseIterator,
  74. class IteratorToBaseConverter = ::boost::mpl::na,
  75. class IteratorFromBaseConverter = ::boost::mpl::na,
  76. class ReverseIteratorFromBaseConverter = ::boost::mpl::na,
  77. class ValueToBaseConverter = ::boost::mpl::na,
  78. class ValueFromBaseConverter = ::boost::mpl::na,
  79. class FunctorsFromDerivedClasses = mpl::vector<>
  80. >
  81. class sequence_container_adaptor :
  82. public sequence_container_adaptor_base
  83. <
  84. Base, Iterator, ConstIterator,
  85. ReverseIterator, ConstReverseIterator,
  86. IteratorToBaseConverter, IteratorFromBaseConverter,
  87. ReverseIteratorFromBaseConverter,
  88. ValueToBaseConverter, ValueFromBaseConverter,
  89. FunctorsFromDerivedClasses
  90. >::type,
  91. ::boost::totally_ordered
  92. <
  93. sequence_container_adaptor
  94. <
  95. Base, Iterator, ConstIterator,
  96. ReverseIterator, ConstReverseIterator,
  97. IteratorToBaseConverter, IteratorFromBaseConverter,
  98. ReverseIteratorFromBaseConverter,
  99. ValueToBaseConverter, ValueFromBaseConverter,
  100. FunctorsFromDerivedClasses
  101. >
  102. >
  103. {
  104. typedef BOOST_DEDUCED_TYPENAME sequence_container_adaptor_base
  105. <
  106. Base, Iterator, ConstIterator,
  107. ReverseIterator, ConstReverseIterator,
  108. IteratorToBaseConverter, IteratorFromBaseConverter,
  109. ReverseIteratorFromBaseConverter,
  110. ValueToBaseConverter, ValueFromBaseConverter,
  111. FunctorsFromDerivedClasses
  112. >::type base_;
  113. // MetaData -------------------------------------------------------------
  114. public:
  115. typedef ReverseIterator reverse_iterator;
  116. typedef ConstReverseIterator const_reverse_iterator;
  117. protected:
  118. typedef BOOST_DEDUCED_TYPENAME mpl::if_<
  119. ::boost::mpl::is_na<ReverseIteratorFromBaseConverter>,
  120. // {
  121. detail::iterator_from_base_identity
  122. <
  123. BOOST_DEDUCED_TYPENAME Base::reverse_iterator,
  124. reverse_iterator,
  125. BOOST_DEDUCED_TYPENAME Base::const_reverse_iterator,
  126. const_reverse_iterator
  127. >,
  128. // }
  129. // else
  130. // {
  131. ReverseIteratorFromBaseConverter
  132. // }
  133. >::type reverse_iterator_from_base;
  134. // Access -----------------------------------------------------------------
  135. public:
  136. explicit sequence_container_adaptor(Base & c)
  137. : base_(c) {}
  138. protected:
  139. typedef sequence_container_adaptor sequence_container_adaptor_;
  140. // Interface --------------------------------------------------------------
  141. public:
  142. reverse_iterator rbegin()
  143. {
  144. return this->template functor<
  145. reverse_iterator_from_base
  146. >() ( this->base().rbegin() );
  147. }
  148. reverse_iterator rend()
  149. {
  150. return this->template functor<
  151. reverse_iterator_from_base
  152. >() ( this->base().rend() );
  153. }
  154. const_reverse_iterator rbegin() const
  155. {
  156. return this->template functor<
  157. reverse_iterator_from_base
  158. >() ( this->base().rbegin() );
  159. }
  160. const_reverse_iterator rend() const
  161. {
  162. return this->template functor<
  163. reverse_iterator_from_base
  164. >() ( this->base().rend() );
  165. }
  166. void resize(BOOST_DEDUCED_TYPENAME base_::size_type n,
  167. BOOST_DEDUCED_TYPENAME ::boost::call_traits<
  168. BOOST_DEDUCED_TYPENAME base_::value_type >::param_type x =
  169. BOOST_DEDUCED_TYPENAME base_::value_type())
  170. {
  171. this->base().resize(n,
  172. this->template functor<BOOST_DEDUCED_TYPENAME base_::value_to_base>()(x)
  173. );
  174. }
  175. BOOST_DEDUCED_TYPENAME base_::reference front()
  176. {
  177. return this->template functor<
  178. BOOST_DEDUCED_TYPENAME base_::value_from_base>()
  179. (
  180. this->base().front()
  181. );
  182. }
  183. BOOST_DEDUCED_TYPENAME base_::reference back()
  184. {
  185. return this->template functor<
  186. BOOST_DEDUCED_TYPENAME base_::value_from_base>()
  187. (
  188. this->base().back()
  189. );
  190. }
  191. BOOST_DEDUCED_TYPENAME base_::const_reference front() const
  192. {
  193. return this->template functor<
  194. BOOST_DEDUCED_TYPENAME base_::value_from_base>()
  195. (
  196. this->base().front()
  197. );
  198. }
  199. BOOST_DEDUCED_TYPENAME base_::const_reference back() const
  200. {
  201. return this->template functor<
  202. BOOST_DEDUCED_TYPENAME base_::value_from_base>()
  203. (
  204. this->base().back()
  205. );
  206. }
  207. void push_front(
  208. BOOST_DEDUCED_TYPENAME ::boost::call_traits<
  209. BOOST_DEDUCED_TYPENAME base_::value_type >::param_type x)
  210. {
  211. this->base().push_front(
  212. this->template functor<BOOST_DEDUCED_TYPENAME base_::value_to_base>()(x));
  213. }
  214. void pop_front()
  215. {
  216. this->base().pop_front();
  217. }
  218. void push_back(
  219. BOOST_DEDUCED_TYPENAME ::boost::call_traits<
  220. BOOST_DEDUCED_TYPENAME base_::value_type >::param_type x)
  221. {
  222. this->base().push_back(
  223. this->template functor<BOOST_DEDUCED_TYPENAME base_::value_to_base>()(x));
  224. }
  225. void pop_back()
  226. {
  227. this->base().pop_back();
  228. }
  229. std::pair<BOOST_DEDUCED_TYPENAME base_::iterator,bool>
  230. insert(BOOST_DEDUCED_TYPENAME base_::iterator position,
  231. BOOST_DEDUCED_TYPENAME ::boost::call_traits<
  232. BOOST_DEDUCED_TYPENAME base_::value_type >::param_type x)
  233. {
  234. std::pair< BOOST_DEDUCED_TYPENAME Base::iterator, bool > r(
  235. this->base().insert(
  236. this->template functor<
  237. BOOST_DEDUCED_TYPENAME base_::iterator_to_base>()(position),
  238. this->template functor<
  239. BOOST_DEDUCED_TYPENAME base_::value_to_base >()(x)
  240. )
  241. );
  242. return std::pair<BOOST_DEDUCED_TYPENAME base_::iterator, bool>(
  243. this->template functor<
  244. BOOST_DEDUCED_TYPENAME base_::iterator_from_base>()(r.first),
  245. r.second
  246. );
  247. }
  248. void insert(BOOST_DEDUCED_TYPENAME base_::iterator position,
  249. BOOST_DEDUCED_TYPENAME base_::size_type m,
  250. BOOST_DEDUCED_TYPENAME ::boost::call_traits<
  251. BOOST_DEDUCED_TYPENAME base_::value_type >::param_type x)
  252. {
  253. this->base().insert(
  254. this->template functor<
  255. BOOST_DEDUCED_TYPENAME base_::iterator_to_base>()(position),
  256. m,
  257. this->template functor<BOOST_DEDUCED_TYPENAME base_::value_to_base >()(x)
  258. );
  259. }
  260. template< class InputIterator >
  261. void insert(BOOST_DEDUCED_TYPENAME base_::iterator position,
  262. InputIterator first, InputIterator last)
  263. {
  264. // This is the same problem found in the insert function
  265. // of container_adaptor
  266. // For now, do the simple thing. This can be optimized
  267. for( ; first != last ; ++first )
  268. {
  269. this->base().insert(
  270. this->template functor<
  271. BOOST_DEDUCED_TYPENAME base_::iterator_to_base>()( position ),
  272. this->template functor<
  273. BOOST_DEDUCED_TYPENAME base_::value_to_base >()( *first )
  274. );
  275. }
  276. }
  277. // Totally ordered implementation
  278. bool operator==(const sequence_container_adaptor & c) const
  279. {
  280. return ( this->base() == c.base() );
  281. }
  282. bool operator<(const sequence_container_adaptor & c) const
  283. {
  284. return ( this->base() < c.base() );
  285. }
  286. };
  287. } // namespace container_adaptor
  288. } // namespace bimaps
  289. } // namespace boost
  290. #endif // BOOST_BIMAP_CONTAINER_ADAPTOR_SEQUENCE_CONTAINER_ADAPTOR_HPP