From beb1f3c207bcf1ebc6ca1e83f1940bd91c867d9a Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Wed, 27 Mar 2019 17:32:09 -0700 Subject: utils: Add scope_exit object This is a utility for RAII-style operations. An object that will run code when a scope is left. Also includes unit tests. --- host/include/uhd/utils/CMakeLists.txt | 1 + host/include/uhd/utils/scope_exit.hpp | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 host/include/uhd/utils/scope_exit.hpp (limited to 'host/include') diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index dc70b9f03..767e56c44 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -26,6 +26,7 @@ UHD_INSTALL(FILES platform.hpp safe_call.hpp safe_main.hpp + scope_exit.hpp static.hpp tasks.hpp thread_priority.hpp diff --git a/host/include/uhd/utils/scope_exit.hpp b/host/include/uhd/utils/scope_exit.hpp new file mode 100644 index 000000000..953e5e47d --- /dev/null +++ b/host/include/uhd/utils/scope_exit.hpp @@ -0,0 +1,55 @@ +// +// Copyright 2019 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_UHD_SCOPE_EXIT_HPP +#define INCLUDED_UHD_SCOPE_EXIT_HPP + +#include +#include + +namespace uhd { namespace utils { + +/*! A class that will execute a function on its destruction + * + * Similar to Boost.ScopeExit. A useful tool for RAII-style operations. + * + * Note: The creation of the object can be costly if converting the exit + * callback to exit_cb_t incurs copying overhead. Keep this in mind when using + * this object in a high-performance path. + */ +class scope_exit +{ +public: + using uptr = std::unique_ptr; + using exit_cb_t = std::function; + + // \param exit_b The function object ("exit callback") that gets executed + // in the destructor + static uptr make(exit_cb_t&& exit_cb) + { + // When we have C++14, use make_unique instead (TODO) + return uptr(new scope_exit(std::forward(exit_cb))); + } + + ~scope_exit() + { + _exit_cb(); + } + +private: + scope_exit(std::function&& exit_cb) + : _exit_cb(std::forward>(exit_cb)) + { + // nop + } + + std::function _exit_cb; +}; + +}} /* namespace uhd::rfnoc */ + +#endif /* INCLUDED_UHD_SCOPE_EXIT_HPP */ + -- cgit v1.2.3