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