From 722e9ed3d1eb96c09b272820c0dc439eac24df99 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sat, 31 Jan 2015 10:10:35 -0700 Subject: [PATCH] Various code cleanups --- include/chaiscript/dispatchkit/bootstrap.hpp | 37 +++----- .../chaiscript/dispatchkit/bootstrap_stl.hpp | 89 +++++++++---------- .../dispatchkit/proxy_functions.hpp | 50 ++++------- .../dispatchkit/register_function.hpp | 4 +- include/chaiscript/dispatchkit/type_info.hpp | 4 +- 5 files changed, 76 insertions(+), 108 deletions(-) diff --git a/include/chaiscript/dispatchkit/bootstrap.hpp b/include/chaiscript/dispatchkit/bootstrap.hpp index 6537ffd..f7177f4 100644 --- a/include/chaiscript/dispatchkit/bootstrap.hpp +++ b/include/chaiscript/dispatchkit/bootstrap.hpp @@ -187,9 +187,7 @@ namespace chaiscript class Bootstrap { private: - /** - * Function allowing for assignment of an unknown type to any other value - */ + /// Function allowing for assignment of an unknown type to any other value static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs) { if (lhs.is_undef()) @@ -262,7 +260,7 @@ namespace chaiscript Const_Proxy_Function f = boxed_cast(params[0]); - return Boxed_Value(Const_Proxy_Function(std::make_shared(f, + return Boxed_Value(Const_Proxy_Function(std::make_shared(std::move(f), std::vector(params.begin() + 1, params.end())))); } @@ -284,15 +282,10 @@ namespace chaiscript static Const_Proxy_Function get_guard(const Const_Proxy_Function &t_pf) { - auto pf = std::dynamic_pointer_cast(t_pf); - if (pf) + const auto pf = std::dynamic_pointer_cast(t_pf); + if (pf && pf->get_guard()) { - if (pf->get_guard()) - { - return pf->get_guard(); - } else { - throw std::runtime_error("Function does not have a guard"); - } + return pf->get_guard(); } else { throw std::runtime_error("Function does not have a guard"); } @@ -337,14 +330,10 @@ namespace chaiscript static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) { - if (auto pf = std::dynamic_pointer_cast(t_pf)) + const auto pf = std::dynamic_pointer_cast(t_pf); + if (pf && pf->get_parse_tree()) { - if (pf->get_parse_tree()) - { - return true; - } else { - return false; - } + return true; } else { return false; } @@ -352,14 +341,10 @@ namespace chaiscript static chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) { - if (auto pf = std::dynamic_pointer_cast(t_pf)) + const auto pf = std::dynamic_pointer_cast(t_pf); + if (pf && pf->get_parse_tree()) { - if (pf->get_parse_tree()) - { - return pf->get_parse_tree(); - } else { - throw std::runtime_error("Function does not have a parse tree"); - } + return pf->get_parse_tree(); } else { throw std::runtime_error("Function does not have a parse tree"); } diff --git a/include/chaiscript/dispatchkit/bootstrap_stl.hpp b/include/chaiscript/dispatchkit/bootstrap_stl.hpp index da3aacf..f6efe6e 100644 --- a/include/chaiscript/dispatchkit/bootstrap_stl.hpp +++ b/include/chaiscript/dispatchkit/bootstrap_stl.hpp @@ -228,8 +228,7 @@ namespace chaiscript std::advance(itr, pos); container.erase(itr); - } - + } } template @@ -303,15 +302,15 @@ namespace chaiscript template ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) { - std::string insert_name; - if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) - { - insert_name = "insert_ref_at"; - } else { - insert_name = "insert_at"; - } + m->add(fun(&detail::insert_at), + [](){ + if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { + return "insert_ref_at"; + } else { + return "insert_at"; + } + }()); - m->add(fun(&detail::insert_at), insert_name); m->add(fun(&detail::erase_at), "erase_at"); return m; @@ -327,16 +326,17 @@ namespace chaiscript m->add(fun(static_cast(&ContainerType::back)), "back"); - std::string push_back_name; - if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) - { - push_back_name = "push_back_ref"; - } else { - push_back_name = "push_back"; - } typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &); - m->add(fun(static_cast(&ContainerType::push_back)), push_back_name); + m->add(fun(static_cast(&ContainerType::push_back)), + [](){ + if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { + return "push_back_ref"; + } else { + return "push_back"; + } + }()); + m->add(fun(&ContainerType::pop_back), "pop_back"); return m; } @@ -356,15 +356,15 @@ namespace chaiscript m->add(fun(static_cast(&ContainerType::front)), "front"); m->add(fun(static_cast(&ContainerType::front)), "front"); - std::string push_front_name; - if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) - { - push_front_name = "push_front_ref"; - } else { - push_front_name = "push_front"; - } + m->add(fun(static_cast(&ContainerType::push_front)), + [](){ + if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) { + return "push_front_ref"; + } else { + return "push_front"; + } + }()); - m->add(fun(static_cast(&ContainerType::push_front)), push_front_name); m->add(fun(static_cast(&ContainerType::pop_front)), "pop_front"); return m; } @@ -417,15 +417,16 @@ namespace chaiscript m->add(fun(&detail::insert), "insert"); - std::string insert_name; - if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) - { - insert_name = "insert_ref"; - } else { - insert_name = "insert"; - } + m->add(fun(&detail::insert_ref), + [](){ + if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) { + return "insert_ref"; + } else { + return "insert"; + } + }()); + - m->add(fun(&detail::insert_ref), insert_name); return m; } @@ -536,14 +537,15 @@ namespace chaiscript input_range_type(type, m); //Special case: add push_back to string (which doesn't support other back_insertion operations - std::string push_back_name; - if (typeid(typename String::value_type) == typeid(Boxed_Value)) - { - push_back_name = "push_back_ref"; - } else { - push_back_name = "push_back"; - } - m->add(fun(&String::push_back), push_back_name); + m->add(fun(&String::push_back), + [](){ + if (typeid(typename String::value_type) == typeid(Boxed_Value)) { + return "push_back_ref"; + } else { + return "push_back"; + } + }()); + typedef std::function find_func; @@ -581,9 +583,6 @@ namespace chaiscript return m; } - - - } } } diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index 639c981..7c3c0a4 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -147,8 +147,7 @@ namespace chaiscript Boxed_Value operator()(const std::vector ¶ms, const chaiscript::Type_Conversions &t_conversions) const { - Boxed_Value bv = do_call(params, t_conversions); - return bv; + return do_call(params, t_conversions); } /// Returns a vector containing all of the types of the parameters the function returns/takes @@ -177,7 +176,7 @@ namespace chaiscript if (m_arity < 0) { return true; - } else if (size_t(m_arity) == vals.size()) { + } else if (static_cast(m_arity) == vals.size()) { if (m_arity == 0) { return true; @@ -234,16 +233,14 @@ namespace chaiscript virtual bool compare_first_type(const Boxed_Value &bv, const Type_Conversions &t_conversions) const { - const std::vector &types = get_param_types(); + const auto &types = get_param_types(); if (types.size() < 2) { return true; } - const Type_Info &ti = types[1]; - return compare_type_to_param(ti, bv, t_conversions); - + return compare_type_to_param(types[1], bv, t_conversions); } static bool compare_types(const std::vector &tis, const std::vector &bvs) @@ -252,7 +249,7 @@ namespace chaiscript { return false; } else { - size_t size = bvs.size(); + const size_t size = bvs.size(); for (size_t i = 0; i < size; ++i) { if (!(tis[i+1].bare_equal(bvs[i].get_type_info()) && tis[i+1].is_const() >= bvs[i].get_type_info().is_const() )) @@ -412,20 +409,16 @@ namespace chaiscript std::function &)> m_f; }; - /** - * An object used by Bound_Function to represent "_" parameters - * of a binding. This allows for unbound parameters during bind. - */ + /// An object used by Bound_Function to represent "_" parameters + /// of a binding. This allows for unbound parameters during bind. struct Placeholder_Object { }; - /** - * An implementation of Proxy_Function that takes a Proxy_Function - * and substitutes bound parameters into the parameter list - * at runtime, when call() is executed. - * it is used for bind(function, param1, _, param2) style calls - */ + /// An implementation of Proxy_Function that takes a Proxy_Function + /// and substitutes bound parameters into the parameter list + /// at runtime, when call() is executed. + /// it is used for bind(function, param1, _, param2) style calls class Bound_Function : public Proxy_Function_Base { public: @@ -451,9 +444,7 @@ namespace chaiscript virtual std::vector get_contained_functions() const CHAISCRIPT_OVERRIDE { - std::vector fs; - fs.push_back(m_f); - return fs; + return std::vector{m_f}; } @@ -504,8 +495,7 @@ namespace chaiscript std::vector types = t_f->get_param_types(); assert(types.size() == t_args.size() + 1); - std::vector retval; - retval.push_back(types[0]); + std::vector retval{types[0]}; for (size_t i = 0; i < types.size()-1; ++i) { if (t_args[i].get_type_info() == chaiscript::detail::Get_Type_Info::get()) @@ -544,7 +534,7 @@ namespace chaiscript virtual bool call_match(const std::vector &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE { - if (int(vals.size()) != get_arity()) + if (static_cast(vals.size()) != get_arity()) { return false; } @@ -555,11 +545,9 @@ namespace chaiscript virtual bool compare_types_with_cast(const std::vector &vals, const Type_Conversions &t_conversions) const = 0; }; - /** - * The standard typesafe function call implementation of Proxy_Function - * It takes a std::function<> object and performs runtime - * type checking of Boxed_Value parameters, in a type safe manner - */ + /// The standard typesafe function call implementation of Proxy_Function + /// It takes a std::function<> object and performs runtime + /// type checking of Boxed_Value parameters, in a type safe manner template class Proxy_Function_Impl : public Proxy_Function_Impl_Base { @@ -598,9 +586,7 @@ namespace chaiscript Func *m_dummy_func; }; - /** - * Attribute getter Proxy_Function implementation - */ + /// Attribute getter Proxy_Function implementation template class Attribute_Access : public Proxy_Function_Base { diff --git a/include/chaiscript/dispatchkit/register_function.hpp b/include/chaiscript/dispatchkit/register_function.hpp index 678f0a8..1ed8ebf 100644 --- a/include/chaiscript/dispatchkit/register_function.hpp +++ b/include/chaiscript/dispatchkit/register_function.hpp @@ -62,7 +62,7 @@ namespace chaiscript /// \todo is it possible to reduce the number of templates generated here? return Proxy_Function( new Proxy_Function_Impl::Signature>(to_function(t))); - } + } }; template<> @@ -171,7 +171,7 @@ namespace chaiscript { return fun(detail::bind_first(detail::bind_first(t, q), r)); } - + } diff --git a/include/chaiscript/dispatchkit/type_info.hpp b/include/chaiscript/dispatchkit/type_info.hpp index eaa791d..30a2853 100644 --- a/include/chaiscript/dispatchkit/type_info.hpp +++ b/include/chaiscript/dispatchkit/type_info.hpp @@ -129,9 +129,7 @@ namespace chaiscript namespace detail { - /** - * Helper used to create a Type_Info object - */ + /// Helper used to create a Type_Info object template struct Get_Type_Info {