11//
2- // Copyright 2014 - 2025 (C). Alex Robenko. All rights reserved.
2+ // Copyright 2014 - 2026 (C). Alex Robenko. All rights reserved.
33//
44// This Source Code Form is subject to the terms of the Mozilla Public
55// License, v. 2.0. If a copy of the MPL was not distributed with this
1515#include < type_traits>
1616#include < utility>
1717
18- namespace comms
19- {
18+ namespace comms {
2019
2120// / @brief Base class for any custom assertion behaviour.
2221// / @details In order to implement custom assertion failure behaviour it
2322// / is necessary to inherit from this class and override
2423// / fail() virtual member function.
2524// / @headerfile comms/Assert.h
26- class Assert
27- {
25+ class Assert {
2826public:
29- // / @brief Destructor
30- virtual ~Assert () noexcept {}
31-
32- // / @brief Pure virtual function to be called when assertion fails.
33- // / @param[in] expr Assertion condition/expression
34- // / @param[in] file File name
35- // / @param[in] line Line number of the assert statement.
36- // / @param[in] function Function name.
37- virtual void fail (
38- const char * expr,
39- const char * file,
40- unsigned int line,
41- const char * function) = 0;
27+ // / @brief Destructor
28+ virtual ~Assert () noexcept {}
29+
30+ // / @brief Pure virtual function to be called when assertion fails.
31+ // / @param[in] expr Assertion condition/expression
32+ // / @param[in] file File name
33+ // / @param[in] line Line number of the assert statement.
34+ // / @param[in] function Function name.
35+ virtual void fail (const char *expr, const char *file, unsigned int line,
36+ const char *function) = 0;
4237
4338private:
4439};
4540
4641// / @cond DOCUMENT_ASSERT_MANAGER
47- class AssertManager
48- {
42+ class AssertManager {
4943public:
44+ static AssertManager &instance () {
45+ static AssertManager mgr;
46+ return mgr;
47+ }
5048
51- static AssertManager& instance ()
52- {
53- static AssertManager mgr;
54- return mgr;
55- }
56-
57- AssertManager (const AssertManager&) = delete ;
49+ AssertManager (const AssertManager &) = delete ;
5850
59- AssertManager& operator =(const AssertManager&) = delete ;
51+ AssertManager & operator =(const AssertManager &) = delete ;
6052
61- Assert* reset (Assert* newAssert = nullptr )
62- {
63- auto prevAssert = m_assert;
64- m_assert = newAssert;
65- return prevAssert;
66- }
53+ Assert *reset (Assert *newAssert = nullptr ) {
54+ auto prevAssert = m_assert;
55+ m_assert = newAssert;
56+ return prevAssert;
57+ }
6758
68- Assert* getAssert ()
69- {
70- return m_assert;
71- }
59+ Assert *getAssert () { return m_assert; }
7260
73- bool hasAssertRegistered () const
74- {
75- return (m_assert != nullptr );
76- }
61+ bool hasAssertRegistered () const { return (m_assert != nullptr ); }
7762
78- static void infiniteLoop ()
79- {
80- while ( true ) { };
81- }
63+ static void infiniteLoop () {
64+ while ( true ) {
65+ };
66+ }
8267
8368private:
84- AssertManager () : m_assert(nullptr ) {}
69+ AssertManager () : m_assert(nullptr ) {}
8570
86- Assert* m_assert;
71+ Assert * m_assert;
8772};
8873
8974// / @endcond
@@ -95,45 +80,36 @@ class AssertManager
9580// / behaviour of the assertion failure.
9681// / @pre TAssert class must be derived from comms::Assert.
9782// / @headerfile comms/Assert.h
98- template < typename TAssert>
99- class EnableAssert
100- {
101- static_assert (std::is_base_of<Assert, TAssert>::value,
102- " TAssert class must be derived class of Assert" );
83+ template <typename TAssert> class EnableAssert {
84+ static_assert (std::is_base_of<Assert, TAssert>::value,
85+ " TAssert class must be derived class of Assert" );
86+
10387public:
104- // / Type of assert object.
105- using AssertType = TAssert;
106-
107- // / @brief Constructor
108- // / @details Registers new assertion failure behaviour. It forwards
109- // / all the provided parameters to the constructor of embedded
110- // / assertion object of type TAssert.
111- // / @param args Arguments to pass to the assertion class constructor.
112- template <typename ... TParams>
113- EnableAssert (TParams&&... args)
114- : m_assert(std::forward<TParams>(args)...),
115- m_prevAssert (AssertManager::instance().reset(&m_assert))
116- {
117- }
118-
119- // / @brief Destructor
120- // / @details Restores the assertion behaviour that was recorded during
121- // / the instantiation of this object.
122- ~EnableAssert () noexcept
123- {
124- AssertManager::instance ().reset (m_prevAssert);
125- }
126-
127- // / @brief Provides reference to internal Assert object
128- // / @return Reference to object of type TAssert.
129- AssertType& getAssert ()
130- {
131- return m_assert;
132- }
88+ // / Type of assert object.
89+ using AssertType = TAssert;
90+
91+ // / @brief Constructor
92+ // / @details Registers new assertion failure behaviour. It forwards
93+ // / all the provided parameters to the constructor of embedded
94+ // / assertion object of type TAssert.
95+ // / @param args Arguments to pass to the assertion class constructor.
96+ template <typename ... TParams>
97+ EnableAssert (TParams &&...args)
98+ : m_assert(std::forward<TParams>(args)...),
99+ m_prevAssert (AssertManager::instance().reset(&m_assert)) {}
100+
101+ // / @brief Destructor
102+ // / @details Restores the assertion behaviour that was recorded during
103+ // / the instantiation of this object.
104+ ~EnableAssert () noexcept { AssertManager::instance ().reset (m_prevAssert); }
105+
106+ // / @brief Provides reference to internal Assert object
107+ // / @return Reference to object of type TAssert.
108+ AssertType &getAssert () { return m_assert; }
133109
134110private:
135- AssertType m_assert;
136- Assert* m_prevAssert;
111+ AssertType m_assert;
112+ Assert * m_prevAssert;
137113};
138114
139115#ifndef COMMS_ASSERT
@@ -154,24 +130,24 @@ class EnableAssert
154130#ifndef COMMS_NOSTDLIB
155131#define COMMS_ASSERT_FAIL_FUNC (expr ) assert (expr)
156132#else // #ifndef COMMS_NOSTDLIB
157- #define COMMS_ASSERT_FAIL_FUNC (expr ) comms::AssertManager::instance().infiniteLoop()
133+ #define COMMS_ASSERT_FAIL_FUNC (expr ) \
134+ comms::AssertManager::instance ().infiniteLoop()
158135#endif // #ifndef COMMS_NOSTDLIB
159136
160137// / @endcond
161138
162139// / @brief Generic assert macro
163140// / @details Will use custom assertion failure behaviour if such is defined,
164141// / otherwise it will use standard "assert()" macro.
165- // / In case COMMS_NOSTDLIB is defined and no custom assertion failure was
166- // / enabled, infinite loop will be executed.
142+ // / In case COMMS_NOSTDLIB is defined and no custom assertion failure
143+ // / was enabled, infinite loop will be executed.
167144// / @param expr Boolean expression
168- #define COMMS_ASSERT (expr ) \
169- ((expr) \
170- ? static_cast <void >(0 ) \
171- : (comms::AssertManager::instance().hasAssertRegistered() \
172- ? comms::AssertManager::instance().getAssert()->fail ( \
173- #expr, __FILE__, __LINE__, COMMS_ASSERT_FUNCTION_STR) \
174- : COMMS_ASSERT_FAIL_FUNC(expr)))
145+ #define COMMS_ASSERT (expr ) \
146+ ((expr) ? static_cast <void >(0 ) \
147+ : (comms::AssertManager::instance().hasAssertRegistered() \
148+ ? comms::AssertManager::instance().getAssert()->fail ( \
149+ #expr, __FILE__, __LINE__, COMMS_ASSERT_FUNCTION_STR) \
150+ : COMMS_ASSERT_FAIL_FUNC(expr)))
175151
176152#else // #ifndef NDEBUG
177153
@@ -186,4 +162,4 @@ class EnableAssert
186162#define GASSERT (expr ) COMMS_ASSERT(expr)
187163#endif // #ifndef GASSERT
188164
189- } // namespace comms
165+ } // namespace comms
0 commit comments