allocators.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #if !defined(BOOST_CB_NO_CXX11_ALLOCATOR)
  16. #include <memory>
  17. #else
  18. #include <new>
  19. #endif
  20. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  21. #include <utility>
  22. #endif
  23. namespace boost {
  24. namespace cb_details {
  25. #if !defined(BOOST_CB_NO_CXX11_ALLOCATOR)
  26. using std::allocator_traits;
  27. #else
  28. template<class A>
  29. struct allocator_traits {
  30. typedef typename A::value_type value_type;
  31. typedef typename A::pointer pointer;
  32. typedef typename A::const_pointer const_pointer;
  33. typedef typename A::difference_type difference_type;
  34. typedef typename A::size_type size_type;
  35. static size_type max_size(const A& a) BOOST_NOEXCEPT {
  36. return a.max_size();
  37. }
  38. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  39. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  40. template<class U, class... Args>
  41. static void construct(const A&, U* ptr, Args&&... args) {
  42. ::new((void*)ptr) U(std::forward<Args>(args)...);
  43. }
  44. #else
  45. template<class U, class V>
  46. static void construct(const A&, U* ptr, V&& value) {
  47. ::new((void*)ptr) U(std::forward<V>(value));
  48. }
  49. #endif
  50. #else
  51. template<class U, class V>
  52. static void construct(const A&, U* ptr, const V& value) {
  53. ::new((void*)ptr) U(value);
  54. }
  55. template<class U, class V>
  56. static void construct(const A&, U* ptr, V& value) {
  57. ::new((void*)ptr) U(value);
  58. }
  59. #endif
  60. template<class U>
  61. static void destroy(const A&, U* ptr) {
  62. (void)ptr;
  63. ptr->~U();
  64. }
  65. };
  66. #endif
  67. } // cb_details
  68. } // boost
  69. #endif