Add maybe_owned_dohptr helper
This class allows to avoid allocating a new DOH object unnecessarily if we can reuse an existing one. It is not used yet, but will be soon.
This commit is contained in:
parent
ca56f14aa5
commit
ad3db88e44
1 changed files with 38 additions and 1 deletions
|
|
@ -71,10 +71,47 @@ public:
|
|||
|
||||
operator DOH*() const { return obj_; }
|
||||
|
||||
private:
|
||||
protected:
|
||||
DOH* obj_;
|
||||
};
|
||||
|
||||
// Wrapper for a DOH object which can be owned or not.
|
||||
class maybe_owned_dohptr : public scoped_dohptr
|
||||
{
|
||||
public:
|
||||
explicit maybe_owned_dohptr(DOH* obj = NULL) : scoped_dohptr(obj), owned_(true) {}
|
||||
|
||||
maybe_owned_dohptr(maybe_owned_dohptr const& other) : scoped_dohptr(other) {
|
||||
owned_ = other.owned_;
|
||||
|
||||
// We can live other.owned_ unchanged, as its pointer is null now anyhow.
|
||||
}
|
||||
|
||||
maybe_owned_dohptr& operator=(maybe_owned_dohptr const& other) {
|
||||
reset(other.release());
|
||||
owned_ = other.owned_;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~maybe_owned_dohptr() {
|
||||
if (!owned_)
|
||||
obj_ = NULL; // Prevent it from being deleted by the base class dtor.
|
||||
}
|
||||
|
||||
void assign_owned(DOH* obj) {
|
||||
reset(obj);
|
||||
}
|
||||
|
||||
void assign_non_owned(DOH* obj) {
|
||||
reset(obj);
|
||||
owned_ = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool owned_;
|
||||
};
|
||||
|
||||
// Helper class to output "begin" fragment in the ctor and "end" in the dtor.
|
||||
class begin_end_output_guard
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue