ledger-core
Promise.hpp
1 /*
2  *
3  * Promise
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 20/01/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 #ifndef LEDGER_CORE_PROMISE_HPP
32 #define LEDGER_CORE_PROMISE_HPP
33 
34 #include "utils/Try.hpp"
35 #include "utils/Option.hpp"
36 #include "utils/Exception.hpp"
37 #include "Future.hpp"
38 
39 namespace ledger {
40  namespace core {
41 
42  template <typename T>
43  class Promise {
44  public:
45  Promise() {
46  _deffer = std::make_shared<Deffered<T>>();
47  };
48  Promise(const T& value) : Promise() {
49  success(value);
50  };
51 
52  Promise(const Exception& failure) : Promise() {
53  this->failure(failure);
54  };
55 
56  void complete(const Try<T>& result) {
57  _deffer->setResult(result);
58  };
59 
60  bool tryComplete(const Try<T>& result) {
61  return Try<Unit>::from([result, this] () {
62  complete(result);
63  return unit;
64  }).isSuccess();
65  }
66 
67  void completeWith(const Future<T>& future) {
68  auto deffer = _deffer;
69  Future<T> f = future;
70  f.onComplete(ImmediateExecutionContext::INSTANCE, [deffer] (const Try<T>& result) {
71  deffer->setResult(result);
72  });
73  };
74 
75  void tryCompleteWith(const Future<T>& future) {
76  auto deffer = _deffer;
77  Future<T> f = future;
78  f.onComplete(ImmediateExecutionContext::INSTANCE, [deffer] (const Try<T>& result) {
79  Try<Unit>::from([result, deffer] () {
80  deffer->setResult(result);
81  return unit;
82  });
83  });
84  }
85 
86  bool trySuccess(const T& value) {
87  return Try<Unit>::from([value, this] () {
88  success(value);
89  return unit;
90  }).isSuccess();
91  }
92 
93  bool tryFailure(const Exception& exception) {
94  return Try<Unit>::from([exception, this] () {
95  failure(exception);
96  return unit;
97  }).isSuccess();
98  }
99 
100  void success(const T& value) {
101  _deffer->setValue(value);
102  };
103 
104  void failure(const Exception& exception) {
105  _deffer->setError(exception);
106  };
107 
108  bool isCompleted() const {
109  return _deffer->hasValue();
110  };
111 
112  Future<T> getFuture() const {
113  return Future<T>(_deffer);
114  };
115 
116  private:
117  std::shared_ptr<Deffered<T>> _deffer;
118  };
119 
120  template <typename T>
121  using PromisePtr = Promise<std::shared_ptr<T>>;
122  }
123 }
124 
125 
126 #endif //LEDGER_CORE_PROMISE_HPP
Definition: Account.cpp:8