piranha  0.10
exceptions.hpp
Go to the documentation of this file.
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_EXCEPTIONS_HPP
30 #define PIRANHA_EXCEPTIONS_HPP
31 
38 #include <stdexcept>
39 #include <string>
40 #include <type_traits>
41 #include <utility>
42 
43 #include <piranha/type_traits.hpp>
44 
45 namespace piranha
46 {
47 inline namespace impl
48 {
49 
50 template <typename Exception>
51 struct ex_thrower {
52  // Determine the type of the __LINE__ macro.
53  using line_type = uncvref_t<decltype(__LINE__)>;
54  explicit ex_thrower(const char *file, line_type line, const char *func) : m_file(file), m_line(line), m_func(func)
55  {
56  }
57  template <typename... Args, typename = enable_if_t<std::is_constructible<Exception, Args &&...>::value>>
58  [[noreturn]] void operator()(Args &&... args) const
59  {
60  throw Exception(std::forward<Args>(args)...);
61  }
62  template <typename Str>
63  using str_add_t = decltype(std::declval<std::string &>() += std::declval<Str>());
64  template <typename Str, typename... Args,
65  typename = enable_if_t<conjunction<std::is_constructible<Exception, std::string, Args &&...>,
66  is_detected<str_add_t, Str &&>>::value>>
67  [[noreturn]] void operator()(Str &&desc, Args &&... args) const
68  {
69  std::string msg("\nfunction: ");
70  msg += m_func;
71  msg += "\nwhere: ";
72  msg += m_file;
73  msg += ", ";
74  msg += std::to_string(m_line);
75  msg += "\nwhat: ";
76  msg += std::forward<Str>(desc);
77  msg += "\n";
78  throw Exception(msg, std::forward<Args>(args)...);
79  }
80  const char *m_file;
81  const line_type m_line;
82  const char *m_func;
83 };
84 }
85 }
86 
88 
115 // NOTE: we need the struct here because we need to split off the __VA_ARGS__ into a separate function call, otherwise
116 // there could be situations in which the throwing function would be called with a set of arguments (a,b,c,), which
117 // would be invalid syntax.
118 #define piranha_throw(exception_type, ...) \
119  piranha::ex_thrower<exception_type>(__FILE__, __LINE__, __func__)(__VA_ARGS__)
120 
121 namespace piranha
122 {
123 
125 
128 struct not_implemented_error final : std::runtime_error {
129  using std::runtime_error::runtime_error;
130 };
131 
133 
136 struct zero_division_error final : std::domain_error {
137  using std::domain_error::domain_error;
138 };
139 }
140 
141 #endif
Exception for signalling division by zero.
Definition: exceptions.hpp:136
Root piranha namespace.
Definition: array_key.hpp:52
Exception for functionality not implemented or not available on the current platform.
Definition: exceptions.hpp:128
Type traits.