ptr_inserter.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2008. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #ifndef BOOST_PTR_CONTAINER_PTR_INSERTER_HPP
  12. #define BOOST_PTR_CONTAINER_PTR_INSERTER_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. #pragma once
  15. #endif
  16. #include <boost/config.hpp>
  17. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  18. #include <iterator>
  19. #include <memory>
  20. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  21. #pragma GCC diagnostic push
  22. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  23. #endif
  24. namespace boost
  25. {
  26. namespace ptr_container
  27. {
  28. template< class PtrContainer >
  29. class ptr_back_insert_iterator;
  30. template< class PtrContainer >
  31. class ptr_front_insert_iterator;
  32. template< class PtrContainer >
  33. class ptr_insert_iterator;
  34. template< class PtrContainer >
  35. ptr_back_insert_iterator<PtrContainer>
  36. ptr_back_inserter( PtrContainer& cont );
  37. template< class PtrContainer >
  38. ptr_front_insert_iterator<PtrContainer>
  39. ptr_front_inserter( PtrContainer& cont );
  40. template< class PtrContainer >
  41. ptr_insert_iterator<PtrContainer>
  42. ptr_inserter( PtrContainer& cont, typename PtrContainer::iterator before );
  43. //////////////////////////////////////////////////////////////////////////
  44. // Implementation
  45. //////////////////////////////////////////////////////////////////////////
  46. template< class PtrContainer >
  47. class ptr_back_insert_iterator
  48. {
  49. public:
  50. typedef std::output_iterator_tag iterator_category;
  51. typedef void value_type;
  52. typedef void difference_type;
  53. typedef void pointer;
  54. typedef void reference;
  55. typedef PtrContainer container_type;
  56. public:
  57. explicit ptr_back_insert_iterator( PtrContainer& cont )
  58. : container(&cont)
  59. { }
  60. ptr_back_insert_iterator&
  61. operator=( typename PtrContainer::value_type r )
  62. {
  63. typename PtrContainer::value_type obj
  64. = container->null_policy_allocate_clone(r);
  65. container->push_back( obj );
  66. return *this;
  67. }
  68. #ifndef BOOST_NO_AUTO_PTR
  69. template< class T >
  70. ptr_back_insert_iterator&
  71. operator=( std::auto_ptr<T> r )
  72. {
  73. container->push_back( r );
  74. return *this;
  75. }
  76. #endif
  77. #ifndef BOOST_NO_CXX11_SMART_PTR
  78. template< class T >
  79. ptr_back_insert_iterator&
  80. operator=( std::unique_ptr<T> r )
  81. {
  82. container->push_back( std::move( r ) );
  83. return *this;
  84. }
  85. #endif
  86. ptr_back_insert_iterator&
  87. operator=( typename PtrContainer::const_reference r )
  88. {
  89. container->push_back( container->null_policy_allocate_clone(&r) );
  90. return *this;
  91. }
  92. ptr_back_insert_iterator& operator*()
  93. {
  94. return *this;
  95. }
  96. ptr_back_insert_iterator& operator++()
  97. {
  98. return *this;
  99. }
  100. ptr_back_insert_iterator operator++(int)
  101. {
  102. return *this;
  103. }
  104. protected:
  105. PtrContainer* container;
  106. };
  107. template< class PtrContainer >
  108. class ptr_front_insert_iterator
  109. {
  110. public:
  111. typedef std::output_iterator_tag iterator_category;
  112. typedef void value_type;
  113. typedef void difference_type;
  114. typedef void pointer;
  115. typedef void reference;
  116. typedef PtrContainer container_type;
  117. public:
  118. explicit ptr_front_insert_iterator( PtrContainer& cont )
  119. : container(&cont)
  120. { }
  121. ptr_front_insert_iterator&
  122. operator=( typename PtrContainer::value_type r )
  123. {
  124. typename PtrContainer::value_type obj
  125. = container->null_policy_allocate_clone(r);
  126. container->push_front( obj );
  127. return *this;
  128. }
  129. #ifndef BOOST_NO_AUTO_PTR
  130. template< class T >
  131. ptr_front_insert_iterator&
  132. operator=( std::auto_ptr<T> r )
  133. {
  134. container->push_front( r );
  135. return *this;
  136. }
  137. #endif
  138. #ifndef BOOST_NO_CXX11_SMART_PTR
  139. template< class T >
  140. ptr_front_insert_iterator&
  141. operator=( std::unique_ptr<T> r )
  142. {
  143. container->push_front( std::move( r ) );
  144. return *this;
  145. }
  146. #endif
  147. ptr_front_insert_iterator&
  148. operator=( typename PtrContainer::const_reference r )
  149. {
  150. container->push_front( container->null_policy_allocate_clone(&r) );
  151. return *this;
  152. }
  153. ptr_front_insert_iterator& operator*()
  154. {
  155. return *this;
  156. }
  157. ptr_front_insert_iterator& operator++()
  158. {
  159. return *this;
  160. }
  161. ptr_front_insert_iterator operator++(int)
  162. {
  163. return *this;
  164. }
  165. protected:
  166. PtrContainer* container;
  167. };
  168. template< class PtrContainer >
  169. class ptr_insert_iterator
  170. {
  171. public:
  172. typedef std::output_iterator_tag iterator_category;
  173. typedef void value_type;
  174. typedef void difference_type;
  175. typedef void pointer;
  176. typedef void reference;
  177. typedef PtrContainer container_type;
  178. public:
  179. ptr_insert_iterator( PtrContainer& cont,
  180. typename PtrContainer::iterator before )
  181. : container(&cont), iter(before)
  182. { }
  183. ptr_insert_iterator&
  184. operator=( typename PtrContainer::value_type r )
  185. {
  186. typename PtrContainer::value_type obj =
  187. container->null_policy_allocate_clone(r);
  188. iter = container->insert( iter, obj );
  189. return *this;
  190. }
  191. #ifndef BOOST_NO_AUTO_PTR
  192. template< class T >
  193. ptr_insert_iterator&
  194. operator=( std::auto_ptr<T> r )
  195. {
  196. iter = container->insert( iter, r );
  197. return *this;
  198. }
  199. #endif
  200. #ifndef BOOST_NO_CXX11_SMART_PTR
  201. template< class T >
  202. ptr_insert_iterator&
  203. operator=( std::unique_ptr<T> r )
  204. {
  205. iter = container->insert( iter, std::move( r ) );
  206. return *this;
  207. }
  208. #endif
  209. ptr_insert_iterator&
  210. operator=( typename PtrContainer::const_reference r )
  211. {
  212. iter = container->insert( iter,
  213. container->null_policy_allocate_clone(&r) );
  214. return *this;
  215. }
  216. ptr_insert_iterator& operator*()
  217. {
  218. return *this;
  219. }
  220. ptr_insert_iterator& operator++()
  221. {
  222. return *this;
  223. }
  224. ptr_insert_iterator operator++(int)
  225. {
  226. return *this;
  227. }
  228. protected:
  229. PtrContainer* container;
  230. typename PtrContainer::iterator iter;
  231. };
  232. template< class PtrContainer >
  233. inline ptr_back_insert_iterator<PtrContainer>
  234. ptr_back_inserter( PtrContainer& cont )
  235. {
  236. return ptr_back_insert_iterator<PtrContainer>( cont );
  237. }
  238. template< class PtrContainer >
  239. inline ptr_front_insert_iterator<PtrContainer>
  240. ptr_front_inserter( PtrContainer& cont )
  241. {
  242. return ptr_front_insert_iterator<PtrContainer>( cont );
  243. }
  244. template< class PtrContainer >
  245. inline ptr_insert_iterator<PtrContainer>
  246. ptr_inserter( PtrContainer& cont,
  247. typename PtrContainer::iterator before )
  248. {
  249. return ptr_insert_iterator<PtrContainer>( cont, before );
  250. }
  251. } // namespace 'ptr_container'
  252. } // namespace 'boost'
  253. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  254. #pragma GCC diagnostic pop
  255. #endif
  256. #endif