]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2007 Tobias Schwinger | |
3 | ||
4 | Use modification and distribution are subject to the Boost Software | |
5 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
6 | http://www.boost.org/LICENSE_1_0.txt). | |
7 | ==============================================================================*/ | |
8 | ||
9 | #include <boost/functional/factory.hpp> | |
11fdf7f2 | 10 | #include <boost/core/lightweight_test.hpp> |
7c673cae FG |
11 | |
12 | #include <cstddef> | |
13 | #include <memory> | |
14 | #include <boost/shared_ptr.hpp> | |
15 | ||
11fdf7f2 TL |
16 | #ifdef BOOST_MSVC |
17 | // none of the deprecated members of std::allocate are used here | |
18 | # pragma warning(disable:4996) // Various members of std::allocator are deprecated in C++17 | |
19 | #endif | |
20 | ||
7c673cae FG |
21 | using std::size_t; |
22 | ||
23 | class sum | |
24 | { | |
25 | int val_sum; | |
26 | public: | |
27 | sum(int a, int b) : val_sum(a + b) { } | |
28 | ||
29 | operator int() const { return this->val_sum; } | |
30 | }; | |
31 | ||
32 | template< typename T > | |
33 | class counting_allocator : public std::allocator<T> | |
34 | { | |
35 | public: | |
36 | counting_allocator() | |
37 | { } | |
38 | ||
39 | template< typename OtherT > | |
40 | struct rebind { typedef counting_allocator<OtherT> other; }; | |
41 | ||
42 | template< typename OtherT > | |
43 | counting_allocator(counting_allocator<OtherT> const& that) | |
44 | { } | |
45 | ||
46 | static size_t n_allocated; | |
47 | T* allocate(size_t n, void const* hint = 0l) | |
48 | { | |
49 | n_allocated += 1; | |
50 | return std::allocator<T>::allocate(n,hint); | |
51 | } | |
52 | ||
53 | static size_t n_deallocated; | |
54 | void deallocate(T* ptr, size_t n) | |
55 | { | |
56 | n_deallocated += 1; | |
57 | return std::allocator<T>::deallocate(ptr,n); | |
58 | } | |
59 | }; | |
60 | template< typename T > size_t counting_allocator<T>::n_allocated = 0; | |
61 | template< typename T > size_t counting_allocator<T>::n_deallocated = 0; | |
62 | ||
63 | int main() | |
64 | { | |
65 | int one = 1, two = 2; | |
66 | { | |
67 | boost::shared_ptr<sum> instance( | |
68 | boost::factory< boost::shared_ptr<sum>, counting_allocator<void>, | |
69 | boost::factory_alloc_for_pointee_and_deleter >()(one,two) ); | |
70 | BOOST_TEST(*instance == 3); | |
71 | } | |
72 | BOOST_TEST(counting_allocator<sum>::n_allocated == 1); | |
73 | BOOST_TEST(counting_allocator<sum>::n_deallocated == 1); | |
74 | { | |
75 | boost::shared_ptr<sum> instance( | |
76 | boost::factory< boost::shared_ptr<sum>, counting_allocator<void>, | |
77 | boost::factory_passes_alloc_to_smart_pointer >()(one,two) ); | |
78 | BOOST_TEST(*instance == 3); | |
79 | } | |
80 | BOOST_TEST(counting_allocator<sum>::n_allocated == 2); | |
81 | BOOST_TEST(counting_allocator<sum>::n_deallocated == 2); | |
82 | return boost::report_errors(); | |
83 | } | |
84 |