1 #ifndef PROTON_THREAD_SAFE_HPP
2 #define PROTON_THREAD_SAFE_HPP
25 #include "./internal/config.hpp"
26 #include "./connection.hpp"
27 #include "./event_loop.hpp"
28 #include "./internal/object.hpp"
29 #include "./internal/type_traits.hpp"
42 template <
class T>
struct endpoint_traits;
43 template<>
struct endpoint_traits<connection> {};
44 template<>
struct endpoint_traits<session> {};
45 template<>
struct endpoint_traits<link> {};
46 template<>
struct endpoint_traits<sender> {};
47 template<>
struct endpoint_traits<receiver> {};
50 template <
class T>
class returned;
69 class thread_safe :
private internal::pn_ptr_base,
private internal::endpoint_traits<T> {
70 typedef typename T::pn_type pn_type;
72 struct inject_decref :
public void_function0 {
74 inject_decref(pn_type* p) : ptr_(p) {}
75 void operator()() PN_CPP_OVERRIDE { decref(ptr_);
delete this; }
80 static void operator delete(
void*) {}
105 static void*
operator new(size_t, pn_type* p) {
return p; }
106 static void operator delete(
void*, pn_type*) {}
107 thread_safe() { incref(ptr()); }
108 pn_type* ptr() {
return reinterpret_cast<pn_type*
>(
this); }
112 thread_safe(
const thread_safe&);
113 thread_safe& operator=(
const thread_safe&);
116 friend class returned<T>;
125 class returned :
private internal::endpoint_traits<T>
129 explicit returned(thread_safe<T>* p) : ptr_(p) {}
131 explicit returned(
const T& obj) : ptr_(thread_safe<T>::create(obj)) {}
134 returned(
const returned& x) : ptr_(const_cast<returned&>(x).release()) {}
136 ~returned() {
if (ptr_)
delete ptr_; }
139 thread_safe<T>* release()
const { thread_safe<T>* p = ptr_; ptr_ = 0;
return p; }
142 operator T() {
return ptr_->unsafe(); }
145 operator std::shared_ptr<thread_safe<T> >() {
147 return std::shared_ptr<thread_safe<T> >(release());
151 operator std::unique_ptr<thread_safe<T> >() {
152 return std::unique_ptr<thread_safe<T> >(release());
157 void operator=(
const returned&);
158 mutable thread_safe<T>* ptr_;
165 template <
class T> std::shared_ptr<thread_safe<T> > make_shared_thread_safe(
const T& obj) {
168 template <
class T> std::unique_ptr<thread_safe<T> > make_unique_thread_safe(
const T& obj) {
175 #endif // PROTON_THREAD_SAFE_HPP
Experimental - A serial execution context.
Definition: event_loop.hpp:57
T unsafe()
Get the thread-unsafe proton object wrapped by this thread_safe<T>
Definition: thread_safe.hpp:101
class event_loop * event_loop()
Get the event loop for this object.
Definition: thread_safe.hpp:98
virtual bool inject(void_function0 &f)=0
Arrange to have f() called in the event_loop's sequence: possibly deferred, possibly in another threa...
returned< T > make_thread_safe(const T &obj)
Make a thread-safe wrapper for obj.
Definition: thread_safe.hpp:162
Experimental - A thread-safe object wrapper.
Definition: connection.hpp:45