Various code cleanups

This commit is contained in:
Jason Turner
2015-01-31 10:10:35 -07:00
parent 76ac7c36fe
commit 722e9ed3d1
5 changed files with 76 additions and 108 deletions

View File

@@ -187,9 +187,7 @@ namespace chaiscript
class Bootstrap class Bootstrap
{ {
private: 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) static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
{ {
if (lhs.is_undef()) if (lhs.is_undef())
@@ -262,7 +260,7 @@ namespace chaiscript
Const_Proxy_Function f = boxed_cast<Const_Proxy_Function>(params[0]); Const_Proxy_Function f = boxed_cast<Const_Proxy_Function>(params[0]);
return Boxed_Value(Const_Proxy_Function(std::make_shared<dispatch::Bound_Function>(f, return Boxed_Value(Const_Proxy_Function(std::make_shared<dispatch::Bound_Function>(std::move(f),
std::vector<Boxed_Value>(params.begin() + 1, params.end())))); std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
} }
@@ -284,18 +282,13 @@ namespace chaiscript
static Const_Proxy_Function get_guard(const Const_Proxy_Function &t_pf) static Const_Proxy_Function get_guard(const Const_Proxy_Function &t_pf)
{ {
auto pf = std::dynamic_pointer_cast<const dispatch::Dynamic_Proxy_Function>(t_pf); const auto pf = std::dynamic_pointer_cast<const dispatch::Dynamic_Proxy_Function>(t_pf);
if (pf) if (pf && pf->get_guard())
{
if (pf->get_guard())
{ {
return pf->get_guard(); return pf->get_guard();
} else { } else {
throw std::runtime_error("Function does not have a guard"); throw std::runtime_error("Function does not have a guard");
} }
} else {
throw std::runtime_error("Function does not have a guard");
}
} }
static void throw_exception(const Boxed_Value &bv) { static void throw_exception(const Boxed_Value &bv) {
@@ -337,32 +330,24 @@ namespace chaiscript
static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) static bool has_parse_tree(const chaiscript::Const_Proxy_Function &t_pf)
{ {
if (auto pf = std::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf)) const auto pf = std::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf);
{ if (pf && pf->get_parse_tree())
if (pf->get_parse_tree())
{ {
return true; return true;
} else { } else {
return false; return false;
} }
} else {
return false;
}
} }
static chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf) static chaiscript::AST_NodePtr get_parse_tree(const chaiscript::Const_Proxy_Function &t_pf)
{ {
if (auto pf = std::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf)) const auto pf = std::dynamic_pointer_cast<const chaiscript::dispatch::Dynamic_Proxy_Function>(t_pf);
{ if (pf && pf->get_parse_tree())
if (pf->get_parse_tree())
{ {
return pf->get_parse_tree(); return pf->get_parse_tree();
} else { } else {
throw std::runtime_error("Function does not have a parse tree"); throw std::runtime_error("Function does not have a parse tree");
} }
} else {
throw std::runtime_error("Function does not have a parse tree");
}
} }
template<typename Function> template<typename Function>

View File

@@ -229,7 +229,6 @@ namespace chaiscript
std::advance(itr, pos); std::advance(itr, pos);
container.erase(itr); container.erase(itr);
} }
} }
template<typename ContainerType> template<typename ContainerType>
@@ -303,15 +302,15 @@ namespace chaiscript
template<typename ContainerType> template<typename ContainerType>
ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module())) ModulePtr sequence_type(const std::string &/*type*/, ModulePtr m = ModulePtr(new Module()))
{ {
std::string insert_name; m->add(fun(&detail::insert_at<ContainerType>),
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) [](){
{ if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
insert_name = "insert_ref_at"; return "insert_ref_at";
} else { } else {
insert_name = "insert_at"; return "insert_at";
} }
}());
m->add(fun(&detail::insert_at<ContainerType>), insert_name);
m->add(fun(&detail::erase_at<ContainerType>), "erase_at"); m->add(fun(&detail::erase_at<ContainerType>), "erase_at");
return m; return m;
@@ -327,16 +326,17 @@ namespace chaiscript
m->add(fun(static_cast<backptr>(&ContainerType::back)), "back"); m->add(fun(static_cast<backptr>(&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 &); typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &);
m->add(fun(static_cast<push_back>(&ContainerType::push_back)), push_back_name); m->add(fun(static_cast<push_back>(&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"); m->add(fun(&ContainerType::pop_back), "pop_back");
return m; return m;
} }
@@ -356,15 +356,15 @@ namespace chaiscript
m->add(fun(static_cast<frontptr>(&ContainerType::front)), "front"); m->add(fun(static_cast<frontptr>(&ContainerType::front)), "front");
m->add(fun(static_cast<constfrontptr>(&ContainerType::front)), "front"); m->add(fun(static_cast<constfrontptr>(&ContainerType::front)), "front");
std::string push_front_name; m->add(fun(static_cast<pushptr>(&ContainerType::push_front)),
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) [](){
{ if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
push_front_name = "push_front_ref"; return "push_front_ref";
} else { } else {
push_front_name = "push_front"; return "push_front";
} }
}());
m->add(fun(static_cast<pushptr>(&ContainerType::push_front)), push_front_name);
m->add(fun(static_cast<popptr>(&ContainerType::pop_front)), "pop_front"); m->add(fun(static_cast<popptr>(&ContainerType::pop_front)), "pop_front");
return m; return m;
} }
@@ -417,15 +417,16 @@ namespace chaiscript
m->add(fun(&detail::insert<ContainerType>), "insert"); m->add(fun(&detail::insert<ContainerType>), "insert");
std::string insert_name; m->add(fun(&detail::insert_ref<ContainerType>),
if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) [](){
{ if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) {
insert_name = "insert_ref"; return "insert_ref";
} else { } else {
insert_name = "insert"; return "insert";
} }
}());
m->add(fun(&detail::insert_ref<ContainerType>), insert_name);
return m; return m;
} }
@@ -536,14 +537,15 @@ namespace chaiscript
input_range_type<String>(type, m); input_range_type<String>(type, m);
//Special case: add push_back to string (which doesn't support other back_insertion operations //Special case: add push_back to string (which doesn't support other back_insertion operations
std::string push_back_name; m->add(fun(&String::push_back),
if (typeid(typename String::value_type) == typeid(Boxed_Value)) [](){
{ if (typeid(typename String::value_type) == typeid(Boxed_Value)) {
push_back_name = "push_back_ref"; return "push_back_ref";
} else { } else {
push_back_name = "push_back"; return "push_back";
} }
m->add(fun(&String::push_back), push_back_name); }());
typedef std::function<size_t (const String *, const String &, size_t)> find_func; typedef std::function<size_t (const String *, const String &, size_t)> find_func;
@@ -581,9 +583,6 @@ namespace chaiscript
return m; return m;
} }
} }
} }
} }

View File

@@ -147,8 +147,7 @@ namespace chaiscript
Boxed_Value operator()(const std::vector<Boxed_Value> &params, const chaiscript::Type_Conversions &t_conversions) const Boxed_Value operator()(const std::vector<Boxed_Value> &params, const chaiscript::Type_Conversions &t_conversions) const
{ {
Boxed_Value bv = do_call(params, t_conversions); return do_call(params, t_conversions);
return bv;
} }
/// Returns a vector containing all of the types of the parameters the function returns/takes /// 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) if (m_arity < 0)
{ {
return true; return true;
} else if (size_t(m_arity) == vals.size()) { } else if (static_cast<size_t>(m_arity) == vals.size()) {
if (m_arity == 0) if (m_arity == 0)
{ {
return true; return true;
@@ -234,16 +233,14 @@ namespace chaiscript
virtual bool compare_first_type(const Boxed_Value &bv, const Type_Conversions &t_conversions) const virtual bool compare_first_type(const Boxed_Value &bv, const Type_Conversions &t_conversions) const
{ {
const std::vector<Type_Info> &types = get_param_types(); const auto &types = get_param_types();
if (types.size() < 2) if (types.size() < 2)
{ {
return true; return true;
} }
const Type_Info &ti = types[1]; return compare_type_to_param(types[1], bv, t_conversions);
return compare_type_to_param(ti, bv, t_conversions);
} }
static bool compare_types(const std::vector<Type_Info> &tis, const std::vector<Boxed_Value> &bvs) static bool compare_types(const std::vector<Type_Info> &tis, const std::vector<Boxed_Value> &bvs)
@@ -252,7 +249,7 @@ namespace chaiscript
{ {
return false; return false;
} else { } else {
size_t size = bvs.size(); const size_t size = bvs.size();
for (size_t i = 0; i < size; ++i) 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() )) 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<Boxed_Value (const std::vector<Boxed_Value> &)> m_f; std::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
}; };
/** /// An object used by Bound_Function to represent "_" parameters
* An object used by Bound_Function to represent "_" parameters /// of a binding. This allows for unbound parameters during bind.
* of a binding. This allows for unbound parameters during bind.
*/
struct Placeholder_Object struct Placeholder_Object
{ {
}; };
/** /// An implementation of Proxy_Function that takes a Proxy_Function
* An implementation of Proxy_Function that takes a Proxy_Function /// and substitutes bound parameters into the parameter list
* and substitutes bound parameters into the parameter list /// at runtime, when call() is executed.
* at runtime, when call() is executed. /// it is used for bind(function, param1, _, param2) style calls
* it is used for bind(function, param1, _, param2) style calls
*/
class Bound_Function : public Proxy_Function_Base class Bound_Function : public Proxy_Function_Base
{ {
public: public:
@@ -451,9 +444,7 @@ namespace chaiscript
virtual std::vector<Const_Proxy_Function> get_contained_functions() const CHAISCRIPT_OVERRIDE virtual std::vector<Const_Proxy_Function> get_contained_functions() const CHAISCRIPT_OVERRIDE
{ {
std::vector<Const_Proxy_Function> fs; return std::vector<Const_Proxy_Function>{m_f};
fs.push_back(m_f);
return fs;
} }
@@ -504,8 +495,7 @@ namespace chaiscript
std::vector<Type_Info> types = t_f->get_param_types(); std::vector<Type_Info> types = t_f->get_param_types();
assert(types.size() == t_args.size() + 1); assert(types.size() == t_args.size() + 1);
std::vector<Type_Info> retval; std::vector<Type_Info> retval{types[0]};
retval.push_back(types[0]);
for (size_t i = 0; i < types.size()-1; ++i) for (size_t i = 0; i < types.size()-1; ++i)
{ {
if (t_args[i].get_type_info() == chaiscript::detail::Get_Type_Info<Placeholder_Object>::get()) if (t_args[i].get_type_info() == chaiscript::detail::Get_Type_Info<Placeholder_Object>::get())
@@ -544,7 +534,7 @@ namespace chaiscript
virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE virtual bool call_match(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const CHAISCRIPT_OVERRIDE
{ {
if (int(vals.size()) != get_arity()) if (static_cast<int>(vals.size()) != get_arity())
{ {
return false; return false;
} }
@@ -555,11 +545,9 @@ namespace chaiscript
virtual bool compare_types_with_cast(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const = 0; virtual bool compare_types_with_cast(const std::vector<Boxed_Value> &vals, const Type_Conversions &t_conversions) const = 0;
}; };
/** /// The standard typesafe function call implementation of Proxy_Function
* The standard typesafe function call implementation of Proxy_Function /// It takes a std::function<> object and performs runtime
* It takes a std::function<> object and performs runtime /// type checking of Boxed_Value parameters, in a type safe manner
* type checking of Boxed_Value parameters, in a type safe manner
*/
template<typename Func> template<typename Func>
class Proxy_Function_Impl : public Proxy_Function_Impl_Base class Proxy_Function_Impl : public Proxy_Function_Impl_Base
{ {
@@ -598,9 +586,7 @@ namespace chaiscript
Func *m_dummy_func; Func *m_dummy_func;
}; };
/** /// Attribute getter Proxy_Function implementation
* Attribute getter Proxy_Function implementation
*/
template<typename T, typename Class> template<typename T, typename Class>
class Attribute_Access : public Proxy_Function_Base class Attribute_Access : public Proxy_Function_Base
{ {

View File

@@ -129,9 +129,7 @@ namespace chaiscript
namespace detail namespace detail
{ {
/** /// Helper used to create a Type_Info object
* Helper used to create a Type_Info object
*/
template<typename T> template<typename T>
struct Get_Type_Info struct Get_Type_Info
{ {