1 #ifndef PROTON_WORK_QUEUE_HPP
2 #define PROTON_WORK_QUEUE_HPP
28 #include "./internal/export.hpp"
32 #include <type_traits>
35 struct pn_connection_t;
46 namespace internal {
namespace v03 {
49 invocable() =
default;
50 virtual ~invocable() =
default;
52 virtual invocable& clone()
const = 0;
53 virtual void operator() () = 0;
57 struct invocable_cloner : invocable {
58 virtual ~invocable_cloner() =
default;
59 virtual invocable& clone()
const {
60 return *
new T(
static_cast<T const&
>(*
this));
64 struct invocable_wrapper {
65 invocable_wrapper(): wrapped_(0) {}
66 invocable_wrapper(
const invocable_wrapper& w): wrapped_(&w.wrapped_->clone()) {}
67 invocable_wrapper& operator=(
const invocable_wrapper& that) {
68 invocable_wrapper newthis(that);
72 invocable_wrapper(invocable_wrapper&& w): wrapped_(w.wrapped_) {}
73 invocable_wrapper& operator=(invocable_wrapper&& that) {
delete wrapped_; wrapped_ = that.wrapped_;
return *
this; }
74 ~invocable_wrapper() {
delete wrapped_; }
76 invocable_wrapper(
const invocable& i): wrapped_(&i.clone()) {}
77 void operator()() { (*wrapped_)(); }
91 work(
const invocable& i): item_(i) {}
94 void operator()() { item_(); }
99 invocable_wrapper item_;
106 struct work0 :
public invocable_cloner<work0<R> > {
117 template <
class R,
class A>
118 struct work1 :
public invocable_cloner<work1<R,A> > {
122 work1(R (* t)(A), A a) :
130 template <
class R,
class A,
class B>
131 struct work2 :
public invocable_cloner<work2<R,A,B> > {
136 work2(R (* t)(A, B), A a, B b) :
137 fn_(t), a_(a), b_(b) {}
144 template <
class R,
class A,
class B,
class C>
145 struct work3 :
public invocable_cloner<work3<R,A,B,C> > {
151 work3(R (* t)(A, B, C), A a, B b, C c) :
152 fn_(t), a_(a), b_(b), c_(c) {}
159 template <
class R,
class T>
160 struct work_pmf0 :
public invocable_cloner<work_pmf0<R,T> > {
164 work_pmf0(R (T::* a)(), T& h) :
165 holder_(h), fn_(a) {}
172 template <
class R,
class T,
class A>
173 struct work_pmf1 :
public invocable_cloner<work_pmf1<R,T,A> > {
178 work_pmf1(R (T::* t)(A), T& h, A a) :
179 holder_(h), fn_(t), a_(a) {}
186 template <
class R,
class T,
class A,
class B>
187 struct work_pmf2 :
public invocable_cloner<work_pmf2<R,T,A,B> > {
193 work_pmf2(R (T::* t)(A, B), T& h, A a, B b) :
194 holder_(h), fn_(t), a_(a), b_(b) {}
197 (holder_.*fn_)(a_, b_);
201 template <
class R,
class T,
class A,
class B,
class C>
202 struct work_pmf3 :
public invocable_cloner<work_pmf3<R,T,A,B,C> > {
204 R (T::* fn_)(A, B, C);
209 work_pmf3(R (T::* t)(A, B, C), T& h, A a, B b, C c) :
210 holder_(h), fn_(t), a_(a), b_(b), c_(c) {}
213 (holder_.*fn_)(a_, b_, c_);
219 template <
class R,
class T>
220 work make_work(R (T::*f)(), T* t) {
221 return work_pmf0<R, T>(f, *t);
224 template <
class R,
class T,
class A>
225 work make_work(R (T::*f)(A), T* t, A a) {
226 return work_pmf1<R, T, A>(f, *t, a);
229 template <
class R,
class T,
class A,
class B>
230 work make_work(R (T::*f)(A, B), T* t, A a, B b) {
231 return work_pmf2<R, T, A, B>(f, *t, a, b);
234 template <
class R,
class T,
class A,
class B,
class C>
235 work make_work(R (T::*f)(A, B, C), T* t, A a, B b, C c) {
236 return work_pmf3<R, T, A, B, C>(f, *t, a, b, c);
240 work make_work(R (*f)()) {
244 template <
class R,
class A>
245 work make_work(R (*f)(A), A a) {
246 return work1<R, A>(f, a);
249 template <
class R,
class A,
class B>
250 work make_work(R (*f)(A, B), A a, B b) {
251 return work2<R, A, B>(f, a, b);
254 template <
class R,
class A,
class B,
class C>
255 work make_work(R (*f)(A, B, C), A a, B b, C c) {
256 return work3<R, A, B, C>(f, a, b, c);
262 namespace internal {
namespace v11 {
276 class =
typename std::enable_if<!std::is_same<typename std::decay<T>::type,work>::value>::type
278 work(T&& f): item_(std::forward<T>(f)) {}
283 void operator()() { item_(); }
288 std::function<void()> item_;
298 template <
class... Rest>
299 work make_work(Rest&&... r) {
300 return std::bind(std::forward<Rest>(r)...);
305 using internal::v11::work;
306 using internal::v11::make_work;
350 PN_CPP_EXTERN
bool add(work fn);
353 PN_CPP_EXTERN PN_CPP_DEPRECATED(
"Use 'work_queue::add(work)'") bool add(void_function0& fn);
371 PN_CPP_EXTERN
bool add(internal::v03::work fn);
372 PN_CPP_EXTERN
void schedule(
duration, internal::v03::work fn);
378 std::unique_ptr<impl> impl_;
382 friend class io::connection_driver;
A top-level container of connections, sessions, and links.
Definition: container.hpp:50
A span of time in milliseconds.
Definition: duration.hpp:39
Unsettled API - A context for thread-safe execution of work.
Definition: work_queue.hpp:327
work_queue()
Unsettled API - Create a work queue.
bool add(work fn)
Unsettled API - Add work fn to the work queue.
work_queue(container &)
Unsettled API - Create a work queue backed by a container.
A span of time in milliseconds.
Deprecated - Use the API in work_queue.hpp.
The main Proton namespace.
Definition: annotation_key.hpp:33
T get(const scalar &s)
Get a contained value of type T.
Definition: scalar.hpp:60
void swap(map< K, T > &, map< K, T > &)
Swap proton::map instances.