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,6 +12,8 @@
|
|||||||
|
|
||||||
namespace chaiscript
|
namespace chaiscript
|
||||||
{
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Set of helper functions for common operators
|
* Set of helper functions for common operators
|
||||||
*/
|
*/
|
||||||
@@ -207,78 +209,86 @@ namespace chaiscript
|
|||||||
return p1 -= P1(r.i);
|
return p1 -= P1(r.i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add canonical form of "=" for type T
|
* Add canonical form of "=" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "+" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "+=" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "-" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "/" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "*" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "!=" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add user defined assignment operator for T = U
|
||||||
*/
|
*/
|
||||||
template<typename T, typename 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
|
* Add canonical form of "=" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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.
|
* Add assignment operator for T = POD.
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
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
|
* Add canonical form of "<" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of ">" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of "<=" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* Add canonical form of ">=" for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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.
|
* Examples: T < R, T == R, etc.
|
||||||
*/
|
*/
|
||||||
template<typename T, typename R>
|
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&>), "==");
|
m->add(fun(&detail::equals<const T&, const R&>), "==");
|
||||||
s.add(fun(¬_equals<const T&, const R&>), "!=");
|
m->add(fun(&detail::not_equals<const T&, const R&>), "!=");
|
||||||
s.add(fun(&less_than<const T&, const R&>), "<");
|
m->add(fun(&detail::less_than<const T&, const R&>), "<");
|
||||||
s.add(fun(&greater_than<const T&, const R&>), ">");
|
m->add(fun(&detail::greater_than<const T&, const R&>), ">");
|
||||||
s.add(fun(&less_than_equals<const T&, const R&>), "<=");
|
m->add(fun(&detail::less_than_equals<const T&, const R&>), "<=");
|
||||||
s.add(fun(&greater_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
|
* Add canonical forms of all comparison operators for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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;
|
* T *= R;
|
||||||
*/
|
*/
|
||||||
template<typename Ret, typename T, typename 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>), "+");
|
m->add(fun(&detail::add<Ret, T, R>), "+");
|
||||||
s.add(fun(&subtract<Ret, T, R>), "-");
|
m->add(fun(&detail::subtract<Ret, T, R>), "-");
|
||||||
s.add(fun(÷<Ret, T, R>), "/");
|
m->add(fun(&detail::divide<Ret, T, R>), "/");
|
||||||
s.add(fun(&multiply<Ret, T, R>), "*");
|
m->add(fun(&detail::multiply<Ret, T, R>), "*");
|
||||||
s.add(fun(×equal<T, R>), "*=");
|
m->add(fun(&detail::timesequal<T, R>), "*=");
|
||||||
s.add(fun(÷sequal<T, R>), "/=");
|
m->add(fun(&detail::dividesequal<T, R>), "/=");
|
||||||
s.add(fun(&subtractsequal<T, R>), "-=");
|
m->add(fun(&detail::subtractsequal<T, R>), "-=");
|
||||||
s.add(fun(&addsequal<T, R>), "+=");
|
m->add(fun(&detail::addsequal<T, R>), "+=");
|
||||||
s.add(fun(&prefixincrement<T>), "++");
|
m->add(fun(&detail::prefixincrement<T>), "++");
|
||||||
s.add(fun(&prefixdecrement<T>), "--");
|
m->add(fun(&detail::prefixdecrement<T>), "--");
|
||||||
s.add(fun(&prefixnegate<T>), "-");
|
m->add(fun(&detail::prefixnegate<T>), "-");
|
||||||
s.add(fun(&prefixnot<T>), "!");
|
m->add(fun(&detail::prefixnot<T>), "!");
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -391,12 +410,13 @@ namespace chaiscript
|
|||||||
* example: POD *= T, POD /= T
|
* example: POD *= T, POD /= T
|
||||||
*/
|
*/
|
||||||
template<typename 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>), "*=");
|
m->add(fun(&detail::timesequal_pod<T>), "*=");
|
||||||
s.add(fun(÷sequal_pod<T>), "/=");
|
m->add(fun(&detail::dividesequal_pod<T>), "/=");
|
||||||
s.add(fun(&subtractsequal_pod<T>), "-=");
|
m->add(fun(&detail::subtractsequal_pod<T>), "-=");
|
||||||
s.add(fun(&addsequal_pod<T>), "+=");
|
m->add(fun(&detail::addsequal_pod<T>), "+=");
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -405,29 +425,32 @@ namespace chaiscript
|
|||||||
* the copy constructor.
|
* the copy constructor.
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
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);
|
m->add(constructor<T (const T &)>(), type);
|
||||||
s.add(constructor<T (const T &)>(), "clone");
|
m->add(constructor<T (const T &)>(), "clone");
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add default and copy constructors (including "clone") for type T
|
* Add default and copy constructors (including "clone") for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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);
|
m->add(constructor<T ()>(), type);
|
||||||
add_copy_constructor<T>(s, type);
|
add_copy_constructor<T>(type, m);
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add POD type constructor for type T. ie: T = type(POD)
|
* Add POD type constructor for type T. ie: T = type(POD)
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
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 &)
|
* T = type(const U &)
|
||||||
*/
|
*/
|
||||||
template<typename T, typename 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
|
* Add canonical forms of all arithmetic operators for type T
|
||||||
*/
|
*/
|
||||||
template<typename 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
|
* common conversions
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
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);
|
m->add(user_type<T>(), name);
|
||||||
add_basic_constructors<T>(s, name);
|
add_basic_constructors<T>(name, m);
|
||||||
add_oper_assign<T>(s);
|
add_oper_assign<T>(m);
|
||||||
add_oper_assign_pod<T>(s);
|
add_oper_assign_pod<T>(m);
|
||||||
add_construct_pod<T>(s, name);
|
add_construct_pod<T>(name, m);
|
||||||
add_opers_arithmetic<T>(s);
|
add_opers_arithmetic<T>(m);
|
||||||
add_opers_arithmetic_modify_pod<T>(s);
|
add_opers_arithmetic_modify_pod<T>(m);
|
||||||
s.add(fun(&to_string<T>), "to_string");
|
m->add(fun(&to_string<T>), "to_string");
|
||||||
s.add(fun(&parse_string<T>), "to_" + name);
|
m->add(fun(&parse_string<T>), "to_" + name);
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -530,8 +555,9 @@ namespace chaiscript
|
|||||||
* Class consisting of only static functions. All default bootstrapping occurs
|
* Class consisting of only static functions. All default bootstrapping occurs
|
||||||
* from this class.
|
* from this class.
|
||||||
*/
|
*/
|
||||||
struct Bootstrap
|
class Bootstrap
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
/**
|
/**
|
||||||
* Function allowing for assignment of an unknown type to any other value
|
* Function allowing for assignment of an unknown type to any other value
|
||||||
*/
|
*/
|
||||||
@@ -558,45 +584,25 @@ namespace chaiscript
|
|||||||
/**
|
/**
|
||||||
* Add all comparison operators for POD types
|
* Add all comparison operators for POD types
|
||||||
*/
|
*/
|
||||||
static void add_opers_comparison_pod(Dispatch_Engine &s)
|
static void add_opers_comparison_pod(ModulePtr m = ModulePtr(new Module()))
|
||||||
{
|
{
|
||||||
s.add(fun(&equals<Boxed_POD_Value, Boxed_POD_Value>), "==");
|
m->add(fun(&detail::equals<Boxed_POD_Value, Boxed_POD_Value>), "==");
|
||||||
s.add(fun(¬_equals<Boxed_POD_Value, Boxed_POD_Value>), "!=");
|
m->add(fun(&detail::not_equals<Boxed_POD_Value, Boxed_POD_Value>), "!=");
|
||||||
s.add(fun(&less_than<Boxed_POD_Value, Boxed_POD_Value>), "<");
|
m->add(fun(&detail::less_than<Boxed_POD_Value, Boxed_POD_Value>), "<");
|
||||||
s.add(fun(&greater_than<Boxed_POD_Value, Boxed_POD_Value>), ">");
|
m->add(fun(&detail::greater_than<Boxed_POD_Value, Boxed_POD_Value>), ">");
|
||||||
s.add(fun(&less_than_equals<Boxed_POD_Value, Boxed_POD_Value>), "<=");
|
m->add(fun(&detail::less_than_equals<Boxed_POD_Value, Boxed_POD_Value>), "<=");
|
||||||
s.add(fun(&greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>), ">=");
|
m->add(fun(&detail::greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>), ">=");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add all arithmetic operators for PODs
|
* Add all arithmetic operators for PODs
|
||||||
*/
|
*/
|
||||||
static void add_opers_arithmetic_pod(Dispatch_Engine &s)
|
static void add_opers_arithmetic_pod(ModulePtr m = ModulePtr(new Module()))
|
||||||
{
|
{
|
||||||
s.add(fun(&add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "+");
|
m->add(fun(&detail::add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "+");
|
||||||
s.add(fun(&subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "-");
|
m->add(fun(&detail::subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "-");
|
||||||
s.add(fun(÷<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "/");
|
m->add(fun(&detail::divide<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "/");
|
||||||
s.add(fun(&multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "*");
|
m->add(fun(&detail::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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -633,53 +639,58 @@ namespace chaiscript
|
|||||||
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
|
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
/**
|
/**
|
||||||
* perform all common bootstrap functions for std::string, void and POD types
|
* perform all common bootstrap functions for std::string, void and POD types
|
||||||
*/
|
*/
|
||||||
static void bootstrap(Dispatch_Engine &s)
|
static ModulePtr bootstrap(ModulePtr m = ModulePtr(new Module()))
|
||||||
{
|
{
|
||||||
s.add(user_type<void>(), "void");
|
m->add(user_type<void>(), "void");
|
||||||
s.add(user_type<bool>(), "bool");
|
m->add(user_type<bool>(), "bool");
|
||||||
s.add(user_type<Boxed_Value>(), "Object");
|
m->add(user_type<Boxed_Value>(), "Object");
|
||||||
s.add(user_type<Boxed_POD_Value>(), "PODObject");
|
m->add(user_type<Boxed_POD_Value>(), "PODObject");
|
||||||
s.add(user_type<Proxy_Function>(), "function");
|
m->add(user_type<Proxy_Function>(), "function");
|
||||||
|
|
||||||
add_basic_constructors<bool>(s, "bool");
|
add_basic_constructors<bool>("bool", m);
|
||||||
add_oper_assign<std::string>(s);
|
add_oper_assign<std::string>(m);
|
||||||
add_oper_assign<bool>(s);
|
add_oper_assign<bool>(m);
|
||||||
|
|
||||||
s.add(fun(&to_string<const std::string &>), "internal_to_string");
|
m->add(fun(&to_string<const std::string &>), "internal_to_string");
|
||||||
s.add(fun(&to_string<bool>), "internal_to_string");
|
m->add(fun(&to_string<bool>), "internal_to_string");
|
||||||
s.add(fun(&unknown_assign), "=");
|
m->add(fun(&unknown_assign), "=");
|
||||||
|
|
||||||
bootstrap_pod_type<double>(s, "double");
|
bootstrap_pod_type<double>("double", m);
|
||||||
bootstrap_pod_type<int>(s, "int");
|
bootstrap_pod_type<int>("int", m);
|
||||||
bootstrap_pod_type<size_t>(s, "size_t");
|
bootstrap_pod_type<size_t>("size_t", m);
|
||||||
bootstrap_pod_type<char>(s, "char");
|
bootstrap_pod_type<char>("char", m);
|
||||||
bootstrap_pod_type<boost::int64_t>(s, "int64_t");
|
bootstrap_pod_type<boost::int64_t>("int64_t", m);
|
||||||
|
|
||||||
add_opers_comparison_pod(s);
|
add_opers_comparison_pod(m);
|
||||||
add_opers_arithmetic_pod(s);
|
add_opers_arithmetic_pod(m);
|
||||||
s.add(fun(&modulus<int, int, int>), "%");
|
|
||||||
|
|
||||||
s.add(fun(&print), "print_string");
|
m->add(fun(&detail::modulus<int, int, int>), "%");
|
||||||
s.add(fun(&println), "println_string");
|
|
||||||
|
|
||||||
s.add(fun(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s)))), "dump_system");
|
m->add(fun(&print), "print_string");
|
||||||
s.add(fun(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(s)))), "dump_object");
|
m->add(fun(&println), "println_string");
|
||||||
s.add(fun(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1))),
|
|
||||||
"is_type");
|
|
||||||
|
|
||||||
s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
m->add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
|
||||||
"bind");
|
"bind");
|
||||||
|
|
||||||
s.add(fun(&shared_ptr_clone<Proxy_Function_Base>), "clone");
|
m->add(fun(&shared_ptr_clone<Proxy_Function_Base>), "clone");
|
||||||
s.add(fun(&ptr_assign<Proxy_Function_Base>), "=");
|
m->add(fun(&ptr_assign<Proxy_Function_Base>), "=");
|
||||||
|
|
||||||
s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
|
m->add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
|
||||||
"call_exists");
|
"call_exists");
|
||||||
|
|
||||||
s.add(fun(&type_match), "type_match");
|
m->add(fun(&type_match), "type_match");
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -10,8 +10,8 @@
|
|||||||
* http://www.sgi.com/tech/stl/table_of_contents.html
|
* http://www.sgi.com/tech/stl/table_of_contents.html
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef __bootstrap_stl_hpp
|
#ifndef __stl_hpp_type
|
||||||
#define __bootstrap_stl_hpp__
|
#define __stl_hpp___type
|
||||||
|
|
||||||
#include "dispatchkit.hpp"
|
#include "dispatchkit.hpp"
|
||||||
#include "register_function.hpp"
|
#include "register_function.hpp"
|
||||||
@@ -74,24 +74,25 @@ namespace chaiscript
|
|||||||
* Add Input_Range support for the given ContainerType
|
* Add Input_Range support for the given ContainerType
|
||||||
*/
|
*/
|
||||||
template<typename 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");
|
m->add(user_type<Input_Range<ContainerType> >(), type + "_Range");
|
||||||
system.add(user_type<typename ContainerType::iterator>(), type+"_Iterator");
|
m->add(user_type<typename ContainerType::iterator>(), type+"_Iterator");
|
||||||
|
|
||||||
system.add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
|
m->add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
|
||||||
system.add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
|
m->add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
|
||||||
|
|
||||||
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair;
|
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");
|
m->add(fun(&Input_Range<ContainerType>::empty), "empty");
|
||||||
system.add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
|
m->add(fun(&Input_Range<ContainerType>::pop_front), "pop_front");
|
||||||
system.add(fun(&Input_Range<ContainerType>::front), "front");
|
m->add(fun(&Input_Range<ContainerType>::front), "front");
|
||||||
system.add(constructor<Input_Range<ContainerType> (const Input_Range<ContainerType> &)>(), "clone");
|
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
|
* http://www.sgi.com/tech/stl/ReversibleContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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
|
* http://www.sgi.com/tech/stl/RandomAccessContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
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.
|
//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))), "[]");
|
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");
|
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
|
* http://www.sgi.com/tech/stl/Assignable.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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_basic_constructors<ContainerType>(type, m);
|
||||||
add_oper_assign<ContainerType>(system);
|
add_oper_assign<ContainerType>(m);
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,13 +142,15 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/Container.html
|
* http://www.sgi.com/tech/stl/Container.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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");
|
m->add(fun(&ContainerType::size), "size");
|
||||||
system.add(fun(&ContainerType::max_size), "max_size");
|
m->add(fun(&ContainerType::max_size), "max_size");
|
||||||
system.add(fun(&ContainerType::empty), "empty");
|
m->add(fun(&ContainerType::empty), "empty");
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,10 +158,12 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/ForwardContainer.html
|
* http://www.sgi.com/tech/stl/ForwardContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
input_range_type<ContainerType>(type, m);
|
||||||
bootstrap_container<ContainerType>(system, type);
|
container_type<ContainerType>(type, m);
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,9 +171,10 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/DefaultConstructible.html
|
* http://www.sgi.com/tech/stl/DefaultConstructible.html
|
||||||
*/
|
*/
|
||||||
template<typename Type>
|
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
|
* http://www.sgi.com/tech/stl/Sequence.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
forward_container_type<ContainerType>(type, m);
|
||||||
bootstrap_default_constructible<ContainerType>(system, type);
|
default_constructible_type<ContainerType>(type, m);
|
||||||
|
|
||||||
std::string insert_name;
|
std::string insert_name;
|
||||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
|
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
|
||||||
@@ -222,8 +231,10 @@ namespace chaiscript
|
|||||||
insert_name = "insert_at";
|
insert_name = "insert_at";
|
||||||
}
|
}
|
||||||
|
|
||||||
system.add(fun(&insert_at<ContainerType>), insert_name);
|
m->add(fun(&insert_at<ContainerType>), insert_name);
|
||||||
system.add(fun(&erase_at<ContainerType>), "erase_at");
|
m->add(fun(&erase_at<ContainerType>), "erase_at");
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -231,14 +242,14 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/BackInsertionSequence.html
|
* http://www.sgi.com/tech/stl/BackInsertionSequence.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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)();
|
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;
|
std::string push_back_name;
|
||||||
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
|
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
|
||||||
@@ -248,8 +259,9 @@ namespace chaiscript
|
|||||||
push_back_name = "push_back";
|
push_back_name = "push_back";
|
||||||
}
|
}
|
||||||
|
|
||||||
system.add(fun(&ContainerType::push_back), push_back_name);
|
m->add(fun(&ContainerType::push_back), push_back_name);
|
||||||
system.add(fun(&ContainerType::pop_back), "pop_back");
|
m->add(fun(&ContainerType::pop_back), "pop_back");
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -257,11 +269,12 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/Vector.html
|
* http://www.sgi.com/tech/stl/Vector.html
|
||||||
*/
|
*/
|
||||||
template<typename VectorType>
|
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);
|
m->add(user_type<VectorType>(), type);
|
||||||
bootstrap_random_access_container<VectorType>(system, type);
|
random_access_container_type<VectorType>(type, m);
|
||||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
back_insertion_sequence_type<VectorType>(type, m);
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -269,10 +282,11 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/Vector.html
|
* http://www.sgi.com/tech/stl/Vector.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
forward_container_type<ContainerType>(type, m);
|
||||||
bootstrap_default_constructible<ContainerType>(system, type);
|
default_constructible_type<ContainerType>(type, m);
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -280,17 +294,19 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/pair.html
|
* http://www.sgi.com/tech/stl/pair.html
|
||||||
*/
|
*/
|
||||||
template<typename PairType>
|
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");
|
m->add(fun(&PairType::first), "first");
|
||||||
system.add(fun(&PairType::second), "second");
|
m->add(fun(&PairType::second), "second");
|
||||||
|
|
||||||
system.add(constructor<PairType ()>(), type);
|
m->add(constructor<PairType ()>(), type);
|
||||||
system.add(constructor<PairType (const PairType &)>(), type);
|
m->add(constructor<PairType (const PairType &)>(), type);
|
||||||
system.add(constructor<PairType (const PairType &)>(), "clone");
|
m->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 (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
|
* http://www.sgi.com/tech/stl/PairAssociativeContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
associative_container_type<ContainerType>(type, m);
|
||||||
bootstrap_pair<typename ContainerType::value_type>(system, type + "_Pair");
|
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
|
* http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
associative_container_type<ContainerType>(type, m);
|
||||||
system.add(fun(&ContainerType::count), "count");
|
m->add(fun(&ContainerType::count), "count");
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -321,14 +341,16 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/SortedAssociativeContainer.html
|
* http://www.sgi.com/tech/stl/SortedAssociativeContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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>
|
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator>
|
||||||
(ContainerType::*eq_range)(const typename ContainerType::key_type &);
|
(ContainerType::*eq_range)(const typename ContainerType::key_type &);
|
||||||
|
|
||||||
bootstrap_reversible_container<ContainerType>(system, type);
|
reversible_container_type<ContainerType>(type, m);
|
||||||
bootstrap_associative_container<ContainerType>(system, type);
|
associative_container_type<ContainerType>(type, m);
|
||||||
system.add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
|
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
|
* http://www.sgi.com/tech/stl/UniqueSortedAssociativeContainer.html
|
||||||
*/
|
*/
|
||||||
template<typename ContainerType>
|
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);
|
sorted_associative_container_type<ContainerType>(type, m);
|
||||||
bootstrap_unique_associative_container<ContainerType>(system, type);
|
unique_associative_container_type<ContainerType>(type, m);
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -347,12 +371,14 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/Map.html
|
* http://www.sgi.com/tech/stl/Map.html
|
||||||
*/
|
*/
|
||||||
template<typename MapType>
|
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);
|
m->add(user_type<MapType>(), type);
|
||||||
system.add(fun(&MapType::operator[]), "[]");
|
m->add(fun(&MapType::operator[]), "[]");
|
||||||
bootstrap_unique_sorted_associative_container<MapType>(system, type);
|
unique_sorted_associative_container_type<MapType>(type, m);
|
||||||
bootstrap_pair_associative_container<MapType>(system, type);
|
pair_associative_container_type<MapType>(type, m);
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -360,21 +386,23 @@ namespace chaiscript
|
|||||||
* http://www.sgi.com/tech/stl/basic_string.html
|
* http://www.sgi.com/tech/stl/basic_string.html
|
||||||
*/
|
*/
|
||||||
template<typename String>
|
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);
|
m->add(user_type<String>(), type);
|
||||||
add_oper_add<String>(system);
|
add_oper_add<String>(m);
|
||||||
add_oper_add_equals<String>(system);
|
add_oper_add_equals<String>(m);
|
||||||
add_opers_comparison<String>(system);
|
add_opers_comparison<String>(m);
|
||||||
bootstrap_random_access_container<String>(system, type);
|
random_access_container_type<String>(type, m);
|
||||||
bootstrap_sequence<String>(system, type);
|
sequence_type<String>(type, m);
|
||||||
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
|
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
|
||||||
system.add(fun(find_func(&String::find)), "find");
|
m->add(fun(find_func(&String::find)), "find");
|
||||||
system.add(fun(find_func(&String::rfind)), "rfind");
|
m->add(fun(find_func(&String::rfind)), "rfind");
|
||||||
system.add(fun(find_func(&String::find_first_of)), "find_first_of");
|
m->add(fun(find_func(&String::find_first_of)), "find_first_of");
|
||||||
system.add(fun(find_func(&String::find_last_of)), "find_last_of");
|
m->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");
|
m->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_last_not_of)), "find_last_not_of");
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -682,6 +682,15 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
return Boxed_Value(t);
|
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
|
#endif
|
||||||
|
@@ -25,6 +25,45 @@
|
|||||||
|
|
||||||
namespace chaiscript
|
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 Proxy_Function implementation that is able to take
|
||||||
* a vector of Proxy_Functions and perform a dispatch on them. It is
|
* a vector of Proxy_Functions and perform a dispatch on them. It is
|
||||||
@@ -110,6 +149,14 @@ namespace chaiscript
|
|||||||
return add_function(f, name);
|
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
|
* Set the value of an object, by name. If the object
|
||||||
* is not available in the current scope it is created
|
* is not available in the current scope it is created
|
||||||
@@ -411,6 +458,19 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
std::cout << std::endl;
|
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
|
#endif
|
||||||
|
@@ -59,9 +59,18 @@ namespace chaiscript
|
|||||||
* Adds an object to the system: type, function, object
|
* Adds an object to the system: type, function, object
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
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);
|
engine.add(t, name);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a module object to the system
|
||||||
|
*/
|
||||||
|
ChaiScript_System &add(const ModulePtr &p)
|
||||||
|
{
|
||||||
|
engine.add(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -129,11 +138,18 @@ namespace chaiscript
|
|||||||
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
|
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
|
||||||
*/
|
*/
|
||||||
void build_eval_system() {
|
void build_eval_system() {
|
||||||
Bootstrap::bootstrap(engine);
|
engine.add(Bootstrap::bootstrap());
|
||||||
bootstrap_vector<std::vector<Boxed_Value> >(engine, "Vector");
|
|
||||||
bootstrap_string<std::string>(engine, "string");
|
engine.add(fun(boost::function<void ()>(boost::bind(&dump_system, boost::ref(engine)))), "dump_system");
|
||||||
bootstrap_map<std::map<std::string, Boxed_Value> >(engine, "Map");
|
engine.add(fun(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(engine)))), "dump_object");
|
||||||
bootstrap_pair<std::pair<Boxed_Value, Boxed_Value > >(engine, "Pair");
|
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(
|
engine.add(Proxy_Function(
|
||||||
new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::internal_eval, boost::ref(*this), _1), 1)), "eval");
|
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 == ":=") {
|
else if (node->children[i+1]->text == ":=") {
|
||||||
Boxed_Value lhs = eval_token(ss, node->children[i]);
|
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);
|
lhs.assign(retval);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@@ -116,10 +116,16 @@ int main(int argc, char *argv[]) {
|
|||||||
// Add usage model for mixed use:
|
// Add usage model for mixed use:
|
||||||
// chai.eval("call(?, ?)", 5, "hello world"); or something
|
// 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);
|
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));
|
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