1 /*=============================================================================
2 Copyright (c) 2007 Tobias Schwinger
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 ==============================================================================*/
9 #include <boost/functional/factory.hpp>
10 #include <boost/detail/lightweight_test.hpp>
14 #include <boost/shared_ptr.hpp>
22 sum(int a
, int b
) : val_sum(a
+ b
) { }
24 operator int() const { return this->val_sum
; }
27 template< typename T
>
28 class counting_allocator
: public std::allocator
<T
>
34 template< typename OtherT
>
35 struct rebind
{ typedef counting_allocator
<OtherT
> other
; };
37 template< typename OtherT
>
38 counting_allocator(counting_allocator
<OtherT
> const& that
)
41 static size_t n_allocated
;
42 T
* allocate(size_t n
, void const* hint
= 0l)
45 return std::allocator
<T
>::allocate(n
,hint
);
48 static size_t n_deallocated
;
49 void deallocate(T
* ptr
, size_t n
)
52 return std::allocator
<T
>::deallocate(ptr
,n
);
55 template< typename T
> size_t counting_allocator
<T
>::n_allocated
= 0;
56 template< typename T
> size_t counting_allocator
<T
>::n_deallocated
= 0;
62 boost::shared_ptr
<sum
> instance(
63 boost::factory
< boost::shared_ptr
<sum
>, counting_allocator
<void>,
64 boost::factory_alloc_for_pointee_and_deleter
>()(one
,two
) );
65 BOOST_TEST(*instance
== 3);
67 BOOST_TEST(counting_allocator
<sum
>::n_allocated
== 1);
68 BOOST_TEST(counting_allocator
<sum
>::n_deallocated
== 1);
70 boost::shared_ptr
<sum
> instance(
71 boost::factory
< boost::shared_ptr
<sum
>, counting_allocator
<void>,
72 boost::factory_passes_alloc_to_smart_pointer
>()(one
,two
) );
73 BOOST_TEST(*instance
== 3);
75 BOOST_TEST(counting_allocator
<sum
>::n_allocated
== 2);
76 BOOST_TEST(counting_allocator
<sum
>::n_deallocated
== 2);
77 return boost::report_errors();