allocators.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2018 Glen Joseph Fernandes
  2. // (glenjofe@gmail.com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_CIRCULAR_BUFFER_ALLOCATORS_HPP
  8. #define BOOST_CIRCULAR_BUFFER_ALLOCATORS_HPP
  9. #include <boost/config.hpp>
  10. #if defined(BOOST_NO_CXX11_ALLOCATOR)
  11. #define BOOST_CB_NO_CXX11_ALLOCATOR
  12. #elif defined(BOOST_LIBSTDCXX_VERSION) && (BOOST_LIBSTDCXX_VERSION < 40800)
  13. #define BOOST_CB_NO_CXX11_ALLOCATOR
  14. #endif
  15. #include <limits>
  16. #if !defined(BOOST_CB_NO_CXX11_ALLOCATOR)
  17. #include <memory>
  18. #else
  19. #include <new>
  20. #endif
  21. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  22. #include <utility>
  23. #endif
  24. namespace boost {
  25. namespace cb_details {
  26. #if !defined(BOOST_CB_NO_CXX11_ALLOCATOR)
  27. template<class A>
  28. struct allocator_traits
  29. : std::allocator_traits<A> {
  30. using typename std::allocator_traits<A>::value_type;
  31. using typename std::allocator_traits<A>::size_type;
  32. static size_type max_size(const A&) BOOST_NOEXCEPT {
  33. return (std::numeric_limits<size_type>::max)() / sizeof(value_type);
  34. }
  35. };
  36. #else
  37. template<class A>
  38. struct allocator_traits {
  39. typedef typename A::value_type value_type;
  40. typedef typename A::pointer pointer;
  41. typedef typename A::const_pointer const_pointer;
  42. typedef typename A::difference_type difference_type;
  43. typedef typename A::size_type size_type;
  44. static size_type max_size(const A&) BOOST_NOEXCEPT {
  45. return (std::numeric_limits<size_type>::max)() / sizeof(value_type);
  46. }
  47. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  48. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  49. template<class U, class... Args>
  50. static void construct(const A&, U* ptr, Args&&... args) {
  51. ::new((void*)ptr) U(std::forward<Args>(args)...);
  52. }
  53. #else
  54. template<class U, class V>
  55. static void construct(const A&, U* ptr, V&& value) {
  56. ::new((void*)ptr) U(std::forward<V>(value));
  57. }
  58. #endif
  59. #else
  60. template<class U, class V>
  61. static void construct(const A&, U* ptr, const V& value) {
  62. ::new((void*)ptr) U(value);
  63. }
  64. template<class U, class V>
  65. static void construct(const A&, U* ptr, V& value) {
  66. ::new((void*)ptr) U(value);
  67. }
  68. #endif
  69. template<class U>
  70. static void destroy(const A&, U* ptr) {
  71. (void)ptr;
  72. ptr->~U();
  73. }
  74. };
  75. #endif
  76. } // cb_details
  77. } // boost
  78. #endif