Skip to content

Commit eb790bd

Browse files
committed
Port a bunch of Godot container templates to GDExtension.
1 parent 9bc489e commit eb790bd

24 files changed

+7246
-3
lines changed

binding_generator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
221221
# Special cases.
222222
if class_name == "String":
223223
result.append("#include <godot_cpp/variant/char_string.hpp>")
224+
result.append("#include <godot_cpp/variant/char_utils.hpp>")
225+
result.append("#include <godot_cpp/variant/ucaps.hpp>")
224226

225227
if class_name == "Array":
226228
result.append("#include <godot_cpp/variant/array_helpers.hpp>")
@@ -1755,8 +1757,8 @@ def get_default_value_for_type(type_name):
17551757
/* GODOT ENGINE */
17561758
/* https://godotengine.org */
17571759
/*************************************************************************/
1758-
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
1759-
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
1760+
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
1761+
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
17601762
/* */
17611763
/* Permission is hereby granted, free of charge, to any person obtaining */
17621764
/* a copy of this software and associated documentation files (the */

include/godot_cpp/core/class_db.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, const char *p_name, M
209209

210210
return bind;
211211
}
212+
213+
#define GDREGISTER_CLASS(m_class) ClassDB::register_class<m_class>();
214+
212215
} // namespace godot
213216

214217
#endif // ! CLASS_DB_HPP

include/godot_cpp/core/math.hpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ namespace Math {
6262
#define Math_INF INFINITY
6363
#define Math_NAN NAN
6464

65+
// Windows badly defines a lot of stuff we'll never use. Undefine it.
66+
#ifdef _WIN32
67+
#undef MIN // override standard definition
68+
#undef MAX // override standard definition
69+
#undef CLAMP // override standard definition
70+
#endif
71+
72+
// Generic ABS function, for math uses please use Math::abs.
73+
#ifndef ABS
74+
#define ABS(m_v) (((m_v) < 0) ? (-(m_v)) : (m_v))
75+
#endif
76+
77+
#ifndef SIGN
78+
#define SIGN(m_v) (((m_v) == 0) ? (0.0) : (((m_v) < 0) ? (-1.0) : (+1.0)))
79+
#endif
80+
81+
#ifndef MIN
82+
#define MIN(m_a, m_b) (((m_a) < (m_b)) ? (m_a) : (m_b))
83+
#endif
84+
85+
#ifndef MAX
86+
#define MAX(m_a, m_b) (((m_a) > (m_b)) ? (m_a) : (m_b))
87+
#endif
88+
89+
#ifndef CLAMP
90+
#define CLAMP(m_a, m_min, m_max) (((m_a) < (m_min)) ? (m_min) : (((m_a) > (m_max)) ? m_max : m_a))
91+
#endif
92+
6593
// Functions reproduced as in Godot's source code `math_funcs.h`.
6694
// Some are overloads to automatically support changing real_t into either double or float in the way Godot does.
6795

@@ -435,7 +463,7 @@ inline int fast_ftoi(float a) {
435463
: "m" (a));*/
436464

437465
#else
438-
b = lrintf(a); //assuming everything but msvc 2012 or earlier has lrint
466+
b = lrintf(a); // assuming everything but msvc 2012 or earlier has lrint
439467
#endif
440468
return b;
441469
}

include/godot_cpp/core/memory.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <type_traits>
4242

4343
void *operator new(size_t p_size, const char *p_description); ///< operator new that takes a description and uses MemoryStaticPool
44+
void *operator new(size_t p_size, void *(*p_allocfunc)(size_t p_size)); ///< operator new that takes a description and uses MemoryStaticPool
4445
void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description); ///< operator new that takes a description and uses a pointer to the preallocated memory
4546

4647
_ALWAYS_INLINE_ void *operator new(size_t p_size, void *p_pointer, size_t check, const char *p_description) {
@@ -76,10 +77,27 @@ _ALWAYS_INLINE_ T *_post_initialize(T *p_obj) {
7677
return p_obj;
7778
}
7879

80+
#define memalloc(m_size) Memory::alloc_static(m_size)
81+
#define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size)
82+
#define memfree(m_mem) Memory::free_static(m_mem)
83+
7984
#define memnew(m_class) _post_initialize(new ("") m_class)
8085

86+
#define memnew_allocator(m_class, m_allocator) _post_initialize(new (m_allocator::alloc) m_class)
8187
#define memnew_placement(m_placement, m_class) _post_initialize(new (m_placement, sizeof(m_class), "") m_class)
8288

89+
// Generic comparator used in Map, List, etc.
90+
template <class T>
91+
struct Comparator {
92+
_ALWAYS_INLINE_ bool operator()(const T &p_a, const T &p_b) const { return (p_a < p_b); }
93+
};
94+
95+
class DefaultAllocator {
96+
public:
97+
_ALWAYS_INLINE_ static void *alloc(size_t p_memory) { return Memory::alloc_static(p_memory); }
98+
_ALWAYS_INLINE_ static void free(void *p_ptr) { Memory::free_static(p_ptr); }
99+
};
100+
83101
template <class T>
84102
void memdelete(T *p_class, typename std::enable_if<!std::is_base_of_v<godot::Wrapped, T>>::type * = 0) {
85103
if (!__has_trivial_destructor(T)) {
@@ -94,6 +112,15 @@ void memdelete(T *p_class) {
94112
godot::internal::gdn_interface->object_destroy(p_class->_owner);
95113
}
96114

115+
template <class T, class A>
116+
void memdelete_allocator(T *p_class) {
117+
if (!__has_trivial_destructor(T)) {
118+
p_class->~T();
119+
}
120+
121+
A::free(p_class);
122+
}
123+
97124
#define memnew_arr(m_class, m_count) memnew_arr_template<m_class>(m_count)
98125

99126
template <typename T>
@@ -137,6 +164,19 @@ void memdelete_arr(T *p_class) {
137164
Memory::free_static(ptr);
138165
}
139166

167+
struct _GlobalNil {
168+
int color = 1;
169+
_GlobalNil *right;
170+
_GlobalNil *left;
171+
_GlobalNil *parent;
172+
173+
_GlobalNil();
174+
};
175+
176+
struct _GlobalNilClass {
177+
static _GlobalNil _nil;
178+
};
179+
140180
} // namespace godot
141181

142182
#endif // ! GODOT_CPP_MEMORY_HPP
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*************************************************************************/
2+
/* mutex_lock.hpp */
3+
/*************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/*************************************************************************/
8+
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
9+
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/*************************************************************************/
30+
31+
#ifndef MUTEX_LOCK_HPP
32+
#define MUTEX_LOCK_HPP
33+
34+
#include <godot_cpp/classes/mutex.hpp>
35+
36+
namespace godot {
37+
38+
class MutexLock {
39+
const Mutex &mutex;
40+
41+
public:
42+
_ALWAYS_INLINE_ explicit MutexLock(const Mutex &p_mutex) :
43+
mutex(p_mutex) {
44+
const_cast<Mutex *>(&mutex)->lock();
45+
}
46+
47+
_ALWAYS_INLINE_ ~MutexLock() {
48+
const_cast<Mutex *>(&mutex)->unlock();
49+
}
50+
};
51+
52+
#define _THREAD_SAFE_CLASS_ mutable Mutex _thread_safe_;
53+
#define _THREAD_SAFE_METHOD_ MutexLock _thread_safe_method_(_thread_safe_);
54+
#define _THREAD_SAFE_LOCK_ _thread_safe_.lock();
55+
#define _THREAD_SAFE_UNLOCK_ _thread_safe_.unlock();
56+
57+
} // namespace godot
58+
59+
#endif // ! MUTEX_LOCK_HPP

0 commit comments

Comments
 (0)