godot-cpp/src/core/Variant.cpp

383 lines
13 KiB
C++
Raw Normal View History

/*************************************************************************/
/* Variant.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2017-03-06 07:49:24 +00:00
#include "Variant.hpp"
2017-10-03 10:37:34 +00:00
#include <gdnative/variant.h>
2017-03-06 07:49:24 +00:00
#include "CoreTypes.hpp"
#include "Defs.hpp"
2017-10-20 23:42:10 +00:00
#include "GodotGlobal.hpp"
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
#include "Object.hpp"
namespace godot {
Variant::Variant() {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_nil(&_godot_variant);
}
Variant::Variant(const Variant &v) {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_copy(&_godot_variant, &v._godot_variant);
}
Variant::Variant(bool p_bool) {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_bool(&_godot_variant, p_bool);
}
Variant::Variant(signed int p_int) // real one
{
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_int(&_godot_variant, p_int);
}
Variant::Variant(unsigned int p_int) {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_uint(&_godot_variant, p_int);
}
Variant::Variant(signed short p_short) // real one
{
godot::api->godot_variant_new_int(&_godot_variant, (int)p_short);
}
Variant::Variant(int64_t p_char) // real one
{
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_int(&_godot_variant, p_char);
}
Variant::Variant(uint64_t p_char) {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_uint(&_godot_variant, p_char);
}
Variant::Variant(float p_float) {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_real(&_godot_variant, p_float);
}
Variant::Variant(double p_double) {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_real(&_godot_variant, p_double);
}
Variant::Variant(const String &p_string) {
godot::api->godot_variant_new_string(&_godot_variant, (godot_string *)&p_string);
}
Variant::Variant(const char *const p_cstring) {
String s = String(p_cstring);
godot::api->godot_variant_new_string(&_godot_variant, (godot_string *)&s);
}
Variant::Variant(const wchar_t *p_wstring) {
String s = p_wstring;
godot::api->godot_variant_new_string(&_godot_variant, (godot_string *)&s);
}
Variant::Variant(const Vector2 &p_vector2) {
godot::api->godot_variant_new_vector2(&_godot_variant, (godot_vector2 *)&p_vector2);
}
Variant::Variant(const Rect2 &p_rect2) {
godot::api->godot_variant_new_rect2(&_godot_variant, (godot_rect2 *)&p_rect2);
}
Variant::Variant(const Vector3 &p_vector3) {
godot::api->godot_variant_new_vector3(&_godot_variant, (godot_vector3 *)&p_vector3);
}
Variant::Variant(const Plane &p_plane) {
godot::api->godot_variant_new_plane(&_godot_variant, (godot_plane *)&p_plane);
}
Variant::Variant(const AABB &p_aabb) {
godot::api->godot_variant_new_aabb(&_godot_variant, (godot_aabb *)&p_aabb);
}
Variant::Variant(const Quat &p_quat) {
godot::api->godot_variant_new_quat(&_godot_variant, (godot_quat *)&p_quat);
}
Variant::Variant(const Basis &p_transform) {
godot::api->godot_variant_new_basis(&_godot_variant, (godot_basis *)&p_transform);
}
Variant::Variant(const Transform2D &p_transform) {
godot::api->godot_variant_new_transform2d(&_godot_variant, (godot_transform2d *)&p_transform);
}
Variant::Variant(const Transform &p_transform) {
godot::api->godot_variant_new_transform(&_godot_variant, (godot_transform *)&p_transform);
}
Variant::Variant(const Color &p_color) {
godot::api->godot_variant_new_color(&_godot_variant, (godot_color *)&p_color);
}
Variant::Variant(const NodePath &p_path) {
godot::api->godot_variant_new_node_path(&_godot_variant, (godot_node_path *)&p_path);
}
Variant::Variant(const RID &p_rid) {
godot::api->godot_variant_new_rid(&_godot_variant, (godot_rid *)&p_rid);
}
Variant::Variant(const Object *p_object) {
if (p_object)
godot::api->godot_variant_new_object(&_godot_variant, p_object->_owner);
else
godot::api->godot_variant_new_nil(&_godot_variant);
}
Variant::Variant(const Dictionary &p_dictionary) {
godot::api->godot_variant_new_dictionary(&_godot_variant, (godot_dictionary *)&p_dictionary);
}
Variant::Variant(const Array &p_array) {
godot::api->godot_variant_new_array(&_godot_variant, (godot_array *)&p_array);
}
Variant::Variant(const PoolByteArray &p_raw_array) {
godot::api->godot_variant_new_pool_byte_array(&_godot_variant, (godot_pool_byte_array *)&p_raw_array);
}
Variant::Variant(const PoolIntArray &p_int_array) {
godot::api->godot_variant_new_pool_int_array(&_godot_variant, (godot_pool_int_array *)&p_int_array);
}
Variant::Variant(const PoolRealArray &p_real_array) {
godot::api->godot_variant_new_pool_real_array(&_godot_variant, (godot_pool_real_array *)&p_real_array);
}
Variant::Variant(const PoolStringArray &p_string_array) {
godot::api->godot_variant_new_pool_string_array(&_godot_variant, (godot_pool_string_array *)&p_string_array);
}
Variant::Variant(const PoolVector2Array &p_vector2_array) {
godot::api->godot_variant_new_pool_vector2_array(&_godot_variant, (godot_pool_vector2_array *)&p_vector2_array);
}
Variant::Variant(const PoolVector3Array &p_vector3_array) {
godot::api->godot_variant_new_pool_vector3_array(&_godot_variant, (godot_pool_vector3_array *)&p_vector3_array);
}
Variant::Variant(const PoolColorArray &p_color_array) {
godot::api->godot_variant_new_pool_color_array(&_godot_variant, (godot_pool_color_array *)&p_color_array);
}
Variant &Variant::operator=(const Variant &v) {
2021-12-03 10:11:32 +00:00
godot::api->godot_variant_destroy(&_godot_variant);
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_new_copy(&_godot_variant, &v._godot_variant);
return *this;
}
Variant::operator bool() const {
2017-10-03 10:37:34 +00:00
return booleanize();
}
Variant::operator signed int() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_int(&_godot_variant);
}
Variant::operator unsigned int() const // this is the real one
{
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_uint(&_godot_variant);
}
Variant::operator signed short() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_int(&_godot_variant);
}
Variant::operator unsigned short() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_uint(&_godot_variant);
}
Variant::operator signed char() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_int(&_godot_variant);
}
Variant::operator unsigned char() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_uint(&_godot_variant);
}
Variant::operator int64_t() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_int(&_godot_variant);
}
Variant::operator uint64_t() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_uint(&_godot_variant);
}
Variant::operator wchar_t() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_int(&_godot_variant);
}
Variant::operator float() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_real(&_godot_variant);
}
Variant::operator double() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_as_real(&_godot_variant);
}
Variant::operator String() const {
godot_string s = godot::api->godot_variant_as_string(&_godot_variant);
return String(s);
}
Variant::operator Vector2() const {
2017-10-20 23:42:10 +00:00
godot_vector2 s = godot::api->godot_variant_as_vector2(&_godot_variant);
return *(Vector2 *)&s;
}
Variant::operator Rect2() const {
2017-10-20 23:42:10 +00:00
godot_rect2 s = godot::api->godot_variant_as_rect2(&_godot_variant);
return *(Rect2 *)&s;
}
Variant::operator Vector3() const {
2017-10-20 23:42:10 +00:00
godot_vector3 s = godot::api->godot_variant_as_vector3(&_godot_variant);
return *(Vector3 *)&s;
}
Variant::operator Plane() const {
2017-10-20 23:42:10 +00:00
godot_plane s = godot::api->godot_variant_as_plane(&_godot_variant);
return *(Plane *)&s;
}
Variant::operator AABB() const {
godot_aabb s = godot::api->godot_variant_as_aabb(&_godot_variant);
return *(AABB *)&s;
}
Variant::operator Quat() const {
2017-10-20 23:42:10 +00:00
godot_quat s = godot::api->godot_variant_as_quat(&_godot_variant);
return *(Quat *)&s;
}
Variant::operator Basis() const {
2017-10-20 23:42:10 +00:00
godot_basis s = godot::api->godot_variant_as_basis(&_godot_variant);
return *(Basis *)&s;
}
Variant::operator Transform() const {
2017-10-20 23:42:10 +00:00
godot_transform s = godot::api->godot_variant_as_transform(&_godot_variant);
return *(Transform *)&s;
}
Variant::operator Transform2D() const {
2017-10-20 23:42:10 +00:00
godot_transform2d s = godot::api->godot_variant_as_transform2d(&_godot_variant);
return *(Transform2D *)&s;
}
Variant::operator Color() const {
2017-10-20 23:42:10 +00:00
godot_color s = godot::api->godot_variant_as_color(&_godot_variant);
return *(Color *)&s;
}
Variant::operator NodePath() const {
godot_node_path ret = godot::api->godot_variant_as_node_path(&_godot_variant);
return NodePath(ret);
}
Variant::operator RID() const {
2017-10-20 23:42:10 +00:00
godot_rid s = godot::api->godot_variant_as_rid(&_godot_variant);
return *(RID *)&s;
}
Variant::operator Dictionary() const {
Dictionary ret(godot::api->godot_variant_as_dictionary(&_godot_variant));
return ret;
}
Variant::operator Array() const {
Array ret(godot::api->godot_variant_as_array(&_godot_variant));
return ret;
}
Variant::operator PoolByteArray() const {
godot_pool_byte_array ret = godot::api->godot_variant_as_pool_byte_array(&_godot_variant);
return PoolByteArray(ret);
}
Variant::operator PoolIntArray() const {
godot_pool_int_array ret = godot::api->godot_variant_as_pool_int_array(&_godot_variant);
return PoolIntArray(ret);
}
Variant::operator PoolRealArray() const {
godot_pool_real_array ret = godot::api->godot_variant_as_pool_real_array(&_godot_variant);
return PoolRealArray(ret);
}
Variant::operator PoolStringArray() const {
godot_pool_string_array ret = godot::api->godot_variant_as_pool_string_array(&_godot_variant);
return PoolStringArray(ret);
}
Variant::operator PoolVector2Array() const {
godot_pool_vector2_array ret = godot::api->godot_variant_as_pool_vector2_array(&_godot_variant);
return PoolVector2Array(ret);
}
Variant::operator PoolVector3Array() const {
godot_pool_vector3_array ret = godot::api->godot_variant_as_pool_vector3_array(&_godot_variant);
return PoolVector3Array(ret);
}
Variant::operator PoolColorArray() const {
godot_pool_color_array ret = godot::api->godot_variant_as_pool_color_array(&_godot_variant);
return PoolColorArray(ret);
}
Variant::operator godot_object *() const {
Nativescript 1.1 implemented instance binding data usage This commit changes the way C++ wrapper classes work. Previously, wrapper classes were merely wrapper *interfaces*. They used the `this` pointer to store the actual foreign Godot Object. With the NativeScript 1.1 extension it is now possible to have low-overhead language binding data attached to Objects. The C++ bindings use that feature to implement *proper* wrappers and enable regular C++ inheritance usage that way. Some things might still be buggy and untested, but the C++ SimpleDemo works with those changes. new and free change, custom free will crash engine, be wary fix exporting of non-object types fix free() crash with custom resources added type tags and safe object casting fix global type registration order fix cast_to changed build system to be more self contained updated .gitignore use typeid() for type tags now fix indentation in bindings generator remove accidentally added files fix gitignore Fixed up registering tool and updated godot_headers Fix crash when calling String::split/split_floats Was casting to the wrong object type. Also adds parse_ints function to String with the same logic Better warning/error macros Change gitignore so we get our gen folders New documentation based on nativescript 1.1 Fixed GODOT_SUBCLASS macro Preventing crash when function returned null ptr Adds needed include <typeinfo> Solves this issue #168 due to not having the include of typeinfo Fix compile error of 'WARN_PRINT' and 'ERR_PRINT'. cannot pass non-trivial object of type 'godot::String' to variadic function; expected type from format string was 'char *' [-Wnon-pod-varargs] update vector3::distance_to Remove godot_api.json as its now in the godot_headers submodule (api.json)
2018-02-11 14:50:01 +00:00
return godot::api->godot_variant_as_object(&_godot_variant);
}
Variant::Type Variant::get_type() const {
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) {
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) {
return godot::api->godot_variant_has_method(&_godot_variant, (godot_string *)&method);
}
bool Variant::operator==(const Variant &b) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_operator_equal(&_godot_variant, &b._godot_variant);
}
bool Variant::operator!=(const Variant &b) const {
return !(*this == b);
}
bool Variant::operator<(const Variant &b) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_operator_less(&_godot_variant, &b._godot_variant);
}
bool Variant::operator<=(const Variant &b) const {
return (*this < b) || (*this == b);
}
bool Variant::operator>(const Variant &b) const {
return !(*this <= b);
}
bool Variant::operator>=(const Variant &b) const {
return !(*this < b);
}
bool Variant::hash_compare(const Variant &b) const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_hash_compare(&_godot_variant, &b._godot_variant);
}
bool Variant::booleanize() const {
2017-10-20 23:42:10 +00:00
return godot::api->godot_variant_booleanize(&_godot_variant);
}
Variant::~Variant() {
2017-10-20 23:42:10 +00:00
godot::api->godot_variant_destroy(&_godot_variant);
}
} // namespace godot