aboutsummaryrefslogtreecommitdiffstats
path: root/host/include/uhd/utils/scope_exit.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'host/include/uhd/utils/scope_exit.hpp')
-rw-r--r--host/include/uhd/utils/scope_exit.hpp55
1 files changed, 55 insertions, 0 deletions
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 <functional>
+#include <memory>
+
+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<scope_exit>;
+ using exit_cb_t = std::function<void(void)>;
+
+ // \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_t>(exit_cb)));
+ }
+
+ ~scope_exit()
+ {
+ _exit_cb();
+ }
+
+private:
+ scope_exit(std::function<void(void)>&& exit_cb)
+ : _exit_cb(std::forward<std::function<void(void)>>(exit_cb))
+ {
+ // nop
+ }
+
+ std::function<void(void)> _exit_cb;
+};
+
+}} /* namespace uhd::rfnoc */
+
+#endif /* INCLUDED_UHD_SCOPE_EXIT_HPP */
+