Add call() overload that takes a string literal as wren's function name.

Only the function name needs to be passed in and not the full
signature. The implementation will append the appropriate
(_,_,...) part making it at build time.
This commit is contained in:
King_DuckZ 2020-04-30 21:18:29 +02:00
parent 4f54739ab6
commit b1c1ae40f5
2 changed files with 19 additions and 1 deletions

View file

@ -36,7 +36,7 @@ int main() {
auto vars = variables<std::string, int>(vm, MN{"my_module", "name"}, MN{"my_module","number"});
std::cout << "name = \"" << std::get<0>(vars) << "\", number = " << std::get<1>(vars) << '\n';
const int sum = wren::call<int>(vm, {"my_module", "Math"}, vm.make_call_handle("sum_params(_,_)"), MN{"my_module", "number"}, 90);
const int sum = wren::call<int>(vm, {"my_module", "Math"}, "sum_params", MN{"my_module", "number"}, 90);
std::cout << "wren method returned " << sum << " (expected " << std::get<1>(vars) + 90 << ")\n";
return 0;

View file

@ -1,6 +1,7 @@
#pragma once
#include "vm.hpp"
#include "string_bt.hpp"
#include <string>
#include <string_view>
#include <vector>
@ -32,6 +33,9 @@ namespace wren {
template <typename R, typename... Args>
R call (VM& vm, const ModuleAndName& object, const Handle& method, const Args&... args);
template <typename R, std::size_t N, typename... Args>
R call (VM& vm, const ModuleAndName& object, const char (&method)[N], const Args&... args);
void interpret (VM& vm, const std::string& module_name, const std::string& script);
void set (VM& vm, int slot_num, const char* value);
void set (VM& vm, int slot_num, double value);
@ -159,4 +163,18 @@ namespace wren {
Handle obj_handle = vm.slot_handle(0);
return call<R, Args...>(vm, obj_handle, method, args...);
}
template <typename R, std::size_t N, typename... Args>
inline R call (VM& vm, const ModuleAndName& object, const char (&method)[N], const Args&... args) {
const constexpr char dummy_name_buff[N] = {0};
const constexpr auto params = dhandy::bt::string<N, char>(dummy_name_buff) +
dhandy::bt::make_string("(") +
((static_cast<void>(args), dhandy::bt::make_string(",_")) + ...).template substr<1>() +
dhandy::bt::make_string(")");
;
char cat_buff[params.size() + 1];
std::copy(method, method + N - 1, cat_buff);
std::copy(params.data() + N - 1, params.data() + params.size() + 1, cat_buff + N - 1);
return call<R, Args...>(vm, object, vm.make_call_handle(cat_buff), args...);
}
} //namespace wren