libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
exit_codes.hpp
Go to the documentation of this file.
1 #ifndef LIBJMMCG_CORE_EXIT_CODES_HPP
2 #define LIBJMMCG_CORE_EXIT_CODES_HPP
3 
4 /******************************************************************************
5 ** Copyright © 2016 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 
24 #include <cassert>
25 #include <cerrno>
26 #include <cstdint>
27 #include <sstream>
28 #include <string>
29 #include <tuple>
30 
31 #include <csignal>
32 
33 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
34 
35  /// A nice enum for the exit codes from main().
36  /**
37  There is some conflicting advice regarding user-defined exit coeds and permissible values:
38  -# <a href="https://www.tldp.org/LDP/abs/html/exitcodes.html"/> - between 64-113.
39  -# Also see <a href="https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+4.3-RELEASE&format=html">sysexits.h</a> implies values over 128 should be used.
40 
41  Note use of POSIX compatibility macros & starting our custom errors at 129. Also note that we shouldn't exceed 255, as the exit code is truncated to 8 bits, which would cause problems...
42  */
43  class exit_codes {
44  public:
45  enum codes : std::uint8_t {
46  exit_success=EXIT_SUCCESS,
47  exit_unknown_failure=EXIT_FAILURE,
48  exit_unknown_exception=128+_NSIG, ///< User-defined codes start from here according to "signum-generic.h".
55  };
56 
57  static std::string
58  to_string(codes e) noexcept(false);
59  static std::string
60  to_string() noexcept(false);
61 
62  private:
63  static inline constexpr std::tuple const descriptions{
64  std::tuple{exit_success, "exit_success", "The process completed successfully."},
65  std::tuple{exit_unknown_failure, "exit_unknown_failure", "Error: an unknown failure-condition caused the process to exit."},
66  std::tuple{exit_unknown_exception, "exit_unknown_exception", "Error: an unknown exception was thrown that caused the process to exit."},
67  std::tuple{exit_stl_exception, "exit_stl_exception", "Error: an STL-derived exception was thrown that caused the process to exit."},
68  std::tuple{exit_jmmcg_exception, "exit_jmmcg_exception", "Error: a libjmmcg-derived exception was thrown that caused the process to exit."},
69  std::tuple{exit_crt_exception, "exit_crt_exception", "Error: a libjmmcg-derived CRT-exception was thrown that caused the process to exit."},
70  std::tuple{exit_parameter_error, "exit_parameter_error", "Error: an incorrect set of command-line options caused the process to exit."},
71  std::tuple{exit_print_help, "exit_print_help", "The '--help' command-line option caused the process to exit."},
72  std::tuple{exit_print_version, "exit_print_version", "The '--version' command-line option caused the process to exit."}
73  };
74  };
75 
76 } }
77 
78 #include "exit_codes_impl.hpp"
79 
80 #endif