Skip to content

Commit

Permalink
Add ptr() / ptrw() to the arrays, add missing String methods, a…
Browse files Browse the repository at this point in the history
…dd missing `CharString` method implementations.
  • Loading branch information
bruvzg committed Feb 14, 2022
1 parent be34bcf commit ef3c391
Show file tree
Hide file tree
Showing 20 changed files with 332 additions and 158 deletions.
10 changes: 9 additions & 1 deletion binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append(f"\tstatic constexpr size_t {snake_class_name}_SIZE = {size};")
result.append(f"\tuint8_t opaque[{snake_class_name}_SIZE] = {{}};")
result.append(
f"\t_FORCE_INLINE_ GDNativeTypePtr ptr() const {{ return const_cast<uint8_t (*)[{snake_class_name}_SIZE]>(&opaque); }}"
f"\t_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const {{ return const_cast<uint8_t (*)[{snake_class_name}_SIZE]>(&opaque); }}"
)

result.append("")
Expand Down Expand Up @@ -378,6 +378,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl

# Special cases.
if class_name == "String":
result.append("\tstatic String utf8(const char *from, int len = -1);")
result.append("\tvoid parse_utf8(const char *from, int len = -1);")
result.append("\tstatic String utf16(const char16_t *from, int len = -1);")
result.append("\tvoid parse_utf16(const char16_t *from, int len = -1);")
result.append("\tCharString utf8() const;")
result.append("\tCharString ascii() const;")
result.append("\tChar16String utf16() const;")
Expand Down Expand Up @@ -426,6 +430,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("bool operator!=(const char32_t *p_str) const;")
result.append(f"\tconst char32_t &operator[](int p_index) const;")
result.append(f"\tchar32_t &operator[](int p_index);")
result.append(f"\tconst char32_t *ptr() const;")
result.append(f"\tchar32_t *ptrw();")

if class_name == "Array":
result.append("\ttemplate <class... Args>")
Expand All @@ -443,6 +449,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
return_type = "float"
result.append(f"\tconst " + return_type + f" &operator[](int p_index) const;")
result.append(f"\t" + return_type + f" &operator[](int p_index);")
result.append(f"\tconst " + return_type + f" *ptr() const;")
result.append(f"\t" + return_type + f" *ptrw();")

if class_name == "Array":
result.append(f"\tconst Variant &operator[](int p_index) const;")
Expand Down
10 changes: 6 additions & 4 deletions include/godot_cpp/variant/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
namespace godot {

class AABB {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
Vector3 position;
Vector3 size;

Expand All @@ -73,8 +75,8 @@ class AABB {
inline bool encloses(const AABB &p_aabb) const; /// p_aabb is completely inside this

AABB merge(const AABB &p_with) const;
void merge_with(const AABB &p_aabb); ///merge with another AABB
AABB intersection(const AABB &p_aabb) const; ///get box where two intersect, empty if no intersection occurs
void merge_with(const AABB &p_aabb); /// merge with another AABB
AABB intersection(const AABB &p_aabb) const; /// get box where two intersect, empty if no intersection occurs
bool intersects_segment(const Vector3 &p_from, const Vector3 &p_to, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
bool intersects_ray(const Vector3 &p_from, const Vector3 &p_dir, Vector3 *r_clip = nullptr, Vector3 *r_normal = nullptr) const;
inline bool smits_intersect_ray(const Vector3 &p_from, const Vector3 &p_dir, real_t t0, real_t t1) const;
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
namespace godot {

class Basis {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
Vector3 elements[3] = {
Vector3(1, 0, 0),
Vector3(0, 1, 0),
Expand Down
4 changes: 4 additions & 0 deletions include/godot_cpp/variant/char_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CharString {
int length() const;
const char *get_data() const;

CharString() {}
~CharString();
};

Expand All @@ -63,6 +64,7 @@ class Char16String {
int length() const;
const char16_t *get_data() const;

Char16String() {}
~Char16String();
};

Expand All @@ -78,6 +80,7 @@ class Char32String {
int length() const;
const char32_t *get_data() const;

Char32String() {}
~Char32String();
};

Expand All @@ -93,6 +96,7 @@ class CharWideString {
int length() const;
const wchar_t *get_data() const;

CharWideString() {}
~CharWideString();
};

Expand Down
8 changes: 5 additions & 3 deletions include/godot_cpp/variant/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ namespace godot {
class String;

class Color {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
union {
struct {
float r;
Expand Down Expand Up @@ -198,7 +200,7 @@ class Color {
static Color from_hsv(float p_h, float p_s, float p_v, float p_a);
static Color from_rgbe9995(uint32_t p_rgbe);

inline bool operator<(const Color &p_color) const; //used in set keys
inline bool operator<(const Color &p_color) const; // used in set keys
operator String() const;

// For the binder.
Expand Down
8 changes: 5 additions & 3 deletions include/godot_cpp/variant/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@
namespace godot {

class Plane {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
Vector3 normal;
real_t d = 0;

void set_normal(const Vector3 &p_normal);
inline Vector3 get_normal() const { return normal; }; ///Point is coplanar, CMP_EPSILON for precision
inline Vector3 get_normal() const { return normal; }; /// Point is coplanar, CMP_EPSILON for precision

void normalize();
Plane normalized() const;
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
namespace godot {

class Quaternion {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
union {
struct {
real_t x;
Expand Down
16 changes: 9 additions & 7 deletions include/godot_cpp/variant/rect2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ namespace godot {
class Transform2D;

class Rect2 {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
Point2 position;
Size2 size;

Expand Down Expand Up @@ -161,7 +163,7 @@ class Rect2 {
new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);

new_rect.size = new_rect.size - new_rect.position; //make relative again
new_rect.size = new_rect.size - new_rect.position; // make relative again

return new_rect;
}
Expand Down Expand Up @@ -225,7 +227,7 @@ class Rect2 {
return r;
}

inline void expand_to(const Vector2 &p_vector) { //in place function for speed
inline void expand_to(const Vector2 &p_vector) { // in place function for speed

Vector2 begin = position;
Vector2 end = position + size;
Expand Down Expand Up @@ -279,7 +281,7 @@ class Rect2 {
continue;
}

//check inside
// check inside
Vector2 tg = r.orthogonal();
float s = tg.dot(center) - tg.dot(a);
if (s < 0.0) {
Expand All @@ -288,7 +290,7 @@ class Rect2 {
side_minus++;
}

//check ray box
// check ray box
r /= l;
Vector2 ir((real_t)1.0 / r.x, (real_t)1.0 / r.y);

Expand All @@ -309,7 +311,7 @@ class Rect2 {
}

if (side_plus * side_minus == 0) {
return true; //all inside
return true; // all inside
} else {
return false;
}
Expand Down
8 changes: 5 additions & 3 deletions include/godot_cpp/variant/rect2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
namespace godot {

class Rect2i {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
Point2i position;
Size2i size;

Expand Down Expand Up @@ -107,7 +109,7 @@ class Rect2i {
new_rect.size.x = Math::max(p_rect.position.x + p_rect.size.x, position.x + size.x);
new_rect.size.y = Math::max(p_rect.position.y + p_rect.size.y, position.y + size.y);

new_rect.size = new_rect.size - new_rect.position; //make relative again
new_rect.size = new_rect.size - new_rect.position; // make relative again

return new_rect;
}
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/transform2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
namespace godot {

class Transform2D {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
// Warning #1: basis of Transform2D is stored differently from Basis. In terms of elements array, the basis matrix looks like "on paper":
// M = (elements[0][0] elements[1][0])
// (elements[0][1] elements[1][1])
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/transform3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
namespace godot {

class Transform3D {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
Basis basis;
Vector3 origin;

Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace godot {
class Variant {
uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 };

_FORCE_INLINE_ GDNativeVariantPtr ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
_FORCE_INLINE_ GDNativeVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }

friend class GDExtensionBinding;
friend class MethodBind;
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ namespace godot {
class Vector2i;

class Vector2 {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
enum Axis {
AXIS_X,
AXIS_Y,
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/vector2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@
namespace godot {

class Vector2i {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
enum Axis {
AXIS_X,
AXIS_Y,
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class Basis;
class Vector3i;

class Vector3 {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
enum Axis {
AXIS_X,
AXIS_Y,
Expand Down
6 changes: 4 additions & 2 deletions include/godot_cpp/variant/vector3i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
namespace godot {

class Vector3i {
public:
_FORCE_INLINE_ GDNativeTypePtr ptr() const { return (void *)this; }
_FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }

friend class Variant;

public:
enum Axis {
AXIS_X,
AXIS_Y,
Expand Down
2 changes: 1 addition & 1 deletion src/core/method_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void MethodBind::bind_call(void *p_method_userdata, GDExtensionClassInstancePtr
Variant ret = bind->call(p_instance, p_args, p_argument_count, *r_error);
// This assumes the return value is an empty Variant, so it doesn't need to call the destructor first.
// Since only NativeExtensionMethodBind calls this from the Godot side, it should always be the case.
internal::gdn_interface->variant_new_copy(r_return, ret.ptr());
internal::gdn_interface->variant_new_copy(r_return, ret._native_ptr());
}

void MethodBind::bind_ptrcall(void *p_method_userdata, GDExtensionClassInstancePtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr r_return) {
Expand Down
Loading

0 comments on commit ef3c391

Please sign in to comment.