4 #include <type_traits>
\r
7 #include <tbb/spin_mutex.h>
\r
11 template<typename T>
\r
14 std::shared_ptr<T> impl_;
\r
15 tbb::spin_mutex mutex_;
\r
16 template <typename> friend class safe_ptr;
\r
18 typedef T element_type;
\r
20 safe_ptr() : impl_(std::make_shared<T>()){}
\r
22 safe_ptr(const safe_ptr<T>& other) : impl_(other.impl_){} // noexcept
\r
23 safe_ptr(safe_ptr<T>&& other) : impl_(std::move(other.impl_)){}
\r
25 template<typename U>
\r
26 safe_ptr(const safe_ptr<U>& other, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(other.impl_){} // noexcept
\r
28 template<typename U>
\r
29 safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
\r
30 : impl_(std::make_shared<U>(impl)) {}
\r
32 template<typename U, typename D>
\r
33 safe_ptr(const U& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
\r
34 : impl_(new U(impl), dtor) {}
\r
36 template<typename U>
\r
37 safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
\r
38 : impl_(std::make_shared<U>(std::forward<U>(impl))) {}
\r
40 template<typename U, typename D>
\r
41 safe_ptr(U&& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
\r
42 : impl_(new U(std::forward<U>(impl)), dtor) {}
\r
44 template<typename U>
\r
45 explicit safe_ptr(const std::shared_ptr<U>& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
\r
48 throw std::invalid_argument("impl");
\r
51 template<typename U>
\r
52 explicit safe_ptr(std::shared_ptr<U>&& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(std::move(impl))
\r
55 throw std::invalid_argument("impl");
\r
58 template<typename U>
\r
59 explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
\r
62 throw std::invalid_argument("impl");
\r
65 template<typename U, typename D>
\r
66 explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)
\r
69 throw std::invalid_argument("impl");
\r
72 template<typename U>
\r
73 typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr<T>&>::type
\r
74 operator=(const safe_ptr<U>& other)
\r
76 safe_ptr<T> temp(other);
\r
81 template <typename U>
\r
82 typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type
\r
85 safe_ptr<T> temp(std::forward<T>(impl));
\r
90 T& operator*() const { return *impl_.get();} // noexcept
\r
92 T* operator->() const { return impl_.get();} // noexcept
\r
94 T* get() const { return impl_.get();} // noexcept
\r
96 bool unique() const { return impl_.unique();} // noexcept
\r
98 long use_count() const { return impl_.use_count();} // noexcept
\r
100 void swap(safe_ptr& other) { impl_.swap(other.impl_); } // noexcept
\r
102 operator const std::shared_ptr<T>&() const { return impl_;} // noexcept
\r
105 bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); } // noexcept
\r
108 bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); } // noexcept
\r
110 template<class D, class U>
\r
111 D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); } // noexcept
\r
114 template<class T, class U>
\r
115 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
117 return a.get() == b.get();
\r
120 template<class T, class U>
\r
121 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
123 return a.get() != b.get();
\r
126 template<class T, class U>
\r
127 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
129 return a.get() < b.get();
\r
132 template<class T, class U>
\r
133 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
135 return a.get() > b.get();
\r
138 template<class T, class U>
\r
139 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
141 return a.get() >= b.get();
\r
144 template<class T, class U>
\r
145 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
147 return a.get() <= b.get();
\r
150 template<class E, class T, class U>
\r
151 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out, const safe_ptr<U>& p)
\r
153 return out << p.get();
\r
157 void swap(safe_ptr<T>& a, safe_ptr<T>& b) // noexcept
\r
163 T* get_pointer(safe_ptr<T> const& p) // noexcept
\r
168 template <class T, class U>
\r
169 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
171 return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
174 template <class T, class U>
\r
175 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
177 return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
180 template <class T, class U>
\r
181 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
\r
183 auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
\r
185 throw std::bad_cast();
\r
186 return safe_ptr<T>(temp);
\r
189 template<typename T>
\r
190 safe_ptr<T> make_safe()
\r
192 return safe_ptr<T>();
\r
195 template<typename T, typename P0>
\r
196 safe_ptr<T> make_safe(P0&& p0)
\r
198 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
\r
201 template<typename T, typename P0, typename P1>
\r
202 safe_ptr<T> make_safe(P0&& p0, P1&& p1)
\r
204 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
\r
207 template<typename T, typename P0, typename P1, typename P2>
\r
208 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)
\r
210 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\r
213 template<typename T, typename P0, typename P1, typename P2, typename P3>
\r
214 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
\r
216 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
\r
219 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
\r
220 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&&)
\r
222 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3), std::forward<P3>(p4)));
\r