aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-03-09 14:42:02 -0500
committerDeterminant <[email protected]>2018-03-09 14:42:02 -0500
commitd97769fc2b9b54c6fe8235b0fcd0cd4a0b7e1fa7 (patch)
tree4b40c33db2d15df0612c664f7906706041faa651
parent3ac928b90e7dd3f11f92490edac283c6e2bc6fda (diff)
use custom (non-thread-safe) impl instead of shared_ptr
-rw-r--r--promise.hpp43
1 files changed, 38 insertions, 5 deletions
diff --git a/promise.hpp b/promise.hpp
index 9fb6170..d3cc7df 100644
--- a/promise.hpp
+++ b/promise.hpp
@@ -121,17 +121,34 @@ namespace promise {
ArgType>::value>;
class Promise;
- class promise_t: public std::shared_ptr<Promise> {
+ //class promise_t: public std::shared_ptr<Promise> {
+ class promise_t {
+ Promise *pm;
+ size_t *ref_cnt;
public:
friend Promise;
template<typename PList> friend promise_t all(const PList &promise_list);
template<typename PList> friend promise_t race(const PList &promise_list);
promise_t() = delete;
- template<typename Func>
- promise_t(Func callback):
- std::shared_ptr<Promise>(std::make_shared<Promise>()) {
- callback(*this);
+ promise_t &operator=(const promise_t &other) = delete;
+ template<typename Func> inline promise_t(Func callback);
+ inline ~promise_t();
+
+ promise_t(const promise_t &other):
+ pm(other.pm),
+ ref_cnt(other.ref_cnt) {
+ ++*ref_cnt;
+ }
+
+ promise_t(promise_t &&other):
+ pm(other.pm),
+ ref_cnt(other.ref_cnt) {
+ other.pm = nullptr;
+ }
+
+ Promise *operator->() const {
+ return pm;
}
template<typename T> inline void resolve(T result) const;
@@ -415,6 +432,22 @@ namespace promise {
});
}
+ template<typename Func>
+ inline promise_t::promise_t(Func callback):
+ pm(new Promise()),
+ ref_cnt(new size_t(1)) {
+ callback(*this);
+ }
+
+ inline promise_t::~promise_t() {
+ if (pm)
+ {
+ if (--*ref_cnt) return;
+ delete pm;
+ delete ref_cnt;
+ }
+ }
+
template<typename T>
inline void promise_t::resolve(T result) const { (*this)->resolve(result); }