We have some resource, which need to be manually released. Except explicitly writing a RAII wrapper for managing its resource, is any built-in template or class in std library to automatically perform a lambda task?
{
auto resource = InitResource();
GuardedTask task ([&resource]{ FreeUp(resource); }); // Simply bind a clean up lambda
...
if(failed_condition_met) { return false; } // Free up
...
if(another_failed_condition_met) { return false; } // Free up
} // Free up
The class may behavior like the following, but I am wondering that wheel may be already built in std library, or I should write my own one.
struct GuaredTask
{
std::function<void()> task;
GuaredTask(std::function<void()> f): task(f) {}
~GuaredTask(){ task(); }
};
Aucun commentaire:
Enregistrer un commentaire