diff --git a/examples/math_vector/main.cpp b/examples/math_vector/main.cpp index 5b975e4..1755578 100644 --- a/examples/math_vector/main.cpp +++ b/examples/math_vector/main.cpp @@ -55,26 +55,27 @@ vec3.z = vec1.x + vec2.y / 4.0 System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>") )script"; - class MathVector { + template + class Vector { public: - MathVector() = default; - explicit MathVector (double value) : + Vector() = default; + explicit Vector (T value) : m_x(value), m_y(value), m_z(value) {} - MathVector (double x, double y, double z) : + Vector (T x, T y, T z) : m_x(x), m_y(y), m_z(z) {} - ~MathVector() noexcept = default; + ~Vector() noexcept = default; - double x() const { return m_x; } - double y() const { return m_y; } - double z() const { return m_z; } - void set_x(double x) { m_x = x; } - void set_y(double y) { m_y = y; } - void set_z(double z) { m_z = z; } + T x() const { return m_x; } + T y() const { return m_y; } + T z() const { return m_z; } + void set_x(T x) { m_x = x; } + void set_y(T y) { m_y = y; } + void set_z(T z) { m_z = z; } private: - double m_x{}, m_y{}, m_z{}; + T m_x{}, m_y{}, m_z{}; }; class MyConf : public wren::DefConfiguration { @@ -82,13 +83,17 @@ System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>") 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() + 1; - char* const buff = static_cast(MyConf::reallocate_fn(nullptr, buff_sz)); + char* const buff = new char[buff_sz]; std::copy(g_math_vector_src.cbegin(), g_math_vector_src.cend(), buff); buff[buff_sz - 1] = '\0'; return buff; } return nullptr; } + + void load_module_complete_fn(wren::VM*, std::string_view, char* buffer) { + delete[] buffer; + } }; } //unnamed namespace @@ -99,15 +104,15 @@ int main() { MyConf conf; wren::VM vm(&conf, nullptr); vm.callback_manager() - .add_callback(false, "math_vector", "MathVector", "x=(_)", make_method_bindable<&MathVector::set_x>()) - .add_callback(false, "math_vector", "MathVector", "y=(_)", make_method_bindable<&MathVector::set_y>()) - .add_callback(false, "math_vector", "MathVector", "z=(_)", make_method_bindable<&MathVector::set_z>()) - .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>()); + .add_callback(false, "math_vector", "MathVector", "x=(_)", make_method_bindable<&Vector::set_x>()) + .add_callback(false, "math_vector", "MathVector", "y=(_)", make_method_bindable<&Vector::set_y>()) + .add_callback(false, "math_vector", "MathVector", "z=(_)", make_method_bindable<&Vector::set_z>()) + .add_callback(false, "math_vector", "MathVector", "x", make_method_bindable<&Vector::x>()) + .add_callback(false, "math_vector", "MathVector", "y", make_method_bindable<&Vector::y>()) + .add_callback(false, "math_vector", "MathVector", "z", make_method_bindable<&Vector::z>()); vm.class_manager() - .add_class_maker("math_vector", "MathVector", &make_foreign_class, double, //single value constructor std::tuple, //x,y,z constructor std::tuple<> //default constructor