Honeycomb  0.1
Component-Model Framework
SharedFuture.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 namespace future { namespace priv
11 {
12  template<class R> struct wrappedResult<SharedFuture<R>> { typedef R type; };
13 
14  template<class R> struct unwrapOnReady<SharedFuture<R>> : mt::FuncptrBase, SmallAllocatorObject
15  {
16  unwrapOnReady(Promise<R>&& promise) : promise(move(promise)) {}
17  void operator()(StateBase& src)
18  {
19  if (src.ready) src.ex ? promise.setException(src.ex) : promise.setValue(static_cast<State<R>&>(src).result());
20  delete_(this);
21  }
22  Promise<R> promise;
23  };
24 
25  template<> struct unwrapOnReady<SharedFuture<void>> : mt::FuncptrBase, SmallAllocatorObject
26  {
27  unwrapOnReady(Promise<void>&& promise) : promise(move(promise)) {}
28  void operator()(StateBase& src)
29  {
30  if (src.ready) src.ex ? promise.setException(src.ex) : promise.setValue();
31  delete_(this);
32  }
33  Promise<void> promise;
34  };
35 } }
38 template<class R>
40 class SharedFuture : public FutureBase, public FutureCommon<SharedFuture<R>, R>
41 {
42  template<class, class> friend class FutureCommon;
43  template<class> friend class Future;
44  template<class> friend class SharedFuture;
45 
46 public:
47  typedef typename std::conditional<mt::isRef<R>::value || std::is_void<R>::value, R, typename mt::addConstRef<R>::type>::type ResultConstRef;
48  typedef typename std::conditional<mt::isRef<R>::value || std::is_void<R>::value, R, typename mt::addRef<R>::type>::type ResultRef;
49  typedef future::priv::State<R> State;
50 
51  SharedFuture() : _state(nullptr) {}
52  SharedFuture(Future<R>&& rhs) : _state(move(rhs._state)) {}
53  SharedFuture(const SharedFuture& rhs) : _state(nullptr) { operator=(rhs); }
54  SharedFuture(SharedFuture&& rhs) : _state(nullptr) { operator=(move(rhs)); }
55 
56  SharedFuture& operator=(const SharedFuture& rhs) { _state = rhs._state; return *this; }
57  SharedFuture& operator=(SharedFuture&& rhs) { _state = move(rhs._state); return *this; }
58 
60  ResultConstRef get() const;
61  ResultRef get();
62 
64  State& __state() const { assert(_state); return *_state; }
65 
66 protected:
67  virtual StateBase* stateBase() const { return _state; }
68 
69 private:
70  SharedPtr<State> _state;
71 };
72 
73 template<class R>
75 {
76  wait();
77  if (_state->ex) _state->ex->raise();
78  return _state->result();
79 }
80 template<class R>
82 {
83  wait();
84  if (_state->ex) _state->ex->raise();
85  return _state->result();
86 }
87 
88 template<>
89 inline void SharedFuture<void>::get() const
90 {
91  wait();
92  if (_state->ex) _state->ex->raise();
93 }
94 template<>
96 {
97  wait();
98  if (_state->ex) _state->ex->raise();
99 }
100 
102 template<class T, class R = typename std::decay<T>::type>
104 {
105  Promise<R> promise;
106  promise.setValue(forward<T>(val));
107  return promise.future().share();
108 }
109 
111 {
112  Promise<void> promise;
113  promise.setValue();
114  return promise.future().share();
115 }
116 
117 }
Unique future, guarantees sole access to a future function result.
Definition: Future.h:53
ResultConstRef get() const
Get the future result, waiting if necessary. Throws any exception stored in the result. The result can be retrieved repeatedly.
Definition: SharedFuture.h:74
State & __state() const
Get the shared state.
Definition: SharedFuture.h:64
Shared future, allows multiple access to a future function result.
Definition: Future.h:54
void delete_(T *&p)
Destruct object, free memory and set pointer to null.
Definition: Allocator.h:75
Future< R > future()
Get future from which delayed result can be retrieved.
Definition: Promise.h:207
SharedFuture(SharedFuture &&rhs)
Definition: SharedFuture.h:54
SharedFuture & operator=(SharedFuture &&rhs)
Definition: SharedFuture.h:57
Container to hold a delayed function result.
Definition: Promise.h:181
AllocatorObject< SmallAllocator > SmallAllocatorObject
Inherit from this class to use the small block allocator.
Definition: SmallAllocator.h:34
mt::disable_if< mt::True< T >::value &&(mt::isRef< R >::value||std::is_void< R >::value)>::type setValue(T &&val)
Set stored result. Result is copy/move constructed from value.
Definition: Promise.h:223
future::priv::State< R > State
Definition: SharedFuture.h:49
Mixin for common future methods.
Definition: Future.h:90
SharedFuture< R > SharedFutureCreate(T &&val)
Create a shared future that is immediately ready with the value.
Definition: SharedFuture.h:103
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
std::conditional< mt::isRef< R >::value||std::is_void< R >::value, R, typename mt::addConstRef< R >::type >::type ResultConstRef
Definition: SharedFuture.h:47
SharedFuture(const SharedFuture &rhs)
Definition: SharedFuture.h:53
future::priv::StateBase StateBase
Definition: Future.h:23
std::conditional< mt::isRef< R >::value||std::is_void< R >::value, R, typename mt::addRef< R >::type >::type ResultRef
Definition: SharedFuture.h:48
SharedFuture(Future< R > &&rhs)
Definition: SharedFuture.h:52
virtual StateBase * stateBase() const
Definition: SharedFuture.h:67
SharedFuture()
Definition: SharedFuture.h:51
addRef< typename std::add_const< T >::type > addConstRef
Add top-level const qualifier and reference to type. Use std::decay to remove top-level const/ref...
Definition: Meta.h:50
std::add_lvalue_reference< T > addRef
Add reference to type.
Definition: Meta.h:42
Global Honeycomb namespace.
SharedFuture & operator=(const SharedFuture &rhs)
Definition: SharedFuture.h:56