associated_allocator.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // associated_allocator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_ASSOCIATED_ALLOCATOR_HPP
  11. #define ASIO_ASSOCIATED_ALLOCATOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <memory>
  17. #include "asio/associator.hpp"
  18. #include "asio/detail/functional.hpp"
  19. #include "asio/detail/type_traits.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. template <typename T, typename Allocator>
  23. struct associated_allocator;
  24. namespace detail {
  25. template <typename T, typename = void>
  26. struct has_allocator_type : false_type
  27. {
  28. };
  29. template <typename T>
  30. struct has_allocator_type<T,
  31. typename void_type<typename T::allocator_type>::type>
  32. : true_type
  33. {
  34. };
  35. template <typename T, typename E, typename = void, typename = void>
  36. struct associated_allocator_impl
  37. {
  38. typedef E type;
  39. static type get(const T&, const E& e) ASIO_NOEXCEPT
  40. {
  41. return e;
  42. }
  43. };
  44. template <typename T, typename E>
  45. struct associated_allocator_impl<T, E,
  46. typename void_type<typename T::allocator_type>::type>
  47. {
  48. typedef typename T::allocator_type type;
  49. static type get(const T& t, const E&) ASIO_NOEXCEPT
  50. {
  51. return t.get_allocator();
  52. }
  53. };
  54. template <typename T, typename E>
  55. struct associated_allocator_impl<T, E,
  56. typename enable_if<
  57. !has_allocator_type<T>::value
  58. >::type,
  59. typename void_type<
  60. typename associator<associated_allocator, T, E>::type
  61. >::type> : associator<associated_allocator, T, E>
  62. {
  63. };
  64. } // namespace detail
  65. /// Traits type used to obtain the allocator associated with an object.
  66. /**
  67. * A program may specialise this traits type if the @c T template parameter in
  68. * the specialisation is a user-defined type. The template parameter @c
  69. * Allocator shall be a type meeting the Allocator requirements.
  70. *
  71. * Specialisations shall meet the following requirements, where @c t is a const
  72. * reference to an object of type @c T, and @c a is an object of type @c
  73. * Allocator.
  74. *
  75. * @li Provide a nested typedef @c type that identifies a type meeting the
  76. * Allocator requirements.
  77. *
  78. * @li Provide a noexcept static member function named @c get, callable as @c
  79. * get(t) and with return type @c type.
  80. *
  81. * @li Provide a noexcept static member function named @c get, callable as @c
  82. * get(t,a) and with return type @c type.
  83. */
  84. template <typename T, typename Allocator = std::allocator<void> >
  85. struct associated_allocator
  86. {
  87. /// If @c T has a nested type @c allocator_type, <tt>T::allocator_type</tt>.
  88. /// Otherwise @c Allocator.
  89. #if defined(GENERATING_DOCUMENTATION)
  90. typedef see_below type;
  91. #else // defined(GENERATING_DOCUMENTATION)
  92. typedef typename detail::associated_allocator_impl<T, Allocator>::type type;
  93. #endif // defined(GENERATING_DOCUMENTATION)
  94. /// If @c T has a nested type @c allocator_type, returns
  95. /// <tt>t.get_allocator()</tt>. Otherwise returns @c a.
  96. static type get(const T& t,
  97. const Allocator& a = Allocator()) ASIO_NOEXCEPT
  98. {
  99. return detail::associated_allocator_impl<T, Allocator>::get(t, a);
  100. }
  101. };
  102. /// Helper function to obtain an object's associated allocator.
  103. /**
  104. * @returns <tt>associated_allocator<T>::get(t)</tt>
  105. */
  106. template <typename T>
  107. ASIO_NODISCARD inline typename associated_allocator<T>::type
  108. get_associated_allocator(const T& t) ASIO_NOEXCEPT
  109. {
  110. return associated_allocator<T>::get(t);
  111. }
  112. /// Helper function to obtain an object's associated allocator.
  113. /**
  114. * @returns <tt>associated_allocator<T, Allocator>::get(t, a)</tt>
  115. */
  116. template <typename T, typename Allocator>
  117. ASIO_NODISCARD inline typename associated_allocator<T, Allocator>::type
  118. get_associated_allocator(const T& t, const Allocator& a) ASIO_NOEXCEPT
  119. {
  120. return associated_allocator<T, Allocator>::get(t, a);
  121. }
  122. #if defined(ASIO_HAS_ALIAS_TEMPLATES)
  123. template <typename T, typename Allocator = std::allocator<void> >
  124. using associated_allocator_t
  125. = typename associated_allocator<T, Allocator>::type;
  126. #endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
  127. #if defined(ASIO_HAS_STD_REFERENCE_WRAPPER) \
  128. || defined(GENERATING_DOCUMENTATION)
  129. /// Specialisation of associated_allocator for @c std::reference_wrapper.
  130. template <typename T, typename Allocator>
  131. struct associated_allocator<reference_wrapper<T>, Allocator>
  132. {
  133. /// Forwards @c type to the associator specialisation for the unwrapped type
  134. /// @c T.
  135. typedef typename associated_allocator<T, Allocator>::type type;
  136. /// Forwards the request to get the allocator to the associator specialisation
  137. /// for the unwrapped type @c T.
  138. static type get(reference_wrapper<T> t,
  139. const Allocator& a = Allocator()) ASIO_NOEXCEPT
  140. {
  141. return associated_allocator<T, Allocator>::get(t.get(), a);
  142. }
  143. };
  144. #endif // defined(ASIO_HAS_STD_REFERENCE_WRAPPER)
  145. // || defined(GENERATING_DOCUMENTATION)
  146. } // namespace asio
  147. #include "asio/detail/pop_options.hpp"
  148. #endif // ASIO_ASSOCIATED_ALLOCATOR_HPP