diff --git a/include/godot_cpp/core/method_ptrcall.hpp b/include/godot_cpp/core/method_ptrcall.hpp index 85d77833a..91eb2b1b8 100644 --- a/include/godot_cpp/core/method_ptrcall.hpp +++ b/include/godot_cpp/core/method_ptrcall.hpp @@ -113,6 +113,62 @@ struct PtrToArg {}; } \ } +#define MAKE_PTRARG_CONVERTER(m_type, m_conv, m_to, m_from) \ + template <> \ + struct PtrToArg { \ + _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ + return m_to(*reinterpret_cast(p_ptr)); \ + } \ + typedef m_conv EncodeT; \ + _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \ + *reinterpret_cast(p_ptr) = m_from(p_val); \ + } \ + _FORCE_INLINE_ static m_conv encode_arg(m_type p_val) { \ + return m_from(p_val); \ + } \ + }; \ + template <> \ + struct PtrToArg { \ + _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ + return m_to(*reinterpret_cast(p_ptr)); \ + } \ + typedef m_conv EncodeT; \ + _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \ + *reinterpret_cast(p_ptr) = m_from(p_val); \ + } \ + _FORCE_INLINE_ static m_conv encode_arg(m_type p_val) { \ + return m_from(p_val); \ + } \ + } + +#define MAKE_PTRARG_CONVERTER_BY_REFERENCE(m_type, m_conv, m_to, m_from) \ + template <> \ + struct PtrToArg { \ + _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ + return m_to(*reinterpret_cast(p_ptr)); \ + } \ + typedef m_conv EncodeT; \ + _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \ + *reinterpret_cast(p_ptr) = m_from(p_val); \ + } \ + _FORCE_INLINE_ static m_conv encode_arg(const m_type &p_val) { \ + return m_from(p_val); \ + } \ + }; \ + template <> \ + struct PtrToArg { \ + _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \ + return m_to(*reinterpret_cast(p_ptr)); \ + } \ + typedef m_conv EncodeT; \ + _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \ + *reinterpret_cast(p_ptr) = m_from(p_val); \ + } \ + _FORCE_INLINE_ static m_conv encode_arg(const m_type &p_val) { \ + return m_from(p_val); \ + } \ + } + MAKE_PTRARGCONV(bool, uint8_t); // Integer types. MAKE_PTRARGCONV(uint8_t, int64_t); diff --git a/include/godot_cpp/variant/converter.hpp b/include/godot_cpp/variant/converter.hpp new file mode 100644 index 000000000..0aa5ca973 --- /dev/null +++ b/include/godot_cpp/variant/converter.hpp @@ -0,0 +1,80 @@ +/**************************************************************************/ +/* converter.hpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* 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. */ +/**************************************************************************/ + +#pragma once + +#include +#include +#include +#include + +#include + +#define GD_REGISTER_CONVERSION(m_type, m_conv, m_to, m_from, m_var_type) \ + namespace godot { \ + template <> \ + struct VariantCaster { \ + _FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \ + return m_to(p_variant); \ + } \ + template <> \ + struct VariantCaster { \ + _FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \ + return m_to(p_variant); \ + } \ + template <> \ + struct VariantCaster { \ + _FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \ + return m_to(p_variant); \ + } \ + MAKE_TYPE_INFO(m_type, m_var_type) \ + MAKE_PTRARG_CONVERTER(m_type, m_conv, m_to, m_from); \ + } + +#define GD_REGISTER_CONVERSION_BY_REFERENCE(m_type, m_conv, m_to, m_from, m_var_type) \ + namespace godot { \ + template <> \ + struct VariantCaster { \ + _FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \ + return m_to(p_variant); \ + } \ + template <> \ + struct VariantCaster { \ + _FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \ + return m_to(p_variant); \ + } \ + template <> \ + struct VariantCaster { \ + _FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \ + return m_to(p_variant); \ + } \ + MAKE_TYPE_INFO(m_type, m_var_type) \ + MAKE_PTRARG_CONVERTER_BY_REFERENCE(m_type, m_conv, m_to, m_from); \ + } diff --git a/include/godot_cpp/variant/variant.hpp b/include/godot_cpp/variant/variant.hpp index fe8e130a1..4981ad81c 100644 --- a/include/godot_cpp/variant/variant.hpp +++ b/include/godot_cpp/variant/variant.hpp @@ -215,6 +215,9 @@ class Variant { Variant(const PackedVector3Array &v); Variant(const PackedColorArray &v); Variant(const PackedVector4Array &v); + template ::encode_arg(std::declval()))>> + Variant(T v) : + Variant(PtrToArg::encode_arg(v)) {} ~Variant(); operator bool() const;