aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-07-11 18:58:54 -0400
committerDeterminant <[email protected]>2018-07-11 18:58:54 -0400
commit6b250ad8e7808384e0c03644a5bf8c43ae3c5245 (patch)
tree794fefdbc5bd248da0f8edd4287a724c817c9a70
parent469a4d4516f009f2436580ed7732c734458744b8 (diff)
use copy-and-swap idiomHEADmaster
-rw-r--r--promise.hpp28
1 files changed, 21 insertions, 7 deletions
diff --git a/promise.hpp b/promise.hpp
index fe701ae..593d5c1 100644
--- a/promise.hpp
+++ b/promise.hpp
@@ -126,21 +126,35 @@ namespace promise {
class promise_t {
Promise *pm;
size_t *ref_cnt;
- inline void clear();
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);
inline promise_t();
+ inline ~promise_t();
template<typename Func> inline promise_t(Func callback);
- ~promise_t() { clear(); }
+
+ void swap(promise_t &other) {
+ std::swap(pm, other.pm);
+ std::swap(ref_cnt, other.ref_cnt);
+ }
promise_t &operator=(const promise_t &other) {
- clear();
- pm = other.pm;
- ref_cnt = other.ref_cnt;
- ++*ref_cnt;
+ if (this != &other)
+ {
+ promise_t tmp(other);
+ tmp.swap(*this);
+ }
+ return *this;
+ }
+
+ promise_t &operator=(promise_t &&other) {
+ if (this != &other)
+ {
+ promise_t tmp(std::move(other));
+ tmp.swap(*this);
+ }
return *this;
}
@@ -623,7 +637,7 @@ namespace promise {
pm(new Promise()),
ref_cnt(new size_t(1)) {}
- inline void promise_t::clear() {
+ inline promise_t::~promise_t() {
if (pm)
{
if (--*ref_cnt) return;