USRP Hardware Driver and USRP Manual  Version: 4.9.0.0
UHD and USRP Manual
scope_exit.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2019 Ettus Research, a National Instruments Brand
3 //
4 // SPDX-License-Identifier: GPL-3.0-or-later
5 //
6 
7 #pragma once
8 
9 #include <functional>
10 #include <memory>
11 #include <utility>
12 
13 namespace uhd { namespace utils {
14 
24 {
25 public:
26  using uptr = std::unique_ptr<scope_exit>;
27  using exit_cb_t = std::function<void(void)>;
28 
29  // \param exit_b The function object ("exit callback") that gets executed
30  // in the destructor
31  static uptr make(exit_cb_t&& exit_cb)
32  {
33  // Note: We can't use make_unique() here because it requires the
34  // constructor to be public.
35  return uptr(new scope_exit(std::forward<exit_cb_t>(exit_cb)));
36  }
37 
39  {
40  _exit_cb();
41  }
42 
43 private:
44  scope_exit(exit_cb_t&& exit_cb) : _exit_cb(std::forward<exit_cb_t>(exit_cb))
45  {
46  // nop
47  }
48 
49  exit_cb_t _exit_cb;
50 };
51 
52 }} // namespace uhd::utils
uhd::utils::scope_exit::uptr
std::unique_ptr< scope_exit > uptr
Definition: scope_exit.hpp:26
uhd
Definition: build_info.hpp:12
uhd::utils::scope_exit::exit_cb_t
std::function< void(void)> exit_cb_t
Definition: scope_exit.hpp:27
uhd::utils::scope_exit::make
static uptr make(exit_cb_t &&exit_cb)
Definition: scope_exit.hpp:31
uhd::utils::scope_exit
Definition: scope_exit.hpp:23
uhd::utils::scope_exit::~scope_exit
~scope_exit()
Definition: scope_exit.hpp:38