/boost_1_57_0/boost/ptr_container/ptr_inserter.hpp

http://github.com/MisterTea/HyperNEAT · C++ Header · 258 lines · 195 code · 50 blank · 13 comment · 7 complexity · 4fd3800038b88d92a487002e69e145b5 MD5 · raw file

  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 <iterator>
  18. #include <memory>
  19. namespace boost
  20. {
  21. namespace ptr_container
  22. {
  23. template< class PtrContainer >
  24. class ptr_back_insert_iterator;
  25. template< class PtrContainer >
  26. class ptr_front_insert_iterator;
  27. template< class PtrContainer >
  28. class ptr_insert_iterator;
  29. template< class PtrContainer >
  30. ptr_back_insert_iterator<PtrContainer>
  31. ptr_back_inserter( PtrContainer& cont );
  32. template< class PtrContainer >
  33. ptr_front_insert_iterator<PtrContainer>
  34. ptr_front_inserter( PtrContainer& cont );
  35. template< class PtrContainer >
  36. ptr_insert_iterator<PtrContainer>
  37. ptr_inserter( PtrContainer& cont, typename PtrContainer::iterator before );
  38. //////////////////////////////////////////////////////////////////////////
  39. // Implementation
  40. //////////////////////////////////////////////////////////////////////////
  41. template< class PtrContainer >
  42. class ptr_back_insert_iterator :
  43. public std::iterator<std::output_iterator_tag,void,void,void,void>
  44. {
  45. public:
  46. typedef PtrContainer container_type;
  47. public:
  48. explicit ptr_back_insert_iterator( PtrContainer& cont )
  49. : container(&cont)
  50. { }
  51. ptr_back_insert_iterator&
  52. operator=( typename PtrContainer::value_type r )
  53. {
  54. typename PtrContainer::value_type obj = 0;
  55. if( r != 0 )
  56. obj = container_type::clone_allocator_type::allocate_clone(*r);
  57. container->push_back( obj );
  58. return *this;
  59. }
  60. template< class T >
  61. ptr_back_insert_iterator&
  62. operator=( std::auto_ptr<T> r )
  63. {
  64. container->push_back( r );
  65. return *this;
  66. }
  67. ptr_back_insert_iterator&
  68. operator=( typename PtrContainer::const_reference r )
  69. {
  70. container->push_back( container_type::clone_allocator_type::
  71. allocate_clone(r) );
  72. return *this;
  73. }
  74. ptr_back_insert_iterator& operator*()
  75. {
  76. return *this;
  77. }
  78. ptr_back_insert_iterator& operator++()
  79. {
  80. return *this;
  81. }
  82. ptr_back_insert_iterator operator++(int)
  83. {
  84. return *this;
  85. }
  86. protected:
  87. PtrContainer* container;
  88. };
  89. template< class PtrContainer >
  90. class ptr_front_insert_iterator :
  91. public std::iterator<std::output_iterator_tag,void,void,void,void>
  92. {
  93. public:
  94. typedef PtrContainer container_type;
  95. public:
  96. explicit ptr_front_insert_iterator( PtrContainer& cont )
  97. : container(&cont)
  98. { }
  99. ptr_front_insert_iterator&
  100. operator=( typename PtrContainer::value_type r )
  101. {
  102. typename PtrContainer::value_type obj = 0;
  103. if( r != 0 )
  104. obj = container_type::clone_allocator_type::allocate_clone(*r);
  105. container->push_front( obj );
  106. return *this;
  107. }
  108. template< class T >
  109. ptr_front_insert_iterator&
  110. operator=( std::auto_ptr<T> r )
  111. {
  112. container->push_front( r );
  113. return *this;
  114. }
  115. ptr_front_insert_iterator&
  116. operator=( typename PtrContainer::const_reference r )
  117. {
  118. container->push_front( container_type::clone_allocator_type::
  119. allocate_clone(r) );
  120. return *this;
  121. }
  122. ptr_front_insert_iterator& operator*()
  123. {
  124. return *this;
  125. }
  126. ptr_front_insert_iterator& operator++()
  127. {
  128. return *this;
  129. }
  130. ptr_front_insert_iterator operator++(int)
  131. {
  132. return *this;
  133. }
  134. protected:
  135. PtrContainer* container;
  136. };
  137. template< class PtrContainer >
  138. class ptr_insert_iterator :
  139. public std::iterator<std::output_iterator_tag,void,void,void,void>
  140. {
  141. public:
  142. typedef PtrContainer container_type;
  143. public:
  144. ptr_insert_iterator( PtrContainer& cont,
  145. typename PtrContainer::iterator before )
  146. : container(&cont), iter(before)
  147. { }
  148. ptr_insert_iterator&
  149. operator=( typename PtrContainer::value_type r )
  150. {
  151. typename PtrContainer::value_type obj = 0;
  152. if( r != 0 )
  153. obj = container_type::clone_allocator_type::allocate_clone(*r);
  154. iter = container->insert( iter, obj );
  155. return *this;
  156. }
  157. template< class T >
  158. ptr_insert_iterator&
  159. operator=( std::auto_ptr<T> r )
  160. {
  161. iter = container->insert( iter, r );
  162. return *this;
  163. }
  164. ptr_insert_iterator&
  165. operator=( typename PtrContainer::const_reference r )
  166. {
  167. iter = container->insert( iter, container_type::clone_allocator_type::
  168. allocate_clone(r) );
  169. return *this;
  170. }
  171. ptr_insert_iterator& operator*()
  172. {
  173. return *this;
  174. }
  175. ptr_insert_iterator& operator++()
  176. {
  177. return *this;
  178. }
  179. ptr_insert_iterator operator++(int)
  180. {
  181. return *this;
  182. }
  183. protected:
  184. PtrContainer* container;
  185. typename PtrContainer::iterator iter;
  186. };
  187. template< class PtrContainer >
  188. inline ptr_back_insert_iterator<PtrContainer>
  189. ptr_back_inserter( PtrContainer& cont )
  190. {
  191. return ptr_back_insert_iterator<PtrContainer>( cont );
  192. }
  193. template< class PtrContainer >
  194. inline ptr_front_insert_iterator<PtrContainer>
  195. ptr_front_inserter( PtrContainer& cont )
  196. {
  197. return ptr_front_insert_iterator<PtrContainer>( cont );
  198. }
  199. template< class PtrContainer >
  200. inline ptr_insert_iterator<PtrContainer>
  201. ptr_inserter( PtrContainer& cont,
  202. typename PtrContainer::iterator before )
  203. {
  204. return ptr_insert_iterator<PtrContainer>( cont, before );
  205. }
  206. } // namespace 'ptr_container'
  207. } // namespace 'boost'
  208. #endif