diff options
-rw-r--r-- | include/core/Array.hpp | 4 | ||||
-rw-r--r-- | include/core/NodePath.hpp | 5 | ||||
-rw-r--r-- | include/core/PoolArrays.hpp | 37 | ||||
-rw-r--r-- | include/core/String.hpp | 5 | ||||
-rw-r--r-- | include/core/Variant.hpp | 9 | ||||
-rw-r--r-- | include/core/Wrapped.hpp | 1 | ||||
-rw-r--r-- | src/core/Array.cpp | 22 | ||||
-rw-r--r-- | src/core/Dictionary.cpp | 12 | ||||
-rw-r--r-- | src/core/NodePath.cpp | 12 | ||||
-rw-r--r-- | src/core/String.cpp | 47 | ||||
-rw-r--r-- | src/core/Variant.cpp | 53 |
11 files changed, 124 insertions, 83 deletions
diff --git a/include/core/Array.hpp b/include/core/Array.hpp index 3d13914..84dc012 100644 --- a/include/core/Array.hpp +++ b/include/core/Array.hpp @@ -58,6 +58,8 @@ class Array { godot_array _godot_array; friend class Variant; + friend class Dictionary; + friend class String; inline explicit Array(const godot_array &other) { _godot_array = other; } @@ -88,7 +90,7 @@ public: Variant &operator[](const int idx); - Variant operator[](const int idx) const; + const Variant &operator[](const int idx) const; void append(const Variant &v); diff --git a/include/core/NodePath.hpp b/include/core/NodePath.hpp index 221c5ce..36dda7d 100644 --- a/include/core/NodePath.hpp +++ b/include/core/NodePath.hpp @@ -10,6 +10,11 @@ namespace godot { class NodePath { godot_node_path _node_path; + friend class Variant; + inline explicit NodePath(godot_node_path node_path) { + _node_path = node_path; + } + public: NodePath(); diff --git a/include/core/PoolArrays.hpp b/include/core/PoolArrays.hpp index 89172af..9421e83 100644 --- a/include/core/PoolArrays.hpp +++ b/include/core/PoolArrays.hpp @@ -18,6 +18,12 @@ class Array; class PoolByteArray { godot_pool_byte_array _godot_array; + friend class String; + friend class Variant; + inline explicit PoolByteArray(godot_pool_byte_array a) { + _godot_array = a; + } + public: class Read { @@ -116,6 +122,11 @@ public: class PoolIntArray { godot_pool_int_array _godot_array; + friend class Variant; + explicit inline PoolIntArray(godot_pool_int_array a) { + _godot_array = a; + } + public: class Read { friend class PoolIntArray; @@ -213,6 +224,11 @@ public: class PoolRealArray { godot_pool_real_array _godot_array; + friend class Variant; + explicit inline PoolRealArray(godot_pool_real_array a) { + _godot_array = a; + } + public: class Read { friend class PoolRealArray; @@ -310,6 +326,12 @@ public: class PoolStringArray { godot_pool_string_array _godot_array; + friend class String; + friend class Variant; + explicit inline PoolStringArray(godot_pool_string_array a) { + _godot_array = a; + } + public: class Read { friend class PoolStringArray; @@ -407,6 +429,11 @@ public: class PoolVector2Array { godot_pool_vector2_array _godot_array; + friend class Variant; + explicit inline PoolVector2Array(godot_pool_vector2_array a) { + _godot_array = a; + } + public: class Read { friend class PoolVector2Array; @@ -504,6 +531,11 @@ public: class PoolVector3Array { godot_pool_vector3_array _godot_array; + friend class Variant; + explicit inline PoolVector3Array(godot_pool_vector3_array a) { + _godot_array = a; + } + public: class Read { friend class PoolVector3Array; @@ -601,6 +633,11 @@ public: class PoolColorArray { godot_pool_color_array _godot_array; + friend class Variant; + explicit inline PoolColorArray(godot_pool_color_array a) { + _godot_array = a; + } + public: class Read { friend class PoolColorArray; diff --git a/include/core/String.hpp b/include/core/String.hpp index ebf161a..57e7eb7 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -29,7 +29,10 @@ public: class String { godot_string _godot_string; - String(godot_string contents) : + friend class Dictionary; + friend class NodePath; + friend class Variant; + explicit inline String(godot_string contents) : _godot_string(contents) {} public: diff --git a/include/core/Variant.hpp b/include/core/Variant.hpp index 5f40013..872a5a4 100644 --- a/include/core/Variant.hpp +++ b/include/core/Variant.hpp @@ -31,6 +31,11 @@ class Array; class Variant { godot_variant _godot_variant; + friend class Array; + inline explicit Variant(godot_variant v) { + _godot_variant = v; + } + public: enum Type { @@ -226,7 +231,9 @@ public: operator NodePath() const; operator RID() const; operator godot_object *() const; - template <typename T> operator T*() const { return static_cast<T*>(T::___get_from_variant(*this)); } + + template <typename T> + operator T *() const { return static_cast<T *>(T::___get_from_variant(*this)); } operator Dictionary() const; operator Array() const; diff --git a/include/core/Wrapped.hpp b/include/core/Wrapped.hpp index a26b448..6610bff 100644 --- a/include/core/Wrapped.hpp +++ b/include/core/Wrapped.hpp @@ -5,6 +5,7 @@ namespace godot { +// This is an internal base class used by the bindings. You should not need to access its members. class _Wrapped { public: godot_object *_owner; diff --git a/src/core/Array.cpp b/src/core/Array.cpp index 676eba5..04b4a3d 100644 --- a/src/core/Array.cpp +++ b/src/core/Array.cpp @@ -52,14 +52,16 @@ Array::Array(const PoolColorArray &a) { Variant &Array::operator[](const int idx) { godot_variant *v = godot::api->godot_array_operator_index(&_godot_array, idx); - return *(Variant *)v; + // We assume it's ok to reinterpret because the value is a pointer whose data is already owned by the array, + // so can return a reference without constructing a Variant + return *reinterpret_cast<Variant *>(v); } -Variant Array::operator[](const int idx) const { +const Variant &Array::operator[](const int idx) const { // Yes, I'm casting away the const... you can hate me now. // since the result is godot_variant *v = godot::api->godot_array_operator_index((godot_array *)&_godot_array, idx); - return *(Variant *)v; + return *reinterpret_cast<const Variant *>(v); } void Array::append(const Variant &v) { @@ -84,12 +86,12 @@ void Array::erase(const Variant &v) { Variant Array::front() const { godot_variant v = godot::api->godot_array_front(&_godot_array); - return *(Variant *)&v; + return Variant(v); } Variant Array::back() const { godot_variant v = godot::api->godot_array_back(&_godot_array); - return *(Variant *)&v; + return Variant(v); } int Array::find(const Variant &what, const int from) { @@ -118,12 +120,12 @@ void Array::invert() { Variant Array::pop_back() { godot_variant v = godot::api->godot_array_pop_back(&_godot_array); - return *(Variant *)&v; + return Variant(v); } Variant Array::pop_front() { godot_variant v = godot::api->godot_array_pop_front(&_godot_array); - return *(Variant *)&v; + return Variant(v); } void Array::push_back(const Variant &v) { @@ -170,17 +172,17 @@ int Array::bsearch_custom(const Variant &value, const Object *obj, Array Array::duplicate(const bool deep) const { godot_array arr = godot::core_1_1_api->godot_array_duplicate(&_godot_array, deep); - return *(Array *)&arr; + return Array(arr); } Variant Array::max() const { godot_variant v = godot::core_1_1_api->godot_array_max(&_godot_array); - return *(Variant *)&v; + return Variant(v); } Variant Array::min() const { godot_variant v = godot::core_1_1_api->godot_array_min(&_godot_array); - return *(Variant *)&v; + return Variant(v); } void Array::shuffle() { diff --git a/src/core/Dictionary.cpp b/src/core/Dictionary.cpp index 17f16a4..9c5be43 100644 --- a/src/core/Dictionary.cpp +++ b/src/core/Dictionary.cpp @@ -45,16 +45,18 @@ uint32_t Dictionary::hash() const { Array Dictionary::keys() const { godot_array a = godot::api->godot_dictionary_keys(&_godot_dictionary); - return *(Array *)&a; + return Array(a); } Variant &Dictionary::operator[](const Variant &key) { - return *(Variant *)godot::api->godot_dictionary_operator_index(&_godot_dictionary, (godot_variant *)&key); + godot_variant *v = godot::api->godot_dictionary_operator_index(&_godot_dictionary, (godot_variant *)&key); + return *reinterpret_cast<Variant *>(v); } const Variant &Dictionary::operator[](const Variant &key) const { // oops I did it again - return *(Variant *)godot::api->godot_dictionary_operator_index((godot_dictionary *)&_godot_dictionary, (godot_variant *)&key); + godot_variant *v = godot::api->godot_dictionary_operator_index((godot_dictionary *)&_godot_dictionary, (godot_variant *)&key); + return *reinterpret_cast<Variant *>(v); } int Dictionary::size() const { @@ -63,12 +65,12 @@ int Dictionary::size() const { String Dictionary::to_json() const { godot_string s = godot::api->godot_dictionary_to_json(&_godot_dictionary); - return *(String *)&s; + return String(s); } Array Dictionary::values() const { godot_array a = godot::api->godot_dictionary_values(&_godot_dictionary); - return *(Array *)&a; + return Array(a); } Dictionary::~Dictionary() { diff --git a/src/core/NodePath.cpp b/src/core/NodePath.cpp index 37ab2a3..bc069b7 100644 --- a/src/core/NodePath.cpp +++ b/src/core/NodePath.cpp @@ -27,8 +27,7 @@ NodePath::NodePath(const char *contents) { String NodePath::get_name(const int idx) const { godot_string str = godot::api->godot_node_path_get_name(&_node_path, idx); - - return *(String *)&str; + return String(str); } int NodePath::get_name_count() const { @@ -37,7 +36,7 @@ int NodePath::get_name_count() const { String NodePath::get_subname(const int idx) const { godot_string str = godot::api->godot_node_path_get_subname(&_node_path, idx); - return *(String *)&str; + return String(str); } int NodePath::get_subname_count() const { @@ -54,17 +53,16 @@ bool NodePath::is_empty() const { NodePath NodePath::get_as_property_path() const { godot_node_path path = godot::core_1_1_api->godot_node_path_get_as_property_path(&_node_path); - return *(NodePath *)&path; + return NodePath(path); } String NodePath::get_concatenated_subnames() const { godot_string str = godot::api->godot_node_path_get_concatenated_subnames(&_node_path); - return *(String *)&str; + return String(str); } NodePath::operator String() const { godot_string str = godot::api->godot_node_path_as_string(&_node_path); - - return *(String *)&str; + return String(str); } bool NodePath::operator==(const NodePath &other) { diff --git a/src/core/String.cpp b/src/core/String.cpp index 73573f7..552ebfe 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -195,8 +195,7 @@ bool String::begins_with_char_array(const char *p_char_array) const { PoolStringArray String::bigrams() const { godot_array arr = godot::api->godot_string_bigrams(&_godot_string); - - return *(Array *)&arr; + return Array(arr); } String String::c_escape() const { @@ -329,7 +328,7 @@ bool String::matchn(String expr) const { PoolByteArray String::md5_buffer() const { godot_pool_byte_array arr = godot::api->godot_string_md5_buffer(&_godot_string); - return *(PoolByteArray *)&arr; + return PoolByteArray(arr); } String String::md5_text() const { @@ -382,8 +381,7 @@ String String::right(int position) const { PoolByteArray String::sha256_buffer() const { godot_pool_byte_array arr = godot::api->godot_string_sha256_buffer(&_godot_string); - - return *(PoolByteArray *)&arr; + return PoolByteArray(arr); } String String::sha256_text() const { @@ -396,20 +394,19 @@ float String::similarity(String text) const { PoolStringArray String::split(String divisor, bool allow_empty) const { godot_array arr = godot::api->godot_string_split(&_godot_string, &divisor._godot_string); - - return *(Array *)&arr; + return Array(arr); } PoolIntArray String::split_ints(String divisor, bool allow_empty) const { godot_array arr = godot::api->godot_string_split_floats(&_godot_string, &divisor._godot_string); - - return *(Array *)&arr; + return Array(arr); } PoolRealArray String::split_floats(String divisor, bool allow_empty) const { + // TODO The GDNative API returns godot_array, when according to the doc, it should have been godot_pool_real_array godot_array arr = godot::api->godot_string_split_floats(&_godot_string, &divisor._godot_string); - - return *(Array *)&arr; + Array wrapped_array(arr); + return PoolRealArray(wrapped_array); } String String::strip_edges(bool left, bool right) const { @@ -457,33 +454,29 @@ signed char String::naturalnocasecmp_to(String p_str) const { } String String::dedent() const { - String new_string; - new_string._godot_string = godot::core_1_1_api->godot_string_dedent(&_godot_string); - return new_string; + godot_string s = godot::core_1_1_api->godot_string_dedent(&_godot_string); + return String(s); } -PoolStringArray String::rsplit(const String &divisor, const bool allow_empty, - const int maxsplit) const { - godot_pool_string_array arr = godot::core_1_1_api->godot_string_rsplit(&_godot_string, &divisor._godot_string, allow_empty, maxsplit); - return *(PoolStringArray *)&arr; +PoolStringArray String::rsplit(const String &divisor, const bool allow_empty, const int maxsplit) const { + godot_pool_string_array arr = + godot::core_1_1_api->godot_string_rsplit(&_godot_string, &divisor._godot_string, allow_empty, maxsplit); + return PoolStringArray(arr); } String String::rstrip(const String &chars) const { - String new_string; - new_string._godot_string = godot::core_1_1_api->godot_string_rstrip(&_godot_string, &chars._godot_string); - return new_string; + godot_string s = godot::core_1_1_api->godot_string_rstrip(&_godot_string, &chars._godot_string); + return String(s); } String String::trim_prefix(const String &prefix) const { - String new_string; - new_string._godot_string = godot::core_1_1_api->godot_string_trim_prefix(&_godot_string, &prefix._godot_string); - return new_string; + godot_string s = godot::core_1_1_api->godot_string_trim_prefix(&_godot_string, &prefix._godot_string); + return String(s); } String String::trim_suffix(const String &suffix) const { - String new_string; - new_string._godot_string = godot::core_1_1_api->godot_string_trim_suffix(&_godot_string, &suffix._godot_string); - return new_string; + godot_string s = godot::core_1_1_api->godot_string_trim_suffix(&_godot_string, &suffix._godot_string); + return String(s); } } // namespace godot diff --git a/src/core/Variant.cpp b/src/core/Variant.cpp index 86d2f91..9e3927f 100644 --- a/src/core/Variant.cpp +++ b/src/core/Variant.cpp @@ -205,9 +205,8 @@ Variant::operator double() const { return godot::api->godot_variant_as_real(&_godot_variant); } Variant::operator String() const { - String ret; - *(godot_string *)&ret = godot::api->godot_variant_as_string(&_godot_variant); - return ret; + godot_string s = godot::api->godot_variant_as_string(&_godot_variant); + return String(s); } Variant::operator Vector2() const { godot_vector2 s = godot::api->godot_variant_as_vector2(&_godot_variant); @@ -251,9 +250,8 @@ Variant::operator Color() const { return *(Color *)&s; } Variant::operator NodePath() const { - NodePath ret; - *(godot_node_path *)&ret = godot::api->godot_variant_as_node_path(&_godot_variant); - return ret; + godot_node_path ret = godot::api->godot_variant_as_node_path(&_godot_variant); + return NodePath(ret); } Variant::operator RID() const { godot_rid s = godot::api->godot_variant_as_rid(&_godot_variant); @@ -271,52 +269,45 @@ Variant::operator Array() const { } Variant::operator PoolByteArray() const { - PoolByteArray ret; - *(godot_pool_byte_array *)&ret = godot::api->godot_variant_as_pool_byte_array(&_godot_variant); - return ret; + godot_pool_byte_array ret = godot::api->godot_variant_as_pool_byte_array(&_godot_variant); + return PoolByteArray(ret); } Variant::operator PoolIntArray() const { - PoolIntArray ret; - *(godot_pool_int_array *)&ret = godot::api->godot_variant_as_pool_int_array(&_godot_variant); - return ret; + godot_pool_int_array ret = godot::api->godot_variant_as_pool_int_array(&_godot_variant); + return PoolIntArray(ret); } Variant::operator PoolRealArray() const { - PoolRealArray ret; - *(godot_pool_real_array *)&ret = godot::api->godot_variant_as_pool_real_array(&_godot_variant); - return ret; + godot_pool_real_array ret = godot::api->godot_variant_as_pool_real_array(&_godot_variant); + return PoolRealArray(ret); } Variant::operator PoolStringArray() const { - PoolStringArray ret; - *(godot_pool_string_array *)&ret = godot::api->godot_variant_as_pool_string_array(&_godot_variant); - return ret; + godot_pool_string_array ret = godot::api->godot_variant_as_pool_string_array(&_godot_variant); + return PoolStringArray(ret); } Variant::operator PoolVector2Array() const { - PoolVector2Array ret; - *(godot_pool_vector2_array *)&ret = godot::api->godot_variant_as_pool_vector2_array(&_godot_variant); - return ret; + godot_pool_vector2_array ret = godot::api->godot_variant_as_pool_vector2_array(&_godot_variant); + return PoolVector2Array(ret); } Variant::operator PoolVector3Array() const { - PoolVector3Array ret; - *(godot_pool_vector3_array *)&ret = godot::api->godot_variant_as_pool_vector3_array(&_godot_variant); - return ret; + godot_pool_vector3_array ret = godot::api->godot_variant_as_pool_vector3_array(&_godot_variant); + return PoolVector3Array(ret); } Variant::operator PoolColorArray() const { - PoolColorArray ret; - *(godot_pool_color_array *)&ret = godot::api->godot_variant_as_pool_color_array(&_godot_variant); - return ret; + godot_pool_color_array ret = godot::api->godot_variant_as_pool_color_array(&_godot_variant); + return PoolColorArray(ret); } Variant::operator godot_object *() const { return godot::api->godot_variant_as_object(&_godot_variant); } Variant::Type Variant::get_type() const { - return (Type)godot::api->godot_variant_get_type(&_godot_variant); + return static_cast<Type>(godot::api->godot_variant_get_type(&_godot_variant)); } Variant Variant::call(const String &method, const Variant **args, const int arg_count) { - Variant v; - *(godot_variant *)&v = godot::api->godot_variant_call(&_godot_variant, (godot_string *)&method, (const godot_variant **)args, arg_count, nullptr); - return v; + godot_variant v = godot::api->godot_variant_call( + &_godot_variant, (godot_string *)&method, (const godot_variant **)args, arg_count, nullptr); + return Variant(v); } bool Variant::has_method(const String &method) { |