From cf591eddfa59839c2620ebf119727f069b965dfe Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Thu, 9 Nov 2023 23:14:58 -0500 Subject: Add ClassBinding helpers OV_BIND_METHOD OV_BIND_SMETHOD OV_BIND_SMETHOD_L Change _bind_methods to use ClassBinding helpers Add utility/StringLiteral Make `GameSingleton::singleton` inline Move `OpenVic::Checksum` implementation to source file Update scripts to ce1aef8d7d9d5ba9851a1abdd981d3b796024079 --- .../openvic-extension/utility/ClassBindings.hpp | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 extension/src/openvic-extension/utility/ClassBindings.hpp (limited to 'extension/src/openvic-extension/utility/ClassBindings.hpp') diff --git a/extension/src/openvic-extension/utility/ClassBindings.hpp b/extension/src/openvic-extension/utility/ClassBindings.hpp new file mode 100644 index 0000000..616250b --- /dev/null +++ b/extension/src/openvic-extension/utility/ClassBindings.hpp @@ -0,0 +1,90 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +#include "openvic-extension/utility/StringLiteral.hpp" + +namespace godot { + class Object; +} + +#define OV_BIND_METHOD(Function, ...) \ + ::OpenVic::detail::bind_method<::OpenVic::detail::get_function_name<#Function>()>(&Function __VA_OPT__(, ) __VA_ARGS__) + +#define OV_BIND_SMETHOD(Function, ...) \ + ::OpenVic::detail::bind_static_method<::OpenVic::detail::get_function_name<#Function>()>( \ + get_class_static(), &Function __VA_OPT__(, ) __VA_ARGS__ \ + ) + +#define OV_BIND_SMETHOD_T(ClassType, Function, ...) \ + ::OpenVic::detail::bind_static_method()>( \ + &Function __VA_OPT__(, ) __VA_ARGS__ \ + ) + +namespace OpenVic::detail { + template + concept IsFunctionPointer = std::is_function_v>; + template + concept IsMemberFunctionPointer = std::is_member_function_pointer_v; + + template + consteval auto get_function_name() { + constexpr auto result = [] { + constexpr auto prefix = std::string_view { "::" }; + + constexpr std::string_view in_sv = In; + constexpr auto start = in_sv.find_last_of(prefix); + + if constexpr (start == std::string_view::npos) { + return In; + } else { + constexpr auto result = in_sv.substr(start + 1); + return StringLiteral { result }; + } + }(); + + return result; + } + + template + void bind_method(Func func, std::initializer_list arg_names, DefaultsT&&... defaults) { + godot::MethodDefinition definition { Name.data() }; + definition.args = { arg_names }; + godot::ClassDB::bind_method(definition, func, defaults...); + } + + template + void bind_method(Func func, DefaultsT&&... defaults) { + bind_method(func, {}, defaults...); + } + + template + void bind_static_method( + godot::StringName class_name, Func func, std::initializer_list arg_names, DefaultsT&&... defaults + ) { + godot::MethodDefinition definition { Name.data() }; + definition.args = { arg_names }; + godot::ClassDB::bind_static_method(class_name, definition, func, defaults...); + } + + template + void bind_static_method(godot::StringName class_name, Func func, DefaultsT&&... defaults) { + bind_static_method(class_name, func, {}, defaults...); + } + + template ClassT, StringLiteral Name, IsFunctionPointer Func, typename... DefaultsT> + void bind_static_method(Func func, std::initializer_list arg_names, DefaultsT&&... defaults) { + bind_static_method(ClassT::get_class_static(), func, arg_names, defaults...); + } + + template ClassT, StringLiteral Name, IsFunctionPointer Func, typename... DefaultsT> + void bind_static_method(Func func, DefaultsT&&... defaults) { + bind_static_method(func, {}, defaults...); + } +} -- cgit v1.2.3-56-ga3b1