noinit_adaptor.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2019 Glen Joseph Fernandes
  3. ([email protected])
  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/core/allocator_access.hpp>
  10. namespace boost {
  11. template<class A>
  12. struct noinit_adaptor
  13. : A {
  14. typedef void _default_construct_destroy;
  15. template<class U>
  16. struct rebind {
  17. typedef noinit_adaptor<typename allocator_rebind<A, U>::type> other;
  18. };
  19. noinit_adaptor()
  20. : A() { }
  21. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  22. template<class U>
  23. noinit_adaptor(U&& u) BOOST_NOEXCEPT
  24. : A(std::forward<U>(u)) { }
  25. #else
  26. template<class U>
  27. noinit_adaptor(const U& u) BOOST_NOEXCEPT
  28. : A(u) { }
  29. template<class U>
  30. noinit_adaptor(U& u) BOOST_NOEXCEPT
  31. : A(u) { }
  32. #endif
  33. template<class U>
  34. noinit_adaptor(const noinit_adaptor<U>& u) BOOST_NOEXCEPT
  35. : A(static_cast<const A&>(u)) { }
  36. template<class U>
  37. void construct(U* p) {
  38. ::new((void*)p) U;
  39. }
  40. #if defined(BOOST_NO_CXX11_ALLOCATOR)
  41. template<class U, class V>
  42. void construct(U* p, const V& v) {
  43. ::new((void*)p) U(v);
  44. }
  45. #endif
  46. template<class U>
  47. void destroy(U* p) {
  48. p->~U();
  49. (void)p;
  50. }
  51. };
  52. template<class T, class U>
  53. inline bool
  54. operator==(const noinit_adaptor<T>& lhs,
  55. const noinit_adaptor<U>& rhs) BOOST_NOEXCEPT
  56. {
  57. return static_cast<const T&>(lhs) == static_cast<const U&>(rhs);
  58. }
  59. template<class T, class U>
  60. inline bool
  61. operator!=(const noinit_adaptor<T>& lhs,
  62. const noinit_adaptor<U>& rhs) BOOST_NOEXCEPT
  63. {
  64. return !(lhs == rhs);
  65. }
  66. template<class A>
  67. inline noinit_adaptor<A>
  68. noinit_adapt(const A& a) BOOST_NOEXCEPT
  69. {
  70. return noinit_adaptor<A>(a);
  71. }
  72. } /* boost */
  73. #endif