OscProb
exceptions.h File Reference
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

Go to the source code of this file.

Macros

#define FUNCTION_NAME   __func__
 
#define THROW_ON_INVALID_ARG(condition, ...)
 

Functions

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...
 

Macro Definition Documentation

◆ FUNCTION_NAME

#define FUNCTION_NAME   __func__

Definition at line 81 of file exceptions.h.

◆ 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 In function: " << FUNCTION_NAME \
<< "\n At file: " << __FILE__ << "\n At line: " << __LINE__; \
throw std::invalid_argument(ss.str()); \
} \
} \
while (0)
#define FUNCTION_NAME
Definition: exceptions.h:81
std::string format_args(const std::string &names, const Args &... args)
The main variadic template function to log the arguments.
Definition: exceptions.h:54

Definition at line 85 of file exceptions.h.

Function Documentation

◆ 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
ArgsThe 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 // Lambda to iterate and print each argument.
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 // Unpack the arguments and print them using the lambda.
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.
Definition: exceptions.h:20

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 // Trim leading/trailing whitespace
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().