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