SignalHandler.hpp
Go to the documentation of this file.
1 
5 #pragma once
6 
7 #include <csignal>
8 #include <functional>
9 #include <list>
10 #include <map>
11 #include <mutex>
12 
13 namespace signal_handler
14 {
21 {
22 public:
23  SignalHandler() = delete;
24 
25  using Handler = std::function<void(int)>;
26 
27  template <typename T>
28  static void bind(int signal, void (T::*fp)(int), T* object)
29  {
30  SignalHandler::bind(signal, std::bind(fp, object, std::placeholders::_1));
31  }
32 
33  static void bind(int signal, const Handler& handler);
34 
35  template <typename T>
36  static void bindAll(void (T::*fp)(int), T* object)
37  {
38  const Handler handler = std::bind(fp, object, std::placeholders::_1);
39  SignalHandler::bind(SIGINT, handler);
40  SignalHandler::bind(SIGTERM, handler); // shell command kill
41  SignalHandler::bind(SIGABRT, handler); // invoked by abort();
42  SignalHandler::bind(SIGFPE, handler);
43  SignalHandler::bind(SIGILL, handler);
44  SignalHandler::bind(SIGQUIT, handler); // the QUIT character, usually C-'\'
45  SignalHandler::bind(SIGHUP, handler); // hang-up” signal is used to report that the user’s terminal is disconnected
46  // SIGKILL cannot be handled
47  }
48 
49  template <typename T>
50  static void unbind(int signal, void (T::*fp)(int), T* object)
51  {
52  SignalHandler::unbind(signal, std::bind(fp, object, std::placeholders::_1));
53  }
54 
55  static void unbind(int signal, const Handler& handler);
56 
57  template <typename T>
58  static void unbindAll(void (T::*fp)(int), T* object)
59  {
60  const Handler handler = std::bind(fp, object, std::placeholders::_1);
61  SignalHandler::unbind(SIGINT, handler);
62  SignalHandler::unbind(SIGTERM, handler);
63  SignalHandler::unbind(SIGABRT, handler);
64  SignalHandler::unbind(SIGFPE, handler);
65  SignalHandler::unbind(SIGILL, handler);
66  SignalHandler::unbind(SIGQUIT, handler);
67  SignalHandler::unbind(SIGHUP, handler);
68  }
69 
70 private:
71  static std::map<int, std::list<Handler> > handlers_;
72  static std::mutex mutex_;
73 
74  static void signaled(int signal);
75 };
76 
77 } // namespace signal_handler
static void unbind(int signal, void(T::*fp)(int), T *object)
static std::map< int, std::list< Handler > > handlers_
static void unbindAll(void(T::*fp)(int), T *object)
static void bind(int signal, void(T::*fp)(int), T *object)
static void signaled(int signal)
std::function< void(int)> Handler
static void bindAll(void(T::*fp)(int), T *object)


bota_signal_handler
Author(s):
autogenerated on Wed Mar 3 2021 03:09:09