qxLib
asserts_manager.h
Go to the documentation of this file.
1 /**
2 
3  @file asserts_manager.h
4  @author Khrapov
5  @date 27.12.2025
6  @copyright © Nick Khrapov, 2025. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/algo/predicates.h>
14 #include <qx/logger/logger.h>
16 #include <qx/windows.h>
17 
18 namespace qx
19 {
20 
21 enum class assert_type
22 {
23  assert,
24  verify,
25  expect,
26  ensure
27 };
28 
29 /**
30 
31  @class asserts_manager
32  @brief Determines assertions behavior
33  @author Khrapov
34  @date 4.01.2026
35 
36 **/
37 class asserts_manager final : public singleton<asserts_manager>
38 {
39 public:
40  struct config
41  {
42  std::function<void(const category& category, assert_type eAssertType)> onExit =
43  [](const category& category, assert_type eAssertType)
44  {
45  // Normal exit after fatal assertions to avoid the second report
46  std::exit(0);
47  };
48 
49  std::function<verbosity(assert_type eAssertType)> getVerbosity = [](assert_type eAssertType)
50  {
51  switch (eAssertType)
52  {
53  case assert_type::assert:
54  case assert_type::verify:
55  return verbosity::critical;
56 
57  case assert_type::expect:
58  case assert_type::ensure:
59  default:
60  return verbosity::error;
61  }
62  };
63 
64  std::function<void(
65  string_view svCondition,
66  const category& category,
67  assert_type eAssertType,
68  string_view svUserMessage,
69  string_view svFunction,
70  string_view svFile,
71  i32 nLine)>
72  onAssertion;
73 
74  bool bLogAssertion = true;
75  };
76 
77 public:
78  /**
79  @brief An entry point for all assertions
80  @param svCondition - a string representation of the condition
81  @param category - a category of the assertion (file wide or manually specified)
82  @param eAssertType - an assertion type
83  @param sUserMessage - formatted user message, if specified
84  @param svFunction - assert location function
85  @param svFile - assert location file
86  @param nLine - assert location line number
87  @retval - true if debug break is required
88  **/
89  bool do_assert(
90  string_view svCondition,
91  const category& category,
92  assert_type eAssertType,
93  string sUserMessage,
94  string_view svFunction,
95  string_view svFile,
96  i32 nLine);
97 
98  /**
99  @brief Called in fatal assertions to exit the application
100  @param category - a category of the assertion (file wide or manually specified)
101  @param eAssertType - an assertion type
102  **/
103  void exit(const category& category, assert_type eAssertType);
104 
105  /**
106  @brief Get config
107  @retval - existing config
108  **/
109  const config& get_config() const;
110 
111  /**
112  @brief Set config
113  @param config - new config
114  **/
115  void set_config(config config);
116 
117 private:
118  config m_Config;
119 };
120 
121 } // namespace qx
122 
123 #if QX_MSVC
124  #define _QX_DEBUG_BREAK() __debugbreak()
125 #elif QX_CLANG
126  #define _QX_DEBUG_BREAK() __builtin_debugtrap()
127 #elif QX_GNU
128  #include <signal.h>
129  #define _QX_DEBUG_BREAK() raise(SIGTRAP)
130 #else
131  #define _QX_DEBUG_BREAK() QX_EMPTY_MACRO
132 #endif
133 
134 #ifndef QX_DEBUG_BREAK
135  #define QX_DEBUG_BREAK() _QX_DEBUG_BREAK()
136 #endif
137 
138 #define _QX_COMMON_ASSERT(condition, category, assert_type, after_debug_break, result_t, ...) \
139  static_cast<result_t>( \
140  qx::predicates::is_valid(condition) \
141  || (qx::asserts_manager::get_instance().do_assert( \
142  qx::details::get_assert_condition_string<QXT(#condition)>(condition), \
143  category, \
144  assert_type, \
145  std::move(_QX_MACRO_USER_MESSAGE(static_cast<qx::string_pool<>*>(nullptr), ##__VA_ARGS__).sValue), \
146  qx::convert_string_literal<qx::char_type, __FUNCTION__>(), \
147  QXT(__FILE__), \
148  QX_LINE) \
149  ? QX_DEBUG_BREAK() \
150  : (void)0, \
151  after_debug_break(category, assert_type), \
152  false))
153 
154 #ifndef _QX_ASSERT_AFTER_DEBUG_BREAK_FATAL
155  #define _QX_ASSERT_AFTER_DEBUG_BREAK_FATAL(category, assert_type) \
156  qx::asserts_manager::get_instance().exit(category, assert_type)
157 #endif
158 
159 #ifndef _QX_ASSERT_AFTER_DEBUG_BREAK_NON_FATAL
160  #define _QX_ASSERT_AFTER_DEBUG_BREAK_NON_FATAL(category, assert_type) false
161 #endif
162 
163 #ifndef QX_WITH_DEBUG_ASSERTS
164  #define QX_WITH_DEBUG_ASSERTS QX_WITH_DEBUG_INFO
165 #endif
166 
Determines assertions behavior.
void set_config(config config)
Set config.
void exit(const category &category, assert_type eAssertType)
Called in fatal assertions to exit the application.
bool do_assert(string_view svCondition, const category &category, assert_type eAssertType, string sUserMessage, string_view svFunction, string_view svFile, i32 nLine)
An entry point for all assertions.
const config & get_config() const
Get config.
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:59
Inherit the necessary singleton class from this.
std::int32_t i32
− 9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807
Definition: typedefs.h:32