Fix build when std::string is a lone parameter

The "any tuple-like" thing was nice but
it was confusing when passing for example
a std::string, which is a typedef to a
tuple-like thing. Making a custom
ConstructorParameters struct is more clear
both to users and to the compiler.
This commit is contained in:
King_DuckZ 2024-05-24 17:12:04 +02:00
parent ea19e35f85
commit 72da0b8d87
2 changed files with 8 additions and 6 deletions

View file

@ -108,6 +108,7 @@ System.print("vec_base_x modified by scripting: <%(vec_base_x.x), %(vec_base_x.y
int main() {
using wren::make_function_bindable;
using wren::make_foreign_class;
using wren::ConstructorParameters;
MyConf conf;
wren::VM vm(&conf, nullptr);
@ -123,8 +124,8 @@ int main() {
vm.class_manager()
.add_class_maker("math_vector", "MathVector", make_foreign_class<Vector<double>,
double, //single value constructor
std::tuple<double, double, double>, //x,y,z constructor
std::tuple<> //default constructor
ConstructorParameters<double, double, double>, //x,y,z constructor
ConstructorParameters<> //default constructor
>);
vm.interpret("main", g_test_script);

View file

@ -23,11 +23,12 @@
# include "guess_class_name.hpp"
#endif
#include <utility>
#include <tuple>
#include <cassert>
#include <cstdint>
namespace wren {
template <typename... Args> struct ConstructorParameters { };
namespace detail {
template <typename T, typename... Args, int... Indices>
inline void construct_single (
@ -46,13 +47,13 @@ namespace wren {
template <typename T, typename... Args>
struct ForeignClassHelper;
template <typename T, template <typename...> class Pack, typename... PackArgs, typename... Args>
struct ForeignClassHelper<T, Pack<PackArgs...>, Args...> {
template <typename T, typename... PackArgs, typename... Args>
struct ForeignClassHelper<T, ConstructorParameters<PackArgs...>, Args...> {
static ConstructResult construct (VM& vm, int cardinality, void* memory) {
using std::make_integer_sequence;
//search by number of parameters.
//once we find a tuple-like parameter that contains the right
//once we find a ConstructorParameters that contains the right
//number of types we invoke construct_single() with those types.
constexpr int pack_size = static_cast<int>(sizeof...(PackArgs));
if (pack_size == cardinality) {