1/*
2Copyright 2012-2015 Glen Joseph Fernandes
3(glenjofe@gmail.com)
4
5Distributed under the Boost Software License, Version 1.0.
6(http://www.boost.org/LICENSE_1_0.txt)
7*/
8#include <boost/core/lightweight_test.hpp>
9#include <boost/smart_ptr/enable_shared_from_this.hpp>
10#include <boost/smart_ptr/make_shared.hpp>
11
12template<class T = void>
13struct creator {
14 typedef T value_type;
15
16 template<class U>
17 struct rebind {
18 typedef creator<U> other;
19 };
20
21 creator() { }
22
23 template<class U>
24 creator(const creator<U>&) { }
25
26 T* allocate(std::size_t size) {
27 return static_cast<T*>(::operator new(sizeof(T) * size));
28 }
29
30 void deallocate(T* ptr, std::size_t) {
31 ::operator delete(ptr);
32 }
33};
34
35template<class T, class U>
36inline bool
37operator==(const creator<T>&, const creator<U>&)
38{
39 return true;
40}
41
42template<class T, class U>
43inline bool
44operator!=(const creator<T>&, const creator<U>&)
45{
46 return false;
47}
48
49class type
50 : public boost::enable_shared_from_this<type> {
51public:
52 static unsigned instances;
53
54 type() {
55 ++instances;
56 }
57
58 ~type() {
59 --instances;
60 }
61
62private:
63 type(const type&);
64 type& operator=(const type&);
65};
66
67unsigned type::instances = 0;
68
69int main()
70{
71 BOOST_TEST(type::instances == 0);
72 {
73 boost::shared_ptr<type[]> result =
74 boost::allocate_shared<type[]>(allocator: creator<type>(), count: 3);
75 try {
76 result[0].shared_from_this();
77 BOOST_ERROR("shared_from_this did not throw");
78 } catch (...) {
79 BOOST_TEST(type::instances == 3);
80 }
81 }
82 BOOST_TEST(type::instances == 0);
83 {
84 boost::shared_ptr<type[]> result =
85 boost::allocate_shared_noinit<type[]>(allocator: creator<>(), count: 3);
86 try {
87 result[0].shared_from_this();
88 BOOST_ERROR("shared_from_this did not throw");
89 } catch (...) {
90 BOOST_TEST(type::instances == 3);
91 }
92 }
93 return boost::report_errors();
94}
95

source code of boost/libs/smart_ptr/test/allocate_shared_array_esft_test.cpp