inkcpp
Loading...
Searching...
No Matches
system.h
1/* Copyright (c) 2024 Julian Benda
2 *
3 * This file is part of inkCPP which is released under MIT license.
4 * See file LICENSE.txt or go to
5 * https://github.com/JBenda/inkcpp for full license details.
6 */
7#pragma once
8
9#include "config.h"
10
11#ifdef INK_ENABLE_UNREAL
12# include "Misc/AssertionMacros.h"
13# include "Misc/CString.h"
14# include "HAL/UnrealMemory.h"
15# include "Hash/CityHash.h"
16#endif
17#ifdef INK_ENABLE_STL
18# ifdef INK_ENABLE_EXCEPTIONS
19# include <exception>
20# endif
21# include <stdexcept>
22# include <optional>
23# include <cctype>
24# include <cstdint>
25#endif
26#ifdef INK_ENABLE_CSTD
27# include <cstdio>
28# include <cstdlib>
29# include <ctype.h>
30# include <cassert>
31#endif
32
33// Platform specific defines //
34
35#ifdef INK_ENABLE_UNREAL
36# define inkZeroMemory(buff, len) FMemory::Memset(buff, 0, len)
37# define FORMAT_STRING_STR "%hs"
38#else
39# define inkZeroMemory ink::internal::zero_memory
40# define FORMAT_STRING_STR "%s"
41#endif
42
43#ifdef INK_ENABLE_UNREAL
44# define inkAssert(condition, text, ...) checkf(condition, TEXT(text), ##__VA_ARGS__)
45# define inkFail(text, ...) checkf(false, TEXT(text), ##__VA_ARGS__)
46#else
47# define inkAssert ink::ink_assert
48# define inkFail(...) ink::ink_assert(false, __VA_ARGS__)
49
50#endif
51
52
53namespace ink
54{
58typedef unsigned int uint32_t;
59
60#ifndef INK_ENABLE_STL
61
63typedef int int32_t;
64typedef short int16_t;
65
67typedef unsigned long long uint64_t;
68typedef unsigned short uint16_t;
69#endif // ndef INK_ENABLE_STL
70
73
76
77
79typedef unsigned char byte_t;
80
82typedef decltype(static_cast<int*>(nullptr) - static_cast<int*>(nullptr)) ptrdiff_t;
83
85static_assert(sizeof(byte_t) == 1);
86static_assert(sizeof(uint16_t) == 2);
87static_assert(sizeof(int16_t) == 2);
88static_assert(sizeof(uint32_t) == 4);
89static_assert(sizeof(int32_t) == 4);
90static_assert(sizeof(uint64_t) == 8);
91static_assert(sizeof(ptrdiff_t) == sizeof(void*));
92
95
97typedef const unsigned char* ip_t;
98
100typedef unsigned int size_t;
101
104
107
109struct list_flag {
110 int16_t list_id;
111 int16_t flag;
112
113 bool operator==(const list_flag& o) const { return list_id == o.list_id && flag == o.flag; }
114
115 bool operator!=(const list_flag& o) const { return ! (*this == o); }
116};
117
119constexpr list_flag null_flag{-1, -1};
121constexpr list_flag empty_flag{-1, 0};
122
123#ifdef INK_ENABLE_UNREAL
125inline hash_t hash_string(const char* string)
126{
127 return CityHash32(string, FCStringAnsi::Strlen(string));
128}
129#else
130hash_t hash_string(const char* string);
131hash_t hash_data(const unsigned char* data, size_t len);
132#endif
133
134namespace internal
135{
136#ifdef __GNUC__
137#else
138# pragma warning(push)
139# pragma warning( \
140 disable : 4514, \
141 justification : "functions are defined in header file, they do not need to be used." \
142 )
143#endif
145 static inline constexpr bool starts_with(const char* string, const char* prefix)
146 {
147 while (*prefix) {
148 if (*string != *prefix) {
149 return false;
150 }
151 string++;
152 prefix++;
153 }
154 return true;
155 }
156
158 static inline constexpr bool is_whitespace(const char* string, bool includeNewline = true)
159 {
160 // Iterate string
161 while (true) {
162 switch (*(string++)) {
163 case 0: return true;
164 case '\f': [[fallthrough]];
165 case '\r': [[fallthrough]];
166 case '\n':
167 if (! includeNewline)
168 return false;
169 [[fallthrough]];
170 case '\t': [[fallthrough]];
171 case '\v': [[fallthrough]];
172 case ' ': continue;
173 default: return false;
174 }
175 }
176 }
177
178 inline bool is_part_of_word(char character) { return isalpha(character) || isdigit(character); }
179
180 static inline constexpr bool is_whitespace(char character, bool includeNewline = true)
181 {
182 switch (character) {
183 case '\n':
184 if (! includeNewline)
185 return false;
186 case '\t': [[fallthrough]];
187 case ' ': return true;
188 default: return false;
189 }
190 }
191
192#ifndef INK_ENABLE_UNREAL
194 void zero_memory(void* buffer, size_t length);
195#endif
196#ifdef __GNUC__
197#else
198# pragma warning(pop)
199#endif
200} // namespace internal
201
202#ifdef INK_ENABLE_STL
204using ink_exception = std::runtime_error;
205#else
206// Non-STL exception class
207class ink_exception
208{
209public:
210 ink_exception(const char* msg)
211 : _msg(msg)
212 {
213 }
214
215 inline const char* message() const { return _msg; }
216
217private:
218 const char* _msg;
219};
220#endif
221
222#ifdef __GNUC__
223# pragma GCC diagnostic push
224# pragma GCC diagnostic ignored "-Wunused-parameter"
225#else
226# pragma warning(push)
227# pragma warning( \
228 disable : 4100, \
229 justification : "dependend on rtti, exception and stl support not all arguments are needed" \
230 )
231#endif
232// assert
233template<typename... Args>
234void ink_assert(bool condition, const char* msg = nullptr, Args... args)
235{
236 static const char* EMPTY = "";
237 if (msg == nullptr) {
238 msg = EMPTY;
239 }
240 if (! condition) {
241#if defined(INK_ENABLE_STL) || defined(INK_ENABLE_CSTD)
242 if constexpr (sizeof...(args) > 0) {
243 size_t size = snprintf(nullptr, 0, msg, args...) + 1;
244 char* message = static_cast<char*>(malloc(size));
245 snprintf(message, size, msg, args...);
246 msg = message;
247 }
248#endif
249#ifdef INK_ENABLE_EXCEPTIONS
250 throw ink_exception(msg);
251#elif defined(INK_ENABLE_CSTD)
252 fprintf(stderr, "Ink Assert: %s\n", msg);
253 abort();
254#else
255# warning no assertion handling this could lead to invalid code paths
256#endif
257 }
258}
259#ifdef __GNUC__
260# pragma GCC diagnostic pop
261#else
262# pragma warning(pop)
263#endif
264
265template<typename... Args>
266[[noreturn]] inline void ink_assert(const char* msg = nullptr, Args... args)
267{
268 ink_assert(false, msg, args...);
269#ifdef INK_ENABLE_CSTD
270 exit(EXIT_FAILURE);
271#endif
272}
273
274namespace runtime::internal
275{
276 constexpr unsigned abs(int i) { return static_cast<unsigned>(i < 0 ? -i : i); }
277
278 template<typename T>
279 struct always_false {
280 static constexpr bool value = false;
281 };
282
283 template<bool Con, typename T1, typename T2>
284 struct if_type {
285 using type = T1;
286 };
287
288 template<typename T1, typename T2>
289 struct if_type<false, T1, T2> {
290 using type = T2;
291 };
292
293 template<bool Con, typename T1, typename T2>
294 using if_t = typename if_type<Con, T1, T2>::type;
295
296 template<bool Enable, typename T = void>
297 struct enable_if {
298 };
299
300 template<typename T>
301 struct enable_if<true, T> {
302 using type = T;
303 };
304
305 template<bool Enable, typename T = void>
306 using enable_if_t = typename enable_if<Enable, T>::type;
307} // namespace runtime::internal
308
309
310#ifdef INK_ENABLE_STL
314template<typename T>
315using optional = std::optional<T>;
317constexpr std::nullopt_t nullopt = std::nullopt;
318#else
319struct nullopt_t {
320};
321
322constexpr nullopt_t nullopt;
323
324template<typename T>
325class optional
326{
327public:
328 optional() {}
329
330 optional(nullopt_t) {}
331
332 optional(T&& val)
333 : _has_value{true}
334 , _value{val}
335 {
336 }
337
338 optional(const T& val)
339 : _has_value{true}
340 , _value{val}
341 {
342 }
343
344 const T& operator*() const { return value(); }
345
346 T& operator*() { return value(); }
347
348 const T* operator->() const { return &value(); }
349
350 T* operator->() { return &value(); }
351
352 constexpr bool has_value() const { return _has_value; }
353
354 constexpr T& value()
355 {
356 test_value();
357 return _value;
358 }
359
360 constexpr const T& value() const
361 {
362 test_value();
363 return _value;
364 }
365
366 constexpr operator bool() const { return has_value(); }
367
368 template<typename U>
369 constexpr T value_or(U&& u) const
370 {
371 return _has_value ? _value : static_cast<T>(u);
372 }
373
374 template<typename... Args>
375 T& emplace(Args... args)
376 {
377 if (_has_value)
378 _value.~T();
379
380 new (&_value) T(args...);
381 _has_value = true;
382 return _value;
383 }
384
385private:
386 void test_value() const
387 {
388 if (! _has_value) {
389 inkFail("Can't access empty optional!");
390 }
391 }
392
393 bool _has_value = false;
394 T _value;
395};
396#endif
397} // namespace ink
Namespace contaning all modules and classes from InkCPP.
Definition choice.h:11
constexpr list_flag null_flag
value of an unset list_flag
Definition system.h:119
unsigned int uint32_t
define basic numeric type
Definition system.h:58
std::optional< T > optional
custom optional implementation for usage if STL is disabled
Definition system.h:315
const hash_t InvalidHash
Invalid hash value.
Definition system.h:75
uint32_t container_t
Used as the unique identifier for an ink container.
Definition system.h:103
unsigned char byte_t
Byte type.
Definition system.h:79
unsigned int size_t
Used for the size of arrays.
Definition system.h:100
uint32_t offset_t
Verify sizes.
Definition system.h:94
constexpr std::nullopt_t nullopt
an empty optional
Definition system.h:317
std::runtime_error ink_exception
exception type thrown if something goes wrong
Definition system.h:204
uint32_t thread_t
Used to uniquely identify threads.
Definition system.h:106
decltype(static_cast< int * >(nullptr) - static_cast< int * >(nullptr)) ptrdiff_t
Ptr difference type.
Definition system.h:82
constexpr list_flag empty_flag
value representing an empty list
Definition system.h:121
hash_t hash_string(const char *string)
Simple hash for serialization of strings.
Definition system.h:125
const unsigned char * ip_t
Instruction pointer used for addressing within the story instructions.
Definition system.h:97
uint32_t hash_t
Name hash (used for temporary variables)
Definition system.h:72