libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
exit_codes_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2019 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
20 
21  namespace private_ {
22  template<std::size_t I>
23  struct finder {
24  template<class E, class T>
25  static constexpr auto get(E e, T const &t) noexcept(true) {
26  auto const &v=std::get<I>(t);
27  return std::get<0>(v)==e ? std::get<1>(v) : finder<I-1>::get(e, t);
28  }
29  };
30  template<>
31  struct finder<0> {
32  template<class E, class T>
33  static constexpr auto get([[maybe_unused]] E e, T const &t) noexcept(true) {
34  auto const &v=std::get<0>(t);
35  assert(std::get<0>(v)==e);
36  return std::get<1>(v);
37  }
38  };
39  template<std::size_t I>
40  struct loop {
41  template<class T, class Fn>
42  static constexpr void get(T const &t, Fn &&f) noexcept(false) {
43  auto const &v=std::get<I>(t);
44  f(v);
45  loop<I-1>::get(t, std::move(f));
46  }
47  };
48  template<>
49  struct loop<0> {
50  template<class T, class Fn>
51  static constexpr void get(T const &t, Fn &&f) noexcept(false) {
52  auto const &v=std::get<0>(t);
53  f(v);
54  }
55  };
56  }
57 
58  inline std::string
59  exit_codes::to_string(codes e) noexcept(false) {
60  return private_::finder<std::tuple_size<decltype(descriptions)>::value-1>::get(e, descriptions);
61  }
62 
63  inline std::string
64  exit_codes::to_string() noexcept(false) {
65  std::ostringstream os;
66  os<<"Exit-code descriptions (error-conditions are output to 'std::cerr'):\n";
67  auto print_element=[&os](auto const &desc) {
68  os<<'['<<std::get<0>(desc)<<"] "<<std::get<1>(desc)<<": "<<std::get<2>(desc)<<'\n';
69  };
70  private_::loop<std::tuple_size<decltype(descriptions)>::value-1>::get(descriptions, std::move(print_element));
71  return os.str();
72  }
73 
74 } }