diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h index 8d5c3578..f21c6dc0 100644 --- a/include/libcamera/base/utils.h +++ b/include/libcamera/base/utils.h @@ -21,6 +21,7 @@ #include #include +#include #include #ifndef __DOXYGEN__ @@ -428,6 +429,43 @@ private: std::vector> actions_; }; +#ifndef __DOXYGEN__ +template +class scope_exit +{ +public: + template>, scope_exit> && + std::is_constructible_v> * = nullptr> + explicit scope_exit(Fn &&fn) + : exitFunction_(std::forward(fn)) + { + static_assert(std::is_nothrow_constructible_v); + } + + ~scope_exit() + { + if (active_) + exitFunction_(); + } + + void release() + { + active_ = false; + } + +private: + LIBCAMERA_DISABLE_COPY_AND_MOVE(scope_exit) + + EF exitFunction_; + bool active_ = true; +}; + +template +scope_exit(EF) -> scope_exit; + +#endif /* __DOXYGEN__ */ + } /* namespace utils */ #ifndef __DOXYGEN__