1 | /* |
---|---|
2 | Copyright 2012-2015 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/core/lightweight_test.hpp> |
9 | #include <boost/smart_ptr/enable_shared_from_this.hpp> |
10 | #include <boost/smart_ptr/make_shared.hpp> |
11 | |
12 | template<class T = void> |
13 | struct 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 | |
35 | template<class T, class U> |
36 | inline bool |
37 | operator==(const creator<T>&, const creator<U>&) |
38 | { |
39 | return true; |
40 | } |
41 | |
42 | template<class T, class U> |
43 | inline bool |
44 | operator!=(const creator<T>&, const creator<U>&) |
45 | { |
46 | return false; |
47 | } |
48 | |
49 | class type |
50 | : public boost::enable_shared_from_this<type> { |
51 | public: |
52 | static unsigned instances; |
53 | |
54 | type() { |
55 | ++instances; |
56 | } |
57 | |
58 | ~type() { |
59 | --instances; |
60 | } |
61 | |
62 | private: |
63 | type(const type&); |
64 | type& operator=(const type&); |
65 | }; |
66 | |
67 | unsigned type::instances = 0; |
68 | |
69 | int 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 |