OGLplus  (0.59.0) a C++ wrapper for rendering APIs

signal_switch.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_SIGNAL_SWITCH_HPP
10 #define EAGINE_SIGNAL_SWITCH_HPP
11 
12 #include <csignal>
13 
14 namespace eagine {
15 
24 public:
27  signal_switch() noexcept
28  : _intr_handler{std::signal(SIGINT, &_flip)}
29  , _term_handler{std::signal(SIGTERM, &_flip)} {}
30 
32  ~signal_switch() noexcept {
33  std::signal(SIGINT, _intr_handler);
34  std::signal(SIGTERM, _term_handler);
35  }
36 
38  signal_switch(signal_switch&&) = delete;
40  signal_switch(const signal_switch&) = delete;
42  auto operator=(signal_switch&&) = delete;
44  auto operator=(const signal_switch&) = delete;
45 
48  auto reset() noexcept -> signal_switch& {
49  _state() = 0;
50  return *this;
51  }
52 
54  auto interrupted() const noexcept -> bool {
55  return _state() == SIGINT;
56  }
57 
59  auto terminated() const noexcept -> bool {
60  return _state() == SIGTERM;
61  }
62 
64  explicit operator bool() const noexcept {
65  return bool(_state());
66  }
67 
68 private:
69  static auto _state() noexcept -> volatile std::sig_atomic_t& {
70  static volatile std::sig_atomic_t state{0};
71  return state;
72  }
73 
74  static void _flip(int sig_num) {
75  _state() = sig_num;
76  }
77 
78  using _sighandler_t = void(int);
79  _sighandler_t* _intr_handler{nullptr};
80  _sighandler_t* _term_handler{nullptr};
81 };
82 
83 } // namespace eagine
84 
85 #endif // EAGINE_SIGNAL_SWITCH_HPP
auto terminated() const noexcept -> bool
Indicates if the terminate signal was received.
Definition: signal_switch.hpp:59
Common code is placed in this namespace.
Definition: eagine.hpp:21
Installs handlers for interrupt signals and flips switch on receipt.
Definition: signal_switch.hpp:23
auto interrupted() const noexcept -> bool
Indicates if the interrupt signal was received.
Definition: signal_switch.hpp:54
signal_switch() noexcept
Default constructor. Stores the original handlers if any.
Definition: signal_switch.hpp:27
auto reset() noexcept -> signal_switch &
Resets the signal state as if no signal was received.
Definition: signal_switch.hpp:48
~signal_switch() noexcept
Destructor. Restores the original signal handlers.
Definition: signal_switch.hpp:32
auto operator=(signal_switch &&)=delete
Not move-assignable.

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).