diff --git a/include/chaiscript/chaiscript_stdlib.hpp b/include/chaiscript/chaiscript_stdlib.hpp index ca0214a..50aa087 100644 --- a/include/chaiscript/chaiscript_stdlib.hpp +++ b/include/chaiscript/chaiscript_stdlib.hpp @@ -40,19 +40,20 @@ namespace chaiscript { using namespace bootstrap; - ModulePtr lib = Bootstrap::bootstrap(); + auto lib = std::make_shared(); + Bootstrap::bootstrap(*lib); - lib->add(standard_library::vector_type >("Vector")); - lib->add(standard_library::string_type("string")); - lib->add(standard_library::map_type >("Map")); - lib->add(standard_library::pair_type >("Pair")); + standard_library::vector_type >("Vector", *lib); + standard_library::string_type("string", *lib); + standard_library::map_type >("Map", *lib); + standard_library::pair_type >("Pair", *lib); #ifndef CHAISCRIPT_NO_THREADS - lib->add(standard_library::future_type>("future")); + standard_library::future_type>("future", *lib); lib->add(chaiscript::fun([](const std::function &t_func){ return std::async(std::launch::async, t_func);}), "async"); #endif - lib->add(json_wrap::library()); + json_wrap::library(*lib); lib->eval(ChaiScript_Prelude::chaiscript_prelude() /*, "standard prelude"*/ ); diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 1f519dc..4bee74d 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -52,12 +52,12 @@ namespace chaiscript } template::value>::type > - ModulePtr array(const std::string &type, ModulePtr m = std::make_shared()) + void array(const std::string &type, Module& m) { typedef typename std::remove_extent::type ReturnType; const auto extent = std::extent::value; - m->add(user_type(), type); - m->add(fun( + m.add(user_type(), type); + m.add(fun( [extent](T& t, size_t index)->ReturnType &{ if (extent > 0 && index >= extent) { throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); @@ -68,7 +68,7 @@ namespace chaiscript ), "[]" ); - m->add(fun( + m.add(fun( [extent](const T &t, size_t index)->const ReturnType &{ if (extent > 0 && index >= extent) { throw std::range_error("Array index out of range. Received: " + std::to_string(index) + " expected < " + std::to_string(extent)); @@ -79,33 +79,29 @@ namespace chaiscript ), "[]" ); - m->add(fun( + m.add(fun( [extent](const T &) { return extent; }), "size"); - - - return m; } /// \brief Adds a copy constructor for the given type to the given Model /// \param[in] type The name of the type. The copy constructor will be named "type". /// \param[in,out] m The Module to add the copy constructor to /// \tparam T The type to add a copy constructor for - /// \returns The passed in ModulePtr, or the newly constructed one if the default param is used + /// \returns The passed in Module template - ModulePtr copy_constructor(const std::string &type, ModulePtr m = std::make_shared()) + void copy_constructor(const std::string &type, Module& m) { - m->add(constructor(), type); - return m; + m.add(constructor(), type); } /// \brief Add all comparison operators for the templated type. Used during bootstrap, also available to users. /// \tparam T Type to create comparison operators for /// \param[in,out] m module to add comparison operators to - /// \returns the passed in ModulePtr or the newly constructed one if the default params are used. + /// \returns the passed in Module. template - ModulePtr opers_comparison(ModulePtr m = std::make_shared()) + void opers_comparison(Module& m) { operators::equal(m); operators::greater_than(m); @@ -113,7 +109,6 @@ namespace chaiscript operators::less_than(m); operators::less_than_equal(m); operators::not_equal(m); - return m; } @@ -122,15 +117,14 @@ namespace chaiscript /// \param[in] type The name of the type to add the constructors for. /// \param[in,out] m The Module to add the basic constructors to /// \tparam T Type to generate basic constructors for - /// \returns The passed in ModulePtr, or the newly constructed one if the default param is used + /// \returns The passed in Module /// \sa copy_constructor /// \sa constructor template - ModulePtr basic_constructors(const std::string &type, ModulePtr m = std::make_shared()) + void basic_constructors(const std::string &type, Module& m) { - m->add(constructor(), type); + m.add(constructor(), type); copy_constructor(type, m); - return m; } /// \brief Adds a constructor for a POD type @@ -138,10 +132,9 @@ namespace chaiscript /// \param[in] type The name of the type /// \param[in,out] m The Module to add the constructor to template - ModulePtr construct_pod(const std::string &type, ModulePtr m = std::make_shared()) + void construct_pod(const std::string &type, Module& m) { - m->add(fun(&detail::construct_pod), type); - return m; + m.add(fun(&detail::construct_pod), type); } @@ -185,14 +178,13 @@ namespace chaiscript /// Add all common functions for a POD type. All operators, and /// common conversions template - ModulePtr bootstrap_pod_type(const std::string &name, ModulePtr m = std::make_shared()) + void bootstrap_pod_type(const std::string &name, Module& m) { - m->add(user_type(), name); - m->add(constructor(), name); + m.add(user_type(), name); + m.add(constructor(), name); construct_pod(name, m); - m->add(fun(&parse_string), "to_" + name); - return m; + m.add(fun(&parse_string), "to_" + name); } @@ -261,41 +253,41 @@ namespace chaiscript /// Add all arithmetic operators for PODs - static void opers_arithmetic_pod(ModulePtr m = std::make_shared()) + static void opers_arithmetic_pod(Module& m) { - m->add(fun(&Boxed_Number::equals), "=="); - m->add(fun(&Boxed_Number::less_than), "<"); - m->add(fun(&Boxed_Number::greater_than), ">"); - m->add(fun(&Boxed_Number::greater_than_equal), ">="); - m->add(fun(&Boxed_Number::less_than_equal), "<="); - m->add(fun(&Boxed_Number::not_equal), "!="); + m.add(fun(&Boxed_Number::equals), "=="); + m.add(fun(&Boxed_Number::less_than), "<"); + m.add(fun(&Boxed_Number::greater_than), ">"); + m.add(fun(&Boxed_Number::greater_than_equal), ">="); + m.add(fun(&Boxed_Number::less_than_equal), "<="); + m.add(fun(&Boxed_Number::not_equal), "!="); - m->add(fun(&Boxed_Number::pre_decrement), "--"); - m->add(fun(&Boxed_Number::pre_increment), "++"); - m->add(fun(&Boxed_Number::sum), "+"); - m->add(fun(&Boxed_Number::unary_plus), "+"); - m->add(fun(&Boxed_Number::unary_minus), "-"); - m->add(fun(&Boxed_Number::difference), "-"); - m->add(fun(&Boxed_Number::assign_bitwise_and), "&="); - m->add(fun(&Boxed_Number::assign), "="); - m->add(fun(&Boxed_Number::assign_bitwise_or), "|="); - m->add(fun(&Boxed_Number::assign_bitwise_xor), "^="); - m->add(fun(&Boxed_Number::assign_remainder), "%="); - m->add(fun(&Boxed_Number::assign_shift_left), "<<="); - m->add(fun(&Boxed_Number::assign_shift_right), ">>="); - m->add(fun(&Boxed_Number::bitwise_and), "&"); - m->add(fun(&Boxed_Number::bitwise_complement), "~"); - m->add(fun(&Boxed_Number::bitwise_xor), "^"); - m->add(fun(&Boxed_Number::bitwise_or), "|"); - m->add(fun(&Boxed_Number::assign_product), "*="); - m->add(fun(&Boxed_Number::assign_quotient), "/="); - m->add(fun(&Boxed_Number::assign_sum), "+="); - m->add(fun(&Boxed_Number::assign_difference), "-="); - m->add(fun(&Boxed_Number::quotient), "/"); - m->add(fun(&Boxed_Number::shift_left), "<<"); - m->add(fun(&Boxed_Number::product), "*"); - m->add(fun(&Boxed_Number::remainder), "%"); - m->add(fun(&Boxed_Number::shift_right), ">>"); + m.add(fun(&Boxed_Number::pre_decrement), "--"); + m.add(fun(&Boxed_Number::pre_increment), "++"); + m.add(fun(&Boxed_Number::sum), "+"); + m.add(fun(&Boxed_Number::unary_plus), "+"); + m.add(fun(&Boxed_Number::unary_minus), "-"); + m.add(fun(&Boxed_Number::difference), "-"); + m.add(fun(&Boxed_Number::assign_bitwise_and), "&="); + m.add(fun(&Boxed_Number::assign), "="); + m.add(fun(&Boxed_Number::assign_bitwise_or), "|="); + m.add(fun(&Boxed_Number::assign_bitwise_xor), "^="); + m.add(fun(&Boxed_Number::assign_remainder), "%="); + m.add(fun(&Boxed_Number::assign_shift_left), "<<="); + m.add(fun(&Boxed_Number::assign_shift_right), ">>="); + m.add(fun(&Boxed_Number::bitwise_and), "&"); + m.add(fun(&Boxed_Number::bitwise_complement), "~"); + m.add(fun(&Boxed_Number::bitwise_xor), "^"); + m.add(fun(&Boxed_Number::bitwise_or), "|"); + m.add(fun(&Boxed_Number::assign_product), "*="); + m.add(fun(&Boxed_Number::assign_quotient), "/="); + m.add(fun(&Boxed_Number::assign_sum), "+="); + m.add(fun(&Boxed_Number::assign_difference), "-="); + m.add(fun(&Boxed_Number::quotient), "/"); + m.add(fun(&Boxed_Number::shift_left), "<<"); + m.add(fun(&Boxed_Number::product), "*"); + m.add(fun(&Boxed_Number::remainder), "%"); + m.add(fun(&Boxed_Number::shift_right), ">>"); } @@ -403,57 +395,57 @@ namespace chaiscript public: /// \brief perform all common bootstrap functions for std::string, void and POD types /// \param[in,out] m Module to add bootstrapped functions to - /// \returns passed in ModulePtr, or newly created one if default argument is used - static ModulePtr bootstrap(ModulePtr m = std::make_shared()) + /// \returns passed in Module + static void bootstrap(Module& m) { - m->add(user_type(), "void"); - m->add(user_type(), "bool"); - m->add(user_type(), "Object"); - m->add(user_type(), "Number"); - m->add(user_type(), "Function"); - m->add(user_type(), "Assignable_Function"); - m->add(user_type(), "exception"); + m.add(user_type(), "void"); + m.add(user_type(), "bool"); + m.add(user_type(), "Object"); + m.add(user_type(), "Number"); + m.add(user_type(), "Function"); + m.add(user_type(), "Assignable_Function"); + m.add(user_type(), "exception"); - m->add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity"); - m->add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation"); - m->add(fun(&dispatch::Proxy_Function_Base::operator==), "=="); + m.add(fun(&dispatch::Proxy_Function_Base::get_arity), "get_arity"); + m.add(fun(&dispatch::Proxy_Function_Base::annotation), "get_annotation"); + m.add(fun(&dispatch::Proxy_Function_Base::operator==), "=="); - m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types"); - m->add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions"); + m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_param_types)), "get_param_types"); + m.add(fun(return_boxed_value_vector(&dispatch::Proxy_Function_Base::get_contained_functions)), "get_contained_functions"); - m->add(user_type(), "out_of_range"); - m->add(user_type(), "logic_error"); - m->add(chaiscript::base_class()); - m->add(chaiscript::base_class()); - m->add(chaiscript::base_class()); + m.add(user_type(), "out_of_range"); + m.add(user_type(), "logic_error"); + m.add(chaiscript::base_class()); + m.add(chaiscript::base_class()); + m.add(chaiscript::base_class()); - m->add(user_type(), "runtime_error"); - m->add(chaiscript::base_class()); + m.add(user_type(), "runtime_error"); + m.add(chaiscript::base_class()); - m->add(constructor(), "runtime_error"); - m->add(fun(std::function(&what)), "what"); + m.add(constructor(), "runtime_error"); + m.add(fun(std::function(&what)), "what"); - m->add(user_type(), "Dynamic_Object"); - m->add(constructor(), "Dynamic_Object"); - m->add(constructor(), "Dynamic_Object"); - m->add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name"); - m->add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs"); - m->add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit"); - m->add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit"); - m->add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr"); + m.add(user_type(), "Dynamic_Object"); + m.add(constructor(), "Dynamic_Object"); + m.add(constructor(), "Dynamic_Object"); + m.add(fun(&dispatch::Dynamic_Object::get_type_name), "get_type_name"); + m.add(fun(&dispatch::Dynamic_Object::get_attrs), "get_attrs"); + m.add(fun(&dispatch::Dynamic_Object::set_explicit), "set_explicit"); + m.add(fun(&dispatch::Dynamic_Object::is_explicit), "is_explicit"); + m.add(fun(&dispatch::Dynamic_Object::has_attr), "has_attr"); - m->add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "get_attr"); - m->add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "get_attr"); + m.add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "get_attr"); + m.add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "get_attr"); - m->add(fun(static_cast(&dispatch::Dynamic_Object::method_missing)), "method_missing"); - m->add(fun(static_cast(&dispatch::Dynamic_Object::method_missing)), "method_missing"); + m.add(fun(static_cast(&dispatch::Dynamic_Object::method_missing)), "method_missing"); + m.add(fun(static_cast(&dispatch::Dynamic_Object::method_missing)), "method_missing"); - m->add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "[]"); - m->add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "[]"); + m.add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "[]"); + m.add(fun(static_cast(&dispatch::Dynamic_Object::get_attr)), "[]"); - m->eval(R"chaiscript( + m.eval(R"chaiscript( def Dynamic_Object::clone() { auto &new_o = Dynamic_Object(this.get_type_name()); for_each(this.get_attrs(), fun[new_o](x) { new_o.get_attr(x.first) = x.second; } ); @@ -490,37 +482,37 @@ namespace chaiscript } )chaiscript"); - m->add(fun(&has_guard), "has_guard"); - m->add(fun(&get_guard), "get_guard"); + m.add(fun(&has_guard), "has_guard"); + m.add(fun(&get_guard), "get_guard"); - m->add(fun(&Boxed_Value::is_undef), "is_var_undef"); - m->add(fun(&Boxed_Value::is_null), "is_var_null"); - m->add(fun(&Boxed_Value::is_const), "is_var_const"); - m->add(fun(&Boxed_Value::is_ref), "is_var_reference"); - m->add(fun(&Boxed_Value::is_pointer), "is_var_pointer"); - m->add(fun(&Boxed_Value::is_return_value), "is_var_return_value"); - m->add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value"); - m->add(fun(&Boxed_Value::is_type), "is_type"); - m->add(fun(&Boxed_Value::get_attr), "get_var_attr"); - m->add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs"); - m->add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs"); + m.add(fun(&Boxed_Value::is_undef), "is_var_undef"); + m.add(fun(&Boxed_Value::is_null), "is_var_null"); + m.add(fun(&Boxed_Value::is_const), "is_var_const"); + m.add(fun(&Boxed_Value::is_ref), "is_var_reference"); + m.add(fun(&Boxed_Value::is_pointer), "is_var_pointer"); + m.add(fun(&Boxed_Value::is_return_value), "is_var_return_value"); + m.add(fun(&Boxed_Value::reset_return_value), "reset_var_return_value"); + m.add(fun(&Boxed_Value::is_type), "is_type"); + m.add(fun(&Boxed_Value::get_attr), "get_var_attr"); + m.add(fun(&Boxed_Value::copy_attrs), "copy_var_attrs"); + m.add(fun(&Boxed_Value::clone_attrs), "clone_var_attrs"); - m->add(fun(&Boxed_Value::get_type_info), "get_type_info"); - m->add(user_type(), "Type_Info"); - m->add(constructor(), "Type_Info"); + m.add(fun(&Boxed_Value::get_type_info), "get_type_info"); + m.add(user_type(), "Type_Info"); + m.add(constructor(), "Type_Info"); operators::equal(m); - m->add(fun(&Type_Info::is_const), "is_type_const"); - m->add(fun(&Type_Info::is_reference), "is_type_reference"); - m->add(fun(&Type_Info::is_void), "is_type_void"); - m->add(fun(&Type_Info::is_undef), "is_type_undef"); - m->add(fun(&Type_Info::is_pointer), "is_type_pointer"); - m->add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic"); - m->add(fun(&Type_Info::name), "cpp_name"); - m->add(fun(&Type_Info::bare_name), "cpp_bare_name"); - m->add(fun(&Type_Info::bare_equal), "bare_equal"); + m.add(fun(&Type_Info::is_const), "is_type_const"); + m.add(fun(&Type_Info::is_reference), "is_type_reference"); + m.add(fun(&Type_Info::is_void), "is_type_void"); + m.add(fun(&Type_Info::is_undef), "is_type_undef"); + m.add(fun(&Type_Info::is_pointer), "is_type_pointer"); + m.add(fun(&Type_Info::is_arithmetic), "is_type_arithmetic"); + m.add(fun(&Type_Info::name), "cpp_name"); + m.add(fun(&Type_Info::bare_name), "cpp_bare_name"); + m.add(fun(&Type_Info::bare_equal), "bare_equal"); basic_constructors("bool", m); @@ -528,14 +520,14 @@ namespace chaiscript operators::equal(m); operators::not_equal(m); - m->add(fun([](const std::string &s) -> std::string { return s; }), "to_string"); - m->add(fun(&Bootstrap::bool_to_string), "to_string"); - m->add(fun(&unknown_assign), "="); - m->add(fun(&throw_exception), "throw"); - m->add(fun(&what), "what"); + m.add(fun([](const std::string &s) -> std::string { return s; }), "to_string"); + m.add(fun(&Bootstrap::bool_to_string), "to_string"); + m.add(fun(&unknown_assign), "="); + m.add(fun(&throw_exception), "throw"); + m.add(fun(&what), "what"); - m->add(fun(&to_string), "to_string"); - m->add(fun(&Boxed_Number::to_string), "to_string"); + m.add(fun(&to_string), "to_string"); + m.add(fun(&Boxed_Number::to_string), "to_string"); bootstrap_pod_type("double", m); bootstrap_pod_type("long_double", m); @@ -565,38 +557,38 @@ namespace chaiscript opers_arithmetic_pod(m); - m->add(fun(&print), "print_string"); - m->add(fun(&println), "println_string"); + m.add(fun(&print), "print_string"); + m.add(fun(&println), "println_string"); - m->add(dispatch::make_dynamic_proxy_function(&bind_function), "bind"); + m.add(dispatch::make_dynamic_proxy_function(&bind_function), "bind"); - m->add(fun(&shared_ptr_unconst_clone), "clone"); - m->add(fun(&ptr_assign::type>), "="); - m->add(fun(&ptr_assign::type>), "="); - m->add(chaiscript::base_class()); - m->add(fun( + m.add(fun(&shared_ptr_unconst_clone), "clone"); + m.add(fun(&ptr_assign::type>), "="); + m.add(fun(&ptr_assign::type>), "="); + m.add(chaiscript::base_class()); + m.add(fun( [](dispatch::Assignable_Proxy_Function &t_lhs, const std::shared_ptr &t_rhs) { t_lhs.assign(t_rhs); } ), "=" ); - m->add(fun(&Boxed_Value::type_match), "type_match"); + m.add(fun(&Boxed_Value::type_match), "type_match"); - m->add(chaiscript::fun(&has_parse_tree), "has_parse_tree"); - m->add(chaiscript::fun(&get_parse_tree), "get_parse_tree"); + m.add(chaiscript::fun(&has_parse_tree), "has_parse_tree"); + m.add(chaiscript::fun(&get_parse_tree), "get_parse_tree"); - m->add(chaiscript::base_class()); + m.add(chaiscript::base_class()); - m->add(chaiscript::user_type(), "arithmetic_error"); - m->add(chaiscript::base_class()); + m.add(chaiscript::user_type(), "arithmetic_error"); + m.add(chaiscript::base_class()); // chaiscript::bootstrap::standard_library::vector_type > >("AST_NodeVector", m); - chaiscript::utility::add_class(*m, + chaiscript::utility::add_class(m, "eval_error", { }, { {fun(&chaiscript::exception::eval_error::reason), "reason"}, @@ -611,7 +603,7 @@ namespace chaiscript ); - chaiscript::utility::add_class(*m, + chaiscript::utility::add_class(m, "File_Position", { constructor(), constructor() }, @@ -620,7 +612,7 @@ namespace chaiscript ); - chaiscript::utility::add_class(*m, + chaiscript::utility::add_class(m, "AST_Node", { }, { {fun(&AST_Node::text), "text"}, @@ -641,16 +633,12 @@ namespace chaiscript ); - chaiscript::utility::add_class(*m, + chaiscript::utility::add_class(m, "ChaiScript_Parser", { constructor() }, { {fun(&parser::ChaiScript_Parser::parse), "parse"}, {fun(&parser::ChaiScript_Parser::ast), "ast"} } ); - - - - return m; } }; } diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 1823c9d..7ba588b 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -177,22 +177,20 @@ namespace chaiscript /// Add Bidir_Range support for the given ContainerType template - ModulePtr input_range_type_impl(const std::string &type, ModulePtr m = std::make_shared()) + void input_range_type_impl(const std::string &type, Module& m) { - m->add(user_type(), type + "_Range"); + m.add(user_type(), type + "_Range"); copy_constructor(type + "_Range", m); - m->add(constructor(), "range_internal"); + m.add(constructor(), "range_internal"); - m->add(fun(&Bidir_Type::empty), "empty"); - m->add(fun(&Bidir_Type::pop_front), "pop_front"); - m->add(fun(&Bidir_Type::front), "front"); - m->add(fun(&Bidir_Type::pop_back), "pop_back"); - m->add(fun(&Bidir_Type::back), "back"); - - return m; - } + m.add(fun(&Bidir_Type::empty), "empty"); + m.add(fun(&Bidir_Type::pop_front), "pop_front"); + m.add(fun(&Bidir_Type::front), "front"); + m.add(fun(&Bidir_Type::pop_back), "pop_back"); + m.add(fun(&Bidir_Type::back), "back"); + } /// Algorithm for inserting at a specific position into a container @@ -230,10 +228,16 @@ namespace chaiscript } template - ModulePtr input_range_type(const std::string &type, ModulePtr m = std::make_shared()) + void input_range_type(const std::string &type, Module& m) { detail::input_range_type_impl >(type,m); detail::input_range_type_impl >("Const_" + type, m); + } + template + ModulePtr input_range_type(const std::string &type) + { + auto m = std::make_shared(); + input_range_type(type, *m); return m; } @@ -241,11 +245,11 @@ namespace chaiscript /// Add random_access_container concept to the given ContainerType /// http://www.sgi.com/tech/stl/RandomAccessContainer.html template - ModulePtr random_access_container_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + void random_access_container_type(const std::string &/*type*/, Module& m) { //In the interest of runtime safety for the m, we prefer the at() method for [] access, //to throw an exception in an out of bounds condition. - m->add( + m.add( fun( [](ContainerType &c, int index) -> typename ContainerType::reference { /// \todo we are prefering to keep the key as 'int' to avoid runtime conversions @@ -253,25 +257,37 @@ namespace chaiscript return c.at(static_cast(index)); }), "[]"); - m->add( + m.add( fun( [](const ContainerType &c, int index) -> typename ContainerType::const_reference { /// \todo we are prefering to keep the key as 'int' to avoid runtime conversions /// during dispatch. reevaluate return c.at(static_cast(index)); }), "[]"); - + } + template + ModulePtr random_access_container_type(const std::string &type) + { + auto m = std::make_shared(); + random_access_container_type(type, *m); return m; } + /// Add assignable concept to the given ContainerType /// http://www.sgi.com/tech/stl/Assignable.html template - ModulePtr assignable_type(const std::string &type, ModulePtr m = std::make_shared()) + void assignable_type(const std::string &type, Module& m) { copy_constructor(type, m); operators::assign(m); + } + template + ModulePtr assignable_type(const std::string &type) + { + auto m = std::make_shared(); + assignable_type(type, *m); return m; } @@ -279,33 +295,44 @@ namespace chaiscript /// Add container concept to the given ContainerType /// http://www.sgi.com/tech/stl/Container.html template - ModulePtr container_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + void container_type(const std::string &/*type*/, Module& m) { - m->add(fun([](const ContainerType *a) { return a->size(); } ), "size"); - m->add(fun([](const ContainerType *a) { return a->empty(); } ), "empty"); - m->add(fun([](ContainerType *a) { a->clear(); } ), "clear"); - return m; + m.add(fun([](const ContainerType *a) { return a->size(); } ), "size"); + m.add(fun([](const ContainerType *a) { return a->empty(); } ), "empty"); + m.add(fun([](ContainerType *a) { a->clear(); } ), "clear"); } + template + ModulePtr container_type(const std::string& type) + { + auto m = std::make_shared(); + container_type(type, *m); + return m; + } /// Add default constructable concept to the given Type /// http://www.sgi.com/tech/stl/DefaultConstructible.html template - ModulePtr default_constructible_type(const std::string &type, ModulePtr m = std::make_shared()) + void default_constructible_type(const std::string &type, Module& m) { - m->add(constructor(), type); + m.add(constructor(), type); + } + template + ModulePtr default_constructible_type(const std::string& type) + { + auto m = std::make_shared(); + default_constructible_type(type, *m); return m; } - /// Add sequence concept to the given ContainerType /// http://www.sgi.com/tech/stl/Sequence.html template - ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + void sequence_type(const std::string &/*type*/, Module& m) { - m->add(fun(&detail::insert_at), + m.add(fun(&detail::insert_at), []()->std::string{ if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { return "insert_ref_at"; @@ -314,27 +341,31 @@ namespace chaiscript } }()); - m->add(fun(&detail::erase_at), "erase_at"); - + m.add(fun(&detail::erase_at), "erase_at"); + } + template + ModulePtr sequence_type(const std::string &type) + { + auto m = std::make_shared(); + sequence_type(type, *m); return m; } - /// Add back insertion sequence concept to the given ContainerType /// http://www.sgi.com/tech/stl/BackInsertionSequence.html template - ModulePtr back_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared()) + void back_insertion_sequence_type(const std::string &type, Module& m) { typedef typename ContainerType::reference (ContainerType::*backptr)(); - m->add(fun(static_cast(&ContainerType::back)), "back"); + m.add(fun(static_cast(&ContainerType::back)), "back"); typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &); - m->add(fun(static_cast(&ContainerType::push_back)), + m.add(fun(static_cast(&ContainerType::push_back)), [&]()->std::string{ if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { - m->eval( + m.eval( "# Pushes the second value onto the container while making a clone of the value\n" "def push_back(" + type + " container, x)\n" "{ \n" @@ -353,7 +384,13 @@ namespace chaiscript } }()); - m->add(fun(&ContainerType::pop_back), "pop_back"); + m.add(fun(&ContainerType::pop_back), "pop_back"); + } + template + ModulePtr back_insertion_sequence_type(const std::string &type) + { + auto m = std::make_shared(); + back_insertion_sequence_type(type, *m); return m; } @@ -362,20 +399,20 @@ namespace chaiscript /// Front insertion sequence /// http://www.sgi.com/tech/stl/FrontInsertionSequence.html template - ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = std::make_shared()) + void front_insertion_sequence_type(const std::string &type, Module& m) { typedef typename ContainerType::reference (ContainerType::*front_ptr)(); typedef typename ContainerType::const_reference (ContainerType::*const_front_ptr)() const; typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference); typedef void (ContainerType::*pop_ptr)(); - m->add(fun(static_cast(&ContainerType::front)), "front"); - m->add(fun(static_cast(&ContainerType::front)), "front"); + m.add(fun(static_cast(&ContainerType::front)), "front"); + m.add(fun(static_cast(&ContainerType::front)), "front"); - m->add(fun(static_cast(&ContainerType::push_front)), + m.add(fun(static_cast(&ContainerType::push_front)), [&]()->std::string{ if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { - m->eval( + m.eval( "# Pushes the second value onto the front of container while making a clone of the value\n" "def push_front(" + type + " container, x)\n" "{ \n" @@ -393,7 +430,13 @@ namespace chaiscript } }()); - m->add(fun(static_cast(&ContainerType::pop_front)), "pop_front"); + m.add(fun(static_cast(&ContainerType::pop_front)), "pop_front"); + } + template + ModulePtr front_insertion_sequence_type(const std::string &type) + { + auto m = std::make_shared(); + front_insertion_sequence_type(type, *m); return m; } @@ -401,20 +444,25 @@ namespace chaiscript /// bootstrap a given PairType /// http://www.sgi.com/tech/stl/pair.html template - ModulePtr pair_type(const std::string &type, ModulePtr m = std::make_shared()) + void pair_type(const std::string &type, Module& m) { - m->add(user_type(), type); + m.add(user_type(), type); typename PairType::first_type PairType::* f = &PairType::first; typename PairType::second_type PairType::* s = &PairType::second; - m->add(fun(f), "first"); - m->add(fun(s), "second"); + m.add(fun(f), "first"); + m.add(fun(s), "second"); basic_constructors(type, m); - m->add(constructor(), type); - + m.add(constructor(), type); + } + template + ModulePtr pair_type(const std::string &type) + { + auto m = std::make_shared(); + pair_type(type, *m); return m; } @@ -424,10 +472,15 @@ namespace chaiscript /// http://www.sgi.com/tech/stl/PairAssociativeContainer.html template - ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = std::make_shared()) + void pair_associative_container_type(const std::string &type, Module& m) { pair_type(type + "_Pair", m); - + } + template + ModulePtr pair_associative_container_type(const std::string &type) + { + auto m = std::make_shared(); + pair_associative_container_type(type, *m); return m; } @@ -435,17 +488,17 @@ namespace chaiscript /// Add unique associative container concept to the given ContainerType /// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html template - ModulePtr unique_associative_container_type(const std::string &/*type*/, ModulePtr m = std::make_shared()) + void unique_associative_container_type(const std::string &/*type*/, Module& m) { - m->add(fun(detail::count), "count"); + m.add(fun(detail::count), "count"); typedef size_t (ContainerType::*erase_ptr)(const typename ContainerType::key_type &); - m->add(fun(static_cast(&ContainerType::erase)), "erase"); + m.add(fun(static_cast(&ContainerType::erase)), "erase"); - m->add(fun(&detail::insert), "insert"); + m.add(fun(&detail::insert), "insert"); - m->add(fun(&detail::insert_ref), + m.add(fun(&detail::insert_ref), []()->std::string{ if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) { return "insert_ref"; @@ -453,8 +506,12 @@ namespace chaiscript return "insert"; } }()); - - + } + template + ModulePtr unique_associative_container_type(const std::string &type) + { + auto m = std::make_shared(); + unique_associative_container_type(type, *m); return m; } @@ -462,21 +519,21 @@ namespace chaiscript /// Add a MapType container /// http://www.sgi.com/tech/stl/Map.html template - ModulePtr map_type(const std::string &type, ModulePtr m = std::make_shared()) + void map_type(const std::string &type, Module& m) { - m->add(user_type(), type); + m.add(user_type(), type); typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &); typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const; - m->add(fun(static_cast(&MapType::operator[])), "[]"); + m.add(fun(static_cast(&MapType::operator[])), "[]"); - m->add(fun(static_cast(&MapType::at)), "at"); - m->add(fun(static_cast(&MapType::at)), "at"); + m.add(fun(static_cast(&MapType::at)), "at"); + m.add(fun(static_cast(&MapType::at)), "at"); if (typeid(MapType) == typeid(std::map)) { - m->eval(R"( + m.eval(R"( def Map::`==`(Map rhs) { if ( rhs.size() != this.size() ) { return false; @@ -504,7 +561,12 @@ namespace chaiscript unique_associative_container_type(type, m); pair_associative_container_type(type, m); input_range_type(type, m); - + } + template + ModulePtr map_type(const std::string &type) + { + auto m = std::make_shared(); + map_type(type, *m); return m; } @@ -512,9 +574,9 @@ namespace chaiscript /// hopefully working List type /// http://www.sgi.com/tech/stl/List.html template - ModulePtr list_type(const std::string &type, ModulePtr m = std::make_shared()) + void list_type(const std::string &type, Module& m) { - m->add(user_type(), type); + m.add(user_type(), type); front_insertion_sequence_type(type, m); back_insertion_sequence_type(type, m); @@ -523,7 +585,12 @@ namespace chaiscript default_constructible_type(type, m); assignable_type(type, m); input_range_type(type, m); - + } + template + ModulePtr list_type(const std::string &type) + { + auto m = std::make_shared(); + list_type(type, m); return m; } @@ -531,15 +598,15 @@ namespace chaiscript /// Create a vector type with associated concepts /// http://www.sgi.com/tech/stl/Vector.html template - ModulePtr vector_type(const std::string &type, ModulePtr m = std::make_shared()) + void vector_type(const std::string &type, Module& m) { - m->add(user_type(), type); + m.add(user_type(), type); typedef typename VectorType::reference (VectorType::*frontptr)(); typedef typename VectorType::const_reference (VectorType::*constfrontptr)() const; - m->add(fun(static_cast(&VectorType::front)), "front"); - m->add(fun(static_cast(&VectorType::front)), "front"); + m.add(fun(static_cast(&VectorType::front)), "front"); + m.add(fun(static_cast(&VectorType::front)), "front"); back_insertion_sequence_type(type, m); @@ -552,7 +619,7 @@ namespace chaiscript if (typeid(VectorType) == typeid(std::vector)) { - m->eval(R"( + m.eval(R"( def Vector::`==`(Vector rhs) { if ( rhs.size() != this.size() ) { return false; @@ -573,16 +640,21 @@ namespace chaiscript } )" ); } - + } + template + ModulePtr vector_type(const std::string &type) + { + auto m = std::make_shared(); + vector_type(type, *m); return m; } /// Add a String container /// http://www.sgi.com/tech/stl/basic_string.html template - ModulePtr string_type(const std::string &type, ModulePtr m = std::make_shared()) + void string_type(const std::string &type, Module& m) { - m->add(user_type(), type); + m.add(user_type(), type); operators::addition(m); operators::assign_sum(m); opers_comparison(m); @@ -594,7 +666,7 @@ namespace chaiscript input_range_type(type, m); //Special case: add push_back to string (which doesn't support other back_insertion operations - m->add(fun(&String::push_back), + m.add(fun(&String::push_back), []()->std::string{ if (typeid(typename String::value_type) == typeid(Boxed_Value)) { return "push_back_ref"; @@ -604,21 +676,26 @@ namespace chaiscript }()); - m->add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find"); - m->add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind"); - m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of"); - m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of"); - m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of"); - m->add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of"); + m.add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find"); + m.add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind"); + m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of"); + m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of"); + m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of"); + m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of"); - m->add(fun([](String *s) { s->clear(); } ), "clear"); - m->add(fun([](const String *s) { return s->empty(); } ), "empty"); - m->add(fun([](const String *s) { return s->size(); } ), "size"); - - m->add(fun([](const String *s) { return s->c_str(); } ), "c_str"); - m->add(fun([](const String *s) { return s->data(); } ), "data"); - m->add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr"); + m.add(fun([](String *s) { s->clear(); } ), "clear"); + m.add(fun([](const String *s) { return s->empty(); } ), "empty"); + m.add(fun([](const String *s) { return s->size(); } ), "size"); + m.add(fun([](const String *s) { return s->c_str(); } ), "c_str"); + m.add(fun([](const String *s) { return s->data(); } ), "data"); + m.add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr"); + } + template + ModulePtr string_type(const std::string &type) + { + auto m = std::make_shared(); + string_type(type, *m); return m; } @@ -627,14 +704,19 @@ namespace chaiscript /// Add a MapType container /// http://www.sgi.com/tech/stl/Map.html template - ModulePtr future_type(const std::string &type, ModulePtr m = std::make_shared()) + void future_type(const std::string &type, Module& m) { - m->add(user_type(), type); - - m->add(fun([](const FutureType &t) { return t.valid(); }), "valid"); - m->add(fun(&FutureType::get), "get"); - m->add(fun(&FutureType::wait), "wait"); + m.add(user_type(), type); + m.add(fun([](const FutureType &t) { return t.valid(); }), "valid"); + m.add(fun(&FutureType::get), "get"); + m.add(fun(&FutureType::wait), "wait"); + } + template + ModulePtr future_type(const std::string &type) + { + auto m = std::make_shared(); + future_type(type, *m); return m; } } diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 352b086..b480809 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -178,12 +178,6 @@ namespace chaiscript return *this; } - Module &add(const std::shared_ptr &m) - { - m->apply(*this, *this); - return *m; - } - template void apply(Eval &t_eval, Engine &t_engine) const { diff --git a/include/chaiscript/dispatchkit/operators.hpp b/include/chaiscript/dispatchkit/operators.hpp index 2d4401e..8dc54ba 100644 --- a/include/chaiscript/dispatchkit/operators.hpp +++ b/include/chaiscript/dispatchkit/operators.hpp @@ -229,234 +229,201 @@ namespace chaiscript template - ModulePtr assign(ModulePtr m = std::make_shared()) + void assign(Module& m) { - m->add(chaiscript::fun(&detail::assign), "="); - return m; + m.add(chaiscript::fun(&detail::assign), "="); } template - ModulePtr assign_bitwise_and(ModulePtr m = std::make_shared()) + void assign_bitwise_and(Module& m) { - m->add(chaiscript::fun(&detail::assign_bitwise_and), "&="); - return m; + m.add(chaiscript::fun(&detail::assign_bitwise_and), "&="); } template - ModulePtr assign_xor(ModulePtr m = std::make_shared()) + void assign_xor(Module& m) { - m->add(chaiscript::fun(&detail::assign_xor), "^="); - return m; + m.add(chaiscript::fun(&detail::assign_xor), "^="); } template - ModulePtr assign_bitwise_or(ModulePtr m = std::make_shared()) + void assign_bitwise_or(Module& m) { - m->add(chaiscript::fun(&detail::assign_bitwise_or), "|="); - return m; + m.add(chaiscript::fun(&detail::assign_bitwise_or), "|="); } template - ModulePtr assign_difference(ModulePtr m = std::make_shared()) + void assign_difference(Module& m) { - m->add(chaiscript::fun(&detail::assign_difference), "-="); - return m; + m.add(chaiscript::fun(&detail::assign_difference), "-="); } template - ModulePtr assign_left_shift(ModulePtr m = std::make_shared()) + void assign_left_shift(Module& m) { - m->add(chaiscript::fun(&detail::assign_left_shift), "<<="); - return m; + m.add(chaiscript::fun(&detail::assign_left_shift), "<<="); } template - ModulePtr assign_product(ModulePtr m = std::make_shared()) + void assign_product(Module& m) { - m->add(chaiscript::fun(&detail::assign_product), "*="); - return m; + m.add(chaiscript::fun(&detail::assign_product), "*="); } template - ModulePtr assign_quotient(ModulePtr m = std::make_shared()) + void assign_quotient(Module& m) { - m->add(chaiscript::fun(&detail::assign_quotient), "/="); - return m; + m.add(chaiscript::fun(&detail::assign_quotient), "/="); } template - ModulePtr assign_remainder(ModulePtr m = std::make_shared()) + void assign_remainder(Module& m) { - m->add(chaiscript::fun(&detail::assign_remainder), "%="); - return m; + m.add(chaiscript::fun(&detail::assign_remainder), "%="); } template - ModulePtr assign_right_shift(ModulePtr m = std::make_shared()) + void assign_right_shift(Module& m) { - m->add(chaiscript::fun(&detail::assign_right_shift), ">>="); - return m; + m.add(chaiscript::fun(&detail::assign_right_shift), ">>="); } template - ModulePtr assign_sum(ModulePtr m = std::make_shared()) + void assign_sum(Module& m) { - m->add(chaiscript::fun(&detail::assign_sum), "+="); - return m; + m.add(chaiscript::fun(&detail::assign_sum), "+="); } template - ModulePtr prefix_decrement(ModulePtr m = std::make_shared()) + void prefix_decrement(Module& m) { - m->add(chaiscript::fun(&detail::prefix_decrement), "--"); - return m; + m.add(chaiscript::fun(&detail::prefix_decrement), "--"); } template - ModulePtr prefix_increment(ModulePtr m = std::make_shared()) + void prefix_increment(Module& m) { - m->add(chaiscript::fun(&detail::prefix_increment), "++"); - return m; + m.add(chaiscript::fun(&detail::prefix_increment), "++"); } template - ModulePtr equal(ModulePtr m = std::make_shared()) + void equal(Module& m) { - m->add(chaiscript::fun(&detail::equal), "=="); - return m; + m.add(chaiscript::fun(&detail::equal), "=="); } template - ModulePtr greater_than(ModulePtr m = std::make_shared()) + void greater_than(Module& m) { - m->add(chaiscript::fun(&detail::greater_than), ">"); - return m; + m.add(chaiscript::fun(&detail::greater_than), ">"); } template - ModulePtr greater_than_equal(ModulePtr m = std::make_shared()) + void greater_than_equal(Module& m) { - m->add(chaiscript::fun(&detail::greater_than_equal), ">="); - return m; + m.add(chaiscript::fun(&detail::greater_than_equal), ">="); } template - ModulePtr less_than(ModulePtr m = std::make_shared()) + void less_than(Module& m) { - m->add(chaiscript::fun(&detail::less_than), "<"); - return m; + m.add(chaiscript::fun(&detail::less_than), "<"); } template - ModulePtr less_than_equal(ModulePtr m = std::make_shared()) + void less_than_equal(Module& m) { - m->add(chaiscript::fun(&detail::less_than_equal), "<="); - return m; + m.add(chaiscript::fun(&detail::less_than_equal), "<="); } template - ModulePtr logical_compliment(ModulePtr m = std::make_shared()) + void logical_compliment(Module& m) { - m->add(chaiscript::fun(&detail::logical_compliment), "!"); - return m; + m.add(chaiscript::fun(&detail::logical_compliment), "!"); } template - ModulePtr not_equal(ModulePtr m = std::make_shared()) + void not_equal(Module& m) { - m->add(chaiscript::fun(&detail::not_equal), "!="); - return m; + m.add(chaiscript::fun(&detail::not_equal), "!="); } template - ModulePtr addition(ModulePtr m = std::make_shared()) + void addition(Module& m) { - m->add(chaiscript::fun(&detail::addition), "+"); - return m; + m.add(chaiscript::fun(&detail::addition), "+"); } template - ModulePtr unary_plus(ModulePtr m = std::make_shared()) + void unary_plus(Module& m) { - m->add(chaiscript::fun(&detail::unary_plus), "+"); - return m; + m.add(chaiscript::fun(&detail::unary_plus), "+"); } template - ModulePtr subtraction(ModulePtr m = std::make_shared()) + void subtraction(Module& m) { - m->add(chaiscript::fun(&detail::subtraction), "-"); - return m; + m.add(chaiscript::fun(&detail::subtraction), "-"); } template - ModulePtr unary_minus(ModulePtr m = std::make_shared()) + void unary_minus(Module& m) { - m->add(chaiscript::fun(&detail::unary_minus), "-"); - return m; + m.add(chaiscript::fun(&detail::unary_minus), "-"); } template - ModulePtr bitwise_and(ModulePtr m = std::make_shared()) + void bitwise_and(Module& m) { - m->add(chaiscript::fun(&detail::bitwise_and), "&"); - return m; + m.add(chaiscript::fun(&detail::bitwise_and), "&"); } template - ModulePtr bitwise_compliment(ModulePtr m = std::make_shared()) + void bitwise_compliment(Module& m) { - m->add(chaiscript::fun(&detail::bitwise_compliment), "~"); - return m; + m.add(chaiscript::fun(&detail::bitwise_compliment), "~"); } template - ModulePtr bitwise_xor(ModulePtr m = std::make_shared()) + void bitwise_xor(Module& m) { - m->add(chaiscript::fun(&detail::bitwise_xor), "^"); - return m; + m.add(chaiscript::fun(&detail::bitwise_xor), "^"); } template - ModulePtr bitwise_or(ModulePtr m = std::make_shared()) + void bitwise_or(Module& m) { - m->add(chaiscript::fun(&detail::bitwise_or), "|"); - return m; + m.add(chaiscript::fun(&detail::bitwise_or), "|"); } template - ModulePtr division(ModulePtr m = std::make_shared()) + void division(Module& m) { - m->add(chaiscript::fun(&detail::division), "/"); - return m; + m.add(chaiscript::fun(&detail::division), "/"); } template - ModulePtr left_shift(ModulePtr m = std::make_shared()) + void left_shift(Module& m) { - m->add(chaiscript::fun(&detail::left_shift), "<<"); - return m; + m.add(chaiscript::fun(&detail::left_shift), "<<"); } template - ModulePtr multiplication(ModulePtr m = std::make_shared()) + void multiplication(Module& m) { - m->add(chaiscript::fun(&detail::multiplication), "*"); - return m; + m.add(chaiscript::fun(&detail::multiplication), "*"); } template - ModulePtr remainder(ModulePtr m = std::make_shared()) + void remainder(Module& m) { - m->add(chaiscript::fun(&detail::remainder), "%"); - return m; + m.add(chaiscript::fun(&detail::remainder), "%"); } template - ModulePtr right_shift(ModulePtr m = std::make_shared()) + void right_shift(Module& m) { - m->add(chaiscript::fun(&detail::right_shift), ">>"); - return m; + m.add(chaiscript::fun(&detail::right_shift), ">>"); } } } diff --git a/include/chaiscript/utility/json_wrap.hpp b/include/chaiscript/utility/json_wrap.hpp index aec927b..c0af1cd 100644 --- a/include/chaiscript/utility/json_wrap.hpp +++ b/include/chaiscript/utility/json_wrap.hpp @@ -9,11 +9,11 @@ namespace chaiscript { public: - static ModulePtr library(ModulePtr m = std::make_shared()) + static Module& library(Module& m) { - m->add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json"); - m->add(chaiscript::fun(&json_wrap::to_json), "to_json"); + m.add(chaiscript::fun([](const std::string &t_str) { return from_json(t_str); }), "from_json"); + m.add(chaiscript::fun(&json_wrap::to_json), "to_json"); return m; diff --git a/include/chaiscript/utility/utility.hpp b/include/chaiscript/utility/utility.hpp index 17950e2..1ca0dfb 100644 --- a/include/chaiscript/utility/utility.hpp +++ b/include/chaiscript/utility/utility.hpp @@ -78,10 +78,9 @@ namespace chaiscript t_module.add(chaiscript::constructor(), t_class_name); using namespace chaiscript::bootstrap::operators; - t_module.add([](){ - // add some comparison and assignment operators - return assign(not_equal(equal())); - }()); + equal(t_module); + not_equal(t_module); + assign(t_module); t_module.add(chaiscript::fun([](const Enum &e, const int &i) { return e == i; }), "=="); t_module.add(chaiscript::fun([](const int &i, const Enum &e) { return i == e; }), "=="); diff --git a/src/stl_extra.cpp b/src/stl_extra.cpp index ec38db7..d3ee375 100644 --- a/src/stl_extra.cpp +++ b/src/stl_extra.cpp @@ -23,9 +23,9 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_stl_extra() { - - auto module = chaiscript::bootstrap::standard_library::list_type >("List"); - module->add(chaiscript::bootstrap::standard_library::vector_type >("u16vector")); + auto module = std::make_shared(); + chaiscript::bootstrap::standard_library::list_type >("List", *module); + chaiscript::bootstrap::standard_library::vector_type >("u16vector", *module); module->add(chaiscript::vector_conversion>()); return module; } diff --git a/src/test_module.cpp b/src/test_module.cpp index 589437e..731e327 100644 --- a/src/test_module.cpp +++ b/src/test_module.cpp @@ -189,9 +189,9 @@ CHAISCRIPT_MODULE_EXPORT chaiscript::ModulePtr create_chaiscript_module_test_mo m->add(chaiscript::fun(&TestBaseType::set_string_val), "set_string_val"); m->add(chaiscript::fun(&TestBaseType::mdarray), "mdarray"); - m->add(chaiscript::bootstrap::array("IntArray_2_3_5")); - m->add(chaiscript::bootstrap::array("IntArray_3_5")); - m->add(chaiscript::bootstrap::array("IntArray_5")); + chaiscript::bootstrap::array("IntArray_2_3_5", *m); + chaiscript::bootstrap::array("IntArray_3_5", *m); + chaiscript::bootstrap::array("IntArray_5", *m); // member that is a function m->add(chaiscript::fun(&TestBaseType::func_member), "func_member");