Major updates to the C++ API. Please see trunk/src/example.cpp to follow along

This commit is contained in:
Jason Turner
2009-07-18 18:05:54 +00:00
parent ac817ff33a
commit 4d4c26bf73
14 changed files with 451 additions and 416 deletions

View File

@@ -10,7 +10,7 @@
#include "dispatchkit.hpp"
#include "register_function.hpp"
namespace dispatchkit
namespace chaiscript
{
/**
* Set of helper functions for common operators
@@ -215,7 +215,7 @@ namespace dispatchkit
template<typename T>
void add_oper_equals(Dispatch_Engine &s)
{
register_function(s, &equals<const T&, const T&>, "=");
s.add(fun(&equals<const T&, const T&>), "=");
}
/**
@@ -224,7 +224,7 @@ namespace dispatchkit
template<typename T>
void add_oper_add(Dispatch_Engine &s)
{
register_function(s, &add<T, const T&, const T&>, "+");
s.add(fun(&add<T, const T&, const T&>), "+");
}
/**
@@ -233,7 +233,7 @@ namespace dispatchkit
template<typename T>
void add_oper_add_equals(Dispatch_Engine &s)
{
register_function(s, &addsequal<T, T>, "+=");
s.add(fun(&addsequal<T, T>), "+=");
}
/**
@@ -242,7 +242,7 @@ namespace dispatchkit
template<typename T>
void add_oper_subtract(Dispatch_Engine &s)
{
register_function(s, &subtract<T, const T&, const T&>, "-");
s.add(fun(&subtract<T, const T&, const T&>), "-");
}
/**
@@ -251,7 +251,7 @@ namespace dispatchkit
template<typename T>
void add_oper_divide(Dispatch_Engine &s)
{
register_function(s, &divide<T, const T&, const T&>, "/");
s.add(fun(&divide<T, const T&, const T&>), "/");
}
/**
@@ -260,7 +260,7 @@ namespace dispatchkit
template<typename T>
void add_oper_multiply(Dispatch_Engine &s)
{
register_function(s, &multiply<T, const T&, const T&>, "*");
s.add(fun(&multiply<T, const T&, const T&>), "*");
}
/**
@@ -269,7 +269,7 @@ namespace dispatchkit
template<typename T>
void add_oper_not_equals(Dispatch_Engine &s)
{
register_function(s, &not_equals<const T&, const T&>, "!=");
s.add(fun(&not_equals<const T&, const T&>), "!=");
}
/**
@@ -278,7 +278,7 @@ namespace dispatchkit
template<typename T, typename U>
void add_oper_assign_overload(Dispatch_Engine &s)
{
register_function(s, &assign<T,U>, "=");
s.add(fun(&assign<T,U>), "=");
}
@@ -288,7 +288,7 @@ namespace dispatchkit
template<typename T>
void add_oper_assign(Dispatch_Engine &s)
{
register_function(s, &assign<T,T>, "=");
s.add(fun(&assign<T,T>), "=");
}
@@ -298,7 +298,7 @@ namespace dispatchkit
template<typename T>
void add_oper_assign_pod(Dispatch_Engine &s)
{
register_function(s, &assign_pod<T>, "=");
s.add(fun(&assign_pod<T>), "=");
}
@@ -308,7 +308,7 @@ namespace dispatchkit
template<typename T>
void add_oper_less_than(Dispatch_Engine &s)
{
register_function(s, &less_than<const T&, const T&>, "<");
s.add(fun(&less_than<const T&, const T&>), "<");
}
/**
@@ -317,7 +317,7 @@ namespace dispatchkit
template<typename T>
void add_oper_greater_than(Dispatch_Engine &s)
{
register_function(s, &greater_than<const T&, const T&>, ">");
s.add(fun(&greater_than<const T&, const T&>), ">");
}
/**
@@ -326,7 +326,7 @@ namespace dispatchkit
template<typename T>
void add_oper_less_than_equals(Dispatch_Engine &s)
{
register_function(s, &less_than_equals<const T&, const T&>, "<=");
s.add(fun(&less_than_equals<const T&, const T&>), "<=");
}
/**
@@ -335,7 +335,7 @@ namespace dispatchkit
template<typename T>
void add_oper_greater_than_equals(Dispatch_Engine &s)
{
register_function(s, &greater_than_equals<const T&, const T&>, ">=");
s.add(fun(&greater_than_equals<const T&, const T&>), ">=");
}
/**
@@ -345,12 +345,12 @@ namespace dispatchkit
template<typename T, typename R>
void add_opers_comparison_overload(Dispatch_Engine &s)
{
register_function(s, &equals<const T&, const R&>, "==");
register_function(s, &not_equals<const T&, const R&>, "!=");
register_function(s, &less_than<const T&, const R&>, "<");
register_function(s, &greater_than<const T&, const R&>, ">");
register_function(s, &less_than_equals<const T&, const R&>, "<=");
register_function(s, &greater_than_equals<const T&, const R&>, ">=");
s.add(fun(&equals<const T&, const R&>), "==");
s.add(fun(&not_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&>), ">=");
}
/**
@@ -372,18 +372,18 @@ namespace dispatchkit
template<typename Ret, typename T, typename R>
void add_opers_arithmetic_overload(Dispatch_Engine &s)
{
register_function(s, &add<Ret, T, R>, "+");
register_function(s, &subtract<Ret, T, R>, "-");
register_function(s, &divide<Ret, T, R>, "/");
register_function(s, &multiply<Ret, T, R>, "*");
register_function(s, &timesequal<T, R>, "*=");
register_function(s, &dividesequal<T, R>, "/=");
register_function(s, &subtractsequal<T, R>, "-=");
register_function(s, &addsequal<T, R>, "+=");
register_function(s, &prefixincrement<T>, "++");
register_function(s, &prefixdecrement<T>, "--");
register_function(s, &prefixnegate<T>, "-");
register_function(s, &prefixnot<T>, "!");
s.add(fun(&add<Ret, T, R>), "+");
s.add(fun(&subtract<Ret, T, R>), "-");
s.add(fun(&divide<Ret, T, R>), "/");
s.add(fun(&multiply<Ret, T, R>), "*");
s.add(fun(&timesequal<T, R>), "*=");
s.add(fun(&dividesequal<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>), "!");
}
/**
@@ -393,10 +393,10 @@ namespace dispatchkit
template<typename T>
void add_opers_arithmetic_modify_pod(Dispatch_Engine &s)
{
register_function(s, &timesequal_pod<T>, "*=");
register_function(s, &dividesequal_pod<T>, "/=");
register_function(s, &subtractsequal_pod<T>, "-=");
register_function(s, &addsequal_pod<T>, "+=");
s.add(fun(&timesequal_pod<T>), "*=");
s.add(fun(&dividesequal_pod<T>), "/=");
s.add(fun(&subtractsequal_pod<T>), "-=");
s.add(fun(&addsequal_pod<T>), "+=");
}
/**
@@ -407,8 +407,8 @@ namespace dispatchkit
template<typename T>
void add_copy_constructor(Dispatch_Engine &s, const std::string &type)
{
s.register_function(build_constructor<T, const T &>(), type);
s.register_function(build_constructor<T, const T &>(), "clone");
s.add(constructor<T (const T &)>(), type);
s.add(constructor<T (const T &)>(), "clone");
}
/**
@@ -417,7 +417,7 @@ namespace dispatchkit
template<typename T>
void add_basic_constructors(Dispatch_Engine &s, const std::string &type)
{
s.register_function(build_constructor<T>(), type);
s.add(constructor<T ()>(), type);
add_copy_constructor<T>(s, type);
}
@@ -427,7 +427,7 @@ namespace dispatchkit
template<typename T>
void add_construct_pod(Dispatch_Engine &s, const std::string &type)
{
register_function(s, &construct_pod<T>, type);
s.add(fun(&construct_pod<T>), type);
}
/**
@@ -437,7 +437,7 @@ namespace dispatchkit
template<typename T, typename U>
void add_constructor_overload(Dispatch_Engine &s, const std::string &type)
{
s.register_function(build_constructor<T, const U &>(), type);
s.add(constructor<T (const U &)>(), type);
}
/**
@@ -489,22 +489,22 @@ namespace dispatchkit
template<typename T>
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
{
s.register_type<T>(name);
s.add(type_<T>(), name);
add_basic_constructors<T>(s, name);
add_oper_assign<T>(s);
add_oper_assign_pod<T>(s);
add_construct_pod<T>(s, name);
add_opers_arithmetic<T>(s);
add_opers_arithmetic_modify_pod<T>(s);
register_function(s, &to_string<T>, "to_string");
register_function(s, &parse_string<T>, "to_" + name);
s.add(fun(&to_string<T>), "to_string");
s.add(fun(&parse_string<T>), "to_" + name);
}
/**
* "clone" function for a shared_ptr type. This is used in the case
* where you do not want to make a deep copy of an object during cloning
* but want to instead maintain the shared_ptr. It is needed internally
* for handling of boost::shared_ptr<Proxy_Function> object (that is,
* for handling of Proxy_Function object (that is,
* function variables.
*/
template<typename Type>
@@ -560,12 +560,12 @@ namespace dispatchkit
*/
static void add_opers_comparison_pod(Dispatch_Engine &s)
{
register_function(s, &equals<Boxed_POD_Value, Boxed_POD_Value>, "==");
register_function(s, &not_equals<Boxed_POD_Value, Boxed_POD_Value>, "!=");
register_function(s, &less_than<Boxed_POD_Value, Boxed_POD_Value>, "<");
register_function(s, &greater_than<Boxed_POD_Value, Boxed_POD_Value>, ">");
register_function(s, &less_than_equals<Boxed_POD_Value, Boxed_POD_Value>, "<=");
register_function(s, &greater_than_equals<Boxed_POD_Value, Boxed_POD_Value>, ">=");
s.add(fun(&equals<Boxed_POD_Value, Boxed_POD_Value>), "==");
s.add(fun(&not_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>), ">=");
}
/**
@@ -573,10 +573,10 @@ namespace dispatchkit
*/
static void add_opers_arithmetic_pod(Dispatch_Engine &s)
{
register_function(s, &add<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "+");
register_function(s, &subtract<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "-");
register_function(s, &divide<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "/");
register_function(s, &multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>, "*");
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(&divide<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "/");
s.add(fun(&multiply<Boxed_Value, Boxed_POD_Value, Boxed_POD_Value>), "*");
}
/**
@@ -611,9 +611,9 @@ namespace dispatchkit
throw arity_error(params.size(), 2);
}
boost::shared_ptr<Proxy_Function> f = boxed_cast<boost::shared_ptr<Proxy_Function> >(params[0]);
Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Bound_Function(f,
return Boxed_Value(Proxy_Function(new Bound_Function(f,
std::vector<Boxed_Value>(params.begin() + 1, params.end()))));
}
@@ -628,7 +628,7 @@ namespace dispatchkit
throw arity_error(params.size(), 1);
}
boost::shared_ptr<Proxy_Function> f = boxed_cast<boost::shared_ptr<Proxy_Function> >(params[0]);
Proxy_Function f = boxed_cast<Proxy_Function >(params[0]);
return Boxed_Value(f->types_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
}
@@ -638,19 +638,19 @@ namespace dispatchkit
*/
static void bootstrap(Dispatch_Engine &s)
{
s.register_type<void>("void");
s.register_type<bool>("bool");
s.register_type<Boxed_Value>("Object");
s.register_type<Boxed_POD_Value>("PODObject");
s.register_type<Proxy_Function>("function");
s.add(type_<void>(), "void");
s.add(type_<bool>(), "bool");
s.add(type_<Boxed_Value>(), "Object");
s.add(type_<Boxed_POD_Value>(), "PODObject");
s.add(type_<Proxy_Function>(), "function");
add_basic_constructors<bool>(s, "bool");
add_oper_assign<std::string>(s);
add_oper_assign<bool>(s);
register_function(s, &to_string<const std::string &>, "internal_to_string");
register_function(s, &to_string<bool>, "internal_to_string");
register_function(s, &unknown_assign, "=");
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), "=");
bootstrap_pod_type<double>(s, "double");
bootstrap_pod_type<int>(s, "int");
@@ -660,26 +660,26 @@ namespace dispatchkit
add_opers_comparison_pod(s);
add_opers_arithmetic_pod(s);
register_function(s, &modulus<int, int, int>, "%");
s.add(fun(&modulus<int, int, int>), "%");
register_function(s, &print, "print_string");
register_function(s, &println, "println_string");
s.add(fun(&print), "print_string");
s.add(fun(&println), "println_string");
s.register_function(boost::function<void ()>(boost::bind(&dump_system, boost::ref(s))), "dump_system");
s.register_function(boost::function<void (Boxed_Value)>(boost::bind(&dump_object, _1, boost::ref(s))), "dump_object");
s.register_function(boost::function<bool (Boxed_Value, const std::string &)>(boost::bind(&is_type, boost::ref(s), _2, _1)),
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");
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))),
"bind");
register_function(s, &shared_ptr_clone<Proxy_Function>, "clone");
register_function(s, &ptr_assign<Proxy_Function>, "=");
s.add(fun(&shared_ptr_clone<Proxy_Function_Base>), "clone");
s.add(fun(&ptr_assign<Proxy_Function_Base>), "=");
s.register_function(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
s.add(Proxy_Function(new Dynamic_Proxy_Function(boost::bind(&call_exists, _1))),
"call_exists");
register_function(s, &type_match, "type_match");
s.add(fun(&type_match), "type_match");
}
};
}

View File

@@ -17,7 +17,7 @@
#include "register_function.hpp"
namespace dispatchkit
namespace chaiscript
{
/**
* Input_Range, based on the D concept of ranges.
@@ -76,24 +76,22 @@ namespace dispatchkit
template<typename ContainerType>
void bootstrap_input_range(Dispatch_Engine &system, const std::string &type)
{
system.register_type<Input_Range<ContainerType> >(type+"_Range");
system.register_type<typename ContainerType::iterator>(type+"_Iterator");
system.add(type_<Input_Range<ContainerType> >(), type + "_Range");
system.add(type_<typename ContainerType::iterator>(), type+"_Iterator");
system.register_function(build_constructor<Input_Range<ContainerType>, ContainerType &>(), "range");
system.register_function(build_constructor<Input_Range<ContainerType>,
typename ContainerType::iterator>(), "range");
system.add(constructor<Input_Range<ContainerType> (ContainerType &)>(), "range");
system.add(constructor<Input_Range<ContainerType> (typename ContainerType::iterator)>(), "range");
typedef std::pair<typename ContainerType::iterator, typename ContainerType::iterator> ItrPair;
system.register_function(build_constructor<Input_Range<ContainerType>,
const ItrPair &>(), "range");
system.register_type<ItrPair>(type+"_Iterator_Pair");
system.add(constructor<Input_Range<ContainerType> (const ItrPair &)>(), "range");
system.add(type_<ItrPair>(), type+"_Iterator_Pair");
register_function(system, &Input_Range<ContainerType>::empty, "empty");
register_function(system, &Input_Range<ContainerType>::pop_front, "pop_front");
register_function(system, &Input_Range<ContainerType>::front, "front");
system.register_function(build_constructor<Input_Range<ContainerType>, const Input_Range<ContainerType> &>(), "clone");
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");
}
/**
@@ -117,10 +115,10 @@ namespace dispatchkit
//In the interest of runtime safety for the system, we prefer the at() method for [] access,
//to throw an exception in an out of bounds condition.
system.register_function(
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at)), "[]");
system.register_function(
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[])), "at");
system.add(
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at))), "[]");
system.add(
fun(boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[]))), "at");
}
@@ -144,9 +142,9 @@ namespace dispatchkit
{
bootstrap_assignable<ContainerType>(system, type);
register_function(system, &ContainerType::size, "size");
register_function(system, &ContainerType::max_size, "max_size");
register_function(system, &ContainerType::empty, "empty");
system.add(fun(&ContainerType::size), "size");
system.add(fun(&ContainerType::max_size), "max_size");
system.add(fun(&ContainerType::empty), "empty");
}
/**
@@ -167,7 +165,7 @@ namespace dispatchkit
template<typename Type>
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
{
system.register_function(build_constructor<Type>(), type);
system.add(constructor<Type ()>(), type);
}
/**
@@ -224,8 +222,8 @@ namespace dispatchkit
insert_name = "insert_at";
}
register_function(system, &insert_at<ContainerType>, insert_name);
register_function(system, &erase_at<ContainerType>, "erase_at");
system.add(fun(&insert_at<ContainerType>), insert_name);
system.add(fun(&erase_at<ContainerType>), "erase_at");
}
/**
@@ -240,7 +238,7 @@ namespace dispatchkit
typedef typename ContainerType::reference (ContainerType::*backptr)();
register_function(system, (backptr(&ContainerType::back)), "back");
system.add(fun(backptr(&ContainerType::back)), "back");
std::string push_back_name;
if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value))
@@ -250,8 +248,8 @@ namespace dispatchkit
push_back_name = "push_back";
}
register_function(system, &ContainerType::push_back, push_back_name);
register_function(system, &ContainerType::pop_back, "pop_back");
system.add(fun(&ContainerType::push_back), push_back_name);
system.add(fun(&ContainerType::pop_back), "pop_back");
}
/**
@@ -261,7 +259,7 @@ namespace dispatchkit
template<typename VectorType>
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
{
system.register_type<VectorType>(type);
system.add(type_<VectorType>(), type);
bootstrap_random_access_container<VectorType>(system, type);
bootstrap_back_insertion_sequence<VectorType>(system, type);
}
@@ -284,15 +282,15 @@ namespace dispatchkit
template<typename PairType>
void bootstrap_pair(Dispatch_Engine &system, const std::string &type)
{
system.register_type<PairType>(type);
system.add(type_<PairType>(), type);
register_member(system, &PairType::first, "first");
register_member(system, &PairType::second, "second");
system.add(fun(&PairType::first), "first");
system.add(fun(&PairType::second), "second");
system.register_function(build_constructor<PairType >(), type);
system.register_function(build_constructor<PairType, const PairType &>(), type);
system.register_function(build_constructor<PairType, const PairType &>(), "clone");
system.register_function(build_constructor<PairType, const typename PairType::first_type &, const typename PairType::second_type &>(), type);
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);
}
@@ -315,7 +313,7 @@ namespace dispatchkit
void bootstrap_unique_associative_container(Dispatch_Engine &system, const std::string &type)
{
bootstrap_associative_container<ContainerType>(system, type);
register_function(system, &ContainerType::count, "count");
system.add(fun(&ContainerType::count), "count");
}
/**
@@ -330,7 +328,7 @@ namespace dispatchkit
bootstrap_reversible_container<ContainerType>(system, type);
bootstrap_associative_container<ContainerType>(system, type);
register_function(system, eq_range(&ContainerType::equal_range), "equal_range");
system.add(fun(eq_range(&ContainerType::equal_range)), "equal_range");
}
/**
@@ -351,8 +349,8 @@ namespace dispatchkit
template<typename MapType>
void bootstrap_map(Dispatch_Engine &system, const std::string &type)
{
system.register_type<MapType>(type);
register_function(system, &MapType::operator[], "[]");
system.add(type_<MapType>(), type);
system.add(fun(&MapType::operator[]), "[]");
bootstrap_unique_sorted_associative_container<MapType>(system, type);
bootstrap_pair_associative_container<MapType>(system, type);
}
@@ -364,19 +362,19 @@ namespace dispatchkit
template<typename String>
void bootstrap_string(Dispatch_Engine &system, const std::string &type)
{
system.register_type<String>(type);
system.add(type_<String>(), type);
add_oper_add<String>(system);
add_oper_add_equals<String>(system);
add_opers_comparison<String>(system);
bootstrap_random_access_container<String>(system, type);
bootstrap_sequence<String>(system, type);
typedef typename String::size_type (String::*find_func)(const String &, typename String::size_type) const;
register_function(system, find_func(&String::find), "find");
register_function(system, find_func(&String::rfind), "rfind");
register_function(system, find_func(&String::find_first_of), "find_first_of");
register_function(system, find_func(&String::find_last_of), "find_last_of");
register_function(system, find_func(&String::find_first_not_of), "find_first_not_of");
register_function(system, find_func(&String::find_last_not_of), "find_last_not_of");
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");
}
}

View File

@@ -17,7 +17,7 @@
#include <boost/cstdint.hpp>
#include <boost/type_traits/add_const.hpp>
namespace dispatchkit
namespace chaiscript
{
/**
* Boxed_Value is the main tool of the dispatchkit. It allows
@@ -148,6 +148,12 @@ namespace dispatchkit
return data;
}
template<typename T>
boost::shared_ptr<Data> get(T *t)
{
return get(boost::ref(*t));
}
template<typename T>
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
{
@@ -670,6 +676,12 @@ namespace dispatchkit
return Boxed_POD_Value(ob);
}
};
template<typename T>
Boxed_Value var(T t)
{
return Boxed_Value(t);
}
}
#endif

View File

@@ -23,22 +23,22 @@
#include "proxy_functions.hpp"
#include "proxy_constructors.hpp"
namespace dispatchkit
namespace chaiscript
{
/**
* A Proxy_Function implementation that is able to take
* a vector of Proxy_Functions and perform a dispatch on them. It is
* used specifically in the case of dealing with Function object variables
*/
class Dispatch_Function : public Proxy_Function
class Dispatch_Function : public Proxy_Function_Base
{
public:
Dispatch_Function(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs)
Dispatch_Function(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs)
: m_funcs(t_funcs)
{
}
virtual bool operator==(const Proxy_Function &) const
virtual bool operator==(const Proxy_Function_Base &) const
{
return false;
}
@@ -57,7 +57,7 @@ namespace dispatchkit
virtual bool types_match(const std::vector<Boxed_Value> &types) const
{
typedef std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > function_vec;
typedef std::vector<std::pair<std::string, Proxy_Function > > function_vec;
function_vec::const_iterator begin = m_funcs.begin();
function_vec::const_iterator end = m_funcs.end();
@@ -81,7 +81,7 @@ namespace dispatchkit
}
private:
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > m_funcs;
std::vector<std::pair<std::string, Proxy_Function > > m_funcs;
};
@@ -92,7 +92,7 @@ namespace dispatchkit
class Dispatch_Engine
{
public:
typedef std::map<std::string, dispatchkit::Type_Info> Type_Name_Map;
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
typedef std::map<std::string, Boxed_Value> Scope;
typedef std::deque<Scope> Stack;
@@ -105,48 +105,37 @@ namespace dispatchkit
/**
* Add a new named Proxy_Function to the system
*/
bool register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
bool add(const Proxy_Function &f, const std::string &name)
{
return add_function(f, name);
}
/**
* Add a generic, named boost::function() to the system
*/
template<typename Function>
bool register_function(const Function &func, const std::string &name)
{
return add_function(boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func)), name);
}
/**
* Set the value of an object, by name. If the object
* is not available in the current scope it is created
*/
template<typename Class>
void set_object(const std::string &name, const Class &obj)
void add(const Boxed_Value &obj, const std::string &name)
{
for (int i = m_scopes.size()-1; i >= 0; --i)
{
for (int i = m_scopes.size()-1; i >= 0; --i)
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
if (itr != m_scopes[i].end())
{
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
if (itr != m_scopes[i].end())
{
m_scopes[i][name] = Boxed_Value(obj);
return;
}
m_scopes[i][name] = Boxed_Value(obj);
return;
}
add_object(name, obj);
}
add_object(name, obj);
}
/**
* Adds a named object to the current scope
*/
template<typename Class>
void add_object(const std::string &name, const Class &obj)
{
m_scopes.back()[name] = Boxed_Value(obj);
}
void add_object(const std::string &name, const Boxed_Value &obj)
{
m_scopes.back()[name] = Boxed_Value(obj);
}
/**
* Adds a new scope to the stack
@@ -209,24 +198,23 @@ namespace dispatchkit
}
}
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs = get_function_impl(name, false);
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> > funcs = get_function_impl(name, false);
if (funcs.empty())
{
throw std::range_error("Object not known: " + name);
} else {
return Boxed_Value(boost::shared_ptr<Proxy_Function>(new Dispatch_Function(funcs)));
return Boxed_Value(Proxy_Function(new Dispatch_Function(funcs)));
}
}
/**
* Registers a new named type
*/
template<typename Type>
void register_type(const std::string &name)
{
m_types.insert(std::make_pair(name, Get_Type_Info<Type>::get()));
}
void add(const Type_Info &ti, const std::string &name)
{
m_types.insert(std::make_pair(name, ti));
}
/**
* Returns the type info for a named type
@@ -274,7 +262,7 @@ namespace dispatchkit
/**
* Return a function by name
*/
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> >
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> >
get_function(const std::string &t_name) const
{
return get_function_impl(t_name, true);
@@ -283,9 +271,9 @@ namespace dispatchkit
/**
* Get a vector of all registered functions
*/
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > get_functions() const
std::vector<std::pair<std::string, Proxy_Function > > get_functions() const
{
return std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >(m_functions.begin(), m_functions.end());
return std::vector<std::pair<std::string, Proxy_Function > >(m_functions.begin(), m_functions.end());
}
private:
@@ -294,10 +282,10 @@ namespace dispatchkit
* Looks for all registered global functions and optionally for an object
* in scope with the same name
*/
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> >
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> >
get_function_impl(const std::string &t_name, bool include_objects) const
{
std::vector<std::pair<std::string, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type> > funcs;
std::vector<std::pair<std::string, std::multimap<std::string, Proxy_Function >::mapped_type> > funcs;
if (include_objects)
{
@@ -305,14 +293,14 @@ namespace dispatchkit
funcs.insert(funcs.end(),
std::make_pair(
t_name,
boxed_cast<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::mapped_type>(get_object(t_name)))
boxed_cast<std::multimap<std::string, Proxy_Function >::mapped_type>(get_object(t_name)))
);
} catch (const bad_boxed_cast &) {
} catch (const std::range_error &) {
}
}
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range
std::pair<std::multimap<std::string, Proxy_Function >::const_iterator, std::multimap<std::string, Proxy_Function >::const_iterator> range
= m_functions.equal_range(t_name);
funcs.insert(funcs.end(), range.first, range.second);
@@ -324,9 +312,9 @@ namespace dispatchkit
* true if the function was added, false if a function with the
* same signature and name already exists.
*/
bool add_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &t_name)
bool add_function(const Proxy_Function &f, const std::string &t_name)
{
std::pair<std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator, std::multimap<std::string, boost::shared_ptr<Proxy_Function> >::const_iterator> range
std::pair<std::multimap<std::string, Proxy_Function >::const_iterator, std::multimap<std::string, Proxy_Function >::const_iterator> range
= m_functions.equal_range(t_name);
while (range.first != range.second)
@@ -344,7 +332,7 @@ namespace dispatchkit
std::deque<Scope> m_scopes;
std::multimap<std::string, boost::shared_ptr<Proxy_Function> > m_functions;
std::multimap<std::string, Proxy_Function > m_functions;
Type_Name_Map m_types;
Boxed_Value m_place_holder;
};
@@ -368,7 +356,7 @@ namespace dispatchkit
/**
* Dump function to stdout
*/
void dump_function(const std::pair<const std::string, boost::shared_ptr<Proxy_Function> > &f, const Dispatch_Engine &e)
void dump_function(const std::pair<const std::string, Proxy_Function > &f, const Dispatch_Engine &e)
{
std::vector<Type_Info> params = f.second->get_param_types();
std::string annotation = f.second->annotation();
@@ -412,10 +400,10 @@ namespace dispatchkit
}
std::cout << std::endl;
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs = s.get_functions();
std::vector<std::pair<std::string, Proxy_Function > > funcs = s.get_functions();
std::cout << "Functions: " << std::endl;
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
for (std::vector<std::pair<std::string, Proxy_Function > >::const_iterator itr = funcs.begin();
itr != funcs.end();
++itr)
{

View File

@@ -21,7 +21,7 @@
#include <vector>
#include "proxy_functions.hpp"
namespace dispatchkit
namespace chaiscript
{
/**
* Internal helper class for handling the return
@@ -35,7 +35,7 @@ namespace dispatchkit
{
}
Ret call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs,
Ret call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
const std::vector<Boxed_Value> &params)
{
return boxed_cast<Ret>(dispatch(t_funcs, params));
@@ -53,7 +53,7 @@ namespace dispatchkit
{
}
void call(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &t_funcs,
void call(const std::vector<std::pair<std::string, Proxy_Function > > &t_funcs,
const std::vector<Boxed_Value> &params)
{
dispatch(t_funcs, params);
@@ -65,7 +65,7 @@ namespace dispatchkit
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/function_call.hpp>
#include BOOST_PP_ITERATE()
namespace dispatchkit
namespace chaiscript
{
/**
* Build a function caller that knows how to dispatch on a set of functions
@@ -77,7 +77,7 @@ namespace dispatchkit
*/
template<typename FunctionType>
boost::function<FunctionType>
build_function_caller(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs)
functor(const std::vector<std::pair<std::string, Proxy_Function > > &funcs)
{
FunctionType *p;
return build_function_caller_helper(p, funcs);
@@ -88,7 +88,7 @@ namespace dispatchkit
* useful in the case that a function is being pass out from scripting back
* into code
* example:
* void my_function(boost::shared_ptr<Proxy_Function> f)
* void my_function(Proxy_Function f)
* {
* boost::function<void (int)> local_f =
* build_function_caller(f);
@@ -98,11 +98,11 @@ namespace dispatchkit
*/
template<typename FunctionType>
boost::function<FunctionType>
build_function_caller(boost::shared_ptr<Proxy_Function> func)
functor(Proxy_Function func)
{
std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > funcs;
std::vector<std::pair<std::string, Proxy_Function > > funcs;
funcs.push_back(std::make_pair(std::string(), func));
return build_function_caller<FunctionType>(funcs);
return functor<FunctionType>(funcs);
}
/**
@@ -111,37 +111,24 @@ namespace dispatchkit
*/
template<typename FunctionType>
boost::function<FunctionType>
build_function_caller(const Boxed_Value &bv)
functor(const Boxed_Value &bv)
{
return build_function_caller<FunctionType>(boxed_cast<boost::shared_ptr<Proxy_Function> >(bv));
return functor<FunctionType>(boxed_cast<Proxy_Function >(bv));
}
/**
* Helper for calling script code as if it were native C++ code
* example:
* boost::function<int (int, int)> f = build_functor(chai, "func(x, y){x+y}");
* \return a boost::function representing the passed in script
* \param[in] e ScriptEngine to build the script execution from
* \param[in] script Script code to build a function from
*/
template<typename FunctionType, typename ScriptEngine>
boost::function<FunctionType> build_functor(ScriptEngine &e, const std::string &script)
{
return build_function_caller<FunctionType>(e.evaluate_string(script));
}
}
# endif
#else
# define n BOOST_PP_ITERATION()
namespace dispatchkit
namespace chaiscript
{
/**
* used internally for unwrapping a function call's types
*/
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
Ret function_caller(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs
Ret function_caller(const std::vector<std::pair<std::string, Proxy_Function > > &funcs
BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
{
std::vector<Boxed_Value> params;
@@ -156,7 +143,7 @@ namespace dispatchkit
*/
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param)) >
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs)
build_function_caller_helper(Ret (BOOST_PP_ENUM_PARAMS(n, Param)), const std::vector<std::pair<std::string, Proxy_Function> > &funcs)
{
return boost::bind(&function_caller<Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>, funcs
BOOST_PP_ENUM_TRAILING(n, curry, ~));

View File

@@ -19,17 +19,28 @@
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_constructors.hpp>
#include BOOST_PP_ITERATE()
# endif
namespace chaiscript
{
template<typename T>
Proxy_Function constructor()
{
T *f;
return (build_constructor_(f));
}
}
#else
# define n BOOST_PP_ITERATION()
namespace dispatchkit
namespace chaiscript
{
/**
* A constructor function, used for creating a new object
* of a given type with a given set of params
*/
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::shared_ptr<Class> constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
boost::shared_ptr<Class> constructor_( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
{
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
}
@@ -41,10 +52,10 @@ namespace dispatchkit
* \todo See if it is possible to make this not be a variadic function
*/
template<typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor()
Proxy_Function build_constructor_(Class (*f)(BOOST_PP_ENUM_PARAMS(n, Param)))
{
typedef boost::shared_ptr<Class> (*func)(BOOST_PP_ENUM_PARAMS(n, Param));
return boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>)));
typedef boost::shared_ptr<Class> (sig)(BOOST_PP_ENUM_PARAMS(n, Param));
return Proxy_Function(new Proxy_Function_Impl<sig>(&(constructor_<Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param)>)));
}
}

View File

@@ -7,9 +7,9 @@
#include <boost/preprocessor.hpp>
#define gettypeinfo(z,n,text) ti.push_back(Get_Type_Info<Param ## n>::get());
#define casthelper(z,n,text) ,dispatchkit::boxed_cast< Param ## n >(params[n])
#define casthelper(z,n,text) ,chaiscript::boxed_cast< Param ## n >(params[n])
#define comparetype(z,n,text) && ((Get_Type_Info<Param ## n>::get() == params[n].get_type_info()))
#define trycast(z,n,text) dispatchkit::boxed_cast<Param ## n>(params[n]);
#define trycast(z,n,text) chaiscript::boxed_cast<Param ## n>(params[n]);
#ifndef BOOST_PP_IS_ITERATING
@@ -26,7 +26,7 @@
#include <stdexcept>
#include <vector>
namespace dispatchkit
namespace chaiscript
{
/**
* Used internally for handling a return value from a Proxy_Function call
@@ -52,18 +52,6 @@ namespace dispatchkit
}
};
/**
* Used internally for handling a return value from a Proxy_Function call
*/
template<typename Ret>
struct Handle_Return<Ret *>
{
Boxed_Value operator()(const boost::function<Ret *()> &f)
{
return Boxed_Value(boost::ref(*f()));
}
};
/**
* Used internally for handling a return value from a Proxy_Function call
*/
@@ -107,7 +95,7 @@ namespace dispatchkit
*
* example usage:
* Boxed_Value retval = dispatch(dispatchengine.get_function("+"),
* dispatchkit::Param_List_Builder() << 5 << 6);
* chaiscript::Param_List_Builder() << 5 << 6);
*/
struct Param_List_Builder
{
@@ -154,7 +142,7 @@ namespace dispatchkit
#define BOOST_PP_FILENAME_1 <chaiscript/dispatchkit/proxy_functions.hpp>
#include BOOST_PP_ITERATE()
namespace dispatchkit
namespace chaiscript
{
/**
* Pure virtual base class for all Proxy_Function implementations
@@ -164,17 +152,19 @@ namespace dispatchkit
* Dispatch_Engine only knows how to work with Proxy_Function, no other
* function classes.
*/
class Proxy_Function
class Proxy_Function_Base
{
public:
virtual ~Proxy_Function() {}
virtual ~Proxy_Function_Base() {}
virtual Boxed_Value operator()(const std::vector<Boxed_Value> &params) = 0;
virtual std::vector<Type_Info> get_param_types() const = 0;
virtual bool operator==(const Proxy_Function &) const = 0;
virtual bool operator==(const Proxy_Function_Base &) const = 0;
virtual bool types_match(const std::vector<Boxed_Value> &types) const = 0;
virtual std::string annotation() const = 0;
};
typedef boost::shared_ptr<Proxy_Function_Base> Proxy_Function;
/**
* Exception thrown if a function's guard fails to execute
*/
@@ -193,19 +183,19 @@ namespace dispatchkit
* A Proxy_Function implementation that is not type safe, the called function
* is expecting a vector<Boxed_Value> that it works with how it chooses.
*/
class Dynamic_Proxy_Function : public Proxy_Function
class Dynamic_Proxy_Function : public Proxy_Function_Base
{
public:
Dynamic_Proxy_Function(
const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f,
int t_arity=-1,
const std::string &t_description = "",
const boost::shared_ptr<Proxy_Function> &t_guard = boost::shared_ptr<Proxy_Function>())
const Proxy_Function &t_guard = Proxy_Function())
: m_f(t_f), m_arity(t_arity), m_description(t_description), m_guard(t_guard)
{
}
virtual bool operator==(const Proxy_Function &) const
virtual bool operator==(const Proxy_Function_Base &) const
{
return false;
}
@@ -279,7 +269,7 @@ namespace dispatchkit
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
int m_arity;
std::string m_description;
boost::shared_ptr<Proxy_Function> m_guard;
Proxy_Function m_guard;
};
/**
@@ -296,16 +286,16 @@ namespace dispatchkit
* at runtime, when call() is executed.
* it is used for bind(function, param1, _, param2) style calls
*/
class Bound_Function : public Proxy_Function
class Bound_Function : public Proxy_Function_Base
{
public:
Bound_Function(const boost::shared_ptr<Proxy_Function> &t_f,
Bound_Function(const Proxy_Function &t_f,
const std::vector<Boxed_Value> &t_args)
: m_f(t_f), m_args(t_args)
{
}
virtual bool operator==(const Proxy_Function &) const
virtual bool operator==(const Proxy_Function_Base &) const
{
return false;
}
@@ -372,7 +362,7 @@ namespace dispatchkit
}
private:
boost::shared_ptr<Proxy_Function> m_f;
Proxy_Function m_f;
std::vector<Boxed_Value> m_args;
};
@@ -382,17 +372,17 @@ namespace dispatchkit
* type checking of Boxed_Value parameters, in a type safe manner
*/
template<typename Func>
class Proxy_Function_Impl : public Proxy_Function
class Proxy_Function_Impl : public Proxy_Function_Base
{
public:
Proxy_Function_Impl(const Func &f)
Proxy_Function_Impl(const boost::function<Func> &f)
: m_f(f)
{
}
virtual ~Proxy_Function_Impl() {}
virtual bool operator==(const Proxy_Function &t_func) const
virtual bool operator==(const Proxy_Function_Base &t_func) const
{
try {
dynamic_cast<const Proxy_Function_Impl<Func> &>(t_func);
@@ -409,12 +399,14 @@ namespace dispatchkit
virtual std::vector<Type_Info> get_param_types() const
{
return build_param_type_list(m_f);
Func *f;
return build_param_type_list(f);
}
virtual bool types_match(const std::vector<Boxed_Value> &types) const
{
return compare_types(m_f, types);
Func *f;
return compare_types(f, types);
}
virtual std::string annotation() const
@@ -423,7 +415,7 @@ namespace dispatchkit
}
private:
Func m_f;
boost::function<Func> m_f;
};
/**
@@ -447,10 +439,10 @@ namespace dispatchkit
* each function against the set of parameters, in order, until a matching
* function is found or throw dispatch_error if no matching function is found
*/
Boxed_Value dispatch(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs,
Boxed_Value dispatch(const std::vector<std::pair<std::string, Proxy_Function> > &funcs,
const std::vector<Boxed_Value> &plist)
{
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
for (std::vector<std::pair<std::string, Proxy_Function> >::const_iterator itr = funcs.begin();
itr != funcs.end();
++itr)
{
@@ -473,14 +465,14 @@ namespace dispatchkit
#else
# define n BOOST_PP_ITERATION()
namespace dispatchkit
namespace chaiscript
{
/**
* Used by Proxy_Function_Impl to return a list of all param types
* it contains.
*/
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param) >
std::vector<Type_Info> build_param_type_list(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &)
std::vector<Type_Info> build_param_type_list(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)))
{
std::vector<Type_Info> ti;
ti.push_back(Get_Type_Info<Ret>::get());
@@ -514,7 +506,7 @@ namespace dispatchkit
* registration of two functions with the exact same signatures
*/
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
bool compare_types(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &,
bool compare_types(Ret (*)(BOOST_PP_ENUM_PARAMS(n, Param)),
const std::vector<Boxed_Value> &params)
{
if (params.size() != n)

View File

@@ -14,8 +14,15 @@
#include <boost/function.hpp>
#include <boost/bind.hpp>
namespace dispatchkit
namespace chaiscript
{
template<typename T>
Proxy_Function fun(const boost::function<T> &f)
{
return Proxy_Function(new Proxy_Function_Impl<T>(f));
}
/**
* Helper function for register_member function
*/
@@ -31,10 +38,11 @@ namespace dispatchkit
* for example, the case of std::pair<>::first and std::pair<>::second
*/
template<typename T, typename Class>
void register_member(Dispatch_Engine &s, T Class::* m, const std::string &name)
Proxy_Function fun(T Class::* m)
{
s.register_function(boost::function<T& (Class *)>(boost::bind(&get_member<T, Class>, m, _1)), name);
return fun(boost::function<T& (Class *)>(boost::bind(&get_member<T, Class>, m, _1)));
}
}
#define BOOST_PP_ITERATION_LIMITS ( 0, 10 )
@@ -45,33 +53,33 @@ namespace dispatchkit
#else
# define n BOOST_PP_ITERATION()
namespace dispatchkit
namespace chaiscript
{
/**
* Register a global function of n parameters with name
*/
template<typename Ret BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(Dispatch_Engine &s, Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
Proxy_Function fun(Ret (*f)(BOOST_PP_ENUM_PARAMS(n, Param)))
{
s.register_function(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
return fun(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f));
}
/**
* Register a class method of n parameters with name
*/
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
Proxy_Function fun(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)))
{
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
return fun(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f));
}
/**
* Register a const class method of n parameters with name
*/
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const, const std::string &name)
Proxy_Function fun(Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param))const)
{
s.register_function(boost::function<Ret (const Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
return fun(boost::function<Ret (const Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f));
}
}

View File

@@ -16,7 +16,7 @@
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/ref.hpp>
namespace dispatchkit
namespace chaiscript
{
/**
* compile time deduced information about a type
@@ -115,6 +115,22 @@ namespace dispatchkit
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
}
};
template<typename T>
Type_Info type_(T)
{
return Get_Type_Info<T>::get();
}
template<typename T>
Type_Info type_()
{
return Get_Type_Info<T>::get();
}
}
#endif