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