#include <iostream>
#include <functional>
class AutoCloser
{
public:
using NoParamFunc = std::function<void()>;
AutoCloser() = delete;
AutoCloser(NoParamFunc&& func)
{
_func = std::move(func);
_iswork = true;
}
~AutoCloser()
{
callOnce();
}
void call()
{
_func();
}
void callOnce()
{
if (_iswork == true)
{
call();
reset();
}
}
void reset()
{
_iswork = false;
}
private:
NoParamFunc _func;
bool _iswork;
};
class Test
{
public:
~Test()
{
std::cout << "Test::~Test()" << std::endl;
}
};
int main()
{
Test* value = new Test;
AutoCloser a([value]()
{
delete value;
});
// do something
}
// delete value
std에 비슷한거 없나..