associated_allocator.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // associated_allocator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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 BOOST_ASIO_ASSOCIATED_ALLOCATOR_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <memory>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. template <typename T, typename E, typename = void>
  23. struct associated_allocator_impl
  24. {
  25. typedef E type;
  26. static type get(const T&, const E& e) BOOST_ASIO_NOEXCEPT
  27. {
  28. return e;
  29. }
  30. };
  31. template <typename T, typename E>
  32. struct associated_allocator_impl<T, E,
  33. typename void_type<typename T::allocator_type>::type>
  34. {
  35. typedef typename T::allocator_type type;
  36. static type get(const T& t, const E&) BOOST_ASIO_NOEXCEPT
  37. {
  38. return t.get_allocator();
  39. }
  40. };
  41. } // namespace detail
  42. /// Traits type used to obtain the allocator associated with an object.
  43. /**
  44. * A program may specialise this traits type if the @c T template parameter in
  45. * the specialisation is a user-defined type. The template parameter @c
  46. * Allocator shall be a type meeting the Allocator requirements.
  47. *
  48. * Specialisations shall meet the following requirements, where @c t is a const
  49. * reference to an object of type @c T, and @c a is an object of type @c
  50. * Allocator.
  51. *
  52. * @li Provide a nested typedef @c type that identifies a type meeting the
  53. * Allocator requirements.
  54. *
  55. * @li Provide a noexcept static member function named @c get, callable as @c
  56. * get(t) and with return type @c type.
  57. *
  58. * @li Provide a noexcept static member function named @c get, callable as @c
  59. * get(t,a) and with return type @c type.
  60. */
  61. template <typename T, typename Allocator = std::allocator<void> >
  62. struct associated_allocator
  63. {
  64. /// If @c T has a nested type @c allocator_type, <tt>T::allocator_type</tt>.
  65. /// Otherwise @c Allocator.
  66. #if defined(GENERATING_DOCUMENTATION)
  67. typedef see_below type;
  68. #else // defined(GENERATING_DOCUMENTATION)
  69. typedef typename detail::associated_allocator_impl<T, Allocator>::type type;
  70. #endif // defined(GENERATING_DOCUMENTATION)
  71. /// If @c T has a nested type @c allocator_type, returns
  72. /// <tt>t.get_allocator()</tt>. Otherwise returns @c a.
  73. static type get(const T& t,
  74. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  75. {
  76. return detail::associated_allocator_impl<T, Allocator>::get(t, a);
  77. }
  78. };
  79. /// Helper function to obtain an object's associated allocator.
  80. /**
  81. * @returns <tt>associated_allocator<T>::get(t)</tt>
  82. */
  83. template <typename T>
  84. inline typename associated_allocator<T>::type
  85. get_associated_allocator(const T& t) BOOST_ASIO_NOEXCEPT
  86. {
  87. return associated_allocator<T>::get(t);
  88. }
  89. /// Helper function to obtain an object's associated allocator.
  90. /**
  91. * @returns <tt>associated_allocator<T, Allocator>::get(t, a)</tt>
  92. */
  93. template <typename T, typename Allocator>
  94. inline typename associated_allocator<T, Allocator>::type
  95. get_associated_allocator(const T& t, const Allocator& a) BOOST_ASIO_NOEXCEPT
  96. {
  97. return associated_allocator<T, Allocator>::get(t, a);
  98. }
  99. #if defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  100. template <typename T, typename Allocator = std::allocator<void> >
  101. using associated_allocator_t
  102. = typename associated_allocator<T, Allocator>::type;
  103. #endif // defined(BOOST_ASIO_HAS_ALIAS_TEMPLATES)
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #endif // BOOST_ASIO_ASSOCIATED_ALLOCATOR_HPP