USRP Hardware Driver and USRP Manual  Version: 4.4.0.HEAD-0-g5fac246b
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 
12 namespace uhd { namespace utils {
13 
23 {
24 public:
25  using uptr = std::unique_ptr<scope_exit>;
26  using exit_cb_t = std::function<void(void)>;
27 
28  // \param exit_b The function object ("exit callback") that gets executed
29  // in the destructor
30  static uptr make(exit_cb_t&& exit_cb)
31  {
32  // When we have C++14, use make_unique instead (TODO)
33  return uptr(new scope_exit(std::forward<exit_cb_t>(exit_cb)));
34  }
35 
37  {
38  _exit_cb();
39  }
40 
41 private:
42  scope_exit(std::function<void(void)>&& exit_cb)
43  : _exit_cb(std::forward<std::function<void(void)>>(exit_cb))
44  {
45  // nop
46  }
47 
48  std::function<void(void)> _exit_cb;
49 };
50 
51 }} // namespace uhd::utils
uhd::utils::scope_exit::uptr
std::unique_ptr< scope_exit > uptr
Definition: scope_exit.hpp:25
uhd
Definition: build_info.hpp:12
uhd::utils::scope_exit::exit_cb_t
std::function< void(void)> exit_cb_t
Definition: scope_exit.hpp:26
uhd::utils::scope_exit::make
static uptr make(exit_cb_t &&exit_cb)
Definition: scope_exit.hpp:30
uhd::utils::scope_exit
Definition: scope_exit.hpp:22
uhd::utils::scope_exit::~scope_exit
~scope_exit()
Definition: scope_exit.hpp:36