#ifndef SMARTHANDLE_H #define SMARTHANDLE_H /** * Smart handle is like a smart pointer. It simply deletes the object on destruction using a custom deleter * It differs from `std::unique_ptr` by that it doesn't expect a pointer */ template class SmartHandle { public: typedef T value_type; typedef Deleter deleter_function; private: T _value; Deleter _deleter; bool _doDelete; void _delete(); public: SmartHandle(); SmartHandle(T value, Deleter deleter); SmartHandle(const SmartHandle& orig) = delete; SmartHandle& operator=(const SmartHandle& orig) = delete; SmartHandle(SmartHandle&& src); ~SmartHandle(); T get() const; void reset(); void reset(T value, Deleter deleter); }; template SmartHandle::SmartHandle() { this->_doDelete = 0; } template SmartHandle::SmartHandle(T value, Deleter deleter) { this->_doDelete = 0; this->reset(value,deleter); } template SmartHandle::SmartHandle(SmartHandle&& src) { this->reset(src._value,src._deleter); src._doDelete = 0; } template void SmartHandle::reset(T value, Deleter deleter) { if(this->_doDelete) { this->_delete(); } this->_value = value; this->_deleter = deleter; this->_doDelete = 1; } template void SmartHandle::reset() { if(this->_doDelete) { this->_delete(); } } template T SmartHandle::get() const { return _value; } template void SmartHandle::_delete() { _deleter(_value); this->_doDelete = 0; } template SmartHandle::~SmartHandle() { if(this->_doDelete) { _delete(); } } #endif // SMARTHANDLE_H