noinit_adaptor.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_CORE_NOINIT_ADAPTOR_HPP
  8. #define BOOST_CORE_NOINIT_ADAPTOR_HPP
  9. #include <boost/config.hpp>
  10. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  11. #include <memory>
  12. #endif
  13. #include <new>
  14. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  15. #include <utility>
  16. #endif
  17. namespace boost {
  18. template<class A>
  19. struct noinit_adaptor
  20. : A {
  21. template<class U>
  22. struct rebind {
  23. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  24. typedef noinit_adaptor<typename std::allocator_traits<A>::template
  25. rebind_alloc<U> > other;
  26. #else
  27. typedef noinit_adaptor<typename A::template rebind<U>::other> other;
  28. #endif
  29. };
  30. noinit_adaptor()
  31. : A() { }
  32. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  33. template<class U>
  34. noinit_adaptor(U&& u) BOOST_NOEXCEPT
  35. : A(std::forward<U>(u)) { }
  36. #else
  37. template<class U>
  38. noinit_adaptor(const U& u) BOOST_NOEXCEPT
  39. : A(u) { }
  40. #endif
  41. template<class U>
  42. noinit_adaptor(const noinit_adaptor<U>& u) BOOST_NOEXCEPT
  43. : A(static_cast<const U&>(u)) { }
  44. template<class U>
  45. void construct(U* p) {
  46. ::new((void*)p) U;
  47. }
  48. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  49. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  50. template<class U, class V, class... Args>
  51. void construct(U* p, V&& v, Args&&... args) {
  52. ::new((void*)p) U(std::forward<V>(v), std::forward<Args>(args)...);
  53. }
  54. #else
  55. template<class U, class V>
  56. void construct(U* p, V&& v) {
  57. ::new((void*)p) U(std::forward<V>(v));
  58. }
  59. #endif
  60. #else
  61. template<class U, class V>
  62. void construct(U* p, const V& v) {
  63. ::new((void*)p) U(v);
  64. }
  65. template<class U, class V>
  66. void construct(U* p, V& v) {
  67. ::new((void*)p) U(v);
  68. }
  69. #endif
  70. template<class U>
  71. void destroy(U* p) {
  72. p->~U();
  73. }
  74. };
  75. template<class T, class U>
  76. inline bool
  77. operator==(const noinit_adaptor<T>& lhs,
  78. const noinit_adaptor<U>& rhs) BOOST_NOEXCEPT
  79. {
  80. return static_cast<const T&>(lhs) == static_cast<const U&>(rhs);
  81. }
  82. template<class T, class U>
  83. inline bool
  84. operator!=(const noinit_adaptor<T>& lhs,
  85. const noinit_adaptor<U>& rhs) BOOST_NOEXCEPT
  86. {
  87. return !(lhs == rhs);
  88. }
  89. template<class A>
  90. inline noinit_adaptor<A>
  91. noinit_adapt(const A& a) BOOST_NOEXCEPT
  92. {
  93. return noinit_adaptor<A>(a);
  94. }
  95. } /* boost */
  96. #endif