Reaktoro  v2.11.0
A unified framework for modeling chemically reactive systems
Exception.hpp
1 // Reaktoro is a unified framework for modeling chemically reactive systems.
2 //
3 // Copyright © 2014-2024 Allan Leal
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library. If not, see <http://www.gnu.org/licenses/>.
17 
18 #pragma once
19 
20 // C++ includes
21 #include <exception>
22 #include <iostream>
23 #include <sstream>
24 #include <stdexcept>
25 #include <string>
26 
27 // Reaktoro includes
28 #include <Reaktoro/Common/StringUtils.hpp>
29 
30 namespace Reaktoro {
31 
45 struct Exception
46 {
48  std::stringstream error;
49 
51  std::stringstream reason;
52 };
53 
54 namespace internal {
55 
62 std::string message(const Exception& exception, const std::string& file, int line);
63 
64 } // namespace internal
65 
69 #define RaiseError(exception) \
70  throw std::runtime_error(Reaktoro::internal::message(exception, __FILE__, __LINE__));
71 
75 #define RuntimeError(errorstr, reasonstr) \
76  { \
77  Reaktoro::Exception exception; \
78  exception.error << errorstr; \
79  exception.reason << reasonstr; \
80  RaiseError(exception); \
81  }
82 
86 #define Assert(condition, errorstr, reasonstr) \
87  { \
88  if(!(condition)) { \
89  Reaktoro::Exception exception; \
90  exception.error << errorstr; \
91  exception.reason << reasonstr; \
92  RaiseError(exception); \
93  } \
94  }
95 
97 template<typename... Args>
98 auto warning(bool condition, Args... items) -> void
99 {
100  if(condition)
101  std::cerr << "\033[1;33m***WARNING*** " << Reaktoro::str(items...) << "\n\033[0m";
102 }
103 
105 template<typename... Args>
106 auto error(bool condition, Args... items) -> void
107 {
108  if(condition)
109  throw std::runtime_error(Reaktoro::str("\033[1;31m***ERROR*** ", Reaktoro::str(items...), "\n\033[0m"));
110 }
111 
118 #define warningif(condition, ...) \
119  { \
120  if((condition)) { \
121  std::cerr << "\033[1;33m***WARNING***\n" << Reaktoro::str(__VA_ARGS__) << "\n\033[0m"; \
122  } \
123  }
124 
127 #define warningifnot(condition, ...) \
128  { \
129  if(!(condition)) { \
130  std::cerr << "\033[1;33m***WARNING***\n" << Reaktoro::str(__VA_ARGS__) << "\n\033[0m"; \
131  } \
132  }
133 
140 #define errorif(condition, ...) \
141  { \
142  if((condition)) { \
143  throw std::runtime_error(Reaktoro::str("\033[1;31m***ERROR***\n", Reaktoro::str(__VA_ARGS__), "\n\033[0m")); \
144  } \
145  }
146 
149 #define errorifnot(condition, ...) \
150  { \
151  if(!(condition)) { \
152  throw std::runtime_error(Reaktoro::str("\033[1;31m***ERROR***\n", Reaktoro::str(__VA_ARGS__), "\n\033[0m")); \
153  } \
154  }
155 
156 } // namespace Reaktoro
The namespace containing all components of the Reaktoro library.
Definition: Algorithms.hpp:29
auto error(bool condition, Args... items) -> void
Raise a runtime error if condition is true.
Definition: Exception.hpp:106
auto warning(bool condition, Args... items) -> void
Issue a warning message if condition is true.
Definition: Exception.hpp:98
auto str(Args... items) -> std::string
Concatenate the arguments into a string without any separator string.
Definition: StringUtils.hpp:73
Provides a convenient way to initialized an exception with helpful error messages.
Definition: Exception.hpp:46
std::stringstream reason
The reason message to be displayed when the exception is raised.
Definition: Exception.hpp:51
std::stringstream error
The error message to be displayed when the exception is raised.
Definition: Exception.hpp:48