Medusa  1.1
Coordinate Free Mehless Method implementation
assert.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_UTILS_ASSERT_HPP_
2 #define MEDUSA_BITS_UTILS_ASSERT_HPP_
3 
9 #include <tinyformat/tinyformat.h>
10 #include "print.hpp"
11 
12 namespace mm {
13 
14 // print macro
16 #define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1, 0)
17 #define VA_NUM_ARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
18 #define macro_dispatcher(func, ...) macro_dispatcher_(func, VA_NUM_ARGS(__VA_ARGS__))
19 #define macro_dispatcher_(func, nargs) macro_dispatcher__(func, nargs)
20 #define macro_dispatcher__(func, nargs) func ## nargs
21 #define addflag(a) {std::cerr << "flags=[flags, " << (a) << "];" << std::endl;}
22 #define prnv2(a, b) {std::cerr << a << " = " << (b) << ";" << std::endl;}
23 #define prnv1(a) {std::cerr << #a << " = " << (a) << ";" << std::endl;}
24 
34 #define prn(...) macro_dispatcher(prnv, __VA_ARGS__)(__VA_ARGS__)
35 
36 using tinyformat::printf;
37 using tinyformat::format;
38 
40 namespace assert_internal {
50 bool assert_handler_implementation(const char* condition, const char* file, const char* func_name,
51  int line, const char* message, tfm::FormatListRef format_list);
53 template<typename... Args>
54 bool assert_handler(const char* condition, const char* file, const char* func_name, int line,
55  const char* message, const Args&... args) { // unpacks first argument
56  tfm::FormatListRef arg_list = tfm::makeFormatList(args...);
57  return assert_handler_implementation(condition, file, func_name, line, message, arg_list);
58 }
59 } // namespace assert_internal
60 
61 #ifdef NDEBUG
62 #define assert_msg(cond, ...) ((void)sizeof(cond))
63 #else
64 
75 #define assert_msg(cond, ...) ((void)(!(cond) && \
76  mm::assert_internal::assert_handler( \
77  #cond, __FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) && (assert(0), 1)))
78 // #cond, __FILE__, __PRETTY_FUNCTION__, __LINE__, __VA_ARGS__) && (exit(1), 1)))
79 #endif
80 
85 inline void print_red(const std::string& s) { std::cout << "\x1b[31;1m" << s << "\x1b[37;0m"; }
90 inline void print_white(const std::string& s) { std::cout << "\x1b[37;1m" << s << "\x1b[37;0m"; }
95 inline void print_green(const std::string& s) { std::cout << "\x1b[32;1m" << s << "\x1b[37;0m"; }
96 
97 } // namespace mm
98 
99 #endif // MEDUSA_BITS_UTILS_ASSERT_HPP_
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
mm::assert_internal::assert_handler_implementation
bool assert_handler_implementation(const char *condition, const char *file, const char *func_name, int line, const char *message, tfm::FormatListRef format_list)
Actual assert implementation.
Definition: assert.cpp:12
mm::print_green
void print_green(const std::string &s)
Prints given text in bold green.
Definition: assert.hpp:95
mm::assert_internal::assert_handler
bool assert_handler(const char *condition, const char *file, const char *func_name, int line, const char *message, const Args &... args)
Assert handler that unpacks varargs.
Definition: assert.hpp:54
mm::print_red
void print_red(const std::string &s)
Prints given text in bold red.
Definition: assert.hpp:85
mm::print_white
void print_white(const std::string &s)
Prints given text in bold white.
Definition: assert.hpp:90
print.hpp