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