Honeycomb  0.1
Component-Model Framework
PackagedTask.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 template<class Sig> class PackagedTask;
11 
12 template<class R, class... Param>
13 class PackagedTask<R (Param...)> : mt::NoCopy
14 {
15  template<class, class> friend class FutureCommon;
16 
17 public:
18  PackagedTask() : _func(nullptr), _invoked(false) {}
19  template<class F>
20  PackagedTask(F&& f) : _func(forward<F>(f)), _invoked(false) {}
21  template<class F, class Alloc>
22  PackagedTask(F&& f, Alloc&& a) : _func(std::allocator_arg_t(), a, forward<F>(f)), _promise(a), _invoked(false) {}
23  PackagedTask(PackagedTask&& rhs) noexcept : _func(move(rhs._func)), _promise(move(rhs._promise)), _invoked(rhs._invoked) {}
24 
25  PackagedTask& operator=(PackagedTask&& rhs) { _func = move(rhs._func); _promise = move(rhs._promise); _invoked = rhs._invoked; return *this; }
26 
28 
31  Future<R> future() { return _promise.future(); }
32 
34 
38  template<class... Args>
39  void operator()(Args&&... args) { invoke(true, forward<Args>(args)...); }
41  template<class... Args>
42  void invoke_delayedReady(Args&&... args) { invoke(false, forward<Args>(args)...); }
43 
45  void setReady(bool reset = false)
46  {
47  if (!valid()) throw_ future::NoState();
48  assert(_invoked);
49  if (reset)
50  {
51  Promise<R> promise(move(_promise));
52  this->reset();
53  promise._state->setReady();
54  }
55  else
56  _promise._state->setReady();
57  }
58 
60  bool valid() const { return _promise.valid(); }
62  void reset() { *this = PackagedTask(move(_func)); }
63 
65  function<R (Param...)>& getFunc() { return _func; }
67  template<class F>
68  void setFunc(F&& f) { _func = forward<F>(f); }
69 
70 private:
71  template<class F, class Alloc>
72  PackagedTask(F&& f, Alloc&& a, Promise<R>&& promise) :
73  _func(std::allocator_arg_t(), forward<Alloc>(a), forward<F>(f)), _promise(move(promise)), _invoked(false) {}
74 
75  template<class... Args>
76  void invoke(bool setReady, Args&&... args)
77  {
78  if (!valid()) throw_ future::NoState();
79  if (_invoked) throw_ future::AlreadySatisfied();
80  _invoked = true;
81  future::priv::invoke<R>()(*_promise._state, setReady, _func, forward<Args>(args)...);
82  }
83 
84  function<R (Param...)> _func;
85  Promise<R> _promise;
86  bool _invoked;
87 };
88 
89 }
void setReady(bool reset=false)
Signal to future that result is ready for retrieval, and optionally reset task before signalling...
Definition: PackagedTask.h:45
function< R(Param...)> & getFunc()
Get function that will be invoked.
Definition: PackagedTask.h:65
Unique future, guarantees sole access to a future function result.
Definition: Future.h:53
PackagedTask & operator=(PackagedTask &&rhs)
Definition: PackagedTask.h:25
void setFunc(F &&f)
Set function to be invoked.
Definition: PackagedTask.h:68
void reset()
Reset the task so it can be invoked again, a new future is created for the next result.
Definition: PackagedTask.h:62
Inherit to declare that class is not copyable.
Definition: Meta.h:286
STL namespace.
PackagedTask(PackagedTask &&rhs) noexcept
Definition: PackagedTask.h:23
PackagedTask(F &&f, Alloc &&a)
Definition: PackagedTask.h:22
Container to hold a delayed function result.
Definition: Promise.h:181
Mixin for common future methods.
Definition: Future.h:90
auto reset()
Reset bytestream manipulator state.
Definition: ByteStream.h:417
PackagedTask(F &&f)
Definition: PackagedTask.h:20
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
A container that wraps a function so that its result is stored in a future when invoked.
Definition: PackagedTask.h:10
Future< R > future()
Get future from which delayed result can be retrieved.
Definition: PackagedTask.h:31
#define throw_
Use in place of throw keyword to throw a honey::Exception object polymorphically and provide debug in...
Definition: Exception.h:11
void invoke_delayedReady(Args &&...args)
Same as operator() except don't make future ready. User is responsible to call setReady() afterwards...
Definition: PackagedTask.h:42
bool valid() const
Check if this instance has state and can be used. State can be transferred out to another instance th...
Definition: PackagedTask.h:60
PackagedTask()
Definition: PackagedTask.h:18
Definition: Promise.h:17
void operator()(Args &&...args)
Invoke stored function to evaluate result for associated future.
Definition: PackagedTask.h:39
Global Honeycomb namespace.