Vcsn  2.2
Be Rational
raise.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdexcept>
4 #include <sstream>
5 #include <utility>
6 
8 #include <vcsn/misc/escape.hh>
9 
10 namespace vcsn
11 {
12  namespace detail
13  {
17  struct pass
18  {
19  template <typename ...T> pass(T...) {}
20  };
21 
22 
24  template <typename T>
25  void print_(std::ostream& o, const T& arg, long)
26  {
27  o << arg;
28  }
29 
33  inline void print_(std::ostream& o, std::istream& is, long)
34  {
35  is.clear();
36  std::string buf;
37  std::getline(is, buf, '\n');
38  if (!is.good())
39  // This shouldn't really happen; however it's best to fail cleanly.
40  is.clear();
41  o << str_escape(buf);
42  }
43 
45  inline void print_(std::ostream& o, std::istringstream& is, long)
46  {
47  print_(o, static_cast<std::istream&>(is), 0);
48  }
49 
51  template <typename T>
52  auto print_(std::ostream& o, const T& arg, int)
53  -> decltype(arg.print_set(o), void())
54  {
55  arg.print_set(o);
56  }
57  }
58 
60  template <typename... Args>
61  ATTRIBUTE_NORETURN
62  inline void raise(Args&&... args)
63  {
64  std::ostringstream o;
65  using swallow = int[];
66  (void) swallow
67  {
68  (detail::print_(o, args, 0), 0)...
69  };
70  throw std::runtime_error{o.str()};
71  }
72 
77  template <typename Bool, typename... Args>
78  inline void require(Bool b, Args&&... args)
79  {
80  if (!b)
81  raise(std::forward<Args>(args)...);
82  }
83 }
84 
89 #define VCSN_REQUIRE(Cond, ...) \
90  do { \
91  if (!(Cond)) \
92  ::vcsn::raise(__VA_ARGS__); \
93  } while (false)
Ignore its arguments.
Definition: raise.hh:17
Definition: a-star.hh:8
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:78
std::ostream & str_escape(std::ostream &os, const std::string &str, const char *special=nullptr)
Output a string, escaping special characters.
Definition: escape.cc:54
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
void print_(std::ostream &o, const T &arg, long)
Serialize arg into o.
Definition: raise.hh:25
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:380