dummy_test_allocator.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP
  11. #define BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/container_fwd.hpp>
  21. #include <boost/container/throw_exception.hpp>
  22. #include <boost/container/detail/addressof.hpp>
  23. #include <boost/container/detail/allocation_type.hpp>
  24. #include <boost/container/detail/mpl.hpp>
  25. #include <boost/container/detail/multiallocation_chain.hpp>
  26. #include <boost/container/detail/type_traits.hpp>
  27. #include <boost/container/detail/version_type.hpp>
  28. #include <boost/move/utility_core.hpp>
  29. #include <boost/move/adl_move_swap.hpp>
  30. #include <boost/assert.hpp>
  31. #include <memory>
  32. #include <algorithm>
  33. #include <cstddef>
  34. #include <cassert>
  35. namespace boost {
  36. namespace container {
  37. namespace test {
  38. //Very simple version 1 allocator
  39. template<class T>
  40. class simple_allocator
  41. {
  42. public:
  43. typedef T value_type;
  44. simple_allocator()
  45. {}
  46. template<class U>
  47. simple_allocator(const simple_allocator<U> &)
  48. {}
  49. T* allocate(std::size_t n)
  50. { return (T*)::new char[sizeof(T)*n]; }
  51. void deallocate(T*p, std::size_t)
  52. { delete[] ((char*)p);}
  53. friend bool operator==(const simple_allocator &, const simple_allocator &)
  54. { return true; }
  55. friend bool operator!=(const simple_allocator &, const simple_allocator &)
  56. { return false; }
  57. };
  58. template< class T
  59. , bool PropagateOnContCopyAssign
  60. , bool PropagateOnContMoveAssign
  61. , bool PropagateOnContSwap
  62. , bool CopyOnPropagateOnContSwap
  63. >
  64. class propagation_test_allocator
  65. {
  66. BOOST_COPYABLE_AND_MOVABLE(propagation_test_allocator)
  67. public:
  68. typedef T value_type;
  69. typedef boost::container::dtl::bool_<PropagateOnContCopyAssign>
  70. propagate_on_container_copy_assignment;
  71. typedef boost::container::dtl::bool_<PropagateOnContMoveAssign>
  72. propagate_on_container_move_assignment;
  73. typedef boost::container::dtl::bool_<PropagateOnContSwap>
  74. propagate_on_container_swap;
  75. template<class T2>
  76. struct rebind
  77. { typedef propagation_test_allocator
  78. < T2
  79. , PropagateOnContCopyAssign
  80. , PropagateOnContMoveAssign
  81. , PropagateOnContSwap
  82. , CopyOnPropagateOnContSwap> other;
  83. };
  84. propagation_test_allocator select_on_container_copy_construction() const
  85. { return CopyOnPropagateOnContSwap ? propagation_test_allocator(*this) : propagation_test_allocator(); }
  86. explicit propagation_test_allocator()
  87. : id_(++unique_id_)
  88. , ctr_copies_(0)
  89. , ctr_moves_(0)
  90. , assign_copies_(0)
  91. , assign_moves_(0)
  92. , swaps_(0)
  93. {}
  94. propagation_test_allocator(const propagation_test_allocator &x)
  95. : id_(x.id_)
  96. , ctr_copies_(x.ctr_copies_+1)
  97. , ctr_moves_(x.ctr_moves_)
  98. , assign_copies_(x.assign_copies_)
  99. , assign_moves_(x.assign_moves_)
  100. , swaps_(x.swaps_)
  101. {}
  102. template<class U>
  103. propagation_test_allocator(const propagation_test_allocator
  104. < U
  105. , PropagateOnContCopyAssign
  106. , PropagateOnContMoveAssign
  107. , PropagateOnContSwap
  108. , CopyOnPropagateOnContSwap> &x)
  109. : id_(x.id_)
  110. , ctr_copies_(x.ctr_copies_+1)
  111. , ctr_moves_(0)
  112. , assign_copies_(0)
  113. , assign_moves_(0)
  114. , swaps_(0)
  115. {}
  116. propagation_test_allocator(BOOST_RV_REF(propagation_test_allocator) x)
  117. : id_(x.id_)
  118. , ctr_copies_(x.ctr_copies_)
  119. , ctr_moves_(x.ctr_moves_ + 1)
  120. , assign_copies_(x.assign_copies_)
  121. , assign_moves_(x.assign_moves_)
  122. , swaps_(x.swaps_)
  123. {}
  124. propagation_test_allocator &operator=(BOOST_COPY_ASSIGN_REF(propagation_test_allocator) x)
  125. {
  126. id_ = x.id_;
  127. ctr_copies_ = x.ctr_copies_;
  128. ctr_moves_ = x.ctr_moves_;
  129. assign_copies_ = x.assign_copies_+1;
  130. assign_moves_ = x.assign_moves_;
  131. swaps_ = x.swaps_;
  132. return *this;
  133. }
  134. propagation_test_allocator &operator=(BOOST_RV_REF(propagation_test_allocator) x)
  135. {
  136. id_ = x.id_;
  137. ctr_copies_ = x.ctr_copies_;
  138. ctr_moves_ = x.ctr_moves_;
  139. assign_copies_ = x.assign_copies_;
  140. assign_moves_ = x.assign_moves_+1;
  141. swaps_ = x.swaps_;
  142. return *this;
  143. }
  144. static void reset_unique_id(unsigned id = 0)
  145. { unique_id_ = id; }
  146. T* allocate(std::size_t n)
  147. { return (T*)::new char[sizeof(T)*n]; }
  148. void deallocate(T*p, std::size_t)
  149. { delete[] ((char*)p);}
  150. friend bool operator==(const propagation_test_allocator &, const propagation_test_allocator &)
  151. { return true; }
  152. friend bool operator!=(const propagation_test_allocator &, const propagation_test_allocator &)
  153. { return false; }
  154. void swap(propagation_test_allocator &r)
  155. {
  156. ++this->swaps_; ++r.swaps_;
  157. boost::adl_move_swap(this->id_, r.id_);
  158. boost::adl_move_swap(this->ctr_copies_, r.ctr_copies_);
  159. boost::adl_move_swap(this->ctr_moves_, r.ctr_moves_);
  160. boost::adl_move_swap(this->assign_copies_, r.assign_copies_);
  161. boost::adl_move_swap(this->assign_moves_, r.assign_moves_);
  162. boost::adl_move_swap(this->swaps_, r.swaps_);
  163. }
  164. friend void swap(propagation_test_allocator &l, propagation_test_allocator &r)
  165. {
  166. l.swap(r);
  167. }
  168. unsigned int id_;
  169. unsigned int ctr_copies_;
  170. unsigned int ctr_moves_;
  171. unsigned int assign_copies_;
  172. unsigned int assign_moves_;
  173. unsigned int swaps_;
  174. static unsigned unique_id_;
  175. };
  176. template< class T
  177. , bool PropagateOnContCopyAssign
  178. , bool PropagateOnContMoveAssign
  179. , bool PropagateOnContSwap
  180. , bool CopyOnPropagateOnContSwap
  181. >
  182. unsigned int propagation_test_allocator< T
  183. , PropagateOnContCopyAssign
  184. , PropagateOnContMoveAssign
  185. , PropagateOnContSwap
  186. , CopyOnPropagateOnContSwap>::unique_id_ = 0;
  187. } //namespace test {
  188. } //namespace container {
  189. } //namespace boost {
  190. #include <boost/container/detail/config_end.hpp>
  191. #endif //BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP