From 2d13e18d076ced9335f496c6321ba714b441752d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 5 Aug 2019 14:31:36 +0200 Subject: [PATCH] Allow reassigning scoped_dohptr Not being able to do it is too restrictive in practice and just forces to use raw DOH pointers, which is not really less dangerous than adding the assignment operator and reset() method to this class. --- Source/Modules/c.cxx | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Source/Modules/c.cxx b/Source/Modules/c.cxx index 107f539a0..52a090c74 100644 --- a/Source/Modules/c.cxx +++ b/Source/Modules/c.cxx @@ -37,7 +37,17 @@ public: ~scoped_dohptr() { Delete(obj_); } // This is an std::auto_ptr<>-like "destructive" copy ctor which allows to return objects of this type from functions. - scoped_dohptr(scoped_dohptr const& tmp) : obj_(tmp.release()) {} + scoped_dohptr(scoped_dohptr const& other) : obj_(other.release()) {} + + // Same for the assignment operator. + scoped_dohptr& operator=(scoped_dohptr const& other) { + if (&other != this) { + Delete(obj_); + obj_ = other.release(); + } + + return *this; + } DOH* get() const { return obj_; } @@ -47,12 +57,17 @@ public: return obj; } + void reset() { + if (obj_) { + Delete(obj_); + obj_ = NULL; + } + } + operator DOH*() const { return obj_; } private: - scoped_dohptr& operator=(scoped_dohptr const&); - - DOH* const obj_; + DOH* obj_; }; // Helper class to output "begin" fragment in the ctor and "end" in the dtor.