diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-11-10 23:39:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-10 23:39:21 +0100 |
commit | 0008bd6bf3d00cc284199be668c0b03d9f2c5d77 (patch) | |
tree | 65ca78284e4ed1a2b07716018a721aaaf68d3830 /extension/src/openvic-extension/utility | |
parent | f8da0860795d273452501fa4d7fbfcc40073a884 (diff) | |
parent | cf591eddfa59839c2620ebf119727f069b965dfe (diff) |
Merge pull request #163 from Spartan322/improve-binding
Diffstat (limited to 'extension/src/openvic-extension/utility')
-rw-r--r-- | extension/src/openvic-extension/utility/ClassBindings.hpp | 90 | ||||
-rw-r--r-- | extension/src/openvic-extension/utility/StringLiteral.hpp | 106 |
2 files changed, 196 insertions, 0 deletions
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 <concepts> +#include <initializer_list> +#include <string_view> +#include <type_traits> + +#include <godot_cpp/core/class_db.hpp> +#include <godot_cpp/variant/string_name.hpp> + +#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<ClassType, ::OpenVic::detail::get_function_name<#Function>()>( \ + &Function __VA_OPT__(, ) __VA_ARGS__ \ + ) + +namespace OpenVic::detail { + template<typename Func> + concept IsFunctionPointer = std::is_function_v<std::remove_pointer_t<Func>>; + template<typename Func> + concept IsMemberFunctionPointer = std::is_member_function_pointer_v<Func>; + + template<StringLiteral In> + 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.size()> { result }; + } + }(); + + return result; + } + + template<StringLiteral Name, IsMemberFunctionPointer Func, typename... DefaultsT> + void bind_method(Func func, std::initializer_list<godot::StringName> arg_names, DefaultsT&&... defaults) { + godot::MethodDefinition definition { Name.data() }; + definition.args = { arg_names }; + godot::ClassDB::bind_method(definition, func, defaults...); + } + + template<StringLiteral Name, IsMemberFunctionPointer Func, typename... DefaultsT> + void bind_method(Func func, DefaultsT&&... defaults) { + bind_method<Name, Func>(func, {}, defaults...); + } + + template<StringLiteral Name, IsFunctionPointer Func, typename... DefaultsT> + void bind_static_method( + godot::StringName class_name, Func func, std::initializer_list<godot::StringName> arg_names, DefaultsT&&... defaults + ) { + godot::MethodDefinition definition { Name.data() }; + definition.args = { arg_names }; + godot::ClassDB::bind_static_method(class_name, definition, func, defaults...); + } + + template<StringLiteral Name, IsFunctionPointer Func, typename... DefaultsT> + void bind_static_method(godot::StringName class_name, Func func, DefaultsT&&... defaults) { + bind_static_method<Name>(class_name, func, {}, defaults...); + } + + template<std::derived_from<godot::Object> ClassT, StringLiteral Name, IsFunctionPointer Func, typename... DefaultsT> + void bind_static_method(Func func, std::initializer_list<godot::StringName> arg_names, DefaultsT&&... defaults) { + bind_static_method<Name>(ClassT::get_class_static(), func, arg_names, defaults...); + } + + template<std::derived_from<godot::Object> ClassT, StringLiteral Name, IsFunctionPointer Func, typename... DefaultsT> + void bind_static_method(Func func, DefaultsT&&... defaults) { + bind_static_method<ClassT, Name>(func, {}, defaults...); + } +} diff --git a/extension/src/openvic-extension/utility/StringLiteral.hpp b/extension/src/openvic-extension/utility/StringLiteral.hpp new file mode 100644 index 0000000..90b18cd --- /dev/null +++ b/extension/src/openvic-extension/utility/StringLiteral.hpp @@ -0,0 +1,106 @@ +#pragma once + +#include <algorithm> +#include <cassert> +#include <cstddef> +#include <iterator> +#include <string_view> +#include <type_traits> + +namespace OpenVic { + template<std::size_t N> + struct StringLiteral { + constexpr StringLiteral(const char (&str)[N]) { + std::copy_n(str, N, value); + } + + consteval StringLiteral(std::string_view string) { + assert(string.size() == N); + std::copy_n(string.begin(), N, value); + } + + char value[N]; + static const constexpr std::integral_constant<std::size_t, N - 1> size {}; + + struct iterator { + using iterator_concept [[maybe_unused]] = std::contiguous_iterator_tag; + using difference_type = std::ptrdiff_t; + using element_type = const char; // element_type is a reserved name that must be used in the definition + using pointer = element_type*; + using reference = element_type&; + + constexpr iterator() = default; + constexpr iterator(pointer p) : _ptr(p) {} + constexpr reference operator*() const { + return *_ptr; + } + constexpr pointer operator->() const { + return _ptr; + } + constexpr auto& operator++() { + _ptr++; + return *this; + } + constexpr auto operator++(int) { + auto tmp = *this; + ++(*this); + return tmp; + } + constexpr iterator& operator+=(int i) { + _ptr += i; + return *this; + } + constexpr iterator operator+(const difference_type other) const { + return _ptr + other; + } + constexpr friend iterator operator+(const difference_type value, const iterator& other) { + return other + value; + } + constexpr iterator& operator--() { + _ptr--; + return *this; + } + constexpr iterator operator--(int) { + iterator tmp = *this; + --(*this); + return tmp; + } + constexpr iterator& operator-=(int i) { + _ptr -= i; + return *this; + } + constexpr difference_type operator-(const iterator& other) const { + return _ptr - other._ptr; + } + constexpr iterator operator-(const difference_type other) const { + return _ptr - other; + } + friend iterator operator-(const difference_type value, const iterator& other) { + return other - value; + } + constexpr reference operator[](difference_type idx) const { + return _ptr[idx]; + } + constexpr auto operator<=>(const iterator&) const = default; // three-way comparison C++20 + + private: + pointer _ptr; + }; + + constexpr iterator begin() const { + return iterator { &value }; + } + + constexpr iterator end() const { + return iterator { &value + N }; + } + + constexpr operator std::string_view() const { + return std::string_view { value, N }; + } + + constexpr decltype(auto) data() const { + return static_cast<std::string_view>(*this).data(); + } + }; +} |