allocator_traits.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright Andrey Semashev 2018.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file allocator_traits.hpp
  9. * \author Andrey Semashev
  10. * \date 03.01.2018
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_ALLOCATOR_TRAITS_HPP_INCLUDED_
  16. #define BOOST_LOG_ALLOCATOR_TRAITS_HPP_INCLUDED_
  17. #include <memory>
  18. #include <boost/log/detail/config.hpp>
  19. #if defined(BOOST_NO_CXX11_ALLOCATOR)
  20. #include <boost/container/allocator_traits.hpp>
  21. #endif
  22. #include <boost/log/utility/use_std_allocator.hpp>
  23. #include <boost/log/detail/header.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. namespace boost {
  28. BOOST_LOG_OPEN_NAMESPACE
  29. namespace aux {
  30. // A portable name for allocator traits
  31. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  32. using std::allocator_traits;
  33. #else
  34. using boost::container::allocator_traits;
  35. #endif
  36. /*!
  37. * \brief A standalone trait to rebind an allocator to another type.
  38. *
  39. * The important difference from <tt>std::allocator_traits&lt;Alloc&gt;::rebind_alloc&lt;U&gt;</tt> is that this
  40. * trait does not require template aliases and thus is compatible with C++03. There is
  41. * <tt>boost::container::allocator_traits&lt;Alloc&gt;::portable_rebind_alloc&lt;U&gt;</tt>, but it is not present in <tt>std::allocator_traits</tt>.
  42. */
  43. template< typename Allocator, typename U >
  44. struct rebind_alloc
  45. {
  46. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  47. typedef typename std::allocator_traits< Allocator >::BOOST_NESTED_TEMPLATE rebind_alloc< U > type;
  48. #else
  49. typedef typename boost::container::allocator_traits< Allocator >::BOOST_NESTED_TEMPLATE portable_rebind_alloc< U >::type type;
  50. #endif
  51. };
  52. /*!
  53. * This specialization mostly exists to keep <tt>std::allocator&lt;void&gt;</tt> working.
  54. * The default template will attempt to instantiate the allocator type to test if it provides the nested <tt>rebind</tt> template.
  55. * We don't want that to happen because it prohibits using <tt>std::allocator&lt;void&gt;</tt> in C++17 and later, which deprecated
  56. * this allocator specialization. This specialization does not use the nested <tt>rebind</tt> template in this case.
  57. */
  58. template< typename T, typename U >
  59. struct rebind_alloc< std::allocator< T >, U >
  60. {
  61. typedef std::allocator< U > type;
  62. };
  63. template< typename U >
  64. struct rebind_alloc< use_std_allocator, U >
  65. {
  66. typedef std::allocator< U > type;
  67. };
  68. } // namespace aux
  69. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  70. } // namespace boost
  71. #include <boost/log/detail/footer.hpp>
  72. #endif // BOOST_LOG_ALLOCATOR_TRAITS_HPP_INCLUDED_