Ookii.CommandLine for C++  2.0.0
scope_helper.h
Go to the documentation of this file.
1 #ifndef OOKII_SCOPE_HELPER_H_
4 #define OOKII_SCOPE_HELPER_H_
5 
6 #include <functional>
7 
8 namespace ookii::details
9 {
10  class scope_exit
11  {
12  public:
13  scope_exit() = default;
14 
15  scope_exit(std::function<void()> callback)
16  : _callback{callback}
17  {
18  }
19 
20  scope_exit(scope_exit &) = delete;
21  scope_exit &operator=(scope_exit &) = delete;
22 
23  ~scope_exit()
24  {
25  reset();
26  }
27 
28  void reset(std::function<void()> callback = {})
29  {
30  if (_callback)
31  {
32  _callback();
33  }
34 
35  _callback = callback;
36  }
37 
38  void release()
39  {
40  _callback = {};
41  }
42 
43  private:
44  std::function<void()> _callback;
45  };
46 }
47 
48 #endif