uses_allocator.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2011-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_USES_ALLOCATOR_HPP
  11. #define BOOST_CONTAINER_USES_ALLOCATOR_HPP
  12. #include <boost/container/uses_allocator_fwd.hpp>
  13. #include <boost/container/detail/type_traits.hpp>
  14. namespace boost {
  15. namespace container {
  16. //! <b>Remark</b>: if a specialization constructible_with_allocator_suffix<X>::value is true, indicates that T may be constructed
  17. //! with an allocator as its last constructor argument. Ideally, all constructors of T (including the
  18. //! copy and move constructors) should have a variant that accepts a final argument of
  19. //! allocator_type.
  20. //!
  21. //! <b>Requires</b>: if a specialization constructible_with_allocator_suffix<X>::value is true, T must have a nested type,
  22. //! allocator_type and at least one constructor for which allocator_type is the last
  23. //! parameter. If not all constructors of T can be called with a final allocator_type argument,
  24. //! and if T is used in a context where a container must call such a constructor, then the program is
  25. //! ill-formed.
  26. //!
  27. //! <code>
  28. //! template <class T, class Allocator = allocator<T> >
  29. //! class Z {
  30. //! public:
  31. //! typedef Allocator allocator_type;
  32. //!
  33. //! // Default constructor with optional allocator suffix
  34. //! Z(const allocator_type& a = allocator_type());
  35. //!
  36. //! // Copy constructor and allocator-extended copy constructor
  37. //! Z(const Z& zz);
  38. //! Z(const Z& zz, const allocator_type& a);
  39. //! };
  40. //!
  41. //! // Specialize trait for class template Z
  42. //! template <class T, class Allocator = allocator<T> >
  43. //! struct constructible_with_allocator_suffix<Z<T,Allocator> >
  44. //! { static const bool value = true; };
  45. //! </code>
  46. //!
  47. //! <b>Note</b>: This trait is a workaround inspired by "N2554: The Scoped A Model (Rev 2)"
  48. //! (Pablo Halpern, 2008-02-29) to backport the scoped allocator model to C++03, as
  49. //! in C++03 there is no mechanism to detect if a type can be constructed from arbitrary arguments.
  50. //! Applications aiming portability with several compilers should always define this trait.
  51. //!
  52. //! In conforming C++11 compilers or compilers supporting SFINAE expressions
  53. //! (when BOOST_NO_SFINAE_EXPR is NOT defined), this trait is ignored and C++11 rules will be used
  54. //! to detect if a type should be constructed with suffix or prefix allocator arguments.
  55. template <class T>
  56. struct constructible_with_allocator_suffix
  57. { static const bool value = false; };
  58. //! <b>Remark</b>: if a specialization constructible_with_allocator_prefix<X>::value is true, indicates that T may be constructed
  59. //! with allocator_arg and T::allocator_type as its first two constructor arguments.
  60. //! Ideally, all constructors of T (including the copy and move constructors) should have a variant
  61. //! that accepts these two initial arguments.
  62. //!
  63. //! <b>Requires</b>: specialization constructible_with_allocator_prefix<X>::value is true, T must have a nested type,
  64. //! allocator_type and at least one constructor for which allocator_arg_t is the first
  65. //! parameter and allocator_type is the second parameter. If not all constructors of T can be
  66. //! called with these initial arguments, and if T is used in a context where a container must call such
  67. //! a constructor, then the program is ill-formed.
  68. //!
  69. //! <code>
  70. //! template <class T, class Allocator = allocator<T> >
  71. //! class Y {
  72. //! public:
  73. //! typedef Allocator allocator_type;
  74. //!
  75. //! // Default constructor with and allocator-extended default constructor
  76. //! Y();
  77. //! Y(allocator_arg_t, const allocator_type& a);
  78. //!
  79. //! // Copy constructor and allocator-extended copy constructor
  80. //! Y(const Y& yy);
  81. //! Y(allocator_arg_t, const allocator_type& a, const Y& yy);
  82. //!
  83. //! // Variadic constructor and allocator-extended variadic constructor
  84. //! template<class ...Args> Y(Args&& args...);
  85. //! template<class ...Args>
  86. //! Y(allocator_arg_t, const allocator_type& a, BOOST_FWD_REF(Args)... args);
  87. //! };
  88. //!
  89. //! // Specialize trait for class template Y
  90. //! template <class T, class Allocator = allocator<T> >
  91. //! struct constructible_with_allocator_prefix<Y<T,Allocator> >
  92. //! { static const bool value = true; };
  93. //!
  94. //! </code>
  95. //!
  96. //! <b>Note</b>: This trait is a workaround inspired by "N2554: The Scoped Allocator Model (Rev 2)"
  97. //! (Pablo Halpern, 2008-02-29) to backport the scoped allocator model to C++03, as
  98. //! in C++03 there is no mechanism to detect if a type can be constructed from arbitrary arguments.
  99. //! Applications aiming portability with several compilers should always define this trait.
  100. //!
  101. //! In conforming C++11 compilers or compilers supporting SFINAE expressions
  102. //! (when BOOST_NO_SFINAE_EXPR is NOT defined), this trait is ignored and C++11 rules will be used
  103. //! to detect if a type should be constructed with suffix or prefix allocator arguments.
  104. template <class T>
  105. struct constructible_with_allocator_prefix
  106. { static const bool value = false; };
  107. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  108. namespace dtl {
  109. template<typename T, typename Allocator>
  110. struct uses_allocator_imp
  111. {
  112. // Use SFINAE (Substitution Failure Is Not An Error) to detect the
  113. // presence of an 'allocator_type' nested type convertilble from Allocator.
  114. private:
  115. typedef char yes_type;
  116. struct no_type{ char dummy[2]; };
  117. // Match this function if T::allocator_type exists and is
  118. // implicitly convertible from Allocator
  119. template <class U>
  120. static yes_type test(typename U::allocator_type);
  121. // Match this function if T::allocator_type exists and it's type is `erased_type`.
  122. template <class U, class V>
  123. static typename dtl::enable_if
  124. < dtl::is_same<typename U::allocator_type, erased_type>
  125. , yes_type
  126. >::type test(const V&);
  127. // Match this function if TypeT::allocator_type does not exist or is
  128. // not convertible from Allocator.
  129. template <typename U>
  130. static no_type test(...);
  131. static Allocator alloc; // Declared but not defined
  132. public:
  133. static const bool value = sizeof(test<T>(alloc)) == sizeof(yes_type);
  134. };
  135. } //namespace dtl {
  136. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  137. //! <b>Remark</b>: Automatically detects whether T has a nested allocator_type that is convertible from
  138. //! Allocator. Meets the BinaryTypeTrait requirements ([meta.rqmts] 20.4.1). A program may
  139. //! specialize this type to define uses_allocator<X>::value as true for a T of user-defined type if T does not
  140. //! have a nested allocator_type but is nonetheless constructible using the specified Allocator where either:
  141. //! the first argument of a constructor has type allocator_arg_t and the second argument has type Alloc or
  142. //! the last argument of a constructor has type Alloc.
  143. //!
  144. //! <b>Result</b>: uses_allocator<T, Allocator>::value== true if a type T::allocator_type
  145. //! exists and either is_convertible<Alloc, T::allocator_type>::value != false or T::allocator_type
  146. //! is an alias `erased_type`. False otherwise.
  147. template <typename T, typename Allocator>
  148. struct uses_allocator
  149. : dtl::uses_allocator_imp<T, Allocator>
  150. {};
  151. }} //namespace boost::container
  152. #endif //BOOST_CONTAINER_USES_ALLOCATOR_HPP