2017-07-29 11:49:34 +08:00
|
|
|
#pragma once
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace MiniEngine
|
|
|
|
{
|
|
|
|
|
2017-07-30 10:03:31 +08:00
|
|
|
template<typename T>
|
|
|
|
class res_ptr
|
2017-07-29 11:49:34 +08:00
|
|
|
{
|
|
|
|
public:
|
2017-07-30 10:03:31 +08:00
|
|
|
res_ptr(T* ptr) : _ptr(ptr) {}
|
|
|
|
res_ptr(T* ptr,const std::function<void(T*)>& delFunc) : _ptr(ptr), _delfunc(delFunc) {}
|
|
|
|
res_ptr(const res_ptr&)=delete;
|
|
|
|
res_ptr& operator = (const res_ptr&)=delete;
|
|
|
|
res_ptr(res_ptr&& t)
|
2017-07-29 11:49:34 +08:00
|
|
|
{
|
2017-07-30 10:03:31 +08:00
|
|
|
_ptr=t._ptr;
|
|
|
|
_delfunc=t._delfunc;
|
|
|
|
t._ptr=nullptr;
|
2017-07-29 11:49:34 +08:00
|
|
|
}
|
2017-07-30 10:03:31 +08:00
|
|
|
res_ptr& operator = (res_ptr&& t)
|
2017-07-29 11:49:34 +08:00
|
|
|
{
|
2017-07-30 10:03:31 +08:00
|
|
|
release();
|
|
|
|
_ptr=t._ptr;
|
|
|
|
_delfunc=t._delfunc;
|
|
|
|
t._ptr=nullptr;
|
|
|
|
return *this;
|
2017-07-29 11:49:34 +08:00
|
|
|
}
|
2017-07-30 10:03:31 +08:00
|
|
|
~res_ptr()
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
void release()
|
|
|
|
{
|
|
|
|
if(_ptr)
|
|
|
|
{
|
|
|
|
if(_delfunc)
|
|
|
|
{
|
|
|
|
_delfunc(_ptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-29 11:49:34 +08:00
|
|
|
|
2017-07-30 10:03:31 +08:00
|
|
|
}
|
|
|
|
_ptr=nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T* get() const
|
2017-07-29 11:49:34 +08:00
|
|
|
{
|
2017-07-30 10:03:31 +08:00
|
|
|
return _ptr;
|
2017-07-29 11:49:34 +08:00
|
|
|
}
|
2017-07-30 10:03:31 +08:00
|
|
|
void reset()
|
2017-07-29 11:49:34 +08:00
|
|
|
private:
|
2017-07-30 10:03:31 +08:00
|
|
|
T* _ptr;
|
|
|
|
std::function<void(T*)> _delfunc;
|
2017-07-29 11:49:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}/// End of namespace MiniEngine
|