PLSSVM - Parallel Least Squares Support Vector Machine  2.0.0
A Least Squares Support Vector Machine implementation using different backends.
assert.hpp
Go to the documentation of this file.
1 
12 #ifndef PLSSVM_DETAIL_ASSERT_HPP_
13 #define PLSSVM_DETAIL_ASSERT_HPP_
14 #pragma once
15 
16 #include "plssvm/exceptions/source_location.hpp" // plssvm::source_location
17 
18 #include "fmt/color.h" // fmt::emphasis, fmt::fg, fmt::color
19 #include "fmt/core.h" // fmt::format
20 
21 #include <cstdlib> // std::abort
22 #include <iostream> // std::cerr, std::endl
23 #include <string_view> // std::string_view
24 #include <utility> // std::forward
25 
26 namespace plssvm::detail {
27 
38 template <typename... Args>
39 inline void check_assertion(const bool cond, const std::string_view cond_str, const source_location &loc, const std::string_view msg, Args &&...args) {
40  // check if assertion holds
41  if (!cond) {
42  // print assertion error message
43  std::cerr << fmt::format(
44  "Assertion '{}' failed!\n"
45  " in file {}\n"
46  " in function {}\n"
47  " @ line {}\n\n"
48  "{}\n",
49  fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::green), "{}", cond_str),
50  loc.file_name(),
51  loc.function_name(),
52  loc.line(),
53  fmt::format(fmt::emphasis::bold | fmt::fg(fmt::color::red), msg, std::forward<Args>(args)...))
54  << std::endl;
55 
56  // abort further execution
57  std::abort();
58  }
59 }
60 
65 #if defined(PLSSVM_ENABLE_ASSERTS) || !defined(NDEBUG)
66  #define PLSSVM_ASSERT_ENABLED
67 #endif
68 
73 #if defined(PLSSVM_ASSERT_ENABLED)
74  #define PLSSVM_ASSERT(cond, msg, ...) plssvm::detail::check_assertion((cond), (#cond), plssvm::source_location::current(), (msg), ##__VA_ARGS__)
75 #else
76  #define PLSSVM_ASSERT(cond, msg, ...)
77 #endif
78 
79 } // namespace plssvm::detail
80 
81 #endif // PLSSVM_DETAIL_ASSERT_HPP_
The plssvm::source_location class represents certain information about the source code,...
Definition: source_location.hpp:25
constexpr std::string_view file_name() const noexcept
Returns the function name without additional signature information (i.e. return type and parameters) ...
Definition: source_location.hpp:60
constexpr std::uint_least32_t line() const noexcept
Returns the line number or 0 if no information could be retrieved.
Definition: source_location.hpp:65
constexpr std::string_view function_name() const noexcept
Returns the absolute path name of the file or "unknown" if no information could be retrieved.
Definition: source_location.hpp:54
Namespace containing implementation details. Should not directly be used by users.
Definition: csvm.hpp:27
void check_assertion(const bool cond, const std::string_view cond_str, const source_location &loc, const std::string_view msg, Args &&...args)
Function called by the PLSSVM_ASSERT macro. Checks the assertion condition. If the condition evaluate...
Definition: assert.hpp:39
Implements a custom std::source_location implementation for C++17.