Skip to content

Commit ad0061b

Browse files
committed
Introduced 'assertion-like' feature to 'ICE_ASSERT_CORE' in constexpr contexts.
* It will now fail compilation when the check fails. * There is no possibility to give a message yes.
1 parent 3d7b00a commit ad0061b

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

source/code/core/core/public/ice/assert_core.hxx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,42 @@
88
#include <cassert>
99
#undef assert
1010

11+
namespace ice::detail
12+
{
13+
14+
// The following function will fail to access element at index [2] only in constexpr scenarios and when the expression is false.
15+
// This is good enough for us to create an assertion macro for constexpr and non-constexpr cases.
16+
constexpr char _iceshard_constexpr_assert(bool v) noexcept
17+
{
18+
int cev = std::is_constant_evaluated();
19+
char constexpr_assert_failure[2]{ };
20+
return constexpr_assert_failure[!v + cev];
21+
};
22+
23+
} // namespace ice::detail::assert
24+
1125
#if ISP_WINDOWS
1226

1327
#define ICE_ASSERT_CORE(expression) do { if (std::is_constant_evaluated() == false) { \
1428
(void)( \
1529
(!!(expression)) || \
1630
(_wassert(_CRT_WIDE(#expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) \
17-
); } } while(false)
31+
); } else { ice::detail::_iceshard_constexpr_assert(expression); } } while(false)
1832

1933
#elif ISP_WEBAPP || ISP_LINUX
2034

21-
#define ICE_ASSERT_CORE(expression) ((expression) \
22-
? (void)0 \
23-
: __assert_fail(#expression, __FILE__, __LINE__, __PRETTY_FUNCTION__))
35+
#define ICE_ASSERT_CORE(expression) do { if (std::is_constant_evaluated() == false) { \
36+
((expression) \
37+
? (void)0 \
38+
: __assert_fail(#expression, __FILE__, __LINE__, __PRETTY_FUNCTION__)); \
39+
} else { ice::detail::_iceshard_constexpr_assert(expression); } } while(false)
2440

2541
#else
2642

27-
#define ICE_ASSERT_CORE(expression) ((expression) \
28-
? __assert_no_op \
29-
: __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #expression))
43+
#define ICE_ASSERT_CORE(expression) do { if (std::is_constant_evaluated() == false) { \
44+
((expression) \
45+
? __assert_no_op \
46+
: __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #expression)); \
47+
} else { ice::detail::_iceshard_constexpr_assert(expression); } } while(false)
3048

3149
#endif

source/code/core/core/public/ice/shard.hxx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,7 @@ namespace ice
238238
ice::Shard result{ };
239239
ice::ShardID const id = ice::shardid(definition);
240240

241-
if (std::is_constant_evaluated() == false)
242-
{
243-
ICE_ASSERT_CORE(id.payload == ice::Constant_ShardPayloadID<T> || id.payload == ice::ShardPayloadID_NotSet);
244-
}
245-
241+
ICE_ASSERT_CORE(id.payload == ice::Constant_ShardPayloadID<T> || id.payload == ice::ShardPayloadID_NotSet);
246242
if (id.payload == ice::Constant_ShardPayloadID<T> || id.payload == ice::ShardPayloadID_NotSet)
247243
{
248244
result.id = id;
@@ -257,11 +253,7 @@ namespace ice
257253
{
258254
ice::Shard result{ };
259255

260-
if (std::is_constant_evaluated() == false)
261-
{
262-
ICE_ASSERT_CORE(id.payload == ice::Constant_ShardPayloadID<T> || id.payload == ice::ShardPayloadID_NotSet);
263-
}
264-
256+
ICE_ASSERT_CORE(id.payload == ice::Constant_ShardPayloadID<T> || id.payload == ice::ShardPayloadID_NotSet);
265257
if (id.payload == ice::Constant_ShardPayloadID<T> || id.payload == ice::ShardPayloadID_NotSet)
266258
{
267259
result.id = id;

source/code/systems/resource_system/public/ice/uri.hxx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,9 @@ namespace ice
8383
if (uri[0] == '/' && uri[1] == '/')
8484
{
8585
ice::ucount const authority_end = ice::string::find_first_of(uri, '/', 2);
86+
ICE_ASSERT_CORE(authority_end != ice::String_NPos);
8687
if (authority_end == ice::String_NPos)
8788
{
88-
if (std::is_constant_evaluated())
89-
{
90-
throw "Invalid URI definition! Do not use '//' without defining a host name!";
91-
}
9289
return false;
9390
}
9491

0 commit comments

Comments
 (0)