Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1
10// Macros so that C++ new calls can allocate using ecs_os_api memory allocation functions
11// Rationale:
12// - Using macros here instead of a templated function bc clients might override ecs_os_malloc
13// to contain extra debug info like source tracking location. Using a template function
14// in that scenario would collapse all source location into said function vs. the
15// actual call site
16// - FLECS_PLACEMENT_NEW(): exists to remove any naked new calls/make it easy to identify any regressions
17// by grepping for new/delete
18
19#define FLECS_PLACEMENT_NEW(_ptr, _type) ::new(flecs::_::placement_new_tag, _ptr) _type
20#define FLECS_NEW(_type) FLECS_PLACEMENT_NEW(ecs_os_malloc(sizeof(_type)), _type)
21#define FLECS_DELETE(_ptr) \
22 do { \
23 if (_ptr) { \
24 flecs::_::destruct_obj(_ptr); \
25 ecs_os_free(_ptr); \
26 } \
27 } while (false)
28
29/* Faster (compile time) alternatives to std::move / std::forward. From:
30 * https://www.foonathan.net/2020/09/move-forward/
31 */
32
33#define FLECS_MOV(...) \
34 static_cast<flecs::remove_reference_t<decltype(__VA_ARGS__)>&&>(__VA_ARGS__)
35
36#define FLECS_FWD(...) \
37 static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
38
39namespace flecs
40{
41
42namespace _
43{
44
45// Dummy Placement new tag to disambiguate from any other operator new overrides
47constexpr placement_new_tag_t placement_new_tag{};
48template<class Ty> inline void destruct_obj(Ty* _ptr) { _ptr->~Ty(); }
49template<class Ty> inline void free_obj(void* _ptr) {
50 if (_ptr) {
51 destruct_obj(static_cast<Ty*>(_ptr));
52 ecs_os_free(_ptr);
53 }
54}
55
56} // namespace _
57
58} // namespace flecs
59
60// Allows overriding flecs_static_assert, which is useful when testing
61#ifndef flecs_static_assert
62#define flecs_static_assert(cond, str) static_assert(cond, str)
63#endif
64
65inline void* operator new(size_t, flecs::_::placement_new_tag_t, void* _ptr) noexcept { return _ptr; }
66inline void operator delete(void*, flecs::_::placement_new_tag_t, void*) noexcept { }
67
68namespace flecs
69{
70
71// faster (compile time) alternative to std::conditional
72template <bool> struct condition;
73
74template <> struct condition<false> {
75 template <typename T, typename F> using type = F;
76};
77
78template <> struct condition<true> {
79 template <typename T, typename F> using type = T;
80};
81
82using std::conditional_t;
83using std::decay_t;
84using std::enable_if_t;
85using std::remove_pointer_t;
86using std::remove_reference_t;
87using std::underlying_type_t;
88using std::is_base_of;
89using std::is_empty;
90using std::is_const;
91using std::is_pointer;
92using std::is_reference;
93using std::is_volatile;
94using std::is_same;
95using std::is_enum;
96using std::is_trivially_constructible;
97using std::is_trivially_move_assignable;
98using std::is_trivially_copy_assignable;
99using std::is_trivially_copy_constructible;
100using std::is_trivially_move_constructible;
101using std::is_trivially_copyable;
102using std::is_move_assignable;
103using std::is_move_constructible;
104using std::is_copy_constructible;
105using std::is_trivially_destructible;
106using std::is_empty_v;
107using std::is_const_v;
108using std::is_pointer_v;
109using std::is_reference_v;
110using std::is_volatile_v;
111using std::is_same_v;
112using std::is_enum_v;
113using std::is_base_of_v;
114using std::is_trivially_constructible_v;
115using std::is_trivially_destructible_v;
116using std::is_trivially_copyable_v;
117using std::is_trivially_move_constructible_v;
118using std::is_trivially_copy_constructible_v;
119using std::is_trivially_move_assignable_v;
120using std::is_trivially_copy_assignable_v;
121using std::is_default_constructible_v;
122using std::is_move_constructible_v;
123using std::is_copy_constructible_v;
124using std::is_move_assignable_v;
125using std::is_copy_assignable_v;
126using std::is_destructible_v;
127
128// Determine constness even if T is a pointer type
129template <typename T>
130using is_const_p = is_const< remove_pointer_t<T> >;
131
132// Apply cv modifiers from source type to destination type
133// (from: https://stackoverflow.com/questions/52559336/add-const-to-type-if-template-arg-is-const)
134template<class Src, class Dst>
135using transcribe_const_t = conditional_t<is_const<Src>::value, Dst const, Dst>;
136
137template<class Src, class Dst>
138using transcribe_volatile_t = conditional_t<is_volatile<Src>::value, Dst volatile, Dst>;
139
140template<class Src, class Dst>
141using transcribe_cv_t = transcribe_const_t< Src, transcribe_volatile_t< Src, Dst> >;
142
143template<class Src, class Dst>
144using transcribe_pointer_t = conditional_t<is_pointer<Src>::value, Dst*, Dst>;
145
146template<class Src, class Dst>
147using transcribe_cvp_t = transcribe_cv_t< Src, transcribe_pointer_t< Src, Dst> >;
148
149
150// More convenience templates. The if_*_t templates use int as default type
151// instead of void. This enables writing code that's a bit less cluttered when
152// the templates are used in a template declaration:
153//
154// enable_if_t<true>* = nullptr
155// vs:
156// if_t<true> = 0
157
158template <bool V>
159using if_t = enable_if_t<V, int>;
160
161template <bool V>
162using if_not_t = enable_if_t<false == V, int>;
163
164namespace _
165{
166
167// Utility to prevent static assert from immediately triggering
168template <class... T>
170 static const bool value = false;
171};
172
173} // namespace _
174
175} // namespace flecs
176
177#include "array.hpp"
178#include "string.hpp"
179#include "enum.hpp"
180#include "stringstream.hpp"
181#include "function_traits.hpp"
Array class.
Compile time enum reflection utilities.
Compile time utilities to inspect properties of functions.
String utility that doesn't implicitly allocate memory.
Wrapper around ecs_strbuf_t that provides a simple stringstream like API.