@@ -9,3 +9,127 @@ void LogEventWarnWrapper(const char* warn_str)
99}
1010
1111}
12+
13+ [[nodiscard]]
14+ uint32 usize_narrow_u32_checked (const size_t source_val, bool should_assert)
15+ {
16+ if (should_assert) {
17+ ASSERT (source_val <= std::numeric_limits<uint32>::max ());
18+ }
19+ else if (source_val > std::numeric_limits<uint32>::max ()) {
20+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from size_t to 32 bits unsigned integer will overflow.\n " );
21+ }
22+ return usize_narrow_u32 (source_val);
23+ }
24+
25+ [[nodiscard]]
26+ int8 i8_from_u8_checked (const uint8 source_val, bool should_assert) // not clamping/capping
27+ {
28+ const bool would_overflow = (source_val > (uint8)std::numeric_limits<int8>::max ());
29+ if (should_assert) {
30+ ASSERT (!would_overflow);
31+ }
32+ else if (would_overflow) {
33+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 8 bits unsigned integer to 8 bits signed integer will overflow.\n " );
34+ }
35+
36+ return static_cast <int8>(source_val);
37+ }
38+
39+ [[nodiscard]]
40+ int16 i8_from_u16_checked (const uint16 source_val, bool should_assert) // not clamping/capping
41+ {
42+ const bool would_overflow = (source_val > (uint16)std::numeric_limits<int8>::max ());
43+ if (should_assert) {
44+ ASSERT (!would_overflow);
45+ }
46+ else if (would_overflow) {
47+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 16 bits unsigned integer to 8 bits signed integer will overflow.\n " );
48+ }
49+
50+ return static_cast <int16>(source_val);
51+ }
52+
53+ [[nodiscard]]
54+ int16 i16_from_u16_checked (const uint16 source_val, bool should_assert) // not clamping/capping
55+ {
56+ const bool would_overflow = (source_val > (uint16)std::numeric_limits<int16>::max ());
57+ if (should_assert) {
58+ ASSERT (!would_overflow);
59+ }
60+ else if (would_overflow) {
61+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 16 bits unsigned integer to 16 bits signed integer will overflow.\n " );
62+ }
63+
64+ return static_cast <int16>(source_val);
65+ }
66+
67+ [[nodiscard]]
68+ int16 i16_from_u32_checked (const uint32 source_val, bool should_assert) // not clamping/capping
69+ {
70+ const bool would_overflow = (source_val > (uint32)std::numeric_limits<int16>::max ());
71+ if (should_assert) {
72+ ASSERT (!would_overflow);
73+ }
74+ else if (would_overflow) {
75+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 32 bits unsigned integer to 16 bits signed integer will overflow.\n " );
76+ }
77+
78+ return static_cast <int16>(source_val);
79+ }
80+
81+ [[nodiscard]]
82+ int16 i16_from_u64_checked (const uint64 source_val, bool should_assert) // not clamping/capping
83+ {
84+ const bool would_overflow = (source_val > (uint64)std::numeric_limits<int16>::max ());
85+ if (should_assert) {
86+ ASSERT (!would_overflow);
87+ }
88+ else if (would_overflow) {
89+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 64 bits unsigned integer to 16 bits signed integer will overflow.\n " );
90+ }
91+
92+ return static_cast <int16>(source_val);
93+ }
94+
95+ [[nodiscard]]
96+ int32 i32_from_u32_checked (const uint32 source_val, bool should_assert) // not clamping/capping
97+ {
98+ const bool would_overflow = (source_val > (uint32)std::numeric_limits<int32>::max ());
99+ if (should_assert) {
100+ ASSERT (!would_overflow);
101+ }
102+ else if (would_overflow) {
103+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 32 bits unsigned integer to 32 bits signed integer will overflow.\n " );
104+ }
105+
106+ return static_cast <int32>(source_val);
107+ }
108+
109+ [[nodiscard]]
110+ int32 i32_from_u64_checked (const uint64 source_val, bool should_assert) // not clamping/capping
111+ {
112+ const bool would_overflow = (source_val > (uint64)std::numeric_limits<int64>::max ());
113+ if (should_assert) {
114+ ASSERT (!would_overflow);
115+ }
116+ else if (would_overflow) {
117+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 64 bits unsigned integer to 32 bits signed integer will overflow.\n " );
118+ }
119+
120+ return static_cast <int32>(source_val);
121+ }
122+
123+ [[nodiscard]]
124+ int64 i64_from_u64_checked (const uint64 source_val, bool should_assert) // not clamping/capping
125+ {
126+ const bool would_overflow = (source_val > (uint64)std::numeric_limits<int64>::max ());
127+ if (should_assert) {
128+ ASSERT (!would_overflow);
129+ }
130+ else if (would_overflow) {
131+ detail_stypecast::LogEventWarnWrapper (" [Internal] Narrowing conversion from 64 bits unsigned integer to 64 bits signed integer will overflow.\n " );
132+ }
133+
134+ return static_cast <int64>(source_val);
135+ }
0 commit comments