Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / functional / factory / test / factory_allocator_throws.cpp
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/functional/factory.hpp>
9 #include <boost/core/lightweight_test.hpp>
10 #include <boost/smart_ptr/shared_ptr.hpp>
11
12 struct type {
13     explicit type(bool b) {
14         if (b) {
15             throw true;
16         }
17     }
18 };
19
20 template<class T>
21 class creator {
22 public:
23     static int count;
24
25     typedef T value_type;
26     typedef T* pointer;
27
28     template<class U>
29     struct rebind {
30         typedef creator<U> other;
31     };
32
33     creator() { }
34
35     template<class U>
36     creator(const creator<U>&) { }
37
38     T* allocate(std::size_t size) {
39         ++count;
40         return static_cast<T*>(::operator new(sizeof(T) * size));
41     }
42
43     void deallocate(T* ptr, std::size_t) {
44         --count;
45         ::operator delete(ptr);
46     }
47 };
48
49 template<class T>
50 int creator<T>::count = 0;
51
52 template<class T, class U>
53 inline bool
54 operator==(const creator<T>&, const creator<U>&)
55 {
56     return true;
57 }
58
59 template<class T, class U>
60 inline bool
61 operator!=(const creator<T>&, const creator<U>&)
62 {
63     return false;
64 }
65
66 int main()
67 {
68     bool b = true;
69     try {
70         boost::shared_ptr<type> s(boost::factory<boost::shared_ptr<type>,
71             creator<void>,
72             boost::factory_alloc_for_pointee_and_deleter>()(b));
73     } catch (...) {
74         BOOST_TEST(creator<type>::count == 0);
75     }
76     try {
77         boost::shared_ptr<type> s(boost::factory<boost::shared_ptr<type>,
78             creator<void>,
79             boost::factory_passes_alloc_to_smart_pointer>()(b));
80     } catch (...) {
81         BOOST_TEST(creator<type>::count == 0);
82     }
83     return boost::report_errors();
84 }
85