Vcsn  2.8
Be Rational
raise.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sstream>
4 #include <stdexcept>
5 #include <utility>
6 
8 #include <vcsn/misc/escape.hh>
9 #include <vcsn/misc/to-string.hh> // to_string
10 
11 namespace vcsn
12 {
13  namespace detail
14  {
18  struct pass
19  {
20  template <typename ...T> pass(T...) {}
21  };
22 
23 
25  template <typename T>
26  void print_(std::ostream& o, const T& arg, long)
27  {
28  o << arg;
29  }
30 
34  inline void print_(std::ostream& o, std::istream& is, long)
35  {
36  is.clear();
37  auto buf = std::string{};
38  std::getline(is, buf, '\n');
39  if (!is.good())
40  // This shouldn't really happen; however it's best to fail cleanly.
41  is.clear();
42  o << str_quote(buf);
43  }
44 
46  inline void print_(std::ostream& o, std::istringstream& is, long)
47  {
48  print_(o, static_cast<std::istream&>(is), 0);
49  }
50 
52  template <typename T>
53  auto print_(std::ostream& o, const T& arg, int)
54  -> decltype(arg.print_set(o), void())
55  {
56  arg.print_set(o);
57  }
58 
60  template <typename T>
61  auto print_(std::ostream& o, const T& e, int)
62  -> decltype(e.what(), void())
63  {
64  o << e.what() << '\n';
65  }
66  }
67 
69  template <typename... Args>
70  ATTRIBUTE_NORETURN
71  void raise(Args&&... args)
72  {
73  std::ostringstream o;
74  using swallow = int[];
75  (void) swallow
76  {
77  (detail::print_(o, args, 0), 0)...
78  };
79  throw std::runtime_error{o.str()};
80  }
81 
86  template <typename Bool, typename... Args>
87  void require(Bool b, Args&&... args)
88  {
89  if (!b)
90  raise(std::forward<Args>(args)...);
91  }
92 }
93 
98 #define VCSN_REQUIRE(Cond, ...) \
99  do { \
100  if (!(Cond)) \
101  ::vcsn::raise(__VA_ARGS__); \
102  } while (false)
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
Ignore its arguments.
Definition: raise.hh:18
Definition: a-star.hh:8
std::string str_quote(Args &&... args)
Convert to a string, in quotes.
Definition: escape.hh:49
void require(Bool b, Args &&... args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:87
void print_(std::ostream &o, const T &arg, long)
Serialize arg into o.
Definition: raise.hh:26