Add a scoped holder similar to scoped_ptr for handles that may or may not be pointers

Originally committed to SVN as r6491.
This commit is contained in:
Thomas Goyne 2012-02-20 18:22:04 +00:00
parent ad7d7c2be3
commit b9ddf00ead

View file

@ -48,4 +48,32 @@ public:
~scoped_ptr() { delete ptr; } ~scoped_ptr() { delete ptr; }
}; };
/// A generic scoped holder for non-pointer handles
template<class T, class Del = void(*)(T)>
class scoped_holder {
T value;
Del destructor;
scoped_holder(scoped_holder const&);
scoped_holder& operator=(scoped_holder const&);
public:
operator T() const { return value; }
T operator->() const { return value; }
scoped_holder& operator=(T new_value) {
if (value)
destructor(value);
value = new_value;
return *this;
}
scoped_holder(T value, Del destructor)
: value(value)
, destructor(destructor)
{
}
~scoped_holder() { if (value) destructor(value); }
};
} }