-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from Spartan322/improve-binding
- Loading branch information
Showing
8 changed files
with
317 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include "Checksum.hpp" | ||
|
||
#include <godot_cpp/core/error_macros.hpp> | ||
#include <godot_cpp/variant/string.hpp> | ||
|
||
#include "openvic-extension/utility/ClassBindings.hpp" | ||
|
||
using namespace OpenVic; | ||
using namespace godot; | ||
|
||
void Checksum::_bind_methods() { | ||
OV_BIND_METHOD(Checksum::get_checksum_text); | ||
} | ||
|
||
Checksum* Checksum::get_singleton() { | ||
return _checksum; | ||
} | ||
|
||
Checksum::Checksum() { | ||
ERR_FAIL_COND(_checksum != nullptr); | ||
_checksum = this; | ||
} | ||
|
||
Checksum::~Checksum() { | ||
ERR_FAIL_COND(_checksum != this); | ||
_checksum = nullptr; | ||
} | ||
|
||
/* REQUIREMENTS: | ||
* DAT-8 | ||
*/ | ||
godot::String Checksum::get_checksum_text() { | ||
return godot::String("1234abcd"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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...); | ||
} | ||
} |
Oops, something went wrong.