• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
14 
15 #ifndef BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
16 #define BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
17 
18 #include <boost/assert.hpp>
19 #include <boost/checked_delete.hpp>
20 #include <boost/core/no_exceptions_support.hpp>
21 #include <boost/signals2/postconstructible.hpp>
22 #include <boost/signals2/predestructible.hpp>
23 #include <boost/shared_ptr.hpp>
24 
25 namespace boost
26 {
27   namespace signals2
28   {
29     namespace detail
30     {
do_postconstruct(const postconstructible * ptr)31       inline void do_postconstruct(const postconstructible *ptr)
32       {
33         postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
34         nonconst_ptr->postconstruct();
35       }
do_postconstruct(...)36       inline void do_postconstruct(...)
37       {
38       }
do_predestruct(...)39       inline void do_predestruct(...)
40       {
41       }
do_predestruct(const predestructible * ptr)42       inline void do_predestruct(const predestructible *ptr)
43       {
44         BOOST_TRY
45         {
46           predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
47           nonconst_ptr->predestruct();
48         }
49         BOOST_CATCH(...)
50         {
51           BOOST_ASSERT(false);
52         }
53         BOOST_CATCH_END
54       }
55     }
56 
57     template<typename T> class predestructing_deleter
58     {
59     public:
operator ()(const T * ptr) const60       void operator()(const T *ptr) const
61       {
62         detail::do_predestruct(ptr);
63         checked_delete(ptr);
64       }
65     };
66 
67     template<typename T>
deconstruct_ptr(T * ptr)68     shared_ptr<T> deconstruct_ptr(T *ptr)
69     {
70       if(ptr == 0) return shared_ptr<T>(ptr);
71       shared_ptr<T> shared(ptr, boost::signals2::predestructing_deleter<T>());
72       detail::do_postconstruct(ptr);
73       return shared;
74     }
75     template<typename T, typename D>
deconstruct_ptr(T * ptr,D deleter)76     shared_ptr<T> deconstruct_ptr(T *ptr, D deleter)
77     {
78       shared_ptr<T> shared(ptr, deleter);
79       if(ptr == 0) return shared;
80       detail::do_postconstruct(ptr);
81       return shared;
82     }
83   }
84 }
85 
86 #endif // BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
87