From c2089568f3f12979800a01b1894da080dd346698 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 29 Apr 2022 18:35:46 +0200 Subject: [PATCH] Bugfixes and improvements, simplify examples --- examples/calendar/main.cpp | 23 ++++-------------- examples/dieroll/main.cpp | 36 ++++------------------------- include/wrenpp/callback_manager.hpp | 2 +- src/callback_manager.cpp | 10 +++++--- 4 files changed, 18 insertions(+), 53 deletions(-) diff --git a/examples/calendar/main.cpp b/examples/calendar/main.cpp index 64b9b77..6fea8f8 100644 --- a/examples/calendar/main.cpp +++ b/examples/calendar/main.cpp @@ -17,6 +17,7 @@ #include "wrenpp/def_configuration.hpp" #include "wrenpp/vm_fun.hpp" +#include "wrenpp/callback_manager.hpp" #include #include #include @@ -107,24 +108,6 @@ System.print("You have %(cale.appointment_count()) appointment(s)") return nullptr; } - wren::foreign_method_t foreign_method_fn( - wren::VM* vm, - std::string_view module, - std::string_view class_name, - bool is_static, - std::string_view signature - ) { - if (module == "calendar" and class_name == "Calendar") { - if (is_static and signature == "today()") - return &Calendar::today; - else if (not is_static and signature == "add_appointment(_)") - return wren::make_method_bindable<&Calendar::add_appointment>(); - else if (not is_static and signature == "appointment_count()") - return wren::make_method_bindable<&Calendar::appointment_count>(); - } - return nullptr; - } - wren::foreign_class_t foreign_class_fn( wren::VM* vm, std::string_view module, @@ -145,6 +128,10 @@ int main() { MyConf conf; wren::VM vm(&conf, nullptr); + vm.callback_manager().add_callback(true, "calendar", "Calendar", "today()", &Calendar::today) + .add_callback(false, "calendar", "Calendar", "add_appointment(_)", wren::make_method_bindable<&Calendar::add_appointment>()) + .add_callback(false, "calendar", "Calendar", "appointment_count()", wren::make_method_bindable<&Calendar::appointment_count>()); + vm.interpret("main", g_test_script); Calendar* const cale = std::get<0>(wren::variables(vm, MN{"main", "cale"})); diff --git a/examples/dieroll/main.cpp b/examples/dieroll/main.cpp index e80975a..030f226 100644 --- a/examples/dieroll/main.cpp +++ b/examples/dieroll/main.cpp @@ -17,6 +17,7 @@ #include "wrenpp/vm_fun.hpp" #include "wrenpp/def_configuration.hpp" +#include "wrenpp/callback_manager.hpp" #include #include #include @@ -47,36 +48,6 @@ namespace { set(vm, 0, cust_data->distrib(cust_data->generator)); } - class MyConfig : public wren::DefConfiguration { - public: - wren::foreign_method_t foreign_method_fn ( - wren::VM* vm, - std::string_view module, - std::string_view class_name, - bool is_static, - std::string_view signature - ) { - //std::cout << "requested: \"" << module << "\" \"" << - // class_name << "\" \"" << signature << "\" static: " << - // std::boolalpha << is_static << '\n'; - - if ("main" == module) { - if ("Game" == class_name) { - if ("user_input()" == signature and is_static) - return &user_input; - else if ("random()" == signature and is_static) - return &random_num; - } - } - return nullptr; - } - - //char* load_module_fn (wren::VM* vm, const char* name) { - // std::cout << "asked to load module \"" << name << "\"\n"; - // return nullptr; - //} - }; - std::string load_file (const std::string& path) { std::ifstream ifs(path); ifs >> std::noskipws; @@ -92,8 +63,11 @@ int main() { std::random_device rand_dev; CustomData custom_data(rand_dev()); - MyConfig config; + wren::DefConfiguration config; wren::VM vm(&config, &custom_data); + vm.callback_manager().add_callback(true, "main", "Game", "user_input()", &user_input) + .add_callback(true, "main", "Game", "random()", &random_num); + interpret(vm, "main", load_file("main.wren")); wren::call(vm, {"main", "the_game"}, "play_game"); diff --git a/include/wrenpp/callback_manager.hpp b/include/wrenpp/callback_manager.hpp index 450c9d5..073d86b 100644 --- a/include/wrenpp/callback_manager.hpp +++ b/include/wrenpp/callback_manager.hpp @@ -116,7 +116,7 @@ namespace wren { CallbackManager(); ~CallbackManager() noexcept; - void add_callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature, foreign_method_t cb); + CallbackManager& add_callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature, foreign_method_t cb); foreign_method_t callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const; private: diff --git a/src/callback_manager.cpp b/src/callback_manager.cpp index b63a1fe..5b5babc 100644 --- a/src/callback_manager.cpp +++ b/src/callback_manager.cpp @@ -60,7 +60,8 @@ namespace wren::detail { TempFullSignature::TempFullSignature (const FullSignatureOwning& full_sig) : FullSignatureBase(full_sig), m_module_name(full_sig.module_name()), - m_class_name(full_sig.class_name()) + m_class_name(full_sig.class_name()), + m_signature(full_sig.signature()) { } TempFullSignature::TempFullSignature (std::string_view module_name, std::string_view class_name, std::string_view signature) : @@ -72,7 +73,8 @@ namespace wren::detail { bool FullSignatureEqual::operator()(TempFullSignature left, TempFullSignature right) const { return left.class_name() == right.class_name() and - left.module_name() == right.module_name(); + left.module_name() == right.module_name() and + left.signature() == right.signature(); } std::size_t FullSignatureHash::operator() (TempFullSignature key) const { @@ -88,7 +90,7 @@ namespace wren { CallbackManager::CallbackManager() = default; CallbackManager::~CallbackManager() noexcept = default; - void CallbackManager::add_callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature, foreign_method_t cb) { + CallbackManager& CallbackManager::add_callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature, foreign_method_t cb) { using detail::FullSignatureOwning; using detail::TempFullSignature; @@ -98,6 +100,8 @@ namespace wren { map.insert(it_found, std::make_pair(FullSignatureOwning{module_name, class_name, signature}, cb)); else it_found->second = cb; + + return *this; } foreign_method_t CallbackManager::callback (bool is_static, std::string_view module_name, std::string_view class_name, std::string_view signature) const {