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:
parent
ad7d7c2be3
commit
b9ddf00ead
1 changed files with 28 additions and 0 deletions
|
@ -48,4 +48,32 @@ public:
|
|||
~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); }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue