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 63531a8 commit 37a441b
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 131 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
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/aabb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace godot {

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

Vector3 position;
Vector3 size;
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/basis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace godot {

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

Vector3 elements[3] = {
Vector3(1, 0, 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 @@ -42,6 +42,7 @@ class CharString {
const char *_data = nullptr;
int _length = 0;

CharString(){};
CharString(const char *str, int length);

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

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

Expand All @@ -72,6 +74,7 @@ class Char32String {
const char32_t *_data = nullptr;
int _length = 0;

Char32String(){};
Char32String(const char32_t *str, int length);

public:
Expand All @@ -87,6 +90,7 @@ class CharWideString {
const wchar_t *_data = nullptr;
int _length = 0;

CharWideString(){};
CharWideString(const wchar_t *str, int length);

public:
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class String;

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

union {
struct {
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/plane.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace godot {

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

Vector3 normal;
real_t d = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace godot {

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

union {
struct {
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/rect2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Transform2D;

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

Point2 position;
Size2 size;
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/rect2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace godot {

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

Point2i position;
Size2i size;
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/transform2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace godot {

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

// 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])
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/transform3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace godot {

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

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
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Vector2i;

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

enum Axis {
AXIS_X,
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/vector2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace godot {

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

enum Axis {
AXIS_X,
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Vector3i;

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

enum Axis {
AXIS_X,
Expand Down
2 changes: 1 addition & 1 deletion include/godot_cpp/variant/vector3i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace godot {

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

enum Axis {
AXIS_X,
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
84 changes: 70 additions & 14 deletions src/variant/char_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@

namespace godot {

int CharString::length() const {
return _length;
}

const char *CharString::get_data() const {
return _data;
}
Expand All @@ -50,20 +54,44 @@ CharString::~CharString() {
memdelete_arr(_data);
}

int Char16String::length() const {
return _length;
}

const char16_t *Char16String::get_data() const {
return _data;
}

Char16String::Char16String(const char16_t *str, int length) :
_data(str), _length(length) {}

Char16String::~Char16String() {
memdelete_arr(_data);
}

int Char32String::length() const {
return _length;
}

const char32_t *Char32String::get_data() const {
return _data;
}

Char32String::Char32String(const char32_t *str, int length) :
_data(str), _length(length) {}

Char32String::~Char32String() {
memdelete_arr(_data);
}

int CharWideString::length() const {
return _length;
}

const wchar_t *CharWideString::get_data() const {
return _data;
}

CharWideString::CharWideString(const wchar_t *str, int length) :
_data(str), _length(length) {}

Expand All @@ -75,65 +103,85 @@ CharWideString::~CharWideString() {
// It's easier to have them written in C++ directly than in a Python script that generates them.

String::String(const char *from) {
internal::gdn_interface->string_new_with_utf8_chars(ptr(), from);
internal::gdn_interface->string_new_with_latin1_chars(_native_ptr(), from);
}

String::String(const wchar_t *from) {
internal::gdn_interface->string_new_with_wide_chars(ptr(), from);
internal::gdn_interface->string_new_with_wide_chars(_native_ptr(), from);
}

String::String(const char16_t *from) {
internal::gdn_interface->string_new_with_utf16_chars(ptr(), from);
internal::gdn_interface->string_new_with_utf16_chars(_native_ptr(), from);
}

String::String(const char32_t *from) {
internal::gdn_interface->string_new_with_utf32_chars(ptr(), from);
internal::gdn_interface->string_new_with_utf32_chars(_native_ptr(), from);
}

String String::utf8(const char *from, int len) {
String ret;
ret.parse_utf8(from, len);
return ret;
}

void String::parse_utf8(const char *from, int len) {
internal::gdn_interface->string_new_with_utf8_chars_and_len(_native_ptr(), from, len);
}

String String::utf16(const char16_t *from, int len) {
String ret;
ret.parse_utf16(from, len);
return ret;
}

void String::parse_utf16(const char16_t *from, int len) {
internal::gdn_interface->string_new_with_utf16_chars_and_len(_native_ptr(), from, len);
}

CharString String::utf8() const {
int size = internal::gdn_interface->string_to_utf8_chars(ptr(), nullptr, 0);
int size = internal::gdn_interface->string_to_utf8_chars(_native_ptr(), nullptr, 0);
char *cstr = memnew_arr(char, size + 1);
internal::gdn_interface->string_to_utf8_chars(ptr(), cstr, size + 1);
internal::gdn_interface->string_to_utf8_chars(_native_ptr(), cstr, size + 1);

cstr[size] = '\0';

return CharString(cstr, size + 1);
}

CharString String::ascii() const {
int size = internal::gdn_interface->string_to_latin1_chars(ptr(), nullptr, 0);
int size = internal::gdn_interface->string_to_latin1_chars(_native_ptr(), nullptr, 0);
char *cstr = memnew_arr(char, size + 1);
internal::gdn_interface->string_to_latin1_chars(ptr(), cstr, size + 1);
internal::gdn_interface->string_to_latin1_chars(_native_ptr(), cstr, size + 1);

cstr[size] = '\0';

return CharString(cstr, size + 1);
}

Char16String String::utf16() const {
int size = internal::gdn_interface->string_to_utf16_chars(ptr(), nullptr, 0);
int size = internal::gdn_interface->string_to_utf16_chars(_native_ptr(), nullptr, 0);
char16_t *cstr = memnew_arr(char16_t, size + 1);
internal::gdn_interface->string_to_utf16_chars(ptr(), cstr, size + 1);
internal::gdn_interface->string_to_utf16_chars(_native_ptr(), cstr, size + 1);

cstr[size] = '\0';

return Char16String(cstr, size + 1);
}

Char32String String::utf32() const {
int size = internal::gdn_interface->string_to_utf32_chars(ptr(), nullptr, 0);
int size = internal::gdn_interface->string_to_utf32_chars(_native_ptr(), nullptr, 0);
char32_t *cstr = memnew_arr(char32_t, size + 1);
internal::gdn_interface->string_to_utf32_chars(ptr(), cstr, size + 1);
internal::gdn_interface->string_to_utf32_chars(_native_ptr(), cstr, size + 1);

cstr[size] = '\0';

return Char32String(cstr, size + 1);
}

CharWideString String::wide_string() const {
int size = internal::gdn_interface->string_to_wide_chars(ptr(), nullptr, 0);
int size = internal::gdn_interface->string_to_wide_chars(_native_ptr(), nullptr, 0);
wchar_t *cstr = memnew_arr(wchar_t, size + 1);
internal::gdn_interface->string_to_wide_chars(ptr(), cstr, size + 1);
internal::gdn_interface->string_to_wide_chars(_native_ptr(), cstr, size + 1);

cstr[size] = '\0';

Expand Down Expand Up @@ -200,6 +248,14 @@ char32_t &String::operator[](int p_index) {
return *internal::gdn_interface->string_operator_index((GDNativeStringPtr)this, p_index);
}

const char32_t *String::ptr() const {
return internal::gdn_interface->string_operator_index_const((GDNativeStringPtr)this, 0);
}

char32_t *String::ptrw() {
return internal::gdn_interface->string_operator_index((GDNativeStringPtr)this, 0);
}

bool operator==(const char *p_chr, const String &p_str) {
return p_str == String(p_chr);
}
Expand Down
Loading

0 comments on commit 37a441b

Please sign in to comment.