| 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/core/noinit_adaptor.hpp> |
| 9 | #include <boost/core/default_allocator.hpp> |
| 10 | #include <boost/core/lightweight_test.hpp> |
| 11 | #include <vector> |
| 12 | |
| 13 | template<class T> |
| 14 | class creator |
| 15 | : public boost::default_allocator<T> { |
| 16 | public: |
| 17 | template<class U> |
| 18 | struct rebind { |
| 19 | typedef creator<U> other; |
| 20 | }; |
| 21 | |
| 22 | creator(int state) |
| 23 | : state_(state) { } |
| 24 | |
| 25 | template<class U> |
| 26 | creator(const creator<U>& other) |
| 27 | : state_(other.state()) { } |
| 28 | |
| 29 | template<class U, class V> |
| 30 | void construct(U*, const V&) { |
| 31 | BOOST_ERROR("construct"); |
| 32 | } |
| 33 | |
| 34 | template<class U> |
| 35 | void destroy(U*) { |
| 36 | BOOST_ERROR("destroy"); |
| 37 | } |
| 38 | |
| 39 | int state() const { |
| 40 | return state_; |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | int state_; |
| 45 | }; |
| 46 | |
| 47 | template<class T, class U> |
| 48 | inline bool |
| 49 | operator==(const creator<T>& lhs, const creator<U>& rhs) |
| 50 | { |
| 51 | return lhs.state() == rhs.state(); |
| 52 | } |
| 53 | |
| 54 | template<class T, class U> |
| 55 | inline bool |
| 56 | operator!=(const creator<T>& lhs, const creator<U>& rhs) |
| 57 | { |
| 58 | return !(lhs == rhs); |
| 59 | } |
| 60 | |
| 61 | class type { |
| 62 | public: |
| 63 | type() { } |
| 64 | |
| 65 | type(int value) |
| 66 | : value_(value) { } |
| 67 | |
| 68 | int value() const { |
| 69 | return value_; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | int value_; |
| 74 | }; |
| 75 | |
| 76 | inline bool |
| 77 | operator==(const type& lhs, const type& rhs) |
| 78 | { |
| 79 | return lhs.value() == rhs.value(); |
| 80 | } |
| 81 | |
| 82 | template<class A> |
| 83 | void test(const A& allocator) |
| 84 | { |
| 85 | std::vector<typename A::value_type, A> v(allocator); |
| 86 | v.push_back(1); |
| 87 | BOOST_TEST(v.front() == 1); |
| 88 | v.clear(); |
| 89 | v.resize(5); |
| 90 | v.front() = 1; |
| 91 | } |
| 92 | |
| 93 | int main() |
| 94 | { |
| 95 | test(allocator: boost::noinit_adaptor<creator<int> >(1)); |
| 96 | test(allocator: boost::noinit_adaptor<creator<type> >(2)); |
| 97 | test(allocator: boost::noinit_adapt(a: creator<int>(3))); |
| 98 | test(allocator: boost::noinit_adapt(a: creator<type>(4))); |
| 99 | return boost::report_errors(); |
| 100 | } |
| 101 |
