propagation_test_allocator.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2015. 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_TEST_ALLOCATOR_ARGUMENT_TESTER_HPP
  11. #define BOOST_CONTAINER_TEST_ALLOCATOR_ARGUMENT_TESTER_HPP
  12. #include <boost/container/uses_allocator.hpp>
  13. #include <boost/container/detail/mpl.hpp>
  14. #include <boost/move/core.hpp>
  15. #include <boost/container/pmr/polymorphic_allocator.hpp>
  16. template<class T, unsigned int Id, bool HasTrueTypes = false>
  17. class propagation_test_allocator
  18. {
  19. BOOST_COPYABLE_AND_MOVABLE(propagation_test_allocator)
  20. public:
  21. template<class U>
  22. struct rebind
  23. {
  24. typedef propagation_test_allocator<U, Id, HasTrueTypes> other;
  25. };
  26. typedef boost::container::dtl::bool_<HasTrueTypes> propagate_on_container_copy_assignment;
  27. typedef boost::container::dtl::bool_<HasTrueTypes> propagate_on_container_move_assignment;
  28. typedef boost::container::dtl::bool_<HasTrueTypes> propagate_on_container_swap;
  29. typedef boost::container::dtl::bool_<HasTrueTypes> is_always_equal;
  30. typedef T value_type;
  31. propagation_test_allocator()
  32. : m_default_contructed(true), m_move_contructed(false), m_move_assigned(false)
  33. {}
  34. propagation_test_allocator(const propagation_test_allocator&)
  35. : m_default_contructed(false), m_move_contructed(false), m_move_assigned(false)
  36. {}
  37. propagation_test_allocator(BOOST_RV_REF(propagation_test_allocator) )
  38. : m_default_contructed(false), m_move_contructed(true), m_move_assigned(false)
  39. {}
  40. template<class U>
  41. propagation_test_allocator(BOOST_RV_REF_BEG propagation_test_allocator<U, Id, HasTrueTypes> BOOST_RV_REF_END)
  42. : m_default_contructed(false), m_move_contructed(true), m_move_assigned(false)
  43. {}
  44. template<class U>
  45. propagation_test_allocator(const propagation_test_allocator<U, Id, HasTrueTypes> &)
  46. {}
  47. propagation_test_allocator & operator=(BOOST_COPY_ASSIGN_REF(propagation_test_allocator))
  48. { return *this; }
  49. propagation_test_allocator & operator=(BOOST_RV_REF(propagation_test_allocator))
  50. {
  51. m_move_assigned = true;
  52. return *this;
  53. }
  54. std::size_t max_size() const
  55. { return std::size_t(-1); }
  56. T* allocate(std::size_t n)
  57. { return (T*)::new char[n*sizeof(T)]; }
  58. void deallocate(T*p, std::size_t)
  59. { delete []static_cast<char*>(static_cast<void*>(p)); }
  60. bool m_default_contructed;
  61. bool m_move_contructed;
  62. bool m_move_assigned;
  63. };
  64. template <class T1, class T2, unsigned int Id, bool HasTrueTypes>
  65. bool operator==( const propagation_test_allocator<T1, Id, HasTrueTypes>&
  66. , const propagation_test_allocator<T2, Id, HasTrueTypes>&)
  67. { return true; }
  68. template <class T1, class T2, unsigned int Id, bool HasTrueTypes>
  69. bool operator!=( const propagation_test_allocator<T1, Id, HasTrueTypes>&
  70. , const propagation_test_allocator<T2, Id, HasTrueTypes>&)
  71. { return false; }
  72. //This enum lists the construction options
  73. //for an allocator-aware type
  74. enum ConstructionTypeEnum
  75. {
  76. ConstructiblePrefix,
  77. ConstructibleSuffix,
  78. ErasedTypePrefix,
  79. ErasedTypeSuffix,
  80. NotUsesAllocator
  81. };
  82. //This base class provices types for
  83. //the derived class to implement each construction
  84. //type. If a construction type does not apply
  85. //the typedef is set to an internal nat
  86. //so that the class is not constructible from
  87. //the user arguments.
  88. template<ConstructionTypeEnum ConstructionType, unsigned int AllocatorTag>
  89. struct uses_allocator_base;
  90. template<unsigned int AllocatorTag>
  91. struct uses_allocator_base<ConstructibleSuffix, AllocatorTag>
  92. {
  93. typedef propagation_test_allocator<int, AllocatorTag> allocator_type;
  94. typedef allocator_type allocator_constructor_type;
  95. struct nat{};
  96. typedef nat allocator_arg_type;
  97. };
  98. template<unsigned int AllocatorTag>
  99. struct uses_allocator_base<ConstructiblePrefix, AllocatorTag>
  100. {
  101. typedef propagation_test_allocator<int, AllocatorTag> allocator_type;
  102. typedef allocator_type allocator_constructor_type;
  103. typedef boost::container::allocator_arg_t allocator_arg_type;
  104. };
  105. template<unsigned int AllocatorTag>
  106. struct uses_allocator_base<ErasedTypePrefix, AllocatorTag>
  107. {
  108. typedef boost::container::erased_type allocator_type;
  109. typedef boost::container::pmr::polymorphic_allocator<int> allocator_constructor_type;
  110. typedef boost::container::allocator_arg_t allocator_arg_type;
  111. };
  112. template<unsigned int AllocatorTag>
  113. struct uses_allocator_base<ErasedTypeSuffix, AllocatorTag>
  114. {
  115. typedef boost::container::erased_type allocator_type;
  116. typedef boost::container::pmr::polymorphic_allocator<int> allocator_constructor_type;
  117. struct nat{};
  118. typedef nat allocator_arg_type;
  119. };
  120. template<unsigned int AllocatorTag>
  121. struct uses_allocator_base<NotUsesAllocator, AllocatorTag>
  122. {
  123. struct nat{};
  124. typedef nat allocator_constructor_type;
  125. typedef nat allocator_arg_type;
  126. };
  127. template<ConstructionTypeEnum ConstructionType, unsigned int AllocatorTag>
  128. struct allocator_argument_tester
  129. : uses_allocator_base<ConstructionType, AllocatorTag>
  130. {
  131. private:
  132. BOOST_COPYABLE_AND_MOVABLE(allocator_argument_tester)
  133. public:
  134. typedef uses_allocator_base<ConstructionType, AllocatorTag> base_type;
  135. //0 user argument constructors
  136. allocator_argument_tester()
  137. : construction_type(NotUsesAllocator), value(0)
  138. {}
  139. explicit allocator_argument_tester
  140. (typename base_type::allocator_constructor_type)
  141. : construction_type(ConstructibleSuffix), value(0)
  142. {}
  143. explicit allocator_argument_tester
  144. (typename base_type::allocator_arg_type, typename base_type::allocator_constructor_type)
  145. : construction_type(ConstructiblePrefix), value(0)
  146. {}
  147. //1 user argument constructors
  148. explicit allocator_argument_tester(int i)
  149. : construction_type(NotUsesAllocator), value(i)
  150. {}
  151. allocator_argument_tester
  152. (int i, typename base_type::allocator_constructor_type)
  153. : construction_type(ConstructibleSuffix), value(i)
  154. {}
  155. allocator_argument_tester
  156. ( typename base_type::allocator_arg_type
  157. , typename base_type::allocator_constructor_type
  158. , int i)
  159. : construction_type(ConstructiblePrefix), value(i)
  160. {}
  161. //Copy constructors
  162. allocator_argument_tester(const allocator_argument_tester &other)
  163. : construction_type(NotUsesAllocator), value(other.value)
  164. {}
  165. allocator_argument_tester( const allocator_argument_tester &other
  166. , typename base_type::allocator_constructor_type)
  167. : construction_type(ConstructibleSuffix), value(other.value)
  168. {}
  169. allocator_argument_tester( typename base_type::allocator_arg_type
  170. , typename base_type::allocator_constructor_type
  171. , const allocator_argument_tester &other)
  172. : construction_type(ConstructiblePrefix), value(other.value)
  173. {}
  174. //Move constructors
  175. allocator_argument_tester(BOOST_RV_REF(allocator_argument_tester) other)
  176. : construction_type(NotUsesAllocator), value(other.value)
  177. { other.value = 0; other.construction_type = NotUsesAllocator; }
  178. allocator_argument_tester( BOOST_RV_REF(allocator_argument_tester) other
  179. , typename base_type::allocator_constructor_type)
  180. : construction_type(ConstructibleSuffix), value(other.value)
  181. { other.value = 0; other.construction_type = ConstructibleSuffix; }
  182. allocator_argument_tester( typename base_type::allocator_arg_type
  183. , typename base_type::allocator_constructor_type
  184. , BOOST_RV_REF(allocator_argument_tester) other)
  185. : construction_type(ConstructiblePrefix), value(other.value)
  186. { other.value = 0; other.construction_type = ConstructiblePrefix; }
  187. ConstructionTypeEnum construction_type;
  188. int value;
  189. };
  190. namespace boost {
  191. namespace container {
  192. template<unsigned int AllocatorTag>
  193. struct constructible_with_allocator_prefix
  194. < ::allocator_argument_tester<ConstructiblePrefix, AllocatorTag> >
  195. {
  196. static const bool value = true;
  197. };
  198. template<unsigned int AllocatorTag>
  199. struct constructible_with_allocator_prefix
  200. < ::allocator_argument_tester<ErasedTypePrefix, AllocatorTag> >
  201. {
  202. static const bool value = true;
  203. };
  204. template<unsigned int AllocatorTag>
  205. struct constructible_with_allocator_suffix
  206. < ::allocator_argument_tester<ConstructibleSuffix, AllocatorTag> >
  207. {
  208. static const bool value = true;
  209. };
  210. template<unsigned int AllocatorTag>
  211. struct constructible_with_allocator_suffix
  212. < ::allocator_argument_tester<ErasedTypeSuffix, AllocatorTag> >
  213. {
  214. static const bool value = true;
  215. };
  216. } //namespace container {
  217. } //namespace boost {
  218. #endif //BOOST_CONTAINER_TEST_ALLOCATOR_ARGUMENT_TESTER_HPP