libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
application.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_APPLICATION_HPP
2 #define LIBJMMCG_CORE_APPLICATION_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2019 by J.M.McGuiness, coder@hussar.me.uk
6 **
7 ** This library is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU Lesser General Public
9 ** License as published by the Free Software Foundation; either
10 ** version 2.1 of the License, or (at your option) any later version.
11 **
12 ** This library is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ** Lesser General Public License for more details.
16 **
17 ** You should have received a copy of the GNU Lesser General Public
18 ** License along with this library; if not, write to the Free Software
19 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 #include "config.h"
23 #include "exit_codes.hpp"
24 
25 #include <boost/asio/ip/address.hpp>
26 
27 #include <csignal>
28 #include <iostream>
29 
30 namespace boost { namespace asio { namespace ip {
31 
32 /// Extract a v4 TCP/IP address from the supplied stream.
33 /**
34  Boost.asio.ip lacks stream-in operators, so isn't compatible with boost.program_options by default. Rather an omission in my opinion...
35 */
36 inline std::istream &
37 operator>>(std::istream &is, boost::asio::ip::address &addr) {
38  std::string s;
39  is>>s;
40  assert(!s.empty());
41  addr=boost::asio::ip::make_address(s);
42  return is;
43 }
44 
45 } } }
46 
47 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
48 
49 /// Derive from this to assist with signal handling in one's executable.
50 class application {
51 public:
52  using element_type=std::sig_atomic_t;
53 
54  /// Detect if a signal has been sent to the application.
55  /**
56  * For example in a loop one may test this to check if CTRL-C has been pressed and exit the loop thus permitting C++ dtors to run.
57  *
58  * \return Non-zero if a signal was trapped.
59  */
60  element_type signal_status() const noexcept(true);
61 
62 protected:
63  /**
64  * By default SIGINT & SIGQUIT are trapped.
65  */
66  application() noexcept(true);
67  application(application const &)=delete;
68  virtual ~application()=default;
69 
70 private:
71  static inline volatile element_type signal_status_{};
72 
73  static void signal_handler(int signal) noexcept(true);
74 };
75 
76 } }
77 
79 
80 #endif