fix doc example typo
[boost.git] / boost / signals2 / deconstruct_ptr.hpp
blob841b19b2c199e55fe3911a36e8aae34fc3c162ee
1 // DEPRECATED in favor of adl_postconstruct and adl_predestruct with
2 // deconstruct<T>().
3 // A factory function for creating a shared_ptr that enhances the plain
4 // shared_ptr constructors by adding support for postconstructors
5 // and predestructors through the boost::signals2::postconstructible and
6 // boost::signals2::predestructible base classes.
7 //
8 // Copyright Frank Mori Hess 2007-2008.
9 //
10 // Use, modification and
11 // distribution is subject to the Boost Software License, Version
12 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 #ifndef BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
16 #define BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
18 #include <boost/assert.hpp>
19 #include <boost/checked_delete.hpp>
20 #include <boost/signals2/postconstructible.hpp>
21 #include <boost/signals2/predestructible.hpp>
22 #include <boost/shared_ptr.hpp>
24 namespace boost
26 namespace signals2
28 namespace detail
30 extern inline void do_postconstruct(const postconstructible *ptr)
32 postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
33 nonconst_ptr->postconstruct();
35 extern inline void do_postconstruct(...)
38 extern inline void do_predestruct(...)
41 extern inline void do_predestruct(const predestructible *ptr)
43 try
45 predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
46 nonconst_ptr->predestruct();
48 catch(...)
50 BOOST_ASSERT(false);
55 template<typename T> class predestructing_deleter
57 public:
58 void operator()(const T *ptr) const
60 detail::do_predestruct(ptr);
61 checked_delete(ptr);
65 template<typename T>
66 shared_ptr<T> deconstruct_ptr(T *ptr)
68 if(ptr == 0) return shared_ptr<T>(ptr);
69 shared_ptr<T> shared(ptr, boost::signals2::predestructing_deleter<T>());
70 detail::do_postconstruct(ptr);
71 return shared;
73 template<typename T, typename D>
74 shared_ptr<T> deconstruct_ptr(T *ptr, D deleter)
76 shared_ptr<T> shared(ptr, deleter);
77 if(ptr == 0) return shared;
78 detail::do_postconstruct(ptr);
79 return shared;
84 #endif // BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP