Move dispatch kit into new include tree
This commit is contained in:
580
include/chaiscript/dispatchkit/bootstrap.hpp
Normal file
580
include/chaiscript/dispatchkit/bootstrap.hpp
Normal file
@@ -0,0 +1,580 @@
|
||||
#ifndef __bootstrap_hpp
|
||||
#define __bootstrap_hpp__
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
#include "register_function.hpp"
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
/**
|
||||
* Set of helper functions for common operators
|
||||
*/
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret add(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 + p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret subtract(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 - p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret divide(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 / p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret multiply(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 * p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret modulus(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 % p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &assign(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 = p2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 == p2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool not_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 != p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 < p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 > p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 <= p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 >= p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ×equal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 *= p2);
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ÷sequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 /= p2);
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &addsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 += p2);
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &subtractsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 -= p2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixincrement(P1 &p1)
|
||||
{
|
||||
return (++p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixdecrement(P1 &p1)
|
||||
{
|
||||
return (--p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixnegate(P1 &p1)
|
||||
{
|
||||
return (p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixnot(P1 &p1)
|
||||
{
|
||||
return (p1);
|
||||
}
|
||||
|
||||
/* Special helpers for generating generic "POD" type operators
|
||||
* The POD operators are needed for general support of C++ POD
|
||||
* types without iterating out all possible combinations of operators
|
||||
* (<, >, +, +=, *=, \=, -, <=, >=, ==) and types
|
||||
* (char, uint8_t, int8_t, uint16_t, int16_t...)
|
||||
*/
|
||||
template<typename P1>
|
||||
P1 &assign_pod(P1 &p1, Boxed_POD_Value v)
|
||||
{
|
||||
if (v.m_isfloat)
|
||||
{
|
||||
return (p1 = P1(v.d));
|
||||
} else {
|
||||
return (p1 = P1(v.i));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 construct_pod(Boxed_POD_Value v)
|
||||
{
|
||||
if (v.m_isfloat)
|
||||
{
|
||||
return P1(v.d);
|
||||
} else {
|
||||
return P1(v.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ×equal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return p1 *= P1(r.d);
|
||||
} else {
|
||||
return p1 *= P1(r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return p1 /= P1(r.d);
|
||||
} else {
|
||||
return p1 /= P1(r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return p1 += P1(r.d);
|
||||
} else {
|
||||
return p1 += P1(r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return p1 -= P1(r.d);
|
||||
} else {
|
||||
return p1 -= P1(r.i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add canonical form of "=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<const T&, const T&>, "=");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "+" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_add(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<T, const T&, const T&>, "+");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "+=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_add_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &addsequal<T, T>, "+=");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "-" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_subtract(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &subtract<T, const T&, const T&>, "-");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "/" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_divide(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ÷<T, const T&, const T&>, "/");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_multiply(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &multiply<T, const T&, const T&>, "*");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_not_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ¬_equals<const T&, const T&>, "!=");
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void add_oper_assign_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign<T,U>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign<T,T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign_pod<T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &less_than<const T&, const T&>, "<");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_greater_than(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &greater_than<const T&, const T&>, ">");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &less_than_equals<const T&, const T&>, "<=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_greater_than_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &greater_than_equals<const T&, const T&>, ">=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T, typename R>
|
||||
void add_opers_comparison_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<const T&, const R&>, "==");
|
||||
register_function(s, ¬_equals<const T&, const R&>, "!=");
|
||||
register_function(s, &less_than<const T&, const R&>, "<");
|
||||
register_function(s, &greater_than<const T&, const R&>, ">");
|
||||
register_function(s, &less_than_equals<const T&, const R&>, "<=");
|
||||
register_function(s, &greater_than_equals<const T&, const R&>, ">=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_comparison(Dispatch_Engine &s)
|
||||
{
|
||||
add_opers_comparison_overload<T, T>(s);
|
||||
}
|
||||
|
||||
template<typename Ret, typename T, typename R>
|
||||
void add_opers_arithmetic_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<Ret, T, R>, "+");
|
||||
register_function(s, &subtract<Ret, T, R>, "-");
|
||||
register_function(s, ÷<Ret, T, R>, "/");
|
||||
register_function(s, &multiply<Ret, T, R>, "*");
|
||||
register_function(s, ×equal<T, R>, "*=");
|
||||
register_function(s, ÷sequal<T, R>, "/=");
|
||||
register_function(s, &subtractsequal<T, R>, "-=");
|
||||
register_function(s, &addsequal<T, R>, "+=");
|
||||
register_function(s, &prefixincrement<T>, "++");
|
||||
register_function(s, &prefixdecrement<T>, "--");
|
||||
register_function(s, &prefixnegate<T>, "-");
|
||||
register_function(s, &prefixnot<T>, "!");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic_modify_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ×equal_pod<T>, "*=");
|
||||
register_function(s, ÷sequal_pod<T>, "/=");
|
||||
register_function(s, &subtractsequal_pod<T>, "-=");
|
||||
register_function(s, &addsequal_pod<T>, "+=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_copy_constructor(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T, const T &>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), "clone");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_basic_constructors(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T>(), type);
|
||||
add_copy_constructor<T>(s, type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_construct_pod(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
register_function(s, &construct_pod<T>, type);
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void add_constructor_overload(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T, const U &>(), type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic(Dispatch_Engine &s)
|
||||
{
|
||||
add_opers_arithmetic_overload<T, T, T>(s);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Built in to_string operator
|
||||
template<typename Input>
|
||||
std::string to_string(Input i)
|
||||
{
|
||||
return boost::lexical_cast<std::string>(i);
|
||||
}
|
||||
|
||||
template<> std::string to_string(bool b)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
return "true";
|
||||
} else {
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Input>
|
||||
Input parse_string(const std::string &i)
|
||||
{
|
||||
return boost::lexical_cast<Input>(i);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename T>
|
||||
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
|
||||
{
|
||||
s.register_type<T>(name);
|
||||
add_basic_constructors<T>(s, name);
|
||||
add_oper_assign<T>(s);
|
||||
add_oper_assign_pod<T>(s);
|
||||
add_construct_pod<T>(s, name);
|
||||
add_opers_arithmetic<T>(s);
|
||||
add_opers_arithmetic_modify_pod<T>(s);
|
||||
register_function(s, &to_string<T>, "to_string");
|
||||
register_function(s, &parse_string<T>, "to_" + name);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
boost::shared_ptr<Type> shared_ptr_clone(boost::shared_ptr<Type> f)
|
||||
{
|
||||
return f;
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
Boxed_Value ptr_assign(Boxed_Value lhs, boost::shared_ptr<Type> rhs)
|
||||
{
|
||||
lhs.assign(Boxed_Value(rhs));
|
||||
|
||||
return lhs;
|
||||
}
|
||||
|
||||
|
||||
struct Bootstrap
|
||||
{
|
||||
static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
|
||||
{
|
||||
if (lhs.is_unknown())
|
||||
{
|
||||
return (lhs.assign(rhs));
|
||||
} else {
|
||||
throw bad_boxed_cast("boxed_value has a set type already");
|
||||
}
|
||||
}
|
||||
|
||||
static void print(const std::string &s)
|
||||
{
|
||||
std::cout << s;
|
||||
}
|
||||
|
||||
static void println(const std::string &s)
|
||||
{
|
||||
std::cout << s << std::endl;
|
||||
}
|
||||
|
||||
static void add_opers_comparison_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<Boxed_POD_Value, Boxed_POD_Value>, "==");
|
||||
register_function(s, ¬_equals<Boxed_POD_Value, Boxed_POD_Value>, "!=");
|
||||
register_function(s, &less_than<Boxed_POD_Value, Boxed_POD_Value>, "<");
|
||||
register_function(s, &greater_than<Boxed_POD_Value, Boxed_POD_Value>, ">");
|
||||
register_function(s, &less_than_equals<Boxed_POD_Value, Boxed_POD_Value>, "<=");
|
||||
register_function(s, &greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>, ">=");
|
||||
}
|
||||
|
||||
static void add_opers_arithmetic_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "+");
|
||||
register_function(s, &subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "-");
|
||||
register_function(s, ÷<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "/");
|
||||
register_function(s, &multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "*");
|
||||
}
|
||||
|
||||
static bool type_match(Boxed_Value l, Boxed_Value r)
|
||||
{
|
||||
return l.get_type_info() == r.get_type_info();
|
||||
}
|
||||
|
||||
static bool is_type(const Dispatch_Engine &e, const std::string &type_name, Boxed_Value r)
|
||||
{
|
||||
try {
|
||||
return e.get_type(type_name) == r.get_type_info();
|
||||
} catch (const std::range_error &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static Boxed_Value bind_function(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() < 2)
|
||||
{
|
||||
throw arity_error(params.size(), 2);
|
||||
}
|
||||
|
||||
boost::shared_ptr<Proxy_Function> f = boxed_cast<boost::shared_ptr<Proxy_Function> >(params[0]);
|
||||
|
||||
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Bound_Function(f,
|
||||
std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
|
||||
}
|
||||
|
||||
static Boxed_Value call_exists(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() < 1)
|
||||
{
|
||||
throw arity_error(params.size(), 1);
|
||||
}
|
||||
|
||||
boost::shared_ptr<Proxy_Function> f = boxed_cast<boost::shared_ptr<Proxy_Function> >(params[0]);
|
||||
|
||||
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
|
||||
}
|
||||
|
||||
static void bootstrap(Dispatch_Engine &s)
|
||||
{
|
||||
s.register_type<void>("void");
|
||||
s.register_type<bool>("bool");
|
||||
s.register_type<Boxed_Value>("Object");
|
||||
s.register_type<Boxed_POD_Value>("PODObject");
|
||||
s.register_type<Proxy_Function>("function");
|
||||
|
||||
add_basic_constructors<bool>(s, "bool");
|
||||
add_oper_assign<std::string>(s);
|
||||
add_oper_assign<bool>(s);
|
||||
|
||||
register_function(s, &to_string<const std::string &>, "internal_to_string");
|
||||
register_function(s, &to_string<bool>, "internal_to_string");
|
||||
register_function(s, &unknown_assign, "=");
|
||||
|
||||
bootstrap_pod_type<double>(s, "double");
|
||||
bootstrap_pod_type<int>(s, "int");
|
||||
bootstrap_pod_type<size_t>(s, "size_t");
|
||||
bootstrap_pod_type<char>(s, "char");
|
||||
bootstrap_pod_type<boost::int64_t>(s, "int64_t");
|
||||
|
||||
add_opers_comparison_pod(s);
|
||||
add_opers_arithmetic_pod(s);
|
||||
register_function(s, &modulus<int, int, int>, "%");
|
||||
|
||||
register_function(s, &print, "print_string");
|
||||
register_function(s, &println, "println_string");
|
||||
|
||||
s.register_function(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s))), "dump_system");
|
||||
s.register_function(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(s))), "dump_object");
|
||||
s.register_function(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1)),
|
||||
"is_type");
|
||||
|
||||
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
||||
"bind");
|
||||
|
||||
register_function(s, &shared_ptr_clone<Proxy_Function>, "clone");
|
||||
register_function(s, &ptr_assign<Proxy_Function>, "=");
|
||||
|
||||
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
|
||||
"call_exists");
|
||||
|
||||
register_function(s, &type_match, "type_match");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
289
include/chaiscript/dispatchkit/bootstrap_stl.hpp
Normal file
289
include/chaiscript/dispatchkit/bootstrap_stl.hpp
Normal file
@@ -0,0 +1,289 @@
|
||||
#ifndef __bootstrap_stl_hpp
|
||||
#define __bootstrap_stl_hpp__
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
#include "register_function.hpp"
|
||||
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename Container>
|
||||
struct Input_Range
|
||||
{
|
||||
Input_Range(Container &c)
|
||||
: m_begin(c.begin()), m_end(c.end())
|
||||
{
|
||||
}
|
||||
|
||||
Input_Range(typename Container::iterator itr)
|
||||
: m_begin(itr), m_end(itr)
|
||||
{
|
||||
}
|
||||
|
||||
Input_Range(const std::pair<typename Container::iterator, typename Container::iterator> &t_p)
|
||||
: m_begin(t_p.first), m_end(t_p.second)
|
||||
{
|
||||
}
|
||||
|
||||
bool empty() const
|
||||
{
|
||||
return m_begin == m_end;
|
||||
}
|
||||
|
||||
void pop_front()
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
throw std::range_error("Range empty");
|
||||
}
|
||||
++m_begin;
|
||||
}
|
||||
|
||||
typename std::iterator_traits<typename Container::iterator>::reference front() const
|
||||
{
|
||||
if (empty())
|
||||
{
|
||||
throw std::range_error("Range empty");
|
||||
}
|
||||
return *m_begin;
|
||||
}
|
||||
|
||||
typename Container::iterator m_begin;
|
||||
typename Container::iterator m_end;
|
||||
};
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<Input_Range<ContainerType> >(type+"_Range");
|
||||
system.register_type<typename ContainerType::iterator>(type+"_Iterator");
|
||||
|
||||
system.register_function(build_constructor<Input_Range<ContainerType>, ContainerType &>(), "range");
|
||||
system.register_function(build_constructor<Input_Range<ContainerType>,
|
||||
typename ContainerType::iterator>(), "range");
|
||||
|
||||
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair;
|
||||
|
||||
system.register_function(build_constructor<Input_Range<ContainerType>,
|
||||
const ItrPair &>(), "range");
|
||||
system.register_type<ItrPair>(type+"_Iterator_Pair");
|
||||
|
||||
|
||||
register_function(system, &Input_Range<ContainerType>::empty, "empty");
|
||||
register_function(system, &Input_Range<ContainerType>::pop_front, "pop_front");
|
||||
register_function(system, &Input_Range<ContainerType>::front, "front");
|
||||
system.register_function(build_constructor<Input_Range<ContainerType>, const Input_Range<ContainerType> &>(), "clone");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_reversible_container(Dispatch_Engine &/*system*/, const std::string &/*type*/)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_random_access_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_reversible_container<ContainerType>(system, type);
|
||||
|
||||
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
|
||||
|
||||
//In the interest of runtime safety for the system, we prefer the at() method for [] access,
|
||||
//to throw an exception in an out of bounds condition.
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at)), "[]");
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[])), "at");
|
||||
}
|
||||
|
||||
template<typename Assignable>
|
||||
void bootstrap_assignable(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
add_basic_constructors<Assignable>(system, type);
|
||||
add_oper_assign<Assignable>(system);
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_assignable<ContainerType>(system, type);
|
||||
|
||||
register_function(system, &ContainerType::size, "size");
|
||||
register_function(system, &ContainerType::max_size, "max_size");
|
||||
register_function(system, &ContainerType::empty, "empty");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_input_range<ContainerType>(system, type);
|
||||
bootstrap_container<ContainerType>(system, type);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_function(build_constructor<Type>(), type);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void insert_at(Type &container, int pos, const typename Type::value_type &v)
|
||||
{
|
||||
typename Type::iterator itr = container.begin();
|
||||
typename Type::iterator end = container.end();
|
||||
|
||||
if (pos < 0 || std::distance(itr, end) < pos)
|
||||
{
|
||||
throw std::range_error("Cannot insert past end of range");
|
||||
}
|
||||
|
||||
std::advance(itr, pos);
|
||||
container.insert(itr, v);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void erase_at(Type &container, int pos)
|
||||
{
|
||||
typename Type::iterator itr = container.begin();
|
||||
typename Type::iterator end = container.end();
|
||||
|
||||
if (pos < 0 || std::distance(itr, end) < (pos-1))
|
||||
{
|
||||
throw std::range_error("Cannot erase past end of range");
|
||||
}
|
||||
|
||||
std::advance(itr, pos);
|
||||
container.erase(itr);
|
||||
}
|
||||
|
||||
template<typename SequenceType>
|
||||
void bootstrap_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_forward_container<SequenceType>(system, type);
|
||||
bootstrap_default_constructible<SequenceType>(system, type);
|
||||
|
||||
std::string insert_name;
|
||||
if (typeid(typename SequenceType::value_type) == typeid(Boxed_Value))
|
||||
{
|
||||
insert_name = "insert_ref_at";
|
||||
} else {
|
||||
insert_name = "insert_at";
|
||||
}
|
||||
|
||||
register_function(system, &insert_at<SequenceType>, insert_name);
|
||||
register_function(system, &erase_at<SequenceType>, "erase_at");
|
||||
}
|
||||
|
||||
template<typename SequenceType>
|
||||
void bootstrap_back_insertion_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_sequence<SequenceType>(system, type);
|
||||
|
||||
|
||||
typedef typename SequenceType::reference (SequenceType::*backptr)();
|
||||
|
||||
system.register_function(boost::function<typename SequenceType::reference (SequenceType *)>(backptr(&SequenceType::back)), "back");
|
||||
|
||||
std::string push_back_name;
|
||||
if (typeid(typename SequenceType::value_type) == typeid(Boxed_Value))
|
||||
{
|
||||
push_back_name = "push_back_ref";
|
||||
} else {
|
||||
push_back_name = "push_back";
|
||||
}
|
||||
|
||||
register_function(system, &SequenceType::push_back, push_back_name);
|
||||
register_function(system, &SequenceType::pop_back, "pop_back");
|
||||
}
|
||||
|
||||
template<typename VectorType>
|
||||
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<VectorType>(type);
|
||||
bootstrap_random_access_container<VectorType>(system, type);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_forward_container<ContainerType>(system, type);
|
||||
bootstrap_default_constructible<ContainerType>(system, type);
|
||||
}
|
||||
|
||||
template<typename PairType>
|
||||
void bootstrap_pair(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<PairType>(type);
|
||||
|
||||
register_member(system, &PairType::first, "first");
|
||||
register_member(system, &PairType::second, "second");
|
||||
|
||||
system.register_function(build_constructor<PairType >(), type);
|
||||
system.register_function(build_constructor<PairType, const PairType &>(), type);
|
||||
system.register_function(build_constructor<PairType, const PairType &>(), "clone");
|
||||
system.register_function(build_constructor<PairType, const typename PairType::first_type &, const typename PairType::second_type &>(), type);
|
||||
}
|
||||
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_pair_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_associative_container<ContainerType>(system, type);
|
||||
bootstrap_pair<typename ContainerType::value_type>(system, type + "_Pair");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_associative_container<ContainerType>(system, type);
|
||||
register_function(system, &ContainerType::count, "count");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_sorted_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator>
|
||||
(ContainerType::*eq_range)(const typename ContainerType::key_type &);
|
||||
|
||||
bootstrap_reversible_container<ContainerType>(system, type);
|
||||
bootstrap_associative_container<ContainerType>(system, type);
|
||||
register_function(system, eq_range(&ContainerType::equal_range), "equal_range");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_unique_sorted_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_sorted_associative_container<ContainerType>(system, type);
|
||||
bootstrap_unique_associative_container<ContainerType>(system, type);
|
||||
}
|
||||
|
||||
template<typename MapType>
|
||||
void bootstrap_map(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<MapType>(type);
|
||||
register_function(system, &MapType::operator[], "[]");
|
||||
bootstrap_unique_sorted_associative_container<MapType>(system, type);
|
||||
bootstrap_pair_associative_container<MapType>(system, type);
|
||||
}
|
||||
|
||||
template<typename String>
|
||||
void bootstrap_string(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<String>(type);
|
||||
add_oper_add<String>(system);
|
||||
add_oper_add_equals<String>(system);
|
||||
add_opers_comparison<String>(system);
|
||||
bootstrap_random_access_container<String>(system, type);
|
||||
bootstrap_sequence<String>(system, type);
|
||||
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
|
||||
register_function(system, find_func(&String::find), "find");
|
||||
register_function(system, find_func(&String::rfind), "rfind");
|
||||
register_function(system, find_func(&String::find_first_of), "find_first_of");
|
||||
register_function(system, find_func(&String::find_last_of), "find_last_of");
|
||||
register_function(system, find_func(&String::find_first_not_of), "find_first_not_of");
|
||||
register_function(system, find_func(&String::find_last_not_of), "find_last_not_of");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
549
include/chaiscript/dispatchkit/boxed_value.hpp
Normal file
549
include/chaiscript/dispatchkit/boxed_value.hpp
Normal file
@@ -0,0 +1,549 @@
|
||||
#ifndef __boxed_value_hpp__
|
||||
#define __boxed_value_hpp__
|
||||
|
||||
#include "type_info.hpp"
|
||||
#include <map>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/any.hpp>
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/type_traits/add_const.hpp>
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
class Boxed_Value
|
||||
{
|
||||
public:
|
||||
struct Void_Type
|
||||
{
|
||||
};
|
||||
|
||||
private:
|
||||
struct Data
|
||||
{
|
||||
struct Shared_Ptr_Proxy
|
||||
{
|
||||
virtual ~Shared_Ptr_Proxy()
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool unique(boost::any *) = 0;
|
||||
virtual long use_count(boost::any *) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Shared_Ptr_Proxy_Impl : Shared_Ptr_Proxy
|
||||
{
|
||||
virtual ~Shared_Ptr_Proxy_Impl()
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool unique(boost::any *a)
|
||||
{
|
||||
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(a);
|
||||
return ptr->unique();
|
||||
}
|
||||
|
||||
virtual long use_count(boost::any *a)
|
||||
{
|
||||
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(a);
|
||||
return ptr->use_count();
|
||||
}
|
||||
};
|
||||
|
||||
Data(const Type_Info &ti,
|
||||
const boost::any &to,
|
||||
bool tr,
|
||||
const boost::shared_ptr<Shared_Ptr_Proxy> &t_proxy = boost::shared_ptr<Shared_Ptr_Proxy>())
|
||||
: m_type_info(ti), m_obj(to),
|
||||
m_is_ref(tr), m_ptr_proxy(t_proxy)
|
||||
{
|
||||
}
|
||||
|
||||
Data &operator=(const Data &rhs)
|
||||
{
|
||||
m_type_info = rhs.m_type_info;
|
||||
m_obj = rhs.m_obj;
|
||||
m_is_ref = rhs.m_is_ref;
|
||||
m_ptr_proxy = rhs.m_ptr_proxy;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
static bool get_false()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Type_Info m_type_info;
|
||||
boost::any m_obj;
|
||||
bool m_is_ref;
|
||||
boost::shared_ptr<Shared_Ptr_Proxy> m_ptr_proxy;
|
||||
};
|
||||
|
||||
struct Object_Cache
|
||||
{
|
||||
boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
|
||||
{
|
||||
return boost::shared_ptr<Data> (new Data(
|
||||
Get_Type_Info<void>::get(),
|
||||
boost::any(),
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(boost::shared_ptr<T> obj)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(obj),
|
||||
false,
|
||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||
);
|
||||
|
||||
std::map<void *, Data >::iterator itr
|
||||
= m_ptrs.find(obj.get());
|
||||
|
||||
if (itr != m_ptrs.end())
|
||||
{
|
||||
(*data) = (itr->second);
|
||||
} else {
|
||||
m_ptrs.insert(std::make_pair(obj.get(), *data));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(obj),
|
||||
true)
|
||||
);
|
||||
|
||||
std::map<void *, Data >::iterator itr
|
||||
= m_ptrs.find(obj.get_pointer());
|
||||
|
||||
if (itr != m_ptrs.end())
|
||||
{
|
||||
// std::cout << "Reference wrapper ptr found, using it" << std::endl;
|
||||
(*data) = (itr->second);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(const T& t)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(boost::shared_ptr<T>(new T(t))),
|
||||
false,
|
||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||
);
|
||||
|
||||
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(&data->m_obj);
|
||||
|
||||
m_ptrs.insert(std::make_pair(ptr->get(), *data));
|
||||
return data;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Data> get()
|
||||
{
|
||||
return boost::shared_ptr<Data> (new Data(
|
||||
Type_Info(),
|
||||
boost::any(),
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
void cull()
|
||||
{
|
||||
std::map<void *, Data >::iterator itr = m_ptrs.begin();
|
||||
|
||||
while (itr != m_ptrs.end())
|
||||
{
|
||||
if (itr->second.m_ptr_proxy->unique(&itr->second.m_obj) == 1)
|
||||
{
|
||||
std::map<void *, Data >::iterator todel = itr;
|
||||
// std::cout << "Releasing unique ptr " << std::endl;
|
||||
++itr;
|
||||
m_ptrs.erase(todel);
|
||||
} else {
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
// std::cout << "References held: " << m_ptrs.size() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::map<void *, Data > m_ptrs;
|
||||
};
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
explicit Boxed_Value(T t)
|
||||
: m_data(get_object_cache().get(t))
|
||||
{
|
||||
get_object_cache().cull();
|
||||
}
|
||||
|
||||
Boxed_Value(const Boxed_Value &t_so)
|
||||
: m_data(t_so.m_data)
|
||||
{
|
||||
}
|
||||
|
||||
Boxed_Value()
|
||||
: m_data(get_object_cache().get())
|
||||
{
|
||||
}
|
||||
|
||||
~Boxed_Value()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Object_Cache &get_object_cache()
|
||||
{
|
||||
static Object_Cache oc;
|
||||
return oc;
|
||||
}
|
||||
|
||||
|
||||
Boxed_Value assign(const Boxed_Value &rhs)
|
||||
{
|
||||
(*m_data) = (*rhs.m_data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Boxed_Value &operator=(const Boxed_Value &rhs)
|
||||
{
|
||||
m_data = rhs.m_data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Type_Info &get_type_info() const
|
||||
{
|
||||
return m_data->m_type_info;
|
||||
}
|
||||
|
||||
bool is_unknown() const
|
||||
{
|
||||
return m_data->m_type_info.m_is_unknown;
|
||||
}
|
||||
|
||||
boost::any get() const
|
||||
{
|
||||
return m_data->m_obj;
|
||||
}
|
||||
|
||||
bool is_ref() const
|
||||
{
|
||||
return m_data->m_is_ref;
|
||||
}
|
||||
|
||||
private:
|
||||
boost::shared_ptr<Data> m_data;
|
||||
};
|
||||
|
||||
|
||||
//cast_help specializations
|
||||
template<typename Result>
|
||||
struct Cast_Helper
|
||||
{
|
||||
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result &>
|
||||
{
|
||||
typedef typename boost::reference_wrapper<typename boost::add_const<Result>::type > Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result *>
|
||||
{
|
||||
typedef const Result * Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result *>
|
||||
{
|
||||
typedef Result * Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result &>
|
||||
{
|
||||
typedef typename boost::reference_wrapper<Result> Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::any_cast<boost::reference_wrapper<Result> >(ob.get());
|
||||
} else {
|
||||
return boost::ref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<typename boost::shared_ptr<Result> >
|
||||
{
|
||||
typedef typename boost::shared_ptr<Result> Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<Boxed_Value>
|
||||
{
|
||||
typedef Boxed_Value Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
return ob;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<const Boxed_Value &>
|
||||
{
|
||||
typedef Boxed_Value Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
return ob;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class bad_boxed_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
bad_boxed_cast(const Type_Info &t_from, const std::type_info &t_to) throw()
|
||||
: from(t_from.m_type_info), to(&t_to), m_what("Cannot perform boxed_cast")
|
||||
{
|
||||
}
|
||||
|
||||
bad_boxed_cast(const std::string &w) throw()
|
||||
: m_what(w)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~bad_boxed_cast() throw() {}
|
||||
|
||||
virtual const char * what () throw()
|
||||
{
|
||||
return m_what.c_str();
|
||||
}
|
||||
const std::type_info *from;
|
||||
const std::type_info *to;
|
||||
|
||||
private:
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
typename Cast_Helper<Type>::Result_Type boxed_cast(const Boxed_Value &bv)
|
||||
{
|
||||
try {
|
||||
return Cast_Helper<Type>::cast(bv);
|
||||
} catch (const boost::bad_any_cast &) {
|
||||
throw bad_boxed_cast(bv.get_type_info(), typeid(Type));
|
||||
}
|
||||
}
|
||||
|
||||
struct Boxed_POD_Value
|
||||
{
|
||||
Boxed_POD_Value(const Boxed_Value &v)
|
||||
: d(0), i(0), m_isfloat(false)
|
||||
{
|
||||
if (!v.get_type_info().m_type_info)
|
||||
{
|
||||
throw boost::bad_any_cast();
|
||||
}
|
||||
|
||||
const std::type_info &inp_ = *v.get_type_info().m_type_info;
|
||||
|
||||
if (inp_ == typeid(double))
|
||||
{
|
||||
d = boxed_cast<double>(v);
|
||||
m_isfloat = true;
|
||||
} else if (inp_ == typeid(float)) {
|
||||
d = boxed_cast<float>(v);
|
||||
m_isfloat = true;
|
||||
} else if (inp_ == typeid(bool)) {
|
||||
i = boxed_cast<bool>(v);
|
||||
} else if (inp_ == typeid(char)) {
|
||||
i = boxed_cast<char>(v);
|
||||
} else if (inp_ == typeid(int)) {
|
||||
i = boxed_cast<int>(v);
|
||||
} else if (inp_ == typeid(unsigned int)) {
|
||||
i = boxed_cast<unsigned int>(v);
|
||||
} else if (inp_ == typeid(long)) {
|
||||
i = boxed_cast<long>(v);
|
||||
} else if (inp_ == typeid(unsigned long)) {
|
||||
i = boxed_cast<unsigned long>(v);
|
||||
} else if (inp_ == typeid(boost::int8_t)) {
|
||||
i = boxed_cast<boost::int8_t>(v);
|
||||
} else if (inp_ == typeid(boost::int16_t)) {
|
||||
i = boxed_cast<boost::int16_t>(v);
|
||||
} else if (inp_ == typeid(boost::int32_t)) {
|
||||
i = boxed_cast<boost::int32_t>(v);
|
||||
} else if (inp_ == typeid(boost::int64_t)) {
|
||||
i = boxed_cast<boost::int64_t>(v);
|
||||
} else if (inp_ == typeid(boost::uint8_t)) {
|
||||
i = boxed_cast<boost::uint8_t>(v);
|
||||
} else if (inp_ == typeid(boost::uint16_t)) {
|
||||
i = boxed_cast<boost::uint16_t>(v);
|
||||
} else if (inp_ == typeid(boost::uint32_t)) {
|
||||
i = boxed_cast<boost::uint32_t>(v);
|
||||
} else {
|
||||
throw boost::bad_any_cast();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) == ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator<(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) < ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator>(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) > ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator>=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) >= ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator<=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) <= ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator!=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) != ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
Boxed_Value operator+(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i + r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) + ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value operator*(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i * r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) * ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value operator/(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i / r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) / ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value operator-(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i - r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) - ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
double d;
|
||||
boost::int64_t i;
|
||||
|
||||
bool m_isfloat;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<Boxed_POD_Value>
|
||||
{
|
||||
typedef Boxed_POD_Value Result_Type;
|
||||
|
||||
static Result_Type cast(const Boxed_Value &ob)
|
||||
{
|
||||
return Boxed_POD_Value(ob);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
347
include/chaiscript/dispatchkit/dispatchkit.hpp
Normal file
347
include/chaiscript/dispatchkit/dispatchkit.hpp
Normal file
@@ -0,0 +1,347 @@
|
||||
#ifndef __dispatchkit_hpp__
|
||||
#define __dispatchkit_hpp__
|
||||
|
||||
#include <typeinfo>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <deque>
|
||||
|
||||
#include "boxed_value.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include "proxy_functions.hpp"
|
||||
#include "proxy_constructors.hpp"
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
class Dispatch_Function : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Dispatch_Function(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs)
|
||||
: m_funcs(t_funcs)
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool operator==(const Proxy_Function &) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual ~Dispatch_Function() {}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return dispatch(m_funcs, params);
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types() const
|
||||
{
|
||||
return std::vector<Type_Info>();
|
||||
}
|
||||
|
||||
virtual bool types_match(const std::vector<Boxed_Value> &types) const
|
||||
{
|
||||
typedef std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > function_vec;
|
||||
|
||||
function_vec::const_iterator begin = m_funcs.begin();
|
||||
function_vec::const_iterator end = m_funcs.end();
|
||||
|
||||
while (begin != end)
|
||||
{
|
||||
if (begin->second->types_match(types))
|
||||
{
|
||||
return true;
|
||||
} else {
|
||||
++begin;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual std::string annotation() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > m_funcs;
|
||||
};
|
||||
|
||||
|
||||
class Dispatch_Engine
|
||||
{
|
||||
public:
|
||||
typedef std::map<std::string, dispatchkit::Type_Info> Type_Name_Map;
|
||||
typedef std::map<std::string, Boxed_Value> Scope;
|
||||
typedef std::deque<Scope> Stack;
|
||||
|
||||
Dispatch_Engine()
|
||||
: m_place_holder(boost::shared_ptr<Placeholder_Object>(new Placeholder_Object()))
|
||||
{
|
||||
m_scopes.push_back(Scope());
|
||||
}
|
||||
|
||||
bool register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
|
||||
{
|
||||
return add_function(f, name);
|
||||
}
|
||||
|
||||
template<typename Function>
|
||||
bool register_function(const Function &func, const std::string &name)
|
||||
{
|
||||
return add_function(boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func)), name);
|
||||
}
|
||||
|
||||
|
||||
template<typename Class>
|
||||
void set_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
for (int i = m_scopes.size()-1; i >= 0; --i)
|
||||
{
|
||||
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
|
||||
if (itr != m_scopes[i].end())
|
||||
{
|
||||
m_scopes[i][name] = Boxed_Value(obj);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
add_object(name, obj);
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
void add_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
m_scopes.back()[name] = Boxed_Value(obj);
|
||||
}
|
||||
|
||||
void new_scope()
|
||||
{
|
||||
m_scopes.push_back(Scope());
|
||||
}
|
||||
|
||||
void pop_scope()
|
||||
{
|
||||
if (m_scopes.size() > 1)
|
||||
{
|
||||
m_scopes.pop_back();
|
||||
} else {
|
||||
throw std::range_error("Unable to pop global stack");
|
||||
}
|
||||
}
|
||||
|
||||
Stack get_stack()
|
||||
{
|
||||
return m_scopes;
|
||||
}
|
||||
|
||||
Stack set_stack(Stack s)
|
||||
{
|
||||
std::swap(s, m_scopes);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
Boxed_Value get_object(const std::string &name) const
|
||||
{
|
||||
if (name == "_")
|
||||
{
|
||||
return m_place_holder;
|
||||
}
|
||||
|
||||
for (int i = m_scopes.size()-1; i >= 0; --i)
|
||||
{
|
||||
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
|
||||
if (itr != m_scopes[i].end())
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs = get_function_impl(name, false);
|
||||
|
||||
if (funcs.empty())
|
||||
{
|
||||
throw std::range_error("Object not known: " + name);
|
||||
} else {
|
||||
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Dispatch_Function(funcs)));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void register_type(const std::string &name)
|
||||
{
|
||||
m_types.insert(std::make_pair(name, Get_Type_Info<Type>::get()));
|
||||
}
|
||||
|
||||
|
||||
Type_Info get_type(const std::string &name) const
|
||||
{
|
||||
Type_Name_Map::const_iterator itr = m_types.find(name);
|
||||
|
||||
if (itr != m_types.end())
|
||||
{
|
||||
return itr->second;
|
||||
}
|
||||
|
||||
throw std::range_error("Type Not Known");
|
||||
}
|
||||
|
||||
std::string get_type_name(const Type_Info &ti) const
|
||||
{
|
||||
for (Type_Name_Map::const_iterator itr = m_types.begin();
|
||||
itr != m_types.end();
|
||||
++itr)
|
||||
{
|
||||
if (itr->second.m_bare_type_info == ti.m_bare_type_info)
|
||||
{
|
||||
return itr->first;
|
||||
}
|
||||
}
|
||||
|
||||
return ti.m_bare_type_info->name();
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, Type_Info> > get_types() const
|
||||
{
|
||||
return std::vector<std::pair<std::string, Type_Info> >(m_types.begin(), m_types.end());
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> >
|
||||
get_function_impl(const std::string &t_name, bool include_objects) const
|
||||
{
|
||||
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs;
|
||||
|
||||
if (include_objects)
|
||||
{
|
||||
try {
|
||||
funcs.insert(funcs.end(),
|
||||
std::make_pair(
|
||||
t_name,
|
||||
boxed_cast<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type>(get_object(t_name)))
|
||||
);
|
||||
} catch (const bad_boxed_cast &) {
|
||||
} catch (const std::range_error &) {
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range
|
||||
= m_functions.equal_range(t_name);
|
||||
|
||||
funcs.insert(funcs.end(), range.first, range.second);
|
||||
return funcs;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> >
|
||||
get_function(const std::string &t_name) const
|
||||
{
|
||||
return get_function_impl(t_name, true);
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > get_functions() const
|
||||
{
|
||||
return std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >(m_functions.begin(), m_functions.end());
|
||||
}
|
||||
|
||||
private:
|
||||
bool add_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &t_name)
|
||||
{
|
||||
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range
|
||||
= m_functions.equal_range(t_name);
|
||||
|
||||
while (range.first != range.second)
|
||||
{
|
||||
if ((*f) == *(range.first->second))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
++range.first;
|
||||
}
|
||||
|
||||
m_functions.insert(std::make_pair(t_name, f));
|
||||
return true;
|
||||
}
|
||||
|
||||
std::deque<Scope> m_scopes;
|
||||
|
||||
std::multimap<std::string, boost::shared_ptr<Proxy_Function> > m_functions;
|
||||
Type_Name_Map m_types;
|
||||
Boxed_Value m_place_holder;
|
||||
};
|
||||
|
||||
void dump_object(Boxed_Value o, const Dispatch_Engine &e)
|
||||
{
|
||||
std::cout << e.get_type_name(o.get_type_info()) << std::endl;
|
||||
}
|
||||
|
||||
void dump_type(const Type_Info &type, const Dispatch_Engine &e)
|
||||
{
|
||||
std::cout << e.get_type_name(type);
|
||||
}
|
||||
|
||||
void dump_function(const std::pair<const std::string, boost::shared_ptr<Proxy_Function> > &f, const Dispatch_Engine &e)
|
||||
{
|
||||
std::vector<Type_Info> params = f.second->get_param_types();
|
||||
std::string annotation = f.second->annotation();
|
||||
|
||||
if (annotation.size() > 0) {
|
||||
std::cout << annotation;
|
||||
}
|
||||
dump_type(params.front(), e);
|
||||
std::cout << " " << f.first << "(";
|
||||
|
||||
for (std::vector<Type_Info>::const_iterator itr = params.begin() + 1;
|
||||
itr != params.end();
|
||||
)
|
||||
{
|
||||
dump_type(*itr, e);
|
||||
++itr;
|
||||
|
||||
if (itr != params.end())
|
||||
{
|
||||
std::cout << ", ";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::cout << ") " << std::endl;
|
||||
}
|
||||
|
||||
void dump_system(const Dispatch_Engine &s)
|
||||
{
|
||||
std::cout << "Registered Types: " << std::endl;
|
||||
std::vector<std::pair<std::string, Type_Info> > types = s.get_types();
|
||||
for (std::vector<std::pair<std::string, Type_Info> >::const_iterator itr = types.begin();
|
||||
itr != types.end();
|
||||
++itr)
|
||||
{
|
||||
std::cout << itr->first << ": ";
|
||||
std::cout << itr->second.m_bare_type_info->name();
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::cout << std::endl;
|
||||
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs = s.get_functions();
|
||||
|
||||
std::cout << "Functions: " << std::endl;
|
||||
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
dump_function(*itr, s);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
118
include/chaiscript/dispatchkit/function_call.hpp
Normal file
118
include/chaiscript/dispatchkit/function_call.hpp
Normal file
@@ -0,0 +1,118 @@
|
||||
#include <boost/preprocessor.hpp>
|
||||
|
||||
#define addparam(z,n,text) params.push_back(Boxed_Value(BOOST_PP_CAT(p, n) ));
|
||||
#define curry(z,n,text) BOOST_PP_CAT(_, BOOST_PP_INC(n))
|
||||
|
||||
|
||||
#ifndef BOOST_PP_IS_ITERATING
|
||||
#ifndef __function_call_hpp__
|
||||
#define __function_call_hpp__
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "proxy_functions.hpp"
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename Ret>
|
||||
class Function_Caller_Ret
|
||||
{
|
||||
public:
|
||||
Function_Caller_Ret()
|
||||
{
|
||||
}
|
||||
|
||||
Ret call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return boxed_cast<Ret>(dispatch(t_funcs, params));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
class Function_Caller_Ret<void>
|
||||
{
|
||||
public:
|
||||
Function_Caller_Ret()
|
||||
{
|
||||
}
|
||||
|
||||
void call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
dispatch(t_funcs, params);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 0, 9 )
|
||||
#define BOOST_PP_FILENAME_1 "function_call.hpp"
|
||||
#include BOOST_PP_ITERATE()
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename FunctionType>
|
||||
boost::function<FunctionType>
|
||||
build_function_caller(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs)
|
||||
{
|
||||
FunctionType *p;
|
||||
return build_function_caller_helper(p, funcs);
|
||||
}
|
||||
|
||||
template<typename FunctionType>
|
||||
boost::function<FunctionType>
|
||||
build_function_caller(boost::shared_ptr<Proxy_Function> func)
|
||||
{
|
||||
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs;
|
||||
funcs.push_back(std::make_pair(std::string(), func));
|
||||
return build_function_caller<FunctionType>(funcs);
|
||||
}
|
||||
|
||||
template<typename FunctionType>
|
||||
boost::function<FunctionType>
|
||||
build_function_caller(const Boxed_Value &bv)
|
||||
{
|
||||
return build_function_caller<FunctionType>(boxed_cast<boost::shared_ptr<Proxy_Function> >(bv));
|
||||
}
|
||||
|
||||
|
||||
template<typename FunctionType, typename ScriptEngine>
|
||||
boost::function<FunctionType> build_functor(ScriptEngine &e, const std::string &script)
|
||||
{
|
||||
return build_function_caller<FunctionType>(e.evaluate_string(script));
|
||||
}
|
||||
}
|
||||
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
Ret function_caller(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs
|
||||
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
||||
{
|
||||
std::vector<Boxed_Value> params;
|
||||
|
||||
BOOST_PP_REPEAT(n, addparam, ~)
|
||||
|
||||
return Function_Caller_Ret<Ret>().call(funcs, params);
|
||||
}
|
||||
|
||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
|
||||
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs)
|
||||
{
|
||||
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs
|
||||
BOOST_PP_ENUM_TRAILING(n, curry, ~));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
36
include/chaiscript/dispatchkit/proxy_constructors.hpp
Normal file
36
include/chaiscript/dispatchkit/proxy_constructors.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <boost/preprocessor.hpp>
|
||||
|
||||
#ifndef BOOST_PP_IS_ITERATING
|
||||
#ifndef __proxy_constructors_hpp__
|
||||
#define __proxy_constructors_hpp__
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
|
||||
#define BOOST_PP_FILENAME_1 "proxy_constructors.hpp"
|
||||
#include BOOST_PP_ITERATE()
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::shared_ptr<Class> constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
|
||||
}
|
||||
|
||||
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)(BOOST_PP_ENUM_PARAMS(n, Param));
|
||||
return boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>)));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
447
include/chaiscript/dispatchkit/proxy_functions.hpp
Normal file
447
include/chaiscript/dispatchkit/proxy_functions.hpp
Normal file
@@ -0,0 +1,447 @@
|
||||
#include <boost/preprocessor.hpp>
|
||||
|
||||
#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get());
|
||||
#define casthelper(z,n,text) ,dispatchkit::boxed_cast< Param ## n >(params[n])
|
||||
#define comparetype(z,n,text) && ((Get_Type_Info<Param ## n>::get() == params[n].get_type_info()))
|
||||
#define trycast(z,n,text) dispatchkit::boxed_cast<Param ## n>(params[n]);
|
||||
|
||||
|
||||
#ifndef BOOST_PP_IS_ITERATING
|
||||
#ifndef __proxy_functions_hpp__
|
||||
#define __proxy_functions_hpp__
|
||||
|
||||
#include "boxed_value.hpp"
|
||||
#include "type_info.hpp"
|
||||
#include <string>
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
// handle_return implementations
|
||||
template<typename Ret>
|
||||
struct Handle_Return
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret ()> &f)
|
||||
{
|
||||
return Boxed_Value(f());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret &()> &f)
|
||||
{
|
||||
return Boxed_Value(boost::ref(f()));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<Boxed_Value>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Boxed_Value ()> &f)
|
||||
{
|
||||
return f();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<Boxed_Value &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Boxed_Value &()> &f)
|
||||
{
|
||||
return f();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<void>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<void ()> &f)
|
||||
{
|
||||
f();
|
||||
return Boxed_Value(Boxed_Value::Void_Type());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct Param_List_Builder
|
||||
{
|
||||
Param_List_Builder &operator<<(const Boxed_Value &so)
|
||||
{
|
||||
objects.push_back(so);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Param_List_Builder &operator<<(T t)
|
||||
{
|
||||
objects.push_back(Boxed_Value(t));
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator const std::vector<Boxed_Value> &() const
|
||||
{
|
||||
return objects;
|
||||
}
|
||||
|
||||
std::vector<Boxed_Value> objects;
|
||||
};
|
||||
|
||||
struct arity_error : std::range_error
|
||||
{
|
||||
arity_error(int t_got, int t_expected)
|
||||
: std::range_error("Function dispatch arity mismatch"),
|
||||
got(t_got), expected(t_expected)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~arity_error() throw() {}
|
||||
int got;
|
||||
int expected;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
|
||||
#define BOOST_PP_FILENAME_1 "proxy_functions.hpp"
|
||||
#include BOOST_PP_ITERATE()
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
class Proxy_Function
|
||||
{
|
||||
public:
|
||||
virtual ~Proxy_Function() {}
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms) = 0;
|
||||
virtual std::vector<Type_Info> get_param_types() const = 0;
|
||||
virtual bool operator==(const Proxy_Function &) const = 0;
|
||||
virtual bool types_match(const std::vector<Boxed_Value> &types) const = 0;
|
||||
virtual std::string annotation() const = 0;
|
||||
};
|
||||
|
||||
class guard_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
guard_error() throw()
|
||||
: std::runtime_error("Guard evaluation failed")
|
||||
{ }
|
||||
|
||||
virtual ~guard_error() throw()
|
||||
{ }
|
||||
};
|
||||
|
||||
class Dynamic_Proxy_Function : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Dynamic_Proxy_Function(
|
||||
const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f,
|
||||
int t_arity=-1,
|
||||
const std::string &t_description = "",
|
||||
const boost::shared_ptr<Proxy_Function> &t_guard = boost::shared_ptr<Proxy_Function>())
|
||||
: m_f(t_f), m_arity(t_arity), m_description(t_description), m_guard(t_guard)
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool operator==(const Proxy_Function &) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual bool types_match(const std::vector<Boxed_Value> &types) const
|
||||
{
|
||||
return (m_arity < 0 || types.size() == size_t(m_arity))
|
||||
&& test_guard(types);
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Proxy_Function() {}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (m_arity < 0 || params.size() == size_t(m_arity))
|
||||
{
|
||||
|
||||
if (test_guard(params))
|
||||
{
|
||||
return m_f(params);
|
||||
} else {
|
||||
throw guard_error();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw arity_error(params.size(), m_arity);
|
||||
}
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types() const
|
||||
{
|
||||
std::vector<Type_Info> types;
|
||||
|
||||
types.push_back(Get_Type_Info<Boxed_Value>::get());
|
||||
|
||||
if (m_arity >= 0)
|
||||
{
|
||||
for (int i = 0; i < m_arity; ++i)
|
||||
{
|
||||
types.push_back(Get_Type_Info<Boxed_Value>::get());
|
||||
}
|
||||
} else {
|
||||
types.push_back(Get_Type_Info<std::vector<Boxed_Value> >::get());
|
||||
}
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
virtual std::string annotation() const
|
||||
{
|
||||
return m_description;
|
||||
}
|
||||
|
||||
private:
|
||||
bool test_guard(const std::vector<Boxed_Value> ¶ms) const
|
||||
{
|
||||
if (m_guard)
|
||||
{
|
||||
try {
|
||||
return boxed_cast<bool>((*m_guard)(params));
|
||||
} catch (const arity_error &) {
|
||||
return false;
|
||||
} catch (const bad_boxed_cast &) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
||||
int m_arity;
|
||||
std::string m_description;
|
||||
boost::shared_ptr<Proxy_Function> m_guard;
|
||||
};
|
||||
|
||||
struct Placeholder_Object
|
||||
{
|
||||
};
|
||||
|
||||
class Bound_Function : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Bound_Function(const boost::shared_ptr<Proxy_Function> &t_f,
|
||||
const std::vector<Boxed_Value> &t_args)
|
||||
: m_f(t_f), m_args(t_args)
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool operator==(const Proxy_Function &) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual ~Bound_Function() {}
|
||||
|
||||
virtual bool types_match(const std::vector<Boxed_Value> &types) const
|
||||
{
|
||||
std::vector<Boxed_Value> params = build_param_list(types);
|
||||
return m_f->types_match(params);
|
||||
}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return (*m_f)(build_param_list(params));
|
||||
}
|
||||
|
||||
std::vector<Boxed_Value> build_param_list(const std::vector<Boxed_Value> ¶ms) const
|
||||
{
|
||||
typedef std::vector<Boxed_Value>::const_iterator pitr;
|
||||
|
||||
pitr parg = params.begin();
|
||||
pitr barg = m_args.begin();
|
||||
|
||||
std::vector<Boxed_Value> args;
|
||||
|
||||
while (true)
|
||||
{
|
||||
while (barg != m_args.end()
|
||||
&& !(barg->get_type_info() == Get_Type_Info<Placeholder_Object>::get()))
|
||||
{
|
||||
args.push_back(*barg);
|
||||
++barg;
|
||||
}
|
||||
|
||||
if (parg != params.end())
|
||||
{
|
||||
args.push_back(*parg);
|
||||
++parg;
|
||||
}
|
||||
|
||||
if (barg != m_args.end()
|
||||
&& barg->get_type_info() == Get_Type_Info<Placeholder_Object>::get())
|
||||
{
|
||||
++barg;
|
||||
}
|
||||
|
||||
if (parg == params.end() && barg == m_args.end())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types() const
|
||||
{
|
||||
return std::vector<Type_Info>();
|
||||
}
|
||||
|
||||
virtual std::string annotation() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
boost::shared_ptr<Proxy_Function> m_f;
|
||||
std::vector<Boxed_Value> m_args;
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
class Proxy_Function_Impl : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Proxy_Function_Impl(const Func &f)
|
||||
: m_f(f)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Proxy_Function_Impl() {}
|
||||
|
||||
virtual bool operator==(const Proxy_Function &t_func) const
|
||||
{
|
||||
try {
|
||||
dynamic_cast<const Proxy_Function_Impl<Func> &>(t_func);
|
||||
return true;
|
||||
} catch (const std::bad_cast &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return call_func(m_f, params);
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types() const
|
||||
{
|
||||
return build_param_type_list(m_f);
|
||||
}
|
||||
|
||||
virtual bool types_match(const std::vector<Boxed_Value> &types) const
|
||||
{
|
||||
return compare_types(m_f, types);
|
||||
}
|
||||
|
||||
virtual std::string annotation() const
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
Func m_f;
|
||||
};
|
||||
|
||||
struct dispatch_error : std::runtime_error
|
||||
{
|
||||
dispatch_error() throw()
|
||||
: std::runtime_error("No matching function to dispatch to")
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~dispatch_error() throw() {}
|
||||
};
|
||||
|
||||
Boxed_Value dispatch(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs,
|
||||
const std::vector<Boxed_Value> &plist)
|
||||
{
|
||||
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
try {
|
||||
return (*itr->second)(plist);
|
||||
} catch (const bad_boxed_cast &) {
|
||||
//parameter failed to cast, try again
|
||||
} catch (const arity_error &) {
|
||||
//invalid num params, try again
|
||||
} catch (const guard_error &) {
|
||||
//guard failed to allow the function to execute,
|
||||
//try again
|
||||
}
|
||||
}
|
||||
|
||||
throw dispatch_error();
|
||||
}
|
||||
}
|
||||
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
|
||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
std::vector<Type_Info> build_param_type_list(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &)
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>::get());
|
||||
|
||||
BOOST_PP_REPEAT(n, gettypeinfo, ~)
|
||||
|
||||
return ti;
|
||||
}
|
||||
|
||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
Boxed_Value call_func(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != n)
|
||||
{
|
||||
throw arity_error(params.size(), n);
|
||||
} else {
|
||||
return Handle_Return<Ret>()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~)));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
bool compare_types(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != n)
|
||||
{
|
||||
return false;
|
||||
} else {
|
||||
bool val = true BOOST_PP_REPEAT(n, comparetype, ~);
|
||||
if (val) return true;
|
||||
|
||||
try {
|
||||
BOOST_PP_REPEAT(n, trycast, ~);
|
||||
} catch (const bad_boxed_cast &) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
57
include/chaiscript/dispatchkit/register_function.hpp
Normal file
57
include/chaiscript/dispatchkit/register_function.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <boost/preprocessor.hpp>
|
||||
|
||||
#ifndef BOOST_PP_IS_ITERATING
|
||||
#ifndef __register_function_hpp__
|
||||
#define __register_function_hpp__
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename T, typename Class>
|
||||
T &get_member(T Class::* m, Class *obj)
|
||||
{
|
||||
return obj->*m;
|
||||
}
|
||||
|
||||
template<typename T, typename Class>
|
||||
void register_member(Dispatch_Engine &s, T Class::* m, const std::string &name)
|
||||
{
|
||||
s.register_function(boost::function<T (Class *)>(boost::bind(&get_member<T, Class>, m, _1)), name);
|
||||
}
|
||||
}
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
|
||||
#define BOOST_PP_FILENAME_1 "register_function.hpp"
|
||||
|
||||
#include BOOST_PP_ITERATE()
|
||||
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
void register_function(Dispatch_Engine &s, Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
|
||||
{
|
||||
s.register_function(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
|
||||
}
|
||||
|
||||
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
|
||||
{
|
||||
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
|
||||
}
|
||||
|
||||
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const, const std::string &name)
|
||||
{
|
||||
s.register_function(boost::function<Ret (const Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
206
include/chaiscript/dispatchkit/test.cpp
Normal file
206
include/chaiscript/dispatchkit/test.cpp
Normal file
@@ -0,0 +1,206 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
#include "bootstrap.hpp"
|
||||
#include "bootstrap_stl.hpp"
|
||||
#include "function_call.hpp"
|
||||
|
||||
using namespace dispatchkit;
|
||||
|
||||
struct Test
|
||||
{
|
||||
Test(const std::string &s)
|
||||
: number(-25), message(s)
|
||||
{
|
||||
std::cout << "Test class constructed with value: " << s << std::endl;
|
||||
}
|
||||
|
||||
void show_message()
|
||||
{
|
||||
std::cout << "Constructed Message: " << message << std::endl;
|
||||
}
|
||||
|
||||
std::string &get_message()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
int number;
|
||||
|
||||
std::string message;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Boxed_Value named_func_call(Dispatch_Engine &ss,
|
||||
const std::string &nametocall, const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() == 2)
|
||||
{
|
||||
return dispatch(ss.get_function(nametocall), params);
|
||||
} else {
|
||||
throw std::runtime_error("Invalid num params");
|
||||
}
|
||||
}
|
||||
|
||||
// A function that takes a dynamic list of params
|
||||
// and calls a bunch of conversion functions on them and
|
||||
// returns the result as a boxed_value
|
||||
Boxed_Value dynamic_function(Dispatch_Engine &ss, const std::string &name,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (name == "concat_string")
|
||||
{
|
||||
Boxed_Value result;
|
||||
|
||||
//Return a void if there is nothing in the array
|
||||
if (params.size() == 0)
|
||||
{
|
||||
return result;
|
||||
} else {
|
||||
//else, prepopulate the result with a string conversion of the first
|
||||
//param
|
||||
result =
|
||||
dispatch(ss.get_function("to_string"), Param_List_Builder() << params[0]);
|
||||
}
|
||||
|
||||
//Then, loop over all remaining params, converting them to strings and adding
|
||||
//them to the result. This example maybe bette served with a string += operator
|
||||
//implementation, but it works.
|
||||
for (size_t i = 1; i < params.size(); ++i)
|
||||
{
|
||||
result =
|
||||
dispatch(ss.get_function("+"), Param_List_Builder() << result <<
|
||||
dispatch(ss.get_function("to_string"), Param_List_Builder() << params[i]));
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
throw std::runtime_error("Unknown function call");
|
||||
}
|
||||
}
|
||||
|
||||
void test(const std::string &p)
|
||||
{
|
||||
std::cout << "Test: " << p << std::endl;
|
||||
}
|
||||
|
||||
//Test main
|
||||
int main()
|
||||
{
|
||||
Dispatch_Engine ss;
|
||||
Bootstrap::bootstrap(ss);
|
||||
bootstrap_vector<std::vector<int> >(ss, "VectorInt");
|
||||
dump_system(ss);
|
||||
|
||||
//Calling a function by name and allowing the built in dispatch mechanism to
|
||||
//choose the most appropriate version of the function
|
||||
Boxed_Value addresult = dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3));
|
||||
|
||||
//Using the cast to unbox the resultant value and output it
|
||||
std::cout << boxed_cast<double>(addresult) << std::endl;
|
||||
|
||||
//Using the Boxed_Value as input to another function, again with automatic dispatch.
|
||||
//This time we will not bother saving the result and will instead send it straight out
|
||||
std::cout << boxed_cast<double>(
|
||||
dispatch(ss.get_function("*"), Param_List_Builder() << 2 << addresult)
|
||||
) << std::endl;
|
||||
|
||||
//Register a new function, this one with typing for us, so we don't have to ubox anything
|
||||
//right here
|
||||
|
||||
//Now we have a print method, let's try to print out the earlier example:
|
||||
//so, we dispatch the to_string and pass its result as a param to "print"
|
||||
//In this example we don't bother with temporaries and we don't have to know
|
||||
//anything about types
|
||||
dispatch(ss.get_function("print_string"),
|
||||
Param_List_Builder() << dispatch(ss.get_function("to_string"), Param_List_Builder() << addresult));
|
||||
|
||||
// Now we are going to register a new dynamic function,
|
||||
// when this function is called the objects are not unboxed, but passed
|
||||
// in in their boxed state
|
||||
ss.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&dynamic_function, boost::ref(ss), "concat_string", _1))), "concat_string");
|
||||
|
||||
|
||||
// Call our newly defined dynamic function with 10 parameters, then send
|
||||
// its output to the "print" function
|
||||
dispatch(ss.get_function("print_string"),
|
||||
Param_List_Builder() << dispatch(ss.get_function("concat_string"),
|
||||
Param_List_Builder() << std::string("\n\t") << std::string("The Value Was: ")
|
||||
<< double(42.5) << std::string(".")
|
||||
<< '\n'
|
||||
<< '\t' << std::string("The old value was: ")
|
||||
<< addresult << '.' << '\n' ));
|
||||
|
||||
|
||||
|
||||
|
||||
//Register some local methods of the "Test" class
|
||||
ss.register_function(build_constructor<Test, const std::string &>(), "Test");
|
||||
|
||||
register_function(ss, &Test::get_message, "get_message");
|
||||
register_function(ss, &Test::show_message, "show_message");
|
||||
register_member(ss, &Test::number, "number");
|
||||
|
||||
//Create a new object using the "Test" constructor, passing the param "Yo".
|
||||
//Then, add the new object to the system with the name "testobj2"
|
||||
ss.add_object("testobj2",
|
||||
dispatch(ss.get_function("Test"), Param_List_Builder() << std::string("Yo")));
|
||||
|
||||
// Look up and store a reference to our new object
|
||||
std::vector<Boxed_Value> sos;
|
||||
sos.push_back(ss.get_object("testobj2"));
|
||||
|
||||
//Build a bound function proxy for calling the script handled function
|
||||
boost::function<void (Test &)> show_message =
|
||||
build_function_caller<void (Test &)>(ss.get_function("show_message"));
|
||||
|
||||
Test &t = boxed_cast<Test &>(ss.get_object("testobj2"));
|
||||
|
||||
//Print the message the object was created with
|
||||
show_message(t);
|
||||
|
||||
//Now, get a reference to the object's stored message
|
||||
Boxed_Value stringref = dispatch(ss.get_function("get_message"), sos);
|
||||
|
||||
//Unbox it using boxed_cast
|
||||
std::string &sr = boxed_cast<std::string &>(stringref);
|
||||
|
||||
//Update the value of the reference
|
||||
sr = "Bob Updated The message";
|
||||
|
||||
//Now, get a reference to the object's stored number
|
||||
Boxed_Value numberref= dispatch(ss.get_function("number"), sos);
|
||||
|
||||
//Unbox it using boxed_cast
|
||||
int &ir = boxed_cast<int &>(numberref);
|
||||
|
||||
std::cout << "Number: " << ir << std::endl;
|
||||
|
||||
//Now, prove that the reference was successfully acquired
|
||||
//and we are able to peek into the boxed types
|
||||
show_message(t);
|
||||
|
||||
|
||||
// Finally, we are going to register some named function aliases, for
|
||||
// the fun of it
|
||||
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
||||
new Dynamic_Proxy_Function(boost::bind(&named_func_call, boost::ref(ss), "+", _1))), "add");
|
||||
|
||||
//Call our newly named "add" function (which in turn dispatches +)
|
||||
|
||||
std::cout << "Result of add function: " <<
|
||||
boxed_cast<int>(dispatch(ss.get_function("add"), Param_List_Builder() << 5 << 2))
|
||||
<< std::endl;
|
||||
|
||||
|
||||
ss.set_object("myfunc", boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<boost::function<void (const std::string &)> >(&test)));
|
||||
|
||||
dispatch(ss.get_function("myfunc"), Param_List_Builder() << std::string("hello function variable"));
|
||||
|
||||
}
|
||||
|
109
include/chaiscript/dispatchkit/type_info.hpp
Normal file
109
include/chaiscript/dispatchkit/type_info.hpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#ifndef __type_info_hpp__
|
||||
#define __type_info_hpp__
|
||||
|
||||
#include <boost/type_traits/is_const.hpp>
|
||||
#include <boost/type_traits/is_void.hpp>
|
||||
#include <boost/type_traits/is_reference.hpp>
|
||||
#include <boost/type_traits/is_pointer.hpp>
|
||||
#include <boost/type_traits/remove_const.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/type_traits/remove_pointer.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
struct Type_Info
|
||||
{
|
||||
Type_Info(bool t_is_const, bool t_is_reference, bool t_is_pointer, bool t_is_void,
|
||||
const std::type_info *t_ti, const std::type_info *t_bareti)
|
||||
: m_is_const(t_is_const), m_is_reference(t_is_reference), m_is_pointer(t_is_pointer),
|
||||
m_is_void(t_is_void),
|
||||
m_type_info(t_ti), m_bare_type_info(t_bareti),
|
||||
m_is_unknown(false)
|
||||
{
|
||||
}
|
||||
|
||||
Type_Info()
|
||||
: m_is_const(false), m_is_reference(false), m_is_pointer(false),
|
||||
m_is_void(false), m_type_info(0), m_bare_type_info(0),
|
||||
m_is_unknown(true)
|
||||
{
|
||||
}
|
||||
|
||||
Type_Info(const Type_Info &ti)
|
||||
: m_is_const(ti.m_is_const), m_is_reference(ti.m_is_reference),
|
||||
m_is_pointer(ti.m_is_pointer),
|
||||
m_is_void(ti.m_is_void), m_type_info(ti.m_type_info),
|
||||
m_bare_type_info(ti.m_bare_type_info),
|
||||
m_is_unknown(ti.m_is_unknown)
|
||||
{
|
||||
}
|
||||
Type_Info &operator=(const Type_Info &ti)
|
||||
{
|
||||
m_is_const = ti.m_is_const;
|
||||
m_is_reference = ti.m_is_reference;
|
||||
m_is_pointer = ti.m_is_pointer;
|
||||
m_is_void = ti.m_is_void;
|
||||
m_type_info = ti.m_type_info;
|
||||
m_bare_type_info = ti.m_bare_type_info;
|
||||
m_is_unknown = ti.m_is_unknown;
|
||||
return *this;
|
||||
}
|
||||
bool operator<(const Type_Info &ti) const
|
||||
{
|
||||
return m_type_info < ti.m_type_info;
|
||||
}
|
||||
|
||||
bool operator==(const Type_Info &ti) const
|
||||
{
|
||||
return ti.m_type_info == m_type_info;
|
||||
}
|
||||
|
||||
bool m_is_const;
|
||||
bool m_is_reference;
|
||||
bool m_is_pointer;
|
||||
bool m_is_void;
|
||||
const std::type_info *m_type_info;
|
||||
const std::type_info *m_bare_type_info;
|
||||
bool m_is_unknown;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(T),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::shared_ptr<T> >
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::shared_ptr<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::reference_wrapper<T> >
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::reference_wrapper<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
17
include/chaiscript/dispatchkit/unittest.cpp
Normal file
17
include/chaiscript/dispatchkit/unittest.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "dispatchkit.hpp"
|
||||
#include "bootstrap.hpp"
|
||||
|
||||
#define BOOST_TEST_DYN_LINK
|
||||
#define BOOST_TEST_MODULE boxedcpp_unittests
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
BOOST_AUTO_TEST_CASE( add_operators )
|
||||
{
|
||||
using namespace dispatchkit;
|
||||
|
||||
Dispatch_Engine ss;
|
||||
Bootstrap::bootstrap(ss);
|
||||
dump_system(ss);
|
||||
|
||||
BOOST_CHECK_EQUAL(boxed_cast<int>(dispatch(ss.get_function("+"), Param_List_Builder() << double(5.1) << double(10.3))), 15.4);
|
||||
}
|
Reference in New Issue
Block a user