Refactor scoped_dohptr() to reuse its reset()

No real changes, just avoid some code duplication and add an optional
argument to reset() to make it more compatible with std::unique_ptr<>
and also more flexible.
This commit is contained in:
Vadim Zeitlin 2021-11-03 01:25:16 +01:00
commit ca56f14aa5

View file

@ -42,20 +42,14 @@ public:
// Same for the assignment operator. // Same for the assignment operator.
scoped_dohptr& operator=(scoped_dohptr const& other) { scoped_dohptr& operator=(scoped_dohptr const& other) {
if (&other != this) { reset(other.release());
Delete(obj_);
obj_ = other.release();
}
return *this; return *this;
} }
// Assignment operator takes ownership of the pointer, just as the ctor does. // Assignment operator takes ownership of the pointer, just as the ctor does.
scoped_dohptr& operator=(DOH* obj) { scoped_dohptr& operator=(DOH* obj) {
if (obj != obj_) { reset(obj);
Delete(obj_);
obj_ = obj;
}
return *this; return *this;
} }
@ -68,10 +62,10 @@ public:
return obj; return obj;
} }
void reset() { void reset(DOH* obj = NULL) {
if (obj_) { if (obj != obj_) {
Delete(obj_); Delete(obj_);
obj_ = NULL; obj_ = obj;
} }
} }