newdelete_allocator.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright 2016-2017 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. #ifndef BOOST_POLY_COLLECTION_DETAIL_NEWDELETE_ALLOCATOR_HPP
  9. #define BOOST_POLY_COLLECTION_DETAIL_NEWDELETE_ALLOCATOR_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/poly_collection/detail/is_constructible.hpp>
  14. #include <memory>
  15. #include <new>
  16. #include <utility>
  17. namespace boost{
  18. namespace poly_collection{
  19. namespace detail{
  20. /* In order to comply with [container.requirements.general]/3,
  21. * newdelete_allocator_adaptor<Allocator> overrides
  22. * Allocator::construct/destroy with vanilla new/delete implementations.
  23. * Used therefore in all auxiliary internal structures.
  24. */
  25. template<typename Allocator>
  26. struct newdelete_allocator_adaptor:Allocator
  27. {
  28. using traits=std::allocator_traits<Allocator>;
  29. using value_type=typename traits::value_type;
  30. using size_type=typename traits::size_type;
  31. using difference_type=typename traits::difference_type;
  32. using pointer=typename traits::pointer;
  33. using const_pointer=typename traits::const_pointer;
  34. using void_pointer=typename traits::void_pointer;
  35. using const_void_pointer=typename traits::const_void_pointer;
  36. using propagate_on_container_copy_assignment=
  37. typename traits::propagate_on_container_copy_assignment;
  38. using propagate_on_container_move_assignment=
  39. typename traits::propagate_on_container_move_assignment;
  40. using propagate_on_container_swap=
  41. typename traits::propagate_on_container_swap;
  42. template<typename U>
  43. struct rebind
  44. {
  45. using other=newdelete_allocator_adaptor<
  46. typename traits::template rebind_alloc<U>>;
  47. };
  48. newdelete_allocator_adaptor()=default;
  49. newdelete_allocator_adaptor(const newdelete_allocator_adaptor&)=default;
  50. template<
  51. typename Allocator2,
  52. typename std::enable_if<
  53. is_constructible<Allocator,Allocator2>::value
  54. >::type* =nullptr
  55. >
  56. newdelete_allocator_adaptor(const Allocator2& x)noexcept:Allocator{x}{}
  57. template<
  58. typename Allocator2,
  59. typename std::enable_if<
  60. is_constructible<Allocator,Allocator2>::value
  61. >::type* =nullptr
  62. >
  63. newdelete_allocator_adaptor(
  64. const newdelete_allocator_adaptor<Allocator2>& x)noexcept:
  65. Allocator{static_cast<const Allocator2&>(x)}{}
  66. newdelete_allocator_adaptor& operator=(
  67. const newdelete_allocator_adaptor&)=default;
  68. template<typename T,typename... Args>
  69. void construct(T* p,Args&&... args)
  70. {
  71. ::new ((void*)p) T(std::forward<Args>(args)...);
  72. }
  73. template<typename T>
  74. void destroy(T* p)
  75. {
  76. p->~T();
  77. }
  78. };
  79. } /* namespace poly_collection::detail */
  80. } /* namespace poly_collection */
  81. } /* namespace boost */
  82. #endif