Chris@16
|
1 // DEPRECATED in favor of adl_postconstruct and adl_predestruct with
|
Chris@16
|
2 // deconstruct<T>().
|
Chris@16
|
3 // A factory function for creating a shared_ptr that enhances the plain
|
Chris@16
|
4 // shared_ptr constructors by adding support for postconstructors
|
Chris@16
|
5 // and predestructors through the boost::signals2::postconstructible and
|
Chris@16
|
6 // boost::signals2::predestructible base classes.
|
Chris@16
|
7 //
|
Chris@16
|
8 // Copyright Frank Mori Hess 2007-2008.
|
Chris@16
|
9 //
|
Chris@16
|
10 // Use, modification and
|
Chris@16
|
11 // distribution is subject to the Boost Software License, Version
|
Chris@16
|
12 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
13 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
|
Chris@16
|
16 #define BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/assert.hpp>
|
Chris@16
|
19 #include <boost/checked_delete.hpp>
|
Chris@16
|
20 #include <boost/signals2/postconstructible.hpp>
|
Chris@16
|
21 #include <boost/signals2/predestructible.hpp>
|
Chris@16
|
22 #include <boost/shared_ptr.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost
|
Chris@16
|
25 {
|
Chris@16
|
26 namespace signals2
|
Chris@16
|
27 {
|
Chris@16
|
28 namespace detail
|
Chris@16
|
29 {
|
Chris@16
|
30 extern inline void do_postconstruct(const postconstructible *ptr)
|
Chris@16
|
31 {
|
Chris@16
|
32 postconstructible *nonconst_ptr = const_cast<postconstructible*>(ptr);
|
Chris@16
|
33 nonconst_ptr->postconstruct();
|
Chris@16
|
34 }
|
Chris@16
|
35 extern inline void do_postconstruct(...)
|
Chris@16
|
36 {
|
Chris@16
|
37 }
|
Chris@16
|
38 extern inline void do_predestruct(...)
|
Chris@16
|
39 {
|
Chris@16
|
40 }
|
Chris@16
|
41 extern inline void do_predestruct(const predestructible *ptr)
|
Chris@16
|
42 {
|
Chris@16
|
43 try
|
Chris@16
|
44 {
|
Chris@16
|
45 predestructible *nonconst_ptr = const_cast<predestructible*>(ptr);
|
Chris@16
|
46 nonconst_ptr->predestruct();
|
Chris@16
|
47 }
|
Chris@16
|
48 catch(...)
|
Chris@16
|
49 {
|
Chris@16
|
50 BOOST_ASSERT(false);
|
Chris@16
|
51 }
|
Chris@16
|
52 }
|
Chris@16
|
53 }
|
Chris@16
|
54
|
Chris@16
|
55 template<typename T> class predestructing_deleter
|
Chris@16
|
56 {
|
Chris@16
|
57 public:
|
Chris@16
|
58 void operator()(const T *ptr) const
|
Chris@16
|
59 {
|
Chris@16
|
60 detail::do_predestruct(ptr);
|
Chris@16
|
61 checked_delete(ptr);
|
Chris@16
|
62 }
|
Chris@16
|
63 };
|
Chris@16
|
64
|
Chris@16
|
65 template<typename T>
|
Chris@16
|
66 shared_ptr<T> deconstruct_ptr(T *ptr)
|
Chris@16
|
67 {
|
Chris@16
|
68 if(ptr == 0) return shared_ptr<T>(ptr);
|
Chris@16
|
69 shared_ptr<T> shared(ptr, boost::signals2::predestructing_deleter<T>());
|
Chris@16
|
70 detail::do_postconstruct(ptr);
|
Chris@16
|
71 return shared;
|
Chris@16
|
72 }
|
Chris@16
|
73 template<typename T, typename D>
|
Chris@16
|
74 shared_ptr<T> deconstruct_ptr(T *ptr, D deleter)
|
Chris@16
|
75 {
|
Chris@16
|
76 shared_ptr<T> shared(ptr, deleter);
|
Chris@16
|
77 if(ptr == 0) return shared;
|
Chris@16
|
78 detail::do_postconstruct(ptr);
|
Chris@16
|
79 return shared;
|
Chris@16
|
80 }
|
Chris@16
|
81 }
|
Chris@16
|
82 }
|
Chris@16
|
83
|
Chris@16
|
84 #endif // BOOST_SIGNALS2_DECONSTRUCT_PTR_HPP
|