Chris@16
|
1
|
Chris@16
|
2 // helper code for dealing with tracking non-boost shared_ptr/weak_ptr
|
Chris@16
|
3
|
Chris@16
|
4 // Copyright Frank Mori Hess 2009.
|
Chris@16
|
5 // Distributed under the Boost Software License, Version
|
Chris@16
|
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/libs/signals2 for library home page.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_SIGNALS2_FOREIGN_PTR_HPP
|
Chris@16
|
12 #define BOOST_SIGNALS2_FOREIGN_PTR_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <algorithm>
|
Chris@16
|
15 #include <boost/config.hpp>
|
Chris@16
|
16 #include <boost/assert.hpp>
|
Chris@16
|
17 #include <boost/scoped_ptr.hpp>
|
Chris@16
|
18 #include <boost/smart_ptr/bad_weak_ptr.hpp>
|
Chris@16
|
19 #include <boost/utility/swap.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #ifndef BOOST_NO_CXX11_SMART_PTR
|
Chris@16
|
22 #include <memory>
|
Chris@16
|
23 #endif
|
Chris@16
|
24
|
Chris@16
|
25 namespace boost
|
Chris@16
|
26 {
|
Chris@16
|
27 template<typename T> class shared_ptr;
|
Chris@16
|
28 template<typename T> class weak_ptr;
|
Chris@16
|
29
|
Chris@16
|
30 namespace signals2
|
Chris@16
|
31 {
|
Chris@16
|
32 template<typename WeakPtr> struct weak_ptr_traits
|
Chris@16
|
33 {};
|
Chris@16
|
34 template<typename T> struct weak_ptr_traits<boost::weak_ptr<T> >
|
Chris@16
|
35 {
|
Chris@16
|
36 typedef boost::shared_ptr<T> shared_type;
|
Chris@16
|
37 };
|
Chris@16
|
38 #ifndef BOOST_NO_CXX11_SMART_PTR
|
Chris@16
|
39 template<typename T> struct weak_ptr_traits<std::weak_ptr<T> >
|
Chris@16
|
40 {
|
Chris@16
|
41 typedef std::shared_ptr<T> shared_type;
|
Chris@16
|
42 };
|
Chris@16
|
43 #endif
|
Chris@16
|
44
|
Chris@16
|
45 template<typename SharedPtr> struct shared_ptr_traits
|
Chris@16
|
46 {};
|
Chris@16
|
47
|
Chris@16
|
48 template<typename T> struct shared_ptr_traits<boost::shared_ptr<T> >
|
Chris@16
|
49 {
|
Chris@16
|
50 typedef boost::weak_ptr<T> weak_type;
|
Chris@16
|
51 };
|
Chris@16
|
52 #ifndef BOOST_NO_CXX11_SMART_PTR
|
Chris@16
|
53 template<typename T> struct shared_ptr_traits<std::shared_ptr<T> >
|
Chris@16
|
54 {
|
Chris@16
|
55 typedef std::weak_ptr<T> weak_type;
|
Chris@16
|
56 };
|
Chris@16
|
57 #endif
|
Chris@16
|
58
|
Chris@16
|
59 namespace detail
|
Chris@16
|
60 {
|
Chris@16
|
61 struct foreign_shared_ptr_impl_base
|
Chris@16
|
62 {
|
Chris@16
|
63 virtual ~foreign_shared_ptr_impl_base() {}
|
Chris@16
|
64 virtual void* get() const = 0;
|
Chris@16
|
65 virtual foreign_shared_ptr_impl_base * clone() const = 0;
|
Chris@16
|
66 };
|
Chris@16
|
67
|
Chris@16
|
68 template<typename FSP>
|
Chris@16
|
69 class foreign_shared_ptr_impl: public foreign_shared_ptr_impl_base
|
Chris@16
|
70 {
|
Chris@16
|
71 public:
|
Chris@16
|
72 foreign_shared_ptr_impl(const FSP &p): _p(p)
|
Chris@16
|
73 {}
|
Chris@16
|
74 virtual void * get() const
|
Chris@16
|
75 {
|
Chris@16
|
76 return _p.get();
|
Chris@16
|
77 }
|
Chris@16
|
78 virtual foreign_shared_ptr_impl * clone() const
|
Chris@16
|
79 {
|
Chris@16
|
80 return new foreign_shared_ptr_impl(*this);
|
Chris@16
|
81 }
|
Chris@16
|
82 private:
|
Chris@16
|
83 FSP _p;
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 class foreign_void_shared_ptr
|
Chris@16
|
87 {
|
Chris@16
|
88 public:
|
Chris@16
|
89 foreign_void_shared_ptr():
|
Chris@16
|
90 _p(0)
|
Chris@16
|
91 {}
|
Chris@16
|
92 foreign_void_shared_ptr(const foreign_void_shared_ptr &other):
|
Chris@16
|
93 _p(other._p->clone())
|
Chris@16
|
94 {}
|
Chris@16
|
95 template<typename FSP>
|
Chris@16
|
96 explicit foreign_void_shared_ptr(const FSP &fsp):
|
Chris@16
|
97 _p(new foreign_shared_ptr_impl<FSP>(fsp))
|
Chris@16
|
98 {}
|
Chris@16
|
99 ~foreign_void_shared_ptr()
|
Chris@16
|
100 {
|
Chris@16
|
101 delete _p;
|
Chris@16
|
102 }
|
Chris@16
|
103 foreign_void_shared_ptr & operator=(const foreign_void_shared_ptr &other)
|
Chris@16
|
104 {
|
Chris@16
|
105 if(&other == this) return *this;
|
Chris@16
|
106 foreign_void_shared_ptr(other).swap(*this);
|
Chris@16
|
107 return *this;
|
Chris@16
|
108 }
|
Chris@16
|
109 void swap(foreign_void_shared_ptr &other)
|
Chris@16
|
110 {
|
Chris@16
|
111 boost::swap(_p, other._p);
|
Chris@16
|
112 }
|
Chris@16
|
113 private:
|
Chris@16
|
114 foreign_shared_ptr_impl_base *_p;
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 struct foreign_weak_ptr_impl_base
|
Chris@16
|
118 {
|
Chris@16
|
119 virtual ~foreign_weak_ptr_impl_base() {}
|
Chris@16
|
120 virtual foreign_void_shared_ptr lock() const = 0;
|
Chris@16
|
121 virtual bool expired() const = 0;
|
Chris@16
|
122 virtual foreign_weak_ptr_impl_base * clone() const = 0;
|
Chris@16
|
123 };
|
Chris@16
|
124
|
Chris@16
|
125 template<typename FWP>
|
Chris@16
|
126 class foreign_weak_ptr_impl: public foreign_weak_ptr_impl_base
|
Chris@16
|
127 {
|
Chris@16
|
128 public:
|
Chris@16
|
129 foreign_weak_ptr_impl(const FWP &p): _p(p)
|
Chris@16
|
130 {}
|
Chris@16
|
131 virtual foreign_void_shared_ptr lock() const
|
Chris@16
|
132 {
|
Chris@16
|
133 return foreign_void_shared_ptr(_p.lock());
|
Chris@16
|
134 }
|
Chris@16
|
135 virtual bool expired() const
|
Chris@16
|
136 {
|
Chris@16
|
137 return _p.expired();
|
Chris@16
|
138 }
|
Chris@16
|
139 virtual foreign_weak_ptr_impl * clone() const
|
Chris@16
|
140 {
|
Chris@16
|
141 return new foreign_weak_ptr_impl(*this);
|
Chris@16
|
142 }
|
Chris@16
|
143 private:
|
Chris@16
|
144 FWP _p;
|
Chris@16
|
145 };
|
Chris@16
|
146
|
Chris@16
|
147 class foreign_void_weak_ptr
|
Chris@16
|
148 {
|
Chris@16
|
149 public:
|
Chris@16
|
150 foreign_void_weak_ptr()
|
Chris@16
|
151 {}
|
Chris@16
|
152 foreign_void_weak_ptr(const foreign_void_weak_ptr &other):
|
Chris@16
|
153 _p(other._p->clone())
|
Chris@16
|
154 {}
|
Chris@16
|
155 template<typename FWP>
|
Chris@16
|
156 explicit foreign_void_weak_ptr(const FWP &fwp):
|
Chris@16
|
157 _p(new foreign_weak_ptr_impl<FWP>(fwp))
|
Chris@16
|
158 {}
|
Chris@16
|
159 foreign_void_weak_ptr & operator=(const foreign_void_weak_ptr &other)
|
Chris@16
|
160 {
|
Chris@16
|
161 if(&other == this) return *this;
|
Chris@16
|
162 foreign_void_weak_ptr(other).swap(*this);
|
Chris@16
|
163 return *this;
|
Chris@16
|
164 }
|
Chris@16
|
165 void swap(foreign_void_weak_ptr &other)
|
Chris@16
|
166 {
|
Chris@16
|
167 boost::swap(_p, other._p);
|
Chris@16
|
168 }
|
Chris@16
|
169 foreign_void_shared_ptr lock() const
|
Chris@16
|
170 {
|
Chris@16
|
171 return _p->lock();
|
Chris@16
|
172 }
|
Chris@16
|
173 bool expired() const
|
Chris@16
|
174 {
|
Chris@16
|
175 return _p->expired();
|
Chris@16
|
176 }
|
Chris@16
|
177 private:
|
Chris@16
|
178 boost::scoped_ptr<foreign_weak_ptr_impl_base> _p;
|
Chris@16
|
179 };
|
Chris@16
|
180 } // namespace detail
|
Chris@16
|
181
|
Chris@16
|
182 } // namespace signals2
|
Chris@16
|
183 } // namespace boost
|
Chris@16
|
184
|
Chris@16
|
185 #endif // BOOST_SIGNALS2_FOREIGN_PTR_HPP
|