1 // DEPRECATED in favor of adl_postconstruct and adl_predestruct with
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.
8 // Copyright Frank Mori Hess 2007-2008.
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>
30 inline void do_postconstruct(const postconstructible *ptr)
32 postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
33 nonconst_ptr->postconstruct();
35 inline void do_postconstruct(...)
38 inline void do_predestruct(...)
41 inline void do_predestruct(const predestructible *ptr)
45 predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
46 nonconst_ptr->predestruct();
55 template<typename T> class predestructing_deleter
58 void operator()(const T *ptr) const
60 detail::do_predestruct(ptr);
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);
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);
84 #endif // BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP