Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/core/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -88,7 +90,7 @@ class Array {

Variant &operator[](const int idx);

Variant operator[](const int idx) const;
const Variant &operator[](const int idx) const;

void append(const Variant &v);

Expand Down
5 changes: 5 additions & 0 deletions include/core/NodePath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
37 changes: 37 additions & 0 deletions include/core/PoolArrays.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -116,6 +122,11 @@ class PoolByteArray {
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;
Expand Down Expand Up @@ -213,6 +224,11 @@ class PoolIntArray {
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;
Expand Down Expand Up @@ -310,6 +326,12 @@ class PoolRealArray {
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;
Expand Down Expand Up @@ -407,6 +429,11 @@ class PoolStringArray {
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;
Expand Down Expand Up @@ -504,6 +531,11 @@ class PoolVector2Array {
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;
Expand Down Expand Up @@ -601,6 +633,11 @@ class PoolVector3Array {
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;
Expand Down
5 changes: 4 additions & 1 deletion include/core/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class CharString {
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:
Expand Down
9 changes: 8 additions & 1 deletion include/core/Variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -226,7 +231,9 @@ class Variant {
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;
Expand Down
1 change: 1 addition & 0 deletions include/core/Wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 12 additions & 10 deletions src/core/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) const {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 7 additions & 5 deletions src/core/Dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down
12 changes: 5 additions & 7 deletions src/core/NodePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
Loading