Revamped method for bootstrapping of types, using a new Module class that collects everything related to a type or group of types
This commit is contained in:
@@ -12,273 +12,283 @@
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
/**
|
||||
* Set of helper functions for common operators
|
||||
*/
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret add(P1 p1, P2 p2)
|
||||
namespace detail
|
||||
{
|
||||
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)
|
||||
/**
|
||||
* Set of helper functions for common operators
|
||||
*/
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret add(P1 p1, P2 p2)
|
||||
{
|
||||
return (p1 = P1(v.d));
|
||||
} else {
|
||||
return (p1 = P1(v.i));
|
||||
return p1 + p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 construct_pod(Boxed_POD_Value v)
|
||||
{
|
||||
if (v.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret subtract(P1 p1, P2 p2)
|
||||
{
|
||||
return P1(v.d);
|
||||
} else {
|
||||
return P1(v.i);
|
||||
return p1 - p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ×equal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret divide(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 *= P1(r.d);
|
||||
} else {
|
||||
return p1 *= P1(r.i);
|
||||
return p1 / p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret multiply(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 /= P1(r.d);
|
||||
} else {
|
||||
return p1 /= P1(r.i);
|
||||
return p1 * p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret modulus(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 += P1(r.d);
|
||||
} else {
|
||||
return p1 += P1(r.i);
|
||||
return p1 % p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename P1, typename P2>
|
||||
P1 &assign(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return p1 -= P1(r.d);
|
||||
} else {
|
||||
return p1 -= P1(r.i);
|
||||
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)
|
||||
ModulePtr add_oper_equals(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&equals<const T&, const T&>), "=");
|
||||
m->add(fun(&detail::equals<const T&, const T&>), "=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "+" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_add(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_add(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&add<T, const T&, const T&>), "+");
|
||||
m->add(fun(&detail::add<T, const T&, const T&>), "+");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "+=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_add_equals(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_add_equals(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&addsequal<T, T>), "+=");
|
||||
m->add(fun(&detail::addsequal<T, T>), "+=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "-" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_subtract(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_subtract(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&subtract<T, const T&, const T&>), "-");
|
||||
m->add(fun(&detail::subtract<T, const T&, const T&>), "-");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "/" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_divide(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_divide(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(÷<T, const T&, const T&>), "/");
|
||||
m->add(fun(&detail::divide<T, const T&, const T&>), "/");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "*" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_multiply(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_multiply(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&multiply<T, const T&, const T&>), "*");
|
||||
m->add(fun(&detail::multiply<T, const T&, const T&>), "*");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "!=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_not_equals(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_not_equals(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(¬_equals<const T&, const T&>), "!=");
|
||||
m->add(fun(&detail::not_equals<const T&, const T&>), "!=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add user defined assignment operator for T = U
|
||||
*/
|
||||
template<typename T, typename U>
|
||||
void add_oper_assign_overload(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_assign_overload(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&assign<T,U>), "=");
|
||||
m->add(fun(&detail::assign<T,U>), "=");
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
@@ -286,9 +296,10 @@ namespace chaiscript
|
||||
* Add canonical form of "=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_assign(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_assign(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&assign<T,T>), "=");
|
||||
m->add(fun(&detail::assign<T,T>), "=");
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
@@ -296,9 +307,10 @@ namespace chaiscript
|
||||
* Add assignment operator for T = POD.
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_assign_pod(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_assign_pod(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&assign_pod<T>), "=");
|
||||
m->add(fun(&detail::assign_pod<T>), "=");
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
@@ -306,36 +318,40 @@ namespace chaiscript
|
||||
* Add canonical form of "<" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_less_than(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_less_than(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&less_than<const T&, const T&>), "<");
|
||||
m->add(fun(&detail::less_than<const T&, const T&>), "<");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of ">" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_greater_than(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_greater_than(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&greater_than<const T&, const T&>), ">");
|
||||
m->add(fun(&detail::greater_than<const T&, const T&>), ">");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of "<=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_less_than_equals(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_less_than_equals(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&less_than_equals<const T&, const T&>), "<=");
|
||||
m->add(fun(&detail::less_than_equals<const T&, const T&>), "<=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical form of ">=" for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_oper_greater_than_equals(Dispatch_Engine &s)
|
||||
ModulePtr add_oper_greater_than_equals(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&greater_than_equals<const T&, const T&>), ">=");
|
||||
m->add(fun(&detail::greater_than_equals<const T&, const T&>), ">=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -343,23 +359,25 @@ namespace chaiscript
|
||||
* Examples: T < R, T == R, etc.
|
||||
*/
|
||||
template<typename T, typename R>
|
||||
void add_opers_comparison_overload(Dispatch_Engine &s)
|
||||
ModulePtr add_opers_comparison_overload(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&equals<const T&, const R&>), "==");
|
||||
s.add(fun(¬_equals<const T&, const R&>), "!=");
|
||||
s.add(fun(&less_than<const T&, const R&>), "<");
|
||||
s.add(fun(&greater_than<const T&, const R&>), ">");
|
||||
s.add(fun(&less_than_equals<const T&, const R&>), "<=");
|
||||
s.add(fun(&greater_than_equals<const T&, const R&>), ">=");
|
||||
m->add(fun(&detail::equals<const T&, const R&>), "==");
|
||||
m->add(fun(&detail::not_equals<const T&, const R&>), "!=");
|
||||
m->add(fun(&detail::less_than<const T&, const R&>), "<");
|
||||
m->add(fun(&detail::greater_than<const T&, const R&>), ">");
|
||||
m->add(fun(&detail::less_than_equals<const T&, const R&>), "<=");
|
||||
m->add(fun(&detail::greater_than_equals<const T&, const R&>), ">=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical forms of all comparison operators for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_opers_comparison(Dispatch_Engine &s)
|
||||
ModulePtr add_opers_comparison(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
add_opers_comparison_overload<T, T>(s);
|
||||
add_opers_comparison_overload<T, T>(m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -370,20 +388,21 @@ namespace chaiscript
|
||||
* T *= R;
|
||||
*/
|
||||
template<typename Ret, typename T, typename R>
|
||||
void add_opers_arithmetic_overload(Dispatch_Engine &s)
|
||||
ModulePtr add_opers_arithmetic_overload(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&add<Ret, T, R>), "+");
|
||||
s.add(fun(&subtract<Ret, T, R>), "-");
|
||||
s.add(fun(÷<Ret, T, R>), "/");
|
||||
s.add(fun(&multiply<Ret, T, R>), "*");
|
||||
s.add(fun(×equal<T, R>), "*=");
|
||||
s.add(fun(÷sequal<T, R>), "/=");
|
||||
s.add(fun(&subtractsequal<T, R>), "-=");
|
||||
s.add(fun(&addsequal<T, R>), "+=");
|
||||
s.add(fun(&prefixincrement<T>), "++");
|
||||
s.add(fun(&prefixdecrement<T>), "--");
|
||||
s.add(fun(&prefixnegate<T>), "-");
|
||||
s.add(fun(&prefixnot<T>), "!");
|
||||
m->add(fun(&detail::add<Ret, T, R>), "+");
|
||||
m->add(fun(&detail::subtract<Ret, T, R>), "-");
|
||||
m->add(fun(&detail::divide<Ret, T, R>), "/");
|
||||
m->add(fun(&detail::multiply<Ret, T, R>), "*");
|
||||
m->add(fun(&detail::timesequal<T, R>), "*=");
|
||||
m->add(fun(&detail::dividesequal<T, R>), "/=");
|
||||
m->add(fun(&detail::subtractsequal<T, R>), "-=");
|
||||
m->add(fun(&detail::addsequal<T, R>), "+=");
|
||||
m->add(fun(&detail::prefixincrement<T>), "++");
|
||||
m->add(fun(&detail::prefixdecrement<T>), "--");
|
||||
m->add(fun(&detail::prefixnegate<T>), "-");
|
||||
m->add(fun(&detail::prefixnot<T>), "!");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -391,12 +410,13 @@ namespace chaiscript
|
||||
* example: POD *= T, POD /= T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_opers_arithmetic_modify_pod(Dispatch_Engine &s)
|
||||
ModulePtr add_opers_arithmetic_modify_pod(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(×equal_pod<T>), "*=");
|
||||
s.add(fun(÷sequal_pod<T>), "/=");
|
||||
s.add(fun(&subtractsequal_pod<T>), "-=");
|
||||
s.add(fun(&addsequal_pod<T>), "+=");
|
||||
m->add(fun(&detail::timesequal_pod<T>), "*=");
|
||||
m->add(fun(&detail::dividesequal_pod<T>), "/=");
|
||||
m->add(fun(&detail::subtractsequal_pod<T>), "-=");
|
||||
m->add(fun(&detail::addsequal_pod<T>), "+=");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -405,29 +425,32 @@ namespace chaiscript
|
||||
* the copy constructor.
|
||||
*/
|
||||
template<typename T>
|
||||
void add_copy_constructor(Dispatch_Engine &s, const std::string &type)
|
||||
ModulePtr add_copy_constructor(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(constructor<T (const T &)>(), type);
|
||||
s.add(constructor<T (const T &)>(), "clone");
|
||||
m->add(constructor<T (const T &)>(), type);
|
||||
m->add(constructor<T (const T &)>(), "clone");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add default and copy constructors (including "clone") for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_basic_constructors(Dispatch_Engine &s, const std::string &type)
|
||||
ModulePtr add_basic_constructors(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(constructor<T ()>(), type);
|
||||
add_copy_constructor<T>(s, type);
|
||||
m->add(constructor<T ()>(), type);
|
||||
add_copy_constructor<T>(type, m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add POD type constructor for type T. ie: T = type(POD)
|
||||
*/
|
||||
template<typename T>
|
||||
void add_construct_pod(Dispatch_Engine &s, const std::string &type)
|
||||
ModulePtr add_construct_pod(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(fun(&construct_pod<T>), type);
|
||||
m->add(fun(&detail::construct_pod<T>), type);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -435,19 +458,20 @@ namespace chaiscript
|
||||
* T = type(const U &)
|
||||
*/
|
||||
template<typename T, typename U>
|
||||
void add_constructor_overload(Dispatch_Engine &s, const std::string &type)
|
||||
ModulePtr add_constructor_overload(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(constructor<T (const U &)>(), type);
|
||||
m->add(constructor<T (const U &)>(), type);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add canonical forms of all arithmetic operators for type T
|
||||
*/
|
||||
template<typename T>
|
||||
void add_opers_arithmetic(Dispatch_Engine &s)
|
||||
ModulePtr add_opers_arithmetic(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
add_opers_arithmetic_overload<T, T, T>(s);
|
||||
|
||||
add_opers_arithmetic_overload<T, T, T>(m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -487,17 +511,18 @@ namespace chaiscript
|
||||
* common conversions
|
||||
*/
|
||||
template<typename T>
|
||||
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
|
||||
ModulePtr bootstrap_pod_type(const std::string &name, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
s.add(user_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);
|
||||
s.add(fun(&to_string<T>), "to_string");
|
||||
s.add(fun(&parse_string<T>), "to_" + name);
|
||||
m->add(user_type<T>(), name);
|
||||
add_basic_constructors<T>(name, m);
|
||||
add_oper_assign<T>(m);
|
||||
add_oper_assign_pod<T>(m);
|
||||
add_construct_pod<T>(name, m);
|
||||
add_opers_arithmetic<T>(m);
|
||||
add_opers_arithmetic_modify_pod<T>(m);
|
||||
m->add(fun(&to_string<T>), "to_string");
|
||||
m->add(fun(&parse_string<T>), "to_" + name);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -530,157 +555,143 @@ namespace chaiscript
|
||||
* Class consisting of only static functions. All default bootstrapping occurs
|
||||
* from this class.
|
||||
*/
|
||||
struct Bootstrap
|
||||
class Bootstrap
|
||||
{
|
||||
/**
|
||||
* Function allowing for assignment of an unknown type to any other value
|
||||
*/
|
||||
static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
|
||||
{
|
||||
if (lhs.is_unknown())
|
||||
private:
|
||||
/**
|
||||
* Function allowing for assignment of an unknown type to any other value
|
||||
*/
|
||||
static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
|
||||
{
|
||||
return (lhs.assign(rhs));
|
||||
} else {
|
||||
throw bad_boxed_cast("boxed_value has a set type already");
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all comparison operators for POD types
|
||||
*/
|
||||
static void add_opers_comparison_pod(Dispatch_Engine &s)
|
||||
{
|
||||
s.add(fun(&equals<Boxed_POD_Value, Boxed_POD_Value>), "==");
|
||||
s.add(fun(¬_equals<Boxed_POD_Value, Boxed_POD_Value>), "!=");
|
||||
s.add(fun(&less_than<Boxed_POD_Value, Boxed_POD_Value>), "<");
|
||||
s.add(fun(&greater_than<Boxed_POD_Value, Boxed_POD_Value>), ">");
|
||||
s.add(fun(&less_than_equals<Boxed_POD_Value, Boxed_POD_Value>), "<=");
|
||||
s.add(fun(&greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>), ">=");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add all arithmetic operators for PODs
|
||||
*/
|
||||
static void add_opers_arithmetic_pod(Dispatch_Engine &s)
|
||||
{
|
||||
s.add(fun(&add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "+");
|
||||
s.add(fun(&subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "-");
|
||||
s.add(fun(÷<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "/");
|
||||
s.add(fun(&multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "*");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the two Boxed_Value's share the same internal type
|
||||
*/
|
||||
static bool type_match(Boxed_Value l, Boxed_Value r)
|
||||
{
|
||||
return l.get_type_info() == r.get_type_info();
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if the Boxed_Value matches the registered type by name
|
||||
*/
|
||||
static bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r)
|
||||
{
|
||||
try {
|
||||
return e.get_type(user_typename) == r.get_type_info();
|
||||
} catch (const std::range_error &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a bound function object. The first param is the function to bind
|
||||
* the remaining parameters are the args to bind into the
|
||||
* result
|
||||
*/
|
||||
static Boxed_Value bind_function(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() < 2)
|
||||
static void print(const std::string &s)
|
||||
{
|
||||
throw arity_error(params.size(), 2);
|
||||
std::cout << s;
|
||||
}
|
||||
|
||||
Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
|
||||
|
||||
return Boxed_Value(Proxy_Function(new Bound_Function(f,
|
||||
std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if a call can be made that consists of the first parameter
|
||||
* (the function) with the remaining parameters as its arguments.
|
||||
*/
|
||||
static Boxed_Value call_exists(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() < 1)
|
||||
static void println(const std::string &s)
|
||||
{
|
||||
throw arity_error(params.size(), 1);
|
||||
std::cout << s << std::endl;
|
||||
}
|
||||
|
||||
Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
|
||||
/**
|
||||
* Add all comparison operators for POD types
|
||||
*/
|
||||
static void add_opers_comparison_pod(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
m->add(fun(&detail::equals<Boxed_POD_Value, Boxed_POD_Value>), "==");
|
||||
m->add(fun(&detail::not_equals<Boxed_POD_Value, Boxed_POD_Value>), "!=");
|
||||
m->add(fun(&detail::less_than<Boxed_POD_Value, Boxed_POD_Value>), "<");
|
||||
m->add(fun(&detail::greater_than<Boxed_POD_Value, Boxed_POD_Value>), ">");
|
||||
m->add(fun(&detail::less_than_equals<Boxed_POD_Value, Boxed_POD_Value>), "<=");
|
||||
m->add(fun(&detail::greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>), ">=");
|
||||
}
|
||||
|
||||
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
|
||||
}
|
||||
/**
|
||||
* Add all arithmetic operators for PODs
|
||||
*/
|
||||
static void add_opers_arithmetic_pod(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
m->add(fun(&detail::add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "+");
|
||||
m->add(fun(&detail::subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "-");
|
||||
m->add(fun(&detail::divide<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "/");
|
||||
m->add(fun(&detail::multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "*");
|
||||
}
|
||||
|
||||
/**
|
||||
* perform all common bootstrap functions for std::string, void and POD types
|
||||
*/
|
||||
static void bootstrap(Dispatch_Engine &s)
|
||||
{
|
||||
s.add(user_type<void>(), "void");
|
||||
s.add(user_type<bool>(), "bool");
|
||||
s.add(user_type<Boxed_Value>(), "Object");
|
||||
s.add(user_type<Boxed_POD_Value>(), "PODObject");
|
||||
s.add(user_type<Proxy_Function>(), "function");
|
||||
/**
|
||||
* Create a bound function object. The first param is the function to bind
|
||||
* the remaining parameters are the args to bind into the
|
||||
* result
|
||||
*/
|
||||
static Boxed_Value bind_function(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() < 2)
|
||||
{
|
||||
throw arity_error(params.size(), 2);
|
||||
}
|
||||
|
||||
add_basic_constructors<bool>(s, "bool");
|
||||
add_oper_assign<std::string>(s);
|
||||
add_oper_assign<bool>(s);
|
||||
Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
|
||||
|
||||
s.add(fun(&to_string<const std::string &>), "internal_to_string");
|
||||
s.add(fun(&to_string<bool>), "internal_to_string");
|
||||
s.add(fun(&unknown_assign), "=");
|
||||
return Boxed_Value(Proxy_Function(new Bound_Function(f,
|
||||
std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
|
||||
}
|
||||
|
||||
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");
|
||||
/**
|
||||
* Returns true if a call can be made that consists of the first parameter
|
||||
* (the function) with the remaining parameters as its arguments.
|
||||
*/
|
||||
static Boxed_Value call_exists(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() < 1)
|
||||
{
|
||||
throw arity_error(params.size(), 1);
|
||||
}
|
||||
|
||||
add_opers_comparison_pod(s);
|
||||
add_opers_arithmetic_pod(s);
|
||||
s.add(fun(&modulus<int, int, int>), "%");
|
||||
Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
|
||||
|
||||
s.add(fun(&print), "print_string");
|
||||
s.add(fun(&println), "println_string");
|
||||
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
|
||||
}
|
||||
|
||||
s.add(fun(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s)))), "dump_system");
|
||||
s.add(fun(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(s)))), "dump_object");
|
||||
s.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1))),
|
||||
"is_type");
|
||||
static boost::shared_ptr<Dispatch_Engine> bootstrap2(boost::shared_ptr<Dispatch_Engine> e = boost::shared_ptr<Dispatch_Engine> (new Dispatch_Engine()))
|
||||
{
|
||||
e->add(user_type<void>(), "void");
|
||||
return e;
|
||||
}
|
||||
|
||||
s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
||||
"bind");
|
||||
public:
|
||||
/**
|
||||
* perform all common bootstrap functions for std::string, void and POD types
|
||||
*/
|
||||
static ModulePtr bootstrap(ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
m->add(user_type<void>(), "void");
|
||||
m->add(user_type<bool>(), "bool");
|
||||
m->add(user_type<Boxed_Value>(), "Object");
|
||||
m->add(user_type<Boxed_POD_Value>(), "PODObject");
|
||||
m->add(user_type<Proxy_Function>(), "function");
|
||||
|
||||
s.add(fun(&shared_ptr_clone<Proxy_Function_Base>), "clone");
|
||||
s.add(fun(&ptr_assign<Proxy_Function_Base>), "=");
|
||||
add_basic_constructors<bool>("bool", m);
|
||||
add_oper_assign<std::string>(m);
|
||||
add_oper_assign<bool>(m);
|
||||
|
||||
s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
|
||||
"call_exists");
|
||||
m->add(fun(&to_string<const std::string &>), "internal_to_string");
|
||||
m->add(fun(&to_string<bool>), "internal_to_string");
|
||||
m->add(fun(&unknown_assign), "=");
|
||||
|
||||
s.add(fun(&type_match), "type_match");
|
||||
}
|
||||
bootstrap_pod_type<double>("double", m);
|
||||
bootstrap_pod_type<int>("int", m);
|
||||
bootstrap_pod_type<size_t>("size_t", m);
|
||||
bootstrap_pod_type<char>("char", m);
|
||||
bootstrap_pod_type<boost::int64_t>("int64_t", m);
|
||||
|
||||
add_opers_comparison_pod(m);
|
||||
add_opers_arithmetic_pod(m);
|
||||
|
||||
m->add(fun(&detail::modulus<int, int, int>), "%");
|
||||
|
||||
m->add(fun(&print), "print_string");
|
||||
m->add(fun(&println), "println_string");
|
||||
|
||||
m->add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
||||
"bind");
|
||||
|
||||
m->add(fun(&shared_ptr_clone<Proxy_Function_Base>), "clone");
|
||||
m->add(fun(&ptr_assign<Proxy_Function_Base>), "=");
|
||||
|
||||
m->add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
|
||||
"call_exists");
|
||||
|
||||
m->add(fun(&type_match), "type_match");
|
||||
|
||||
return m;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -10,8 +10,8 @@
|
||||
* http://www.sgi.com/tech/stl/table_of_contents.html
|
||||
*/
|
||||
|
||||
#ifndef __bootstrap_stl_hpp
|
||||
#define __bootstrap_stl_hpp__
|
||||
#ifndef __stl_hpp_type
|
||||
#define __stl_hpp___type
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
#include "register_function.hpp"
|
||||
@@ -74,24 +74,25 @@ namespace chaiscript
|
||||
* Add Input_Range support for the given ContainerType
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr input_range_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
system.add(user_type<Input_Range<ContainerType> >(), type + "_Range");
|
||||
system.add(user_type<typename ContainerType::iterator>(), type+"_Iterator");
|
||||
m->add(user_type<Input_Range<ContainerType> >(), type + "_Range");
|
||||
m->add(user_type<typename ContainerType::iterator>(), type+"_Iterator");
|
||||
|
||||
system.add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
|
||||
system.add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
|
||||
m->add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
|
||||
m->add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
|
||||
|
||||
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair;
|
||||
|
||||
system.add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
|
||||
m->add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
|
||||
|
||||
system.add(user_type<ItrPair>(), type+"_Iterator_Pair");
|
||||
m->add(user_type<ItrPair>(), type+"_Iterator_Pair");
|
||||
|
||||
system.add(fun(&Input_Range<ContainerType>::empty), "empty");
|
||||
system.add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
|
||||
system.add(fun(&Input_Range<ContainerType>::front), "front");
|
||||
system.add(constructor<Input_Range<ContainerType> (const Input_Range<ContainerType> &)>(), "clone");
|
||||
m->add(fun(&Input_Range<ContainerType>::empty), "empty");
|
||||
m->add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
|
||||
m->add(fun(&Input_Range<ContainerType>::front), "front");
|
||||
m->add(constructor<Input_Range<ContainerType> (const Input_Range<ContainerType> &)>(), "clone");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,8 +100,9 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/ReversibleContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_reversible_container(Dispatch_Engine &/*system*/, const std::string &/*type*/)
|
||||
ModulePtr reversible_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,18 +110,19 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/RandomAccessContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_random_access_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr random_access_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_reversible_container<ContainerType>(system, type);
|
||||
reversible_container_type<ContainerType>(type, m);
|
||||
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
|
||||
|
||||
//In the interest of runtime safety for the system, we prefer the at() method for [] access,
|
||||
//In the interest of runtime safety for the m, we prefer the at() method for [] access,
|
||||
//to throw an exception in an out of bounds condition.
|
||||
system.add(
|
||||
m->add(
|
||||
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at))), "[]");
|
||||
system.add(
|
||||
m->add(
|
||||
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[]))), "at");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,10 +130,11 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/Assignable.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_assignable(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr assignable_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
add_basic_constructors<ContainerType>(system, type);
|
||||
add_oper_assign<ContainerType>(system);
|
||||
add_basic_constructors<ContainerType>(type, m);
|
||||
add_oper_assign<ContainerType>(m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,13 +142,15 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/Container.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_assignable<ContainerType>(system, type);
|
||||
assignable_type<ContainerType>(type, m);
|
||||
|
||||
system.add(fun(&ContainerType::size), "size");
|
||||
system.add(fun(&ContainerType::max_size), "max_size");
|
||||
system.add(fun(&ContainerType::empty), "empty");
|
||||
m->add(fun(&ContainerType::size), "size");
|
||||
m->add(fun(&ContainerType::max_size), "max_size");
|
||||
m->add(fun(&ContainerType::empty), "empty");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,10 +158,12 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/ForwardContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr forward_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_input_range<ContainerType>(system, type);
|
||||
bootstrap_container<ContainerType>(system, type);
|
||||
input_range_type<ContainerType>(type, m);
|
||||
container_type<ContainerType>(type, m);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,9 +171,10 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/DefaultConstructible.html
|
||||
*/
|
||||
template<typename Type>
|
||||
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr default_constructible_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
system.add(constructor<Type ()>(), type);
|
||||
m->add(constructor<Type ()>(), type);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,10 +218,10 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/Sequence.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_forward_container<ContainerType>(system, type);
|
||||
bootstrap_default_constructible<ContainerType>(system, type);
|
||||
forward_container_type<ContainerType>(type, m);
|
||||
default_constructible_type<ContainerType>(type, m);
|
||||
|
||||
std::string insert_name;
|
||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
|
||||
@@ -222,8 +231,10 @@ namespace chaiscript
|
||||
insert_name = "insert_at";
|
||||
}
|
||||
|
||||
system.add(fun(&insert_at<ContainerType>), insert_name);
|
||||
system.add(fun(&erase_at<ContainerType>), "erase_at");
|
||||
m->add(fun(&insert_at<ContainerType>), insert_name);
|
||||
m->add(fun(&erase_at<ContainerType>), "erase_at");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,14 +242,14 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/BackInsertionSequence.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_back_insertion_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr back_insertion_sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_sequence<ContainerType>(system, type);
|
||||
sequence_type<ContainerType>(type, m);
|
||||
|
||||
|
||||
typedef typename ContainerType::reference (ContainerType::*backptr)();
|
||||
|
||||
system.add(fun(backptr(&ContainerType::back)), "back");
|
||||
m->add(fun(backptr(&ContainerType::back)), "back");
|
||||
|
||||
std::string push_back_name;
|
||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
|
||||
@@ -248,8 +259,9 @@ namespace chaiscript
|
||||
push_back_name = "push_back";
|
||||
}
|
||||
|
||||
system.add(fun(&ContainerType::push_back), push_back_name);
|
||||
system.add(fun(&ContainerType::pop_back), "pop_back");
|
||||
m->add(fun(&ContainerType::push_back), push_back_name);
|
||||
m->add(fun(&ContainerType::pop_back), "pop_back");
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -257,11 +269,12 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/Vector.html
|
||||
*/
|
||||
template<typename VectorType>
|
||||
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr vector_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
system.add(user_type<VectorType>(), type);
|
||||
bootstrap_random_access_container<VectorType>(system, type);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
||||
m->add(user_type<VectorType>(), type);
|
||||
random_access_container_type<VectorType>(type, m);
|
||||
back_insertion_sequence_type<VectorType>(type, m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -269,10 +282,11 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/Vector.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_forward_container<ContainerType>(system, type);
|
||||
bootstrap_default_constructible<ContainerType>(system, type);
|
||||
forward_container_type<ContainerType>(type, m);
|
||||
default_constructible_type<ContainerType>(type, m);
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -280,18 +294,20 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/pair.html
|
||||
*/
|
||||
template<typename PairType>
|
||||
void bootstrap_pair(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr pair_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
system.add(user_type<PairType>(), type);
|
||||
m->add(user_type<PairType>(), type);
|
||||
|
||||
system.add(fun(&PairType::first), "first");
|
||||
system.add(fun(&PairType::second), "second");
|
||||
m->add(fun(&PairType::first), "first");
|
||||
m->add(fun(&PairType::second), "second");
|
||||
|
||||
system.add(constructor<PairType ()>(), type);
|
||||
system.add(constructor<PairType (const PairType &)>(), type);
|
||||
system.add(constructor<PairType (const PairType &)>(), "clone");
|
||||
system.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
|
||||
}
|
||||
m->add(constructor<PairType ()>(), type);
|
||||
m->add(constructor<PairType (const PairType &)>(), type);
|
||||
m->add(constructor<PairType (const PairType &)>(), "clone");
|
||||
m->add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -299,10 +315,12 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/PairAssociativeContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_pair_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr pair_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_associative_container<ContainerType>(system, type);
|
||||
bootstrap_pair<typename ContainerType::value_type>(system, type + "_Pair");
|
||||
associative_container_type<ContainerType>(type, m);
|
||||
pair_type<typename ContainerType::value_type>(type + "_Pair", m);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -310,10 +328,12 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr unique_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_associative_container<ContainerType>(system, type);
|
||||
system.add(fun(&ContainerType::count), "count");
|
||||
associative_container_type<ContainerType>(type, m);
|
||||
m->add(fun(&ContainerType::count), "count");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -321,14 +341,16 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/SortedAssociativeContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_sorted_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr sorted_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
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);
|
||||
system.add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
|
||||
reversible_container_type<ContainerType>(type, m);
|
||||
associative_container_type<ContainerType>(type, m);
|
||||
m->add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -336,10 +358,12 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/UniqueSortedAssociativeContainer.html
|
||||
*/
|
||||
template<typename ContainerType>
|
||||
void bootstrap_unique_sorted_associative_container(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr unique_sorted_associative_container_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
bootstrap_sorted_associative_container<ContainerType>(system, type);
|
||||
bootstrap_unique_associative_container<ContainerType>(system, type);
|
||||
sorted_associative_container_type<ContainerType>(type, m);
|
||||
unique_associative_container_type<ContainerType>(type, m);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -347,12 +371,14 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/Map.html
|
||||
*/
|
||||
template<typename MapType>
|
||||
void bootstrap_map(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr map_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
system.add(user_type<MapType>(), type);
|
||||
system.add(fun(&MapType::operator[]), "[]");
|
||||
bootstrap_unique_sorted_associative_container<MapType>(system, type);
|
||||
bootstrap_pair_associative_container<MapType>(system, type);
|
||||
m->add(user_type<MapType>(), type);
|
||||
m->add(fun(&MapType::operator[]), "[]");
|
||||
unique_sorted_associative_container_type<MapType>(type, m);
|
||||
pair_associative_container_type<MapType>(type, m);
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -360,21 +386,23 @@ namespace chaiscript
|
||||
* http://www.sgi.com/tech/stl/basic_string.html
|
||||
*/
|
||||
template<typename String>
|
||||
void bootstrap_string(Dispatch_Engine &system, const std::string &type)
|
||||
ModulePtr string_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
|
||||
{
|
||||
system.add(user_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);
|
||||
m->add(user_type<String>(), type);
|
||||
add_oper_add<String>(m);
|
||||
add_oper_add_equals<String>(m);
|
||||
add_opers_comparison<String>(m);
|
||||
random_access_container_type<String>(type, m);
|
||||
sequence_type<String>(type, m);
|
||||
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
|
||||
system.add(fun(find_func(&String::find)), "find");
|
||||
system.add(fun(find_func(&String::rfind)), "rfind");
|
||||
system.add(fun(find_func(&String::find_first_of)), "find_first_of");
|
||||
system.add(fun(find_func(&String::find_last_of)), "find_last_of");
|
||||
system.add(fun(find_func(&String::find_first_not_of)), "find_first_not_of");
|
||||
system.add(fun(find_func(&String::find_last_not_of)), "find_last_not_of");
|
||||
m->add(fun(find_func(&String::find)), "find");
|
||||
m->add(fun(find_func(&String::rfind)), "rfind");
|
||||
m->add(fun(find_func(&String::find_first_of)), "find_first_of");
|
||||
m->add(fun(find_func(&String::find_last_of)), "find_last_of");
|
||||
m->add(fun(find_func(&String::find_first_not_of)), "find_first_not_of");
|
||||
m->add(fun(find_func(&String::find_last_not_of)), "find_last_not_of");
|
||||
|
||||
return m;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -682,6 +682,15 @@ namespace chaiscript
|
||||
{
|
||||
return Boxed_Value(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the two Boxed_Value's share the same internal type
|
||||
*/
|
||||
static bool type_match(Boxed_Value l, Boxed_Value r)
|
||||
{
|
||||
return l.get_type_info() == r.get_type_info();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -25,6 +25,45 @@
|
||||
|
||||
namespace chaiscript
|
||||
{
|
||||
class Module
|
||||
{
|
||||
public:
|
||||
Module &add(const Type_Info &ti, const std::string &name)
|
||||
{
|
||||
m_typeinfos.push_back(std::make_pair(ti, name));
|
||||
return *this;
|
||||
}
|
||||
|
||||
Module &add(const Proxy_Function &f, const std::string &name)
|
||||
{
|
||||
m_funcs.push_back(std::make_pair(f, name));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void apply(T &t) const
|
||||
{
|
||||
apply(m_typeinfos.begin(), m_typeinfos.end(), t);
|
||||
apply(m_funcs.begin(), m_funcs.end(), t);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::pair<Type_Info, std::string> > m_typeinfos;
|
||||
std::vector<std::pair<Proxy_Function, std::string> > m_funcs;
|
||||
|
||||
template<typename T, typename InItr>
|
||||
void apply(InItr begin, InItr end, T &t) const
|
||||
{
|
||||
while (begin != end)
|
||||
{
|
||||
t.add(begin->first, begin->second);
|
||||
++begin;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<Module> ModulePtr;
|
||||
|
||||
/**
|
||||
* A Proxy_Function implementation that is able to take
|
||||
* a vector of Proxy_Functions and perform a dispatch on them. It is
|
||||
@@ -110,6 +149,14 @@ namespace chaiscript
|
||||
return add_function(f, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a module's worth of registrations to the system
|
||||
*/
|
||||
void add(const ModulePtr &m)
|
||||
{
|
||||
m->apply(*this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of an object, by name. If the object
|
||||
* is not available in the current scope it is created
|
||||
@@ -411,6 +458,19 @@ namespace chaiscript
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if the Boxed_Value matches the registered type by name
|
||||
*/
|
||||
static bool is_type(const Dispatch_Engine &e, const std::string &user_typename, Boxed_Value r)
|
||||
{
|
||||
try {
|
||||
return e.get_type(user_typename) == r.get_type_info();
|
||||
} catch (const std::range_error &) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -59,11 +59,20 @@ namespace chaiscript
|
||||
* Adds an object to the system: type, function, object
|
||||
*/
|
||||
template<typename T>
|
||||
void add(const T &t, const std::string &name)
|
||||
ChaiScript_System &add(const T &t, const std::string &name)
|
||||
{
|
||||
engine.add(t, name);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a module object to the system
|
||||
*/
|
||||
ChaiScript_System &add(const ModulePtr &p)
|
||||
{
|
||||
engine.add(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper for calling script code as if it were native C++ code
|
||||
* example:
|
||||
@@ -129,11 +138,18 @@ namespace chaiscript
|
||||
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
|
||||
*/
|
||||
void build_eval_system() {
|
||||
Bootstrap::bootstrap(engine);
|
||||
bootstrap_vector<std::vector<Boxed_Value> >(engine, "Vector");
|
||||
bootstrap_string<std::string>(engine, "string");
|
||||
bootstrap_map<std::map<std::string, Boxed_Value> >(engine, "Map");
|
||||
bootstrap_pair<std::pair<Boxed_Value, Boxed_Value > >(engine, "Pair");
|
||||
engine.add(Bootstrap::bootstrap());
|
||||
|
||||
engine.add(fun(boost::function<void ()>(boost::bind(&dump_system, boost::ref(engine)))), "dump_system");
|
||||
engine.add(fun(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(engine)))), "dump_object");
|
||||
engine.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(engine), _2, _1))),
|
||||
"is_type");
|
||||
|
||||
|
||||
engine.add(vector_type<std::vector<Boxed_Value> >("Vector"));
|
||||
engine.add(string_type<std::string>("string"));
|
||||
engine.add(map_type<std::map<std::string, Boxed_Value> >("Map"));
|
||||
engine.add(pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
|
||||
|
||||
engine.add(Proxy_Function(
|
||||
new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::internal_eval, boost::ref(*this), _1), 1)), "eval");
|
||||
|
@@ -138,7 +138,7 @@ namespace chaiscript
|
||||
}
|
||||
else if (node->children[i+1]->text == ":=") {
|
||||
Boxed_Value lhs = eval_token(ss, node->children[i]);
|
||||
if (lhs.is_unknown() || Bootstrap::type_match(lhs, retval)) {
|
||||
if (lhs.is_unknown() || type_match(lhs, retval)) {
|
||||
lhs.assign(retval);
|
||||
}
|
||||
else {
|
||||
|
@@ -116,10 +116,16 @@ int main(int argc, char *argv[]) {
|
||||
// Add usage model for mixed use:
|
||||
// chai.eval("call(?, ?)", 5, "hello world"); or something
|
||||
|
||||
// add examples for and clean up usage of bootstrap stuffs
|
||||
|
||||
//Creating a functor on the stack and using it immediatly
|
||||
int x = chai.functor<int (int, int)>("fun (x, y) { return x + y; }")(5, 6);
|
||||
|
||||
log("Functor test output", boost::lexical_cast<std::string>(x));
|
||||
|
||||
|
||||
//Ability to create our own container types when needed. std::vector and std::map are
|
||||
//mostly supported currently
|
||||
chai.add(vector_type<std::vector<int> >("IntVector"));
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user