#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
Go to the source code of this file.
|
std::vector< std::string > | split (const std::string &s, char delimiter) |
| Helper function to split a string by a delimiter. More...
|
|
template<typename... Args> |
std::string | format_args (const std::string &names, const Args &... args) |
| The main variadic template function to log the arguments. More...
|
|
◆ FUNCTION_NAME
#define FUNCTION_NAME __func__ |
◆ THROW_ON_INVALID_ARG
#define THROW_ON_INVALID_ARG |
( |
|
condition, |
|
|
|
... |
|
) |
| |
Value: do { \
if (!(condition)) { \
std::stringstream ss; \
ss << "\n Condition '" << #condition << "' failed." \
<<
"\n With: " <<
format_args(#__VA_ARGS__, __VA_ARGS__) \
<< "\n At file: " << __FILE__ << "\n At line: " << __LINE__; \
throw std::invalid_argument(ss.str()); \
} \
} \
while (0)
std::string format_args(const std::string &names, const Args &... args)
The main variadic template function to log the arguments.
Definition at line 85 of file exceptions.h.
◆ format_args()
template<typename... Args>
std::string format_args |
( |
const std::string & |
names, |
|
|
const Args &... |
args |
|
) |
| |
It unpacks the names and values from the macro call and returns a string of name = value pairs.
- Template Parameters
-
Args | The types of the arguments. |
- Parameters
-
names | - The comma-separated string of argument names. |
args | - The actual argument values. |
- Returns
- The string containing the formatted log message.
Definition at line 54 of file exceptions.h.
55{
56 std::stringstream ss;
57
58 std::vector<std::string> arg_names =
split(names,
',');
59
60 std::size_t i = 0;
61
62 auto printer = [&](const auto& arg_value) {
63 if (i < arg_names.size()) {
64 if (i > 0) ss << ", ";
65 ss << arg_names[i] << " = " << arg_value;
66 }
67 i++;
68 };
69
70 (printer(args), ...);
71
72 return ss.str();
73}
std::vector< std::string > split(const std::string &s, char delimiter)
Helper function to split a string by a delimiter.
References split().
◆ split()
std::vector< std::string > split |
( |
const std::string & |
s, |
|
|
char |
delimiter |
|
) |
| |
|
inline |
This is used to parse the comma-separated argument names.
- Parameters
-
s | - The string to split. |
delimiter | - The character delimiter. |
- Returns
- A vector of the split strings.
Definition at line 20 of file exceptions.h.
21{
22 std::string token;
23 std::vector<std::string> tokens;
24 std::istringstream tokenStream(s);
25 while (std::getline(tokenStream, token, delimiter)) {
26
27 size_t first = token.find_first_not_of(" \t\n\r");
28 if (std::string::npos != first) {
29 size_t last = token.find_last_not_of(" \t\n\r");
30 tokens.push_back(token.substr(first, (last - first + 1)));
31 }
32 else {
33 tokens.push_back("");
34 }
35 }
36 return tokens;
37}
Referenced by format_args().