allocate_unique_noinit_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include <boost/config.hpp>
  8. #if (!defined(BOOST_LIBSTDCXX_VERSION) || \
  9. BOOST_LIBSTDCXX_VERSION >= 46000) && \
  10. !defined(BOOST_NO_CXX11_SMART_PTR)
  11. #include <boost/smart_ptr/allocate_unique.hpp>
  12. #include <boost/core/lightweight_test.hpp>
  13. template<class T = void>
  14. struct creator {
  15. typedef T value_type;
  16. typedef T* pointer;
  17. template<class U>
  18. struct rebind {
  19. typedef creator<U> other;
  20. };
  21. creator() { }
  22. template<class U>
  23. creator(const creator<U>&) { }
  24. T* allocate(std::size_t size) {
  25. return static_cast<T*>(::operator new(sizeof(T) * size));
  26. }
  27. void deallocate(T* ptr, std::size_t) {
  28. ::operator delete(ptr);
  29. }
  30. };
  31. template<class T, class U>
  32. inline bool
  33. operator==(const creator<T>&, const creator<U>&)
  34. {
  35. return true;
  36. }
  37. template<class T, class U>
  38. inline bool
  39. operator!=(const creator<T>&, const creator<U>&)
  40. {
  41. return false;
  42. }
  43. class type {
  44. public:
  45. static unsigned instances;
  46. type()
  47. : value_(0.0) {
  48. ++instances;
  49. }
  50. ~type() {
  51. --instances;
  52. }
  53. void set(long double value) {
  54. value_ = value;
  55. }
  56. long double get() const {
  57. return value_;
  58. }
  59. private:
  60. type(const type&);
  61. type& operator=(const type&);
  62. long double value_;
  63. };
  64. unsigned type::instances = 0;
  65. int main()
  66. {
  67. {
  68. std::unique_ptr<int,
  69. boost::alloc_deleter<int,
  70. boost::noinit_adaptor<creator<int> > > > result =
  71. boost::allocate_unique_noinit<int>(creator<int>());
  72. BOOST_TEST(result.get() != 0);
  73. }
  74. {
  75. std::unique_ptr<const int,
  76. boost::alloc_deleter<const int,
  77. boost::noinit_adaptor<creator<> > > > result =
  78. boost::allocate_unique_noinit<const int>(creator<>());
  79. BOOST_TEST(result.get() != 0);
  80. }
  81. {
  82. std::unique_ptr<type,
  83. boost::alloc_deleter<type,
  84. boost::noinit_adaptor<creator<type> > > > result =
  85. boost::allocate_unique_noinit<type>(creator<type>());
  86. BOOST_TEST(result.get() != 0);
  87. BOOST_TEST(type::instances == 1);
  88. result.reset();
  89. BOOST_TEST(type::instances == 0);
  90. }
  91. {
  92. std::unique_ptr<const type,
  93. boost::alloc_deleter<const type,
  94. boost::noinit_adaptor<creator<> > > > result =
  95. boost::allocate_unique_noinit<const type>(creator<>());
  96. BOOST_TEST(result.get() != 0);
  97. BOOST_TEST(type::instances == 1);
  98. result.reset();
  99. BOOST_TEST(type::instances == 0);
  100. }
  101. return boost::report_errors();
  102. }
  103. #else
  104. int main()
  105. {
  106. return 0;
  107. }
  108. #endif