1 /*
2 Copyright 2019 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #include <boost/config.hpp>
9 #if (!defined(BOOST_LIBSTDCXX_VERSION) || \
10 BOOST_LIBSTDCXX_VERSION >= 46000) && \
11 !defined(BOOST_NO_CXX11_SMART_PTR)
12 #include <boost/smart_ptr/allocate_unique.hpp>
13 #include <boost/core/lightweight_test.hpp>
14
15 template<class T = void>
16 struct creator {
17 typedef T value_type;
18 typedef T* pointer;
19
20 template<class U>
21 struct rebind {
22 typedef creator<U> other;
23 };
24
creatorcreator25 creator() { }
26
27 template<class U>
creatorcreator28 creator(const creator<U>&) { }
29
allocatecreator30 T* allocate(std::size_t size) {
31 return static_cast<T*>(::operator new(sizeof(T) * size));
32 }
33
deallocatecreator34 void deallocate(T* ptr, std::size_t) {
35 ::operator delete(ptr);
36 }
37 };
38
39 template<class T, class U>
40 inline bool
operator ==(const creator<T> &,const creator<U> &)41 operator==(const creator<T>&, const creator<U>&)
42 {
43 return true;
44 }
45
46 template<class T, class U>
47 inline bool
operator !=(const creator<T> &,const creator<U> &)48 operator!=(const creator<T>&, const creator<U>&)
49 {
50 return false;
51 }
52
53 class type {
54 public:
55 static unsigned instances;
56
type()57 type()
58 : value_(0.0) {
59 ++instances;
60 }
61
~type()62 ~type() {
63 --instances;
64 }
65
set(long double value)66 void set(long double value) {
67 value_ = value;
68 }
69
get() const70 long double get() const {
71 return value_;
72 }
73
74 private:
75 type(const type&);
76 type& operator=(const type&);
77
78 long double value_;
79 };
80
81 unsigned type::instances = 0;
82
main()83 int main()
84 {
85 {
86 std::unique_ptr<int,
87 boost::alloc_deleter<int,
88 boost::noinit_adaptor<creator<int> > > > result =
89 boost::allocate_unique_noinit<int>(creator<int>());
90 BOOST_TEST(result.get() != 0);
91 }
92 {
93 std::unique_ptr<const int,
94 boost::alloc_deleter<const int,
95 boost::noinit_adaptor<creator<> > > > result =
96 boost::allocate_unique_noinit<const int>(creator<>());
97 BOOST_TEST(result.get() != 0);
98 }
99 {
100 std::unique_ptr<type,
101 boost::alloc_deleter<type,
102 boost::noinit_adaptor<creator<type> > > > result =
103 boost::allocate_unique_noinit<type>(creator<type>());
104 BOOST_TEST(result.get() != 0);
105 BOOST_TEST(type::instances == 1);
106 result.reset();
107 BOOST_TEST(type::instances == 0);
108 }
109 {
110 std::unique_ptr<const type,
111 boost::alloc_deleter<const type,
112 boost::noinit_adaptor<creator<> > > > result =
113 boost::allocate_unique_noinit<const type>(creator<>());
114 BOOST_TEST(result.get() != 0);
115 BOOST_TEST(type::instances == 1);
116 result.reset();
117 BOOST_TEST(type::instances == 0);
118 }
119 return boost::report_errors();
120 }
121 #else
main()122 int main()
123 {
124 return 0;
125 }
126 #endif
127