associated_allocator.hpp 3.8 KB

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