Flecs v4.0
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// C++11/C++14 convenience template replacements
72
73template <bool V, typename T, typename F>
74using conditional_t = typename std::conditional<V, T, F>::type;
75
76template <typename T>
77using decay_t = typename std::decay<T>::type;
78
79template <bool V, typename T = void>
80using enable_if_t = typename std::enable_if<V, T>::type;
81
82template <typename T>
83using remove_pointer_t = typename std::remove_pointer<T>::type;
84
85template <typename T>
86using remove_reference_t = typename std::remove_reference<T>::type;
87
88template <typename T>
89using underlying_type_t = typename std::underlying_type<T>::type;
90
91using std::is_base_of;
92using std::is_empty;
93using std::is_const;
94using std::is_pointer;
95using std::is_reference;
96using std::is_volatile;
97using std::is_same;
98using std::is_enum;
99
100// GCC 4.9.2 compatibility: missing C++11 type traits
101#if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 9)
102// Direct implementations for missing type traits in GCC 4.9.2
103} // namespace flecs
104
105namespace std {
106 // Only implement the ones that are actually missing in GCC 4.9.2
107 template<typename T>
108 struct is_trivially_constructible {
109 static const bool value = __is_pod(T);
110 };
111
112 template<typename T>
113 struct is_trivially_move_assignable {
114 static const bool value = __is_pod(T);
115 };
116
117 template<typename T>
118 struct is_trivially_copy_assignable {
119 static const bool value = __is_pod(T);
120 };
121
122 template<typename T>
123 struct is_trivially_copy_constructible {
124 static const bool value = __is_pod(T);
125 };
126
127 template<typename T>
128 struct is_trivially_move_constructible {
129 static const bool value = __is_pod(T);
130 };
131
132 template<typename T>
133 struct is_trivially_copyable {
134 static const bool value = __is_pod(T);
135 };
136}
137
138namespace flecs {
139#else
140using std::is_trivially_constructible;
141using std::is_trivially_move_assignable;
142using std::is_trivially_copy_assignable;
143using std::is_trivially_copy_constructible;
144using std::is_trivially_move_constructible;
145using std::is_trivially_copyable;
146#endif
147
148// These exist in GCC 4.9.2, so we can always use them
149using std::is_move_assignable;
150using std::is_move_constructible;
151using std::is_copy_constructible;
152using std::is_trivially_destructible;
153
154// Determine constness even if T is a pointer type
155template <typename T>
156using is_const_p = is_const< remove_pointer_t<T> >;
157
158// Apply cv modifiers from source type to destination type
159// (from: https://stackoverflow.com/questions/52559336/add-const-to-type-if-template-arg-is-const)
160template<class Src, class Dst>
161using transcribe_const_t = conditional_t<is_const<Src>::value, Dst const, Dst>;
162
163template<class Src, class Dst>
164using transcribe_volatile_t = conditional_t<is_volatile<Src>::value, Dst volatile, Dst>;
165
166template<class Src, class Dst>
167using transcribe_cv_t = transcribe_const_t< Src, transcribe_volatile_t< Src, Dst> >;
168
169template<class Src, class Dst>
170using transcribe_pointer_t = conditional_t<is_pointer<Src>::value, Dst*, Dst>;
171
172template<class Src, class Dst>
173using transcribe_cvp_t = transcribe_cv_t< Src, transcribe_pointer_t< Src, Dst> >;
174
175
176// More convenience templates. The if_*_t templates use int as default type
177// instead of void. This enables writing code that's a bit less cluttered when
178// the templates are used in a template declaration:
179//
180// enable_if_t<true>* = nullptr
181// vs:
182// if_t<true> = 0
183
184template <bool V>
185using if_t = enable_if_t<V, int>;
186
187template <bool V>
188using if_not_t = enable_if_t<false == V, int>;
189
190namespace _
191{
192
193// Utility to prevent static assert from immediately triggering
194template <class... T>
196 static const bool value = false;
197};
198
199} // namespace _
200
201} // namespace flecs
202
203#include "array.hpp"
204#include "string.hpp"
205#include "enum.hpp"
206#include "stringstream.hpp"
207#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.