#pragma once #include namespace utilities { /* * Copied from here: https://github.com/microsoft/GSL/blob/e0880931ae5885eb988d1a8a57acf8bc2b8dacda/include/gsl/util#L57 */ template class final_action { public: static_assert(!std::is_reference::value && !std::is_const::value && !std::is_volatile::value, "Final_action should store its callable by value"); explicit final_action(F f) noexcept : f_(std::move(f)) { } final_action(final_action&& other) noexcept : f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false)) { } final_action(const final_action&) = delete; final_action& operator=(const final_action&) = delete; final_action& operator=(final_action&&) = delete; ~final_action() noexcept { if (invoke_) f_(); } // Added by momo5502 void cancel() { invoke_ = false; } private: F f_; bool invoke_{true}; }; template final_action::type>::type> finally(F&& f) noexcept { return final_action::type>::type>( std::forward(f)); } }