From d97769fc2b9b54c6fe8235b0fcd0cd4a0b7e1fa7 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 9 Mar 2018 14:42:02 -0500 Subject: use custom (non-thread-safe) impl instead of shared_ptr --- promise.hpp | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file 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 { + //class promise_t: public std::shared_ptr { + class promise_t { + Promise *pm; + size_t *ref_cnt; public: friend Promise; template friend promise_t all(const PList &promise_list); template friend promise_t race(const PList &promise_list); promise_t() = delete; - template - promise_t(Func callback): - std::shared_ptr(std::make_shared()) { - callback(*this); + promise_t &operator=(const promise_t &other) = delete; + template 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 inline void resolve(T result) const; @@ -415,6 +432,22 @@ namespace promise { }); } + template + 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 inline void promise_t::resolve(T result) const { (*this)->resolve(result); } -- cgit v1.2.3-70-g09d2