Skip to content

Commit bcf7a95

Browse files
author
John Wellbelove
committed
Renamed ETL_ASSERT_UNTYPED to ETL_ASSERT_GENERIC
1 parent a9265b1 commit bcf7a95

File tree

5 files changed

+89
-68
lines changed

5 files changed

+89
-68
lines changed

include/etl/error_handler.h

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -364,60 +364,6 @@ namespace etl
364364
#endif
365365
#endif
366366

367-
#if ETL_IS_DEBUG_BUILD
368-
#if defined(ETL_DEBUG_USE_ASSERT_FUNCTION)
369-
#define ETL_DEBUG_ASSERT(b, e) {if (!(b)) ETL_UNLIKELY {etl::private_error_handler::assert_handler<0>::assert_function_ptr((e));}} // If the condition fails, calls the assert function
370-
#define ETL_DEBUG_ASSERT_OR_RETURN(b, e) {if (!(b)) ETL_UNLIKELY {etl::private_error_handler::assert_handler<0>::assert_function_ptr((e)); return;}} // If the condition fails, calls the assert function and return
371-
#define ETL_DEBUG_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) ETL_UNLIKELY {etl::private_error_handler::assert_handler<0>::assert_function_ptr((e)); return (v);}} // If the condition fails, calls the assert function and return a value
372-
373-
#define ETL_DEBUG_ASSERT_FAIL(e) {etl::private_error_handler::assert_handler<0>::assert_function_ptr((e));} // Calls the assert function
374-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN(e) {etl::private_error_handler::assert_handler<0>::assert_function_ptr((e)); return;} // Calls the assert function and return
375-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {etl::private_error_handler::assert_handler<0>::assert_function_ptr((e)); return (v);} // Calls the assert function and return a value
376-
#elif ETL_DEBUG_USING_EXCEPTIONS
377-
#if defined(ETL_DEBUG_LOG_ERRORS)
378-
#define ETL_DEBUG_ASSERT(b, e) {if (!(b)) ETL_UNLIKELY {etl::error_handler::error((e)); throw((e));}} // If the condition fails, calls the error handler then throws an exception.
379-
#define ETL_DEBUG_ASSERT_OR_RETURN(b, e) {if (!(b)) ETL_UNLIKELY {etl::error_handler::error((e)); throw((e)); return;}} // If the condition fails, calls the error handler then throws an exception.
380-
#define ETL_DEBUG_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) ETL_UNLIKELY {etl::error_handler::error((e)); throw((e)); return(v);}} // If the condition fails, calls the error handler then throws an exception.
381-
382-
#define ETL_DEBUG_ASSERT_FAIL(e) {etl::error_handler::error((e)); throw((e));} // Calls the error handler then throws an exception.
383-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN(e) {etl::error_handler::error((e)); throw((e)); return;} // Calls the error handler then throws an exception.
384-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {etl::error_handler::error((e)); throw((e)); return(v);} // Calls the error handler then throws an exception.
385-
#else
386-
#define ETL_DEBUG_ASSERT(b, e) {if (!(b)) ETL_UNLIKELY {throw((e));}} // If the condition fails, throws an exception.
387-
#define ETL_DEBUG_ASSERT_OR_RETURN(b, e) {if (!(b)) ETL_UNLIKELY {throw((e));}} // If the condition fails, throws an exception.
388-
#define ETL_DEBUG_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) ETL_UNLIKELY {throw((e));}} // If the condition fails, throws an exception.
389-
390-
#define ETL_DEBUG_ASSERT_FAIL(e) {throw((e));} // Throws an exception.
391-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN(e) {throw((e));} // Throws an exception.
392-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {throw((e));} // Throws an exception.
393-
#endif
394-
#elif defined(ETL_DEBUG_LOG_ERRORS)
395-
#define ETL_DEBUG_ASSERT(b, e) {if (!(b)) ETL_UNLIKELY {etl::error_handler::error((e));}} // If the condition fails, calls the error handler
396-
#define ETL_DEBUG_ASSERT_OR_RETURN(b, e) {if (!(b)) ETL_UNLIKELY {etl::error_handler::error((e)); return;}} // If the condition fails, calls the error handler and return
397-
#define ETL_DEBUG_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) ETL_UNLIKELY {etl::error_handler::error((e)); return (v);}} // If the condition fails, calls the error handler and return a value
398-
399-
#define ETL_DEBUG_ASSERT_FAIL(e) {etl::error_handler::error((e));} // Calls the error handler
400-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN(e) {etl::error_handler::error((e)); return;} // Calls the error handler and return
401-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {etl::error_handler::error((e)); return (v);} // Calls the error handler and return a value
402-
#else
403-
#define ETL_DEBUG_ASSERT(b, e) assert((b)) // If the condition fails, asserts.
404-
#define ETL_DEBUG_ASSERT_OR_RETURN(b, e) {if (!(b)) ETL_UNLIKELY {assert(false); return;}} // If the condition fails, asserts and return.
405-
#define ETL_DEBUG_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) ETL_UNLIKELY {assert(false); return(v);}} // If the condition fails, asserts and return a value.
406-
407-
#define ETL_DEBUG_ASSERT_FAIL(e) assert(false) // Asserts.
408-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN(e) {assert(false); return;} // Asserts.
409-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {assert(false); return(v);} // Asserts.
410-
#endif
411-
#else
412-
#define ETL_DEBUG_ASSERT(b, e) // Does nothing.
413-
#define ETL_DEBUG_ASSERT_OR_RETURN(b, e) {if (!(b)) ETL_UNLIKELY return;} // Returns.
414-
#define ETL_DEBUG_ASSERT_OR_RETURN_VALUE(b, e, v) {if (!(b)) ETL_UNLIKELY return(v);} // Returns a value.
415-
416-
#define ETL_DEBUG_ASSERT_FAIL(e) // Does nothing.
417-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN(e) {return;} // Returns.
418-
#define ETL_DEBUG_ASSERT_FAIL_AND_RETURN_VALUE(e, v) {return(v);} // Returns a value.
419-
#endif
420-
421367
//*************************************
422368
#if defined(ETL_CHECK_PUSH_POP)
423369
#define ETL_ASSERT_CHECK_PUSH_POP(b, e) ETL_ASSERT(b, e)
@@ -443,15 +389,15 @@ namespace etl
443389

444390
//*************************************
445391
#if defined(ETL_VERBOSE_ERRORS)
446-
#define ETL_ERROR(e) (e(__FILE__, __LINE__)) // Make an exception with the file name and line number.
447-
#define ETL_ERROR_WITH_VALUE(e, v) (e(__FILE__, __LINE__, (v))) // Make an exception with the file name, line number and value.
448-
#define ETL_ERROR_TEXT(verbose_text, terse_text) (verbose_text) // Use the verbose text.
449-
#define ETL_ERROR_UNTYPED(text) (etl::exception((text),__FILE__,__LINE__)) // Make a generic exception with a message, file name and line number.
392+
#define ETL_ERROR(e) (e(__FILE__, __LINE__)) // Make an exception with the file name and line number.
393+
#define ETL_ERROR_WITH_VALUE(e, v) (e(__FILE__, __LINE__, (v))) // Make an exception with the file name, line number and value.
394+
#define ETL_ERROR_TEXT(verbose_text, terse_text) (verbose_text) // Use the verbose text.
395+
#define ETL_ERROR_GENERIC(text) (etl::exception((text),__FILE__, __LINE__)) // Make a generic exception with a message, file name and line number.
450396
#else
451-
#define ETL_ERROR(e) (e("", __LINE__)) // Make an exception with the line number.
452-
#define ETL_ERROR_WITH_VALUE(e, v) (e("", __LINE__, (v))) // Make an exception with the file name, line number and value.
453-
#define ETL_ERROR_TEXT(verbose_text, terse_text) (terse_text) // Use the terse text.
454-
#define ETL_ERROR_UNTYPED(text) (etl::exception((text),"",__LINE__)) // Make a generic exception with a message and line number.
397+
#define ETL_ERROR(e) (e("", __LINE__)) // Make an exception with the line number.
398+
#define ETL_ERROR_WITH_VALUE(e, v) (e("", __LINE__, (v))) // Make an exception with the file name, line number and value.
399+
#define ETL_ERROR_TEXT(verbose_text, terse_text) (terse_text) // Use the terse text.
400+
#define ETL_ERROR_GENERIC(text) (etl::exception((text),"", __LINE__)) // Make a generic exception with a message and line number.
455401
#endif
456402

457403
#endif

include/etl/expected.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ namespace etl
675675
//*******************************************
676676
value_type* operator ->()
677677
{
678-
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));
678+
ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
679679

680680
return etl::addressof(etl::get<value_type>(storage));
681681
}
@@ -685,7 +685,7 @@ namespace etl
685685
//*******************************************
686686
const value_type* operator ->() const
687687
{
688-
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));
688+
ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
689689

690690
return etl::addressof(etl::get<value_type>(storage));
691691
}
@@ -695,7 +695,7 @@ namespace etl
695695
//*******************************************
696696
value_type& operator *() ETL_LVALUE_REF_QUALIFIER
697697
{
698-
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));
698+
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
699699

700700
return etl::get<value_type>(storage);
701701
}
@@ -705,7 +705,7 @@ namespace etl
705705
//*******************************************
706706
const value_type& operator *() const ETL_LVALUE_REF_QUALIFIER
707707
{
708-
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));
708+
ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
709709

710710
return etl::get<value_type>(storage);
711711
}
@@ -716,7 +716,7 @@ namespace etl
716716
//*******************************************
717717
value_type&& operator *()&&
718718
{
719-
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));
719+
ETL_ASSERT_OR_RETURN_VALUE(has_value(), ETL_ERROR(expected_invalid), ETL_NULLPTR);
720720

721721
return etl::move(etl::get<value_type>(storage));
722722
}
@@ -726,7 +726,7 @@ namespace etl
726726
//*******************************************
727727
const value_type&& operator *() const&&
728728
{
729-
ETL_DEBUG_ASSERT(has_value(), ETL_ERROR(expected_invalid));
729+
ETL_ASSERT(has_value(), ETL_ERROR(expected_invalid));
730730

731731
return etl::move(etl::get<value_type>(storage));
732732
}

test/test_etl_assert.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/******************************************************************************
2+
The MIT License(MIT)
3+
4+
Embedded Template Library.
5+
https://github.com/ETLCPP/etl
6+
https://www.etlcpp.com
7+
8+
Documentation:
9+
10+
Copyright(c) 2025 John Wellbelove
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a copy
13+
of this software and associated documentation files(the "Software"), to deal
14+
in the Software without restriction, including without limitation the rights
15+
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16+
copies of the Software, and to permit persons to whom the Software is
17+
furnished to do so, subject to the following conditions :
18+
19+
The above copyright notice and this permission notice shall be included in all
20+
copies or substantial portions of the Software.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
SOFTWARE.
29+
******************************************************************************/
30+
31+
#include "unit_test_framework.h"
32+
33+
#include "etl/platform.h"
34+
#include "etl/error_handler.h"
35+
#include "etl/exception.h"
36+
37+
namespace
38+
{
39+
class TestException1 : public etl::exception
40+
{
41+
public:
42+
43+
TestException1(string_type file_name_, numeric_type line_number_)
44+
: exception(ETL_ERROR_TEXT("Test:error1", "1A"), file_name_, line_number_)
45+
{
46+
}
47+
};
48+
49+
SUITE(test_etl_asserts)
50+
{
51+
TEST(test_etl_assert)
52+
{
53+
auto te = ETL_ERROR(TestException1);
54+
55+
CHECK_EQUAL(0, strcmp(te.what(), "Test:error1"));
56+
57+
CHECK_THROW(ETL_ASSERT(false, ETL_ERROR(TestException1)), TestException1);
58+
CHECK_NO_THROW(ETL_ASSERT(true, ETL_ERROR(TestException1)));
59+
}
60+
61+
TEST(test_etl_assert_generic)
62+
{
63+
auto te = ETL_ERROR_GENERIC("Generic Assert Exception");
64+
65+
CHECK_EQUAL(0, strcmp(te.what(), "Generic Assert Exception"));
66+
67+
CHECK_THROW(ETL_ASSERT(false, ETL_ERROR_GENERIC("Generic Assert Exception")), etl::exception);
68+
CHECK_NO_THROW(ETL_ASSERT(true, ETL_ERROR_GENERIC("Generic Assert Exception")));
69+
}
70+
};
71+
}

test/vs2022/etl.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10257,6 +10257,7 @@
1025710257
<ClCompile Include="..\test_crc8_rohc.cpp" />
1025810258
<ClCompile Include="..\test_crc8_wcdma.cpp" />
1025910259
<ClCompile Include="..\test_delegate_observable.cpp" />
10260+
<ClCompile Include="..\test_etl_assert.cpp" />
1026010261
<ClCompile Include="..\test_expected.cpp" />
1026110262
<ClCompile Include="..\test_function_traits.cpp" />
1026210263
<ClCompile Include="..\test_hfsm_recurse_to_inner_state_on_start.cpp" />

test/vs2022/etl.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3713,6 +3713,9 @@
37133713
<ClCompile Include="..\syntax_check\delegate_observable.h.t.cpp">
37143714
<Filter>Tests\Syntax Checks\Source</Filter>
37153715
</ClCompile>
3716+
<ClCompile Include="..\test_etl_assert.cpp">
3717+
<Filter>Tests\Misc</Filter>
3718+
</ClCompile>
37163719
</ItemGroup>
37173720
<ItemGroup>
37183721
<None Include="..\..\library.properties">

0 commit comments

Comments
 (0)