From b8066a3f34be7711aa3cff8d1963e5ffced5b269 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 15 May 2022 16:03:29 +0200 Subject: [PATCH] Add working example of constructor overloading --- examples/math_vector/main.cpp | 97 +++++++++++++++++++ examples/math_vector/meson.build | 6 ++ examples/meson.build | 1 + .../wrenpp/detail/construct_foreign_class.hpp | 6 +- include/wrenpp/meson.build | 18 ++-- 5 files changed, 118 insertions(+), 10 deletions(-) create mode 100644 examples/math_vector/main.cpp create mode 100644 examples/math_vector/meson.build diff --git a/examples/math_vector/main.cpp b/examples/math_vector/main.cpp new file mode 100644 index 0000000..0cdd0e1 --- /dev/null +++ b/examples/math_vector/main.cpp @@ -0,0 +1,97 @@ +/* Copyright 2020-2022, Michele Santullo + * This file is part of wrenpp. + * + * Wrenpp is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Wrenpp is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with wrenpp. If not, see . + */ + +#include "wrenpp/def_configuration.hpp" +#include "wrenpp/vm_fun.hpp" +#include "wrenpp/callback_manager.hpp" +#include "wrenpp/class_manager.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace { + constexpr std::string_view g_math_vector_src{ +"foreign class MathVector {" R"script( + construct new(value) {} + construct new(x, y, z) {} + + foreign x + foreign y + foreign z +} +)script"}; + + constexpr const char g_test_script[] = +R"script(import "math_vector" for MathVector +var vec1 = MathVector.new(1.0) +var vec2 = MathVector.new(10.0, 20.0, 30.0) +System.print("vec1: <%(vec1.x), %(vec1.y), %(vec1.z)>") +System.print("vec2: <%(vec2.x), %(vec2.y), %(vec2.z)>") +)script"; + + class MathVector { + public: + MathVector() = default; + explicit MathVector (double value) : + m_x(value), m_y(value), m_z(value) + {} + MathVector (double x, double y, double z) : + m_x(x), m_y(y), m_z(z) + {} + ~MathVector() noexcept = default; + + double x() const { return m_x; } + double y() const { return m_y; } + double z() const { return m_z; } + + private: + double m_x, m_y, m_z; + }; + + class MyConf : public wren::DefConfiguration { + public: + char* load_module_fn(wren::VM* vm, std::string_view module_name) { + if (module_name == "math_vector") { + constexpr const std::size_t buff_sz = g_math_vector_src.size(); + char* const buff = static_cast(MyConf::reallocate_fn(nullptr, buff_sz)); + std::copy(g_math_vector_src.cbegin(), g_math_vector_src.cend(), buff); + return buff; + } + return nullptr; + } + }; +} //unnamed namespace + +int main() { + using wren::make_method_bindable; + + MyConf conf; + wren::VM vm(&conf, nullptr); + vm.callback_manager() + .add_callback(false, "math_vector", "MathVector", "x", make_method_bindable<&MathVector::x>()) + .add_callback(false, "math_vector", "MathVector", "y", make_method_bindable<&MathVector::y>()) + .add_callback(false, "math_vector", "MathVector", "z", make_method_bindable<&MathVector::z>()); + vm.class_manager().add_class_maker("math_vector", "MathVector", &wren::make_overloaded_foreign_class>); + + vm.interpret("main", g_test_script); + + return 0; +} diff --git a/examples/math_vector/meson.build b/examples/math_vector/meson.build new file mode 100644 index 0000000..a7e05b8 --- /dev/null +++ b/examples/math_vector/meson.build @@ -0,0 +1,6 @@ +executable('math_vector', + 'main.cpp', + dependencies: wrenpp_dep, + install: false, +) + diff --git a/examples/meson.build b/examples/meson.build index 46175a9..25b06ea 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,3 +1,4 @@ subdir('dieroll') subdir('greet') subdir('calendar') +subdir('math_vector') diff --git a/include/wrenpp/detail/construct_foreign_class.hpp b/include/wrenpp/detail/construct_foreign_class.hpp index cffc504..3c547eb 100644 --- a/include/wrenpp/detail/construct_foreign_class.hpp +++ b/include/wrenpp/detail/construct_foreign_class.hpp @@ -25,7 +25,7 @@ namespace wren { namespace detail { - template + template inline void construct_single ( std::integer_sequence, VM& vm, @@ -47,7 +47,7 @@ namespace wren { //number of types we invoke construct_single() with those types. constexpr int pack_size = static_cast(sizeof...(PackArgs)); if (pack_size == cardinality) { - construct_single(make_integer_sequence{}, vm, memory); + construct_single(make_integer_sequence{}, vm, memory); return true; } else { @@ -62,7 +62,7 @@ namespace wren { using std::make_integer_sequence; if (1 == cardinality) { - construct_single(make_integer_sequence{}, vm, memory); + construct_single(make_integer_sequence{}, vm, memory); return true; } else { diff --git a/include/wrenpp/meson.build b/include/wrenpp/meson.build index d4b441d..5e2e6e3 100644 --- a/include/wrenpp/meson.build +++ b/include/wrenpp/meson.build @@ -1,16 +1,20 @@ include_files = [ + 'callback_manager.hpp', + 'class_manager.hpp', 'configuration.hpp', 'def_configuration.hpp', - 'error_type.hpp', - 'guess_class_name.hpp', 'handle.hpp', - 'has_method.hpp', - 'meson.build', - 'string_bt.hpp', - 'StringCRC32.hpp', 'vm_fun.hpp', 'vm.hpp', - 'wren_types.hpp', + 'detail/construct_foreign_class.hpp', + 'detail/error_type.hpp', + 'detail/guess_class_name.hpp', + 'detail/has_method.hpp', + 'detail/seters_getters.hpp', + 'detail/string_bt.hpp', + 'detail/StringCRC32.hpp', + 'detail/strings_in_vector.hpp', + 'detail/wren_types.hpp', ] if not meson.is_subproject()