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// Determine constness even if T is a pointer type
101template <typename T>
102using is_const_p = is_const< remove_pointer_t<T> >;
103
104// Apply cv modifiers from source type to destination type
105// (from: https://stackoverflow.com/questions/52559336/add-const-to-type-if-template-arg-is-const)
106template<class Src, class Dst>
107using transcribe_const_t = conditional_t<is_const<Src>::value, Dst const, Dst>;
108
109template<class Src, class Dst>
110using transcribe_volatile_t = conditional_t<is_volatile<Src>::value, Dst volatile, Dst>;
111
112template<class Src, class Dst>
113using transcribe_cv_t = transcribe_const_t< Src, transcribe_volatile_t< Src, Dst> >;
114
115template<class Src, class Dst>
116using transcribe_pointer_t = conditional_t<is_pointer<Src>::value, Dst*, Dst>;
117
118template<class Src, class Dst>
119using transcribe_cvp_t = transcribe_cv_t< Src, transcribe_pointer_t< Src, Dst> >;
120
121
122// More convenience templates. The if_*_t templates use int as default type
123// instead of void. This enables writing code that's a bit less cluttered when
124// the templates are used in a template declaration:
125//
126// enable_if_t<true>* = nullptr
127// vs:
128// if_t<true> = 0
129
130template <bool V>
131using if_t = enable_if_t<V, int>;
132
133template <bool V>
134using if_not_t = enable_if_t<false == V, int>;
135
136namespace _
137{
138
139// Utility to prevent static assert from immediately triggering
140template <class... T>
142 static const bool value = false;
143};
144
145} // namespace _
146
147} // namespace flecs
148
149#include <stdlib.h>
150#include "array.hpp"
151#include "string.hpp"
152#include "enum.hpp"
153#include "stringstream.hpp"
154#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.