diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 22a439c..c4fd18a 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -156,9 +156,7 @@ namespace chaiscript return p; } - /** - * Specific version of shared_ptr_clone just for Proxy_Functions - */ + /// Specific version of shared_ptr_clone just for Proxy_Functions template std::shared_ptr::type> shared_ptr_unconst_clone(const std::shared_ptr::type> &p) @@ -168,11 +166,9 @@ namespace chaiscript - /** - * Assignment function for shared_ptr objects, does not perform a copy of the - * object pointed to, instead maintains the shared_ptr concept. - * Similar to shared_ptr_clone. Used for Proxy_Function. - */ + /// Assignment function for shared_ptr objects, does not perform a copy of the + /// object pointed to, instead maintains the shared_ptr concept. + /// Similar to shared_ptr_clone. Used for Proxy_Function. template Boxed_Value ptr_assign(Boxed_Value lhs, const std::shared_ptr &rhs) { @@ -186,10 +182,8 @@ namespace chaiscript } } - /** - * Class consisting of only static functions. All default bootstrapping occurs - * from this class. - */ + /// Class consisting of only static functions. All default bootstrapping occurs + /// from this class. class Bootstrap { private: @@ -217,9 +211,7 @@ namespace chaiscript } - /** - * Add all arithmetic operators for PODs - */ + /// Add all arithmetic operators for PODs static void opers_arithmetic_pod(ModulePtr m = ModulePtr(new Module())) { m->add(fun(&Boxed_Number::equals), "=="); @@ -259,11 +251,8 @@ namespace chaiscript } - /** - * Create a bound function object. The first param is the function to bind - * the remaining parameters are the args to bind into the - * result - */ + /// Create a bound function object. The first param is the function to bind + /// the remaining parameters are the args to bind into the result static Boxed_Value bind_function(const std::vector ¶ms) { if (params.size() < 2) @@ -273,7 +262,7 @@ namespace chaiscript Const_Proxy_Function f = boxed_cast(params[0]); - return Boxed_Value(Const_Proxy_Function(new dispatch::Bound_Function(f, + return Boxed_Value(Const_Proxy_Function(std::make_shared(f, std::vector(params.begin() + 1, params.end())))); } @@ -318,9 +307,7 @@ namespace chaiscript return e.what(); } - /** - * Boolean specialization of internal to_string function - */ + /// Boolean specialization of internal to_string function static std::string bool_to_string(bool b) { if (b) @@ -350,9 +337,7 @@ namespace chaiscript static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) { - std::shared_ptr pf - = std::dynamic_pointer_cast(t_pf); - if (pf) + if (auto pf = std::dynamic_pointer_cast(t_pf)) { if (pf->get_parse_tree()) { @@ -367,9 +352,7 @@ namespace chaiscript static chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) { - std::shared_ptr pf - = std::dynamic_pointer_cast(t_pf); - if (pf) + if (auto pf = std::dynamic_pointer_cast(t_pf)) { if (pf->get_parse_tree()) { diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index 6797e5d..acb8344 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -278,9 +278,9 @@ namespace chaiscript template ModulePtr container_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) { - m->add(fun( std::function( [](const ContainerType *a) { return a->size(); } ) ), "size"); - m->add(fun( std::function( [](const ContainerType *a) { return a->empty(); } ) ), "empty"); - m->add(fun( std::function( [](ContainerType *a) { a->clear(); } ) ), "clear"); + 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; } @@ -493,24 +493,26 @@ namespace chaiscript if (typeid(VectorType) == typeid(std::vector)) { - m->eval("def Vector::`==`(rhs) : type_match(rhs, this) { \ - if ( rhs.size() != this.size() ) { \ - return false; \ - } else { \ - auto r1 = range(this); \ - auto r2 = range(rhs); \ - while (!r1.empty()) \ - { \ - if (!eq(r1.front(), r2.front())) \ - { \ - return false; \ - } \ - r1.pop_front(); \ - r2.pop_front(); \ - } \ - return true; \ - } \ - }"); + m->eval(R"( + def Vector::`==`(rhs) : type_match(rhs, this) { + if ( rhs.size() != this.size() ) { + return false; + } else { + auto r1 = range(this); + auto r2 = range(rhs); + while (!r1.empty()) + { + if (!eq(r1.front(), r2.front())) + { + return false; + } + r1.pop_front(); + r2.pop_front(); + } + true; + } + } )" + ); } return m; diff --git a/include/chaiscript/dispatchkit/boxed_number.hpp b/include/chaiscript/dispatchkit/boxed_number.hpp index 62eabe9..f54cd13 100644 --- a/include/chaiscript/dispatchkit/boxed_number.hpp +++ b/include/chaiscript/dispatchkit/boxed_number.hpp @@ -826,9 +826,7 @@ namespace chaiscript namespace detail { - /** - * Cast_Helper for converting from Boxed_Value to Boxed_Number - */ + /// Cast_Helper for converting from Boxed_Value to Boxed_Number template<> struct Cast_Helper { @@ -840,17 +838,13 @@ namespace chaiscript } }; - /** - * Cast_Helper for converting from Boxed_Value to Boxed_Number - */ + /// Cast_Helper for converting from Boxed_Value to Boxed_Number template<> struct Cast_Helper : Cast_Helper { }; - /** - * Cast_Helper for converting from Boxed_Value to Boxed_Number - */ + /// Cast_Helper for converting from Boxed_Value to Boxed_Number template<> struct Cast_Helper : Cast_Helper { diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 77076f1..b525281 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -13,6 +13,7 @@ #include #include "../chaiscript_threading.hpp" +#include "../chaiscript_defines.hpp" #include "any.hpp" #include "type_info.hpp" diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index 63a0038..b49d14c 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -166,9 +166,9 @@ namespace chaiscript //Add a bit of ChaiScript to eval during module implementation - Module &eval(std::string str) + Module &eval(const std::string &str) { - m_evals.push_back(std::move(str)); + m_evals.push_back(str); return *this; } @@ -265,7 +265,7 @@ namespace chaiscript virtual bool operator==(const dispatch::Proxy_Function_Base &rhs) const CHAISCRIPT_OVERRIDE { try { - const Dispatch_Function &dispatchfun = dynamic_cast(rhs); + const auto &dispatchfun = dynamic_cast(rhs); return m_funcs == dispatchfun.m_funcs; } catch (const std::bad_cast &) { return false; @@ -286,7 +286,7 @@ namespace chaiscript return -1; } - const int arity = m_funcs.front()->get_arity(); + const auto arity = m_funcs.front()->get_arity(); for (const auto &func : m_funcs) { @@ -302,15 +302,8 @@ namespace chaiscript virtual bool call_match(const std::vector &vals, const Dynamic_Cast_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE { - for (const auto &func : m_funcs) - { - if (func->call_match(vals, t_conversions)) - { - return true; - } - } - - return false; + return std::any_of(m_funcs.cbegin(), m_funcs.cend(), + [&vals, &t_conversions](const Proxy_Function &f){ return f->call_match(vals, t_conversions); }); } virtual std::string annotation() const CHAISCRIPT_OVERRIDE @@ -321,7 +314,7 @@ namespace chaiscript protected: virtual Boxed_Value do_call(const std::vector ¶ms, const Dynamic_Cast_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE { - return dispatch::dispatch(m_funcs.begin(), m_funcs.end(), params, t_conversions); + return dispatch::dispatch(m_funcs.cbegin(), m_funcs.cend(), params, t_conversions); } private: @@ -329,10 +322,8 @@ namespace chaiscript static std::vector build_type_infos(const std::vector &t_funcs) { - typedef std::vector function_vec; - - auto begin = t_funcs.begin(); - const function_vec::const_iterator end = t_funcs.end(); + auto begin = t_funcs.cbegin(); + const auto &end = t_funcs.cend(); if (begin != end) { @@ -380,10 +371,8 @@ namespace chaiscript namespace detail { - /** - * Main class for the dispatchkit. Handles management - * of the object stack, functions and registered types. - */ + /// Main class for the dispatchkit. Handles management + /// of the object stack, functions and registered types. class Dispatch_Engine { public: @@ -437,12 +426,12 @@ namespace chaiscript void add(const Boxed_Value &obj, const std::string &name) { validate_object_name(name); - StackData &stack = get_stack_data(); + auto &stack = get_stack_data(); - for (int i = static_cast(stack.size())-1; i >= 0; --i) + for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem) { - const auto itr = stack[i].find(name); - if (itr != stack[i].end()) + auto itr = stack_elem->find(name); + if (itr != stack_elem->end()) { itr->second = std::move(obj); return; @@ -456,15 +445,15 @@ namespace chaiscript /// Adds a named object to the current scope void add_object(const std::string &name, const Boxed_Value &obj) const { - StackData &stack = get_stack_data(); + auto &stack = get_stack_data(); validate_object_name(name); - const Scope &scope = stack.back(); + auto &scope = stack.back(); if (scope.find(name) != scope.end()) { throw chaiscript::exception::name_conflict_error(name); } else { - stack.back().insert(std::make_pair(name, std::move(obj))); + scope.insert(std::make_pair(name, obj)); } } @@ -546,13 +535,13 @@ namespace chaiscript return m_place_holder; } - StackData &stack = get_stack_data(); + auto &stack = get_stack_data(); // Is it in the stack? - for (int i = static_cast(stack.size())-1; i >= 0; --i) + for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem) { - const auto stackitr = stack[i].find(name); - if (stackitr != stack[i].end()) + const auto stackitr = stack_elem->find(name); + if (stackitr != stack_elem->end()) { return stackitr->second; } @@ -672,7 +661,7 @@ namespace chaiscript /// the current scope. std::map get_parent_locals() const { - StackData &stack = get_stack_data(); + auto &stack = get_stack_data(); if (stack.size() > 1) { return stack[1]; @@ -684,8 +673,8 @@ namespace chaiscript /// \returns All values in the local thread state, added through the add() function std::map get_locals() const { - StackData &stack = get_stack_data(); - Scope &scope = stack.front(); + auto &stack = get_stack_data(); + auto &scope = stack.front(); return scope; } @@ -696,8 +685,8 @@ namespace chaiscript /// Any existing locals are removed and the given set of variables is added void set_locals(const std::map &t_locals) { - StackData &stack = get_stack_data(); - Scope &scope = stack.front(); + auto &stack = get_stack_data(); + auto &scope = stack.front(); scope = t_locals; } @@ -850,7 +839,7 @@ namespace chaiscript /// (the function) with the remaining parameters as its arguments. Boxed_Value call_exists(const std::vector ¶ms) { - if (params.size() < 1) + if (params.empty()) { throw chaiscript::exception::arity_error(static_cast(params.size()), 1); } @@ -984,17 +973,17 @@ namespace chaiscript static bool function_less_than(const Proxy_Function &lhs, const Proxy_Function &rhs) { - const std::vector &lhsparamtypes = lhs->get_param_types(); - const std::vector &rhsparamtypes = rhs->get_param_types(); + const auto &lhsparamtypes = lhs->get_param_types(); + const auto &rhsparamtypes = rhs->get_param_types(); - const size_t lhssize = lhsparamtypes.size(); - const size_t rhssize = rhsparamtypes.size(); + const auto lhssize = lhsparamtypes.size(); + const auto rhssize = rhsparamtypes.size(); - const Type_Info boxed_type = user_type(); - const Type_Info boxed_pod_type = user_type(); + CHAISCRIPT_CONSTEXPR auto boxed_type = user_type(); + CHAISCRIPT_CONSTEXPR auto boxed_pod_type = user_type(); - std::shared_ptr dynamic_lhs(std::dynamic_pointer_cast(lhs)); - std::shared_ptr dynamic_rhs(std::dynamic_pointer_cast(rhs)); + auto dynamic_lhs(std::dynamic_pointer_cast(lhs)); + auto dynamic_rhs(std::dynamic_pointer_cast(rhs)); if (dynamic_lhs && dynamic_rhs) { diff --git a/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp b/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp index 359d0a5..184246e 100644 --- a/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp +++ b/include/chaiscript/dispatchkit/dynamic_cast_conversion.hpp @@ -229,17 +229,12 @@ namespace chaiscript std::set >::const_iterator find( const Type_Info &base, const Type_Info &derived) const { - for (auto itr = m_conversions.begin(); - itr != m_conversions.end(); - ++itr) - { - if ((*itr)->base().bare_equal(base) && (*itr)->derived().bare_equal(derived)) - { - return itr; - } - } - - return m_conversions.end(); + return std::find_if(m_conversions.begin(), m_conversions.end(), + [&base, &derived](const std::shared_ptr &conversion) + { + return conversion->base().bare_equal(base) && conversion->derived().bare_equal(derived); + } + ); } std::set > get_conversions() const diff --git a/include/chaiscript/dispatchkit/dynamic_object.hpp b/include/chaiscript/dispatchkit/dynamic_object.hpp index beb1c8e..5c93240 100644 --- a/include/chaiscript/dispatchkit/dynamic_object.hpp +++ b/include/chaiscript/dispatchkit/dynamic_object.hpp @@ -64,11 +64,9 @@ namespace chaiscript namespace detail { - /** - * A Proxy_Function implementation designed for calling a function - * that is automatically guarded based on the first param based on the - * param's type name - */ + /// A Proxy_Function implementation designed for calling a function + /// that is automatically guarded based on the first param based on the + /// param's type name class Dynamic_Object_Function : public Proxy_Function_Base { public: @@ -96,11 +94,11 @@ namespace chaiscript virtual ~Dynamic_Object_Function() {} Dynamic_Object_Function &operator=(const Dynamic_Object_Function) = delete; + Dynamic_Object_Function(Dynamic_Object_Function &) = delete; virtual bool operator==(const Proxy_Function_Base &f) const CHAISCRIPT_OVERRIDE { - const Dynamic_Object_Function *df = dynamic_cast(&f); - if (df) + if (const auto *df = dynamic_cast(&f)) { return df->m_type_name == m_type_name && (*df->m_func) == (*m_func); } else { @@ -164,7 +162,7 @@ namespace chaiscript } bool dynamic_object_typename_match(const Boxed_Value &bv, const std::string &name, - const std::shared_ptr &ti, const Dynamic_Cast_Conversions &t_conversions) const + const std::unique_ptr &ti, const Dynamic_Cast_Conversions &t_conversions) const { if (bv.get_type_info().bare_equal(m_doti)) { @@ -186,7 +184,7 @@ namespace chaiscript } bool dynamic_object_typename_match(const std::vector &bvs, const std::string &name, - const std::shared_ptr &ti, const Dynamic_Cast_Conversions &t_conversions) const + const std::unique_ptr &ti, const Dynamic_Cast_Conversions &t_conversions) const { if (bvs.size() > 0) { @@ -198,7 +196,7 @@ namespace chaiscript std::string m_type_name; Proxy_Function m_func; - std::shared_ptr m_ti; + std::unique_ptr m_ti; const Type_Info m_doti; @@ -252,8 +250,7 @@ namespace chaiscript virtual bool call_match(const std::vector &vals, const Dynamic_Cast_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE { - std::vector new_vals; - new_vals.push_back(Boxed_Value(Dynamic_Object(m_type_name))); + std::vector new_vals{Boxed_Value(Dynamic_Object(m_type_name))}; new_vals.insert(new_vals.end(), vals.begin(), vals.end()); return m_func->call_match(new_vals, t_conversions); @@ -274,9 +271,8 @@ namespace chaiscript protected: virtual Boxed_Value do_call(const std::vector ¶ms, const Dynamic_Cast_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE { - std::vector new_params; - chaiscript::Boxed_Value bv = var(Dynamic_Object(m_type_name)); - new_params.push_back(bv); + auto bv = var(Dynamic_Object(m_type_name)); + std::vector new_params{bv}; new_params.insert(new_params.end(), params.begin(), params.end()); (*m_func)(new_params, t_conversions); diff --git a/include/chaiscript/dispatchkit/function_call.hpp b/include/chaiscript/dispatchkit/function_call.hpp index 8b6c9ff..66ce0d6 100644 --- a/include/chaiscript/dispatchkit/function_call.hpp +++ b/include/chaiscript/dispatchkit/function_call.hpp @@ -28,14 +28,12 @@ namespace chaiscript { namespace dispatch { - /** - * Build a function caller that knows how to dispatch on a set of functions - * example: - * std::function f = - * build_function_caller(dispatchkit.get_function("print")); - * \returns A std::function object for dispatching - * \param[in] funcs the set of functions to dispatch on. - */ + /// Build a function caller that knows how to dispatch on a set of functions + /// example: + /// std::function f = + /// build_function_caller(dispatchkit.get_function("print")); + /// \returns A std::function object for dispatching + /// \param[in] funcs the set of functions to dispatch on. template std::function functor(const std::vector &funcs, const Dynamic_Cast_Conversions *t_conversions) @@ -44,32 +42,26 @@ namespace chaiscript return detail::build_function_caller_helper(p, funcs, t_conversions); } - /** - * Build a function caller for a particular Proxy_Function object - * useful in the case that a function is being pass out from scripting back - * into code - * example: - * void my_function(Proxy_Function f) - * { - * std::function local_f = - * build_function_caller(f); - * } - * \returns A std::function object for dispatching - * \param[in] func A function to execute. - */ + /// Build a function caller for a particular Proxy_Function object + /// useful in the case that a function is being pass out from scripting back + /// into code + /// example: + /// void my_function(Proxy_Function f) + /// { + /// std::function local_f = + /// build_function_caller(f); + /// } + /// \returns A std::function object for dispatching + /// \param[in] func A function to execute. template std::function functor(Const_Proxy_Function func, const Dynamic_Cast_Conversions *t_conversions) { - std::vector funcs; - funcs.push_back(func); - return functor(funcs, t_conversions); + return functor(std::vector({func}), t_conversions); } - /** - * Helper for automatically unboxing a Boxed_Value that contains a function object - * and creating a typesafe C++ function caller from it. - */ + /// Helper for automatically unboxing a Boxed_Value that contains a function object + /// and creating a typesafe C++ function caller from it. template std::function functor(const Boxed_Value &bv, const Dynamic_Cast_Conversions *t_conversions) @@ -79,9 +71,7 @@ namespace chaiscript } namespace detail{ - /** - * Cast helper to handle automatic casting to const std::function & - */ + /// Cast helper to handle automatic casting to const std::function & template struct Cast_Helper &> { @@ -98,9 +88,7 @@ namespace chaiscript } }; - /** - * Cast helper to handle automatic casting to std::function - */ + /// Cast helper to handle automatic casting to std::function template struct Cast_Helper > { @@ -117,9 +105,7 @@ namespace chaiscript } }; - /** - * Cast helper to handle automatic casting to const std::function - */ + /// Cast helper to handle automatic casting to const std::function template struct Cast_Helper > { diff --git a/include/chaiscript/dispatchkit/function_call_detail.hpp b/include/chaiscript/dispatchkit/function_call_detail.hpp index e86bb25..f3c012b 100644 --- a/include/chaiscript/dispatchkit/function_call_detail.hpp +++ b/include/chaiscript/dispatchkit/function_call_detail.hpp @@ -26,10 +26,8 @@ namespace chaiscript { namespace detail { - /** - * Internal helper class for handling the return - * value of a build_function_caller - */ + /// Internal helper class for handling the return + /// value of a build_function_caller template struct Function_Caller_Ret {