Add namespace dispatchkit
This commit is contained in:
parent
416242286a
commit
785263628b
@ -23,11 +23,11 @@ public:
|
||||
return engine;
|
||||
}
|
||||
|
||||
const Boxed_Value eval(const std::vector<Boxed_Value> &vals) {
|
||||
const dispatchkit::Boxed_Value eval(const std::vector<dispatchkit::Boxed_Value> &vals) {
|
||||
std::string val;
|
||||
|
||||
try {
|
||||
val = Cast_Helper<std::string &>()(vals[0]);
|
||||
val = dispatchkit::Cast_Helper<std::string &>()(vals[0]);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
throw EvalError("Can not evaluate string: " + val, langkit::TokenPtr());
|
||||
@ -191,11 +191,11 @@ public:
|
||||
Eval_Engine build_eval_system(langkit::Lexer &lexer, langkit::Rule &parser) {
|
||||
using namespace langkit;
|
||||
Eval_Engine ss;
|
||||
Bootstrap::bootstrap(ss);
|
||||
bootstrap_vector<std::vector<Boxed_Value> >(ss, "Vector");
|
||||
dispatchkit::Bootstrap::bootstrap(ss);
|
||||
dispatchkit::bootstrap_vector<std::vector<dispatchkit::Boxed_Value> >(ss, "Vector");
|
||||
|
||||
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
||||
new Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::eval, boost::ref(*this), _1), 1)), "eval");
|
||||
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
|
||||
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&ChaiScript_System<Eval_Engine>::eval, boost::ref(*this), _1), 1)), "eval");
|
||||
|
||||
evaluate_string("def print(x) { print_string(x.to_string()) } ");
|
||||
|
||||
@ -220,10 +220,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
|
||||
dispatchkit::Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
|
||||
using namespace langkit;
|
||||
std::vector<TokenPtr> tokens = lexer.lex(input, filename);
|
||||
Boxed_Value value;
|
||||
dispatchkit::Boxed_Value value;
|
||||
|
||||
for (unsigned int i = 0; i < tokens.size(); ++i) {
|
||||
if ((tokens[i]->identifier == TokenType::Quoted_String) || (tokens[i]->identifier == TokenType::Single_Quoted_String)) {
|
||||
@ -262,12 +262,12 @@ public:
|
||||
return value;
|
||||
}
|
||||
|
||||
Boxed_Value evaluate_file(const char *filename) {
|
||||
dispatchkit::Boxed_Value evaluate_file(const char *filename) {
|
||||
return evaluate_string(load_file(filename), filename);
|
||||
}
|
||||
};
|
||||
|
||||
typedef ChaiScript_System<Dispatch_Engine> ChaiScript_Engine;
|
||||
typedef ChaiScript_System<dispatchkit::Dispatch_Engine> ChaiScript_Engine;
|
||||
|
||||
#endif /* CHAISCRIPT_ENGINE_HPP_ */
|
||||
|
||||
|
@ -21,10 +21,10 @@ struct EvalError {
|
||||
};
|
||||
|
||||
struct ReturnValue {
|
||||
Boxed_Value retval;
|
||||
dispatchkit::Boxed_Value retval;
|
||||
langkit::TokenPtr location;
|
||||
|
||||
ReturnValue(const Boxed_Value &return_value, const langkit::TokenPtr where) : retval(return_value), location(where) { }
|
||||
ReturnValue(const dispatchkit::Boxed_Value &return_value, const langkit::TokenPtr where) : retval(return_value), location(where) { }
|
||||
};
|
||||
|
||||
struct BreakLoop {
|
||||
@ -34,21 +34,21 @@ struct BreakLoop {
|
||||
};
|
||||
|
||||
template <typename Eval_System>
|
||||
const Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<Boxed_Value> &vals) {
|
||||
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
|
||||
ss.new_scope();
|
||||
|
||||
for (unsigned int i = 0; i < param_names.size(); ++i) {
|
||||
ss.add_object(param_names[i], vals[i]);
|
||||
}
|
||||
|
||||
Boxed_Value retval = eval_token(ss, node);
|
||||
dispatchkit::Boxed_Value retval = eval_token(ss, node);
|
||||
ss.pop_scope();
|
||||
return retval;
|
||||
}
|
||||
|
||||
template <typename Eval_System>
|
||||
Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
Boxed_Value retval;
|
||||
dispatchkit::Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
unsigned int i, j;
|
||||
|
||||
switch (node->identifier) {
|
||||
@ -60,10 +60,10 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
break;
|
||||
case (TokenType::Identifier) :
|
||||
if (node->text == "true") {
|
||||
retval = Boxed_Value(true);
|
||||
retval = dispatchkit::Boxed_Value(true);
|
||||
}
|
||||
else if (node->text == "false") {
|
||||
retval = Boxed_Value(false);
|
||||
retval = dispatchkit::Boxed_Value(false);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
@ -75,22 +75,22 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
}
|
||||
break;
|
||||
case (TokenType::Real_Number) :
|
||||
retval = Boxed_Value(double(atof(node->text.c_str())));
|
||||
retval = dispatchkit::Boxed_Value(double(atof(node->text.c_str())));
|
||||
break;
|
||||
case (TokenType::Integer) :
|
||||
retval = Boxed_Value(atoi(node->text.c_str()));
|
||||
retval = dispatchkit::Boxed_Value(atoi(node->text.c_str()));
|
||||
break;
|
||||
case (TokenType::Quoted_String) :
|
||||
retval = Boxed_Value(node->text);
|
||||
retval = dispatchkit::Boxed_Value(node->text);
|
||||
break;
|
||||
case (TokenType::Single_Quoted_String) :
|
||||
retval = Boxed_Value(node->text);
|
||||
retval = dispatchkit::Boxed_Value(node->text);
|
||||
break;
|
||||
case (TokenType::Equation) :
|
||||
retval = eval_token(ss, node->children.back());
|
||||
if (node->children.size() > 1) {
|
||||
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) {
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
plb << eval_token(ss, node->children[i]);
|
||||
plb << retval;
|
||||
try {
|
||||
@ -103,7 +103,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
}
|
||||
break;
|
||||
case (TokenType::Variable_Decl): {
|
||||
ss.add_object(node->children[0]->text, Boxed_Value());
|
||||
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());
|
||||
retval = ss.get_object(node->children[0]->text);
|
||||
}
|
||||
break;
|
||||
@ -115,7 +115,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
if (node->children.size() > 1) {
|
||||
for (i = 1; i < node->children.size(); i += 2) {
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
plb << retval;
|
||||
plb << eval_token(ss, node->children[i + 1]);
|
||||
|
||||
@ -132,7 +132,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
case (TokenType::Array_Call) : {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
for (i = 1; i < node->children.size(); ++i) {
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
plb << retval;
|
||||
plb << eval_token(ss, node->children[i]);
|
||||
try {
|
||||
@ -149,9 +149,9 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
break;
|
||||
case (TokenType::Negate) : {
|
||||
retval = eval_token(ss, node->children[1]);
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
plb << retval;
|
||||
plb << Boxed_Value(-1);
|
||||
plb << dispatchkit::Boxed_Value(-1);
|
||||
|
||||
try {
|
||||
retval = dispatch(ss.get_function("*"), plb);
|
||||
@ -165,17 +165,17 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
bool cond;
|
||||
try {
|
||||
retval = eval_token(ss, node->children[1]);
|
||||
cond = Cast_Helper<bool &>()(retval);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(retval);
|
||||
}
|
||||
catch (std::exception) {
|
||||
throw EvalError("Boolean not('!') condition not boolean", node->children[0]);
|
||||
}
|
||||
retval = Boxed_Value(!cond);
|
||||
retval = dispatchkit::Boxed_Value(!cond);
|
||||
}
|
||||
break;
|
||||
case (TokenType::Prefix) : {
|
||||
retval = eval_token(ss, node->children[1]);
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
plb << retval;
|
||||
|
||||
try {
|
||||
@ -188,11 +188,11 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
break;
|
||||
case (TokenType::Array_Init) : {
|
||||
try {
|
||||
retval = dispatch(ss.get_function("Vector"), Param_List_Builder());
|
||||
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder());
|
||||
for (i = 0; i < node->children.size(); ++i) {
|
||||
try {
|
||||
Boxed_Value tmp = eval_token(ss, node->children[i]);
|
||||
dispatch(ss.get_function("push_back"), Param_List_Builder() << retval << tmp);
|
||||
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[i]);
|
||||
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp);
|
||||
}
|
||||
catch (std::exception inner_e) {
|
||||
throw EvalError("Can not find appropriate 'push_back'", node->children[i]);
|
||||
@ -206,13 +206,13 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
break;
|
||||
case (TokenType::Fun_Call) : {
|
||||
|
||||
std::vector<std::pair<std::string, Dispatch_Engine::Function_Map::mapped_type> > fn;
|
||||
Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
|
||||
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
|
||||
Dispatch_Engine::Stack new_stack;
|
||||
new_stack.push_back(Dispatch_Engine::Scope());
|
||||
dispatchkit::Dispatch_Engine::Stack new_stack;
|
||||
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
|
||||
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
for (i = 1; i < node->children.size(); ++i) {
|
||||
plb << eval_token(ss, node->children[i]);
|
||||
}
|
||||
@ -237,16 +237,16 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
}
|
||||
break;
|
||||
case (TokenType::Method_Call) : {
|
||||
std::vector<std::pair<std::string, Dispatch_Engine::Function_Map::mapped_type> > fn;
|
||||
Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
|
||||
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
|
||||
|
||||
Dispatch_Engine::Stack new_stack;
|
||||
new_stack.push_back(Dispatch_Engine::Scope());
|
||||
dispatchkit::Dispatch_Engine::Stack new_stack;
|
||||
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
|
||||
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
if (node->children.size() > 1) {
|
||||
for (i = 1; i < node->children.size(); ++i) {
|
||||
Param_List_Builder plb;
|
||||
dispatchkit::Param_List_Builder plb;
|
||||
plb << retval;
|
||||
|
||||
for (j = 1; j < node->children[i]->children.size(); ++j) {
|
||||
@ -279,7 +279,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
bool cond;
|
||||
try {
|
||||
cond = Cast_Helper<bool &>()(retval);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(retval);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
throw EvalError("If condition not boolean", node->children[0]);
|
||||
@ -298,7 +298,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
else if (node->children[i]->text == "elseif") {
|
||||
retval = eval_token(ss, node->children[i+1]);
|
||||
try {
|
||||
cond = Cast_Helper<bool &>()(retval);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(retval);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
throw EvalError("Elseif condition not boolean", node->children[i+1]);
|
||||
@ -317,7 +317,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
bool cond;
|
||||
try {
|
||||
cond = Cast_Helper<bool &>()(retval);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(retval);
|
||||
}
|
||||
catch (std::exception) {
|
||||
throw EvalError("While condition not boolean", node->children[0]);
|
||||
@ -327,7 +327,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
eval_token(ss, node->children[1]);
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
try {
|
||||
cond = Cast_Helper<bool &>()(retval);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(retval);
|
||||
}
|
||||
catch (std::exception) {
|
||||
throw EvalError("While condition not boolean", node->children[0]);
|
||||
@ -337,11 +337,11 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
cond = false;
|
||||
}
|
||||
}
|
||||
retval = Boxed_Value();
|
||||
retval = dispatchkit::Boxed_Value();
|
||||
}
|
||||
break;
|
||||
case(TokenType::For_Block) : {
|
||||
Boxed_Value condition;
|
||||
dispatchkit::Boxed_Value condition;
|
||||
bool cond;
|
||||
|
||||
try {
|
||||
@ -352,7 +352,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
else if (node->children.size() == 3){
|
||||
condition = eval_token(ss, node->children[0]);
|
||||
}
|
||||
cond = Cast_Helper<bool &>()(condition);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(condition);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
throw EvalError("For condition not boolean", node);
|
||||
@ -369,7 +369,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
eval_token(ss, node->children[1]);
|
||||
condition = eval_token(ss, node->children[0]);
|
||||
}
|
||||
cond = Cast_Helper<bool &>()(condition);
|
||||
cond = dispatchkit::Cast_Helper<bool &>()(condition);
|
||||
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
@ -379,7 +379,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
cond = false;
|
||||
}
|
||||
}
|
||||
retval = Boxed_Value();
|
||||
retval = dispatchkit::Boxed_Value();
|
||||
}
|
||||
break;
|
||||
case (TokenType::Function_Def) : {
|
||||
@ -389,8 +389,8 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
param_names.push_back(node->children[i+1]->text);
|
||||
}
|
||||
|
||||
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
||||
new Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1))), node->children[0]->text);
|
||||
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
|
||||
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1))), node->children[0]->text);
|
||||
}
|
||||
break;
|
||||
case (TokenType::Lambda_Def) : {
|
||||
@ -400,8 +400,9 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
param_names.push_back(node->children[i]->text);
|
||||
}
|
||||
|
||||
//retval = boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<boost::function<void (const std::string &)> >(&test));
|
||||
retval = Boxed_Value(boost::shared_ptr<Proxy_Function>(new Dynamic_Proxy_Function(
|
||||
//retval = boost::shared_ptr<dispatchkit::Proxy_Function>(new dispatchkit::Proxy_Function_Impl<boost::function<void (const std::string &)> >(&test));
|
||||
retval = dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
|
||||
new dispatchkit::Dynamic_Proxy_Function(
|
||||
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1))));
|
||||
}
|
||||
break;
|
||||
@ -418,7 +419,7 @@ Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
retval = eval_token(ss, node->children[0]);
|
||||
}
|
||||
else {
|
||||
retval = Boxed_Value();
|
||||
retval = dispatchkit::Boxed_Value();
|
||||
}
|
||||
throw ReturnValue(retval, node);
|
||||
}
|
||||
|
@ -3,6 +3,8 @@
|
||||
#include "chaiscript.hpp"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
using namespace dispatchkit;
|
||||
|
||||
std::string input;
|
||||
|
||||
ChaiScript_Engine chai;
|
||||
|
@ -4,456 +4,460 @@
|
||||
#include "dispatchkit.hpp"
|
||||
#include "bootstrap_pod.hpp"
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret add(P1 p1, P2 p2)
|
||||
namespace dispatchkit
|
||||
{
|
||||
return p1 + p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret subtract(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 - p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret divide(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 / p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret multiply(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 * p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool bool_and(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 && p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool bool_or(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 || p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &assign(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 = p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &assign_pod(P1 &p1, Boxed_POD_Value v)
|
||||
{
|
||||
if (v.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret add(P1 p1, P2 p2)
|
||||
{
|
||||
return (p1 = v.d);
|
||||
} else {
|
||||
return (p1 = v.i);
|
||||
return p1 + p2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 == p2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool not_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 != p2;
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 < p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 > p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 <= p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 >= p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ×equal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 *= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ×equal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret subtract(P1 p1, P2 p2)
|
||||
{
|
||||
return (p1 *= r.d);
|
||||
} else {
|
||||
return (p1 *= r.i);
|
||||
return p1 - p2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ÷sequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 /= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret divide(P1 p1, P2 p2)
|
||||
{
|
||||
return (p1 /= r.d);
|
||||
} else {
|
||||
return (p1 /= r.i);
|
||||
return p1 / p2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &addsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 += p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename Ret, typename P1, typename P2>
|
||||
Ret multiply(P1 p1, P2 p2)
|
||||
{
|
||||
return (p1 += r.d);
|
||||
} else {
|
||||
return (p1 += r.i);
|
||||
return p1 * p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &subtractsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 -= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
template<typename P1, typename P2>
|
||||
bool bool_and(P1 p1, P2 p2)
|
||||
{
|
||||
return (p1 -= r.d);
|
||||
} else {
|
||||
return (p1 -= r.i);
|
||||
return p1 && p2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixincrement(P1 &p1)
|
||||
{
|
||||
return (++p1);
|
||||
}
|
||||
template<typename P1, typename P2>
|
||||
bool bool_or(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 || p2;
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixdecrement(P1 &p1)
|
||||
{
|
||||
return (--p1);
|
||||
}
|
||||
template<typename P1, typename P2>
|
||||
P1 &assign(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 = p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixnegate(P1 &p1)
|
||||
{
|
||||
return (p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixnot(P1 &p1)
|
||||
{
|
||||
return (p1);
|
||||
}
|
||||
|
||||
|
||||
//Add canonical forms of operators
|
||||
template<typename T>
|
||||
void add_oper_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<const T&, const T&>, "=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_add(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<T, const T&, const T&>, "+");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_add_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &addsequal<T, T>, "+=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_subtract(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &subtract<T, const T&, const T&>, "-");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_divide(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ÷<T, const T&, const T&>, "-");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_multiply(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &multiply<T, const T&, const T&>, "*");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_not_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ¬_equals<const T&, const T&>, "!=");
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void add_oper_assign_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign<T,U>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign<T,T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign_pod<T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &less_than<const T&, const T&>, "<");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_greater_than(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &greater_than<const T&, const T&>, ">");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &less_than_equals<const T&, const T&>, "<=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_greater_than_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &greater_than_equals<const T&, const T&>, ">=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T, typename R>
|
||||
void add_opers_comparison_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<const T&, const R&>, "==");
|
||||
register_function(s, ¬_equals<const T&, const R&>, "!=");
|
||||
register_function(s, &less_than<const T&, const R&>, "<");
|
||||
register_function(s, &greater_than<const T&, const R&>, ">");
|
||||
register_function(s, &less_than_equals<const T&, const R&>, "<=");
|
||||
register_function(s, &greater_than_equals<const T&, const R&>, ">=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_comparison(Dispatch_Engine &s)
|
||||
{
|
||||
add_opers_comparison_overload<T, T>(s);
|
||||
}
|
||||
|
||||
template<typename Ret, typename T, typename R>
|
||||
void add_opers_arithmetic_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<Ret, T, R>, "+");
|
||||
register_function(s, &subtract<Ret, T, R>, "-");
|
||||
register_function(s, ÷<Ret, T, R>, "/");
|
||||
register_function(s, &multiply<Ret, T, R>, "*");
|
||||
register_function(s, ×equal<T, R>, "*=");
|
||||
register_function(s, ÷sequal<T, R>, "/=");
|
||||
register_function(s, &subtractsequal<T, R>, "-=");
|
||||
register_function(s, &addsequal<T, R>, "+=");
|
||||
register_function(s, &prefixincrement<T>, "++");
|
||||
register_function(s, &prefixdecrement<T>, "--");
|
||||
register_function(s, &prefixnegate<T>, "-");
|
||||
register_function(s, &prefixnot<T>, "!");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic_modify_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ×equal_pod<T>, "*=");
|
||||
register_function(s, ÷sequal_pod<T>, "/=");
|
||||
register_function(s, &subtractsequal_pod<T>, "-=");
|
||||
register_function(s, &addsequal_pod<T>, "+=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_basic_constructors(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), "clone");
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void add_constructor_overload(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T, const U &>(), type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic(Dispatch_Engine &s)
|
||||
{
|
||||
add_opers_arithmetic_overload<T, T, T>(s);
|
||||
|
||||
}
|
||||
|
||||
class bad_boxed_value_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
bad_boxed_value_cast(const std::string &val) throw()
|
||||
: m_val(val)
|
||||
template<typename P1>
|
||||
P1 &assign_pod(P1 &p1, Boxed_POD_Value v)
|
||||
{
|
||||
if (v.m_isfloat)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~bad_boxed_value_cast() throw()
|
||||
{
|
||||
}
|
||||
|
||||
virtual const char * what() const throw()
|
||||
{
|
||||
return m_val.c_str();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_val;
|
||||
};
|
||||
|
||||
|
||||
|
||||
//Built in to_string operator
|
||||
template<typename Input>
|
||||
std::string to_string(Input i)
|
||||
{
|
||||
return boost::lexical_cast<std::string>(i);
|
||||
}
|
||||
|
||||
template<> std::string to_string(bool b)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
return "true";
|
||||
} else {
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
|
||||
{
|
||||
s.register_type<T>(name);
|
||||
add_basic_constructors<T>(s, name);
|
||||
add_oper_assign<T>(s);
|
||||
add_oper_assign_pod<T>(s);
|
||||
add_opers_arithmetic<T>(s);
|
||||
add_opers_arithmetic_modify_pod<T>(s);
|
||||
register_function(s, &to_string<T>, "to_string");
|
||||
}
|
||||
|
||||
struct Bootstrap
|
||||
{
|
||||
static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
|
||||
{
|
||||
if (lhs.is_unknown())
|
||||
{
|
||||
return (lhs.assign(rhs));
|
||||
return (p1 = v.d);
|
||||
} else {
|
||||
throw bad_boxed_value_cast("boxed_value has a set type alread");
|
||||
return (p1 = v.i);
|
||||
}
|
||||
}
|
||||
|
||||
static void print(const std::string &s)
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool equals(P1 p1, P2 p2)
|
||||
{
|
||||
std::cout << s << std::endl;
|
||||
return p1 == p2;
|
||||
}
|
||||
|
||||
static void bootstrap(Dispatch_Engine &s)
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool not_equals(P1 p1, P2 p2)
|
||||
{
|
||||
s.register_type<void>("void");
|
||||
|
||||
s.register_type<std::string>("string");
|
||||
|
||||
add_basic_constructors<bool>(s, "bool");
|
||||
add_basic_constructors<std::string>(s, "string");
|
||||
add_oper_assign<std::string>(s);
|
||||
|
||||
register_function(s, &to_string<const std::string &>, "to_string");
|
||||
register_function(s, &to_string<bool>, "to_string");
|
||||
register_function(s, &unknown_assign, "=");
|
||||
|
||||
bootstrap_pod_type<double>(s, "double");
|
||||
bootstrap_pod_type<int>(s, "int");
|
||||
bootstrap_pod_type<size_t>(s, "size_t");
|
||||
bootstrap_pod_type<char>(s, "char");
|
||||
bootstrap_pod_type<int64_t>(s, "int64_t");
|
||||
|
||||
|
||||
|
||||
Pod_Bootstrap::add_opers_comparison(s);
|
||||
Pod_Bootstrap::add_opers_arithmetic(s);
|
||||
|
||||
add_oper_add<std::string>(s);
|
||||
add_oper_add_equals <std::string>(s);
|
||||
|
||||
register_function(s, &print, "print_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)), "dump_object");
|
||||
|
||||
register_function(s, &bool_and<bool, bool>, "&&");
|
||||
register_function(s, &bool_or<bool, bool>, "||");
|
||||
return p1 != p2;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 < p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 > p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool less_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 <= p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
bool greater_than_equals(P1 p1, P2 p2)
|
||||
{
|
||||
return p1 >= p2;
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ×equal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 *= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ×equal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 *= r.d);
|
||||
} else {
|
||||
return (p1 *= r.i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 ÷sequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 /= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 ÷sequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 /= r.d);
|
||||
} else {
|
||||
return (p1 /= r.i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &addsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 += p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &addsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 += r.d);
|
||||
} else {
|
||||
return (p1 += r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1, typename P2>
|
||||
P1 &subtractsequal(P1 &p1, const P2 &p2)
|
||||
{
|
||||
return (p1 -= p2);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &subtractsequal_pod(P1 &p1, Boxed_POD_Value r)
|
||||
{
|
||||
if (r.m_isfloat)
|
||||
{
|
||||
return (p1 -= r.d);
|
||||
} else {
|
||||
return (p1 -= r.i);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixincrement(P1 &p1)
|
||||
{
|
||||
return (++p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixdecrement(P1 &p1)
|
||||
{
|
||||
return (--p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixnegate(P1 &p1)
|
||||
{
|
||||
return (p1);
|
||||
}
|
||||
|
||||
template<typename P1>
|
||||
P1 &prefixnot(P1 &p1)
|
||||
{
|
||||
return (p1);
|
||||
}
|
||||
|
||||
|
||||
//Add canonical forms of operators
|
||||
template<typename T>
|
||||
void add_oper_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<const T&, const T&>, "=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_add(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<T, const T&, const T&>, "+");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_add_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &addsequal<T, T>, "+=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_subtract(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &subtract<T, const T&, const T&>, "-");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_divide(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ÷<T, const T&, const T&>, "-");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_multiply(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &multiply<T, const T&, const T&>, "*");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_not_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ¬_equals<const T&, const T&>, "!=");
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void add_oper_assign_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign<T,U>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign<T,T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_assign_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &assign_pod<T>, "=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &less_than<const T&, const T&>, "<");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_greater_than(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &greater_than<const T&, const T&>, ">");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_less_than_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &less_than_equals<const T&, const T&>, "<=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_oper_greater_than_equals(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &greater_than_equals<const T&, const T&>, ">=");
|
||||
}
|
||||
|
||||
|
||||
template<typename T, typename R>
|
||||
void add_opers_comparison_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals<const T&, const R&>, "==");
|
||||
register_function(s, ¬_equals<const T&, const R&>, "!=");
|
||||
register_function(s, &less_than<const T&, const R&>, "<");
|
||||
register_function(s, &greater_than<const T&, const R&>, ">");
|
||||
register_function(s, &less_than_equals<const T&, const R&>, "<=");
|
||||
register_function(s, &greater_than_equals<const T&, const R&>, ">=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_comparison(Dispatch_Engine &s)
|
||||
{
|
||||
add_opers_comparison_overload<T, T>(s);
|
||||
}
|
||||
|
||||
template<typename Ret, typename T, typename R>
|
||||
void add_opers_arithmetic_overload(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add<Ret, T, R>, "+");
|
||||
register_function(s, &subtract<Ret, T, R>, "-");
|
||||
register_function(s, ÷<Ret, T, R>, "/");
|
||||
register_function(s, &multiply<Ret, T, R>, "*");
|
||||
register_function(s, ×equal<T, R>, "*=");
|
||||
register_function(s, ÷sequal<T, R>, "/=");
|
||||
register_function(s, &subtractsequal<T, R>, "-=");
|
||||
register_function(s, &addsequal<T, R>, "+=");
|
||||
register_function(s, &prefixincrement<T>, "++");
|
||||
register_function(s, &prefixdecrement<T>, "--");
|
||||
register_function(s, &prefixnegate<T>, "-");
|
||||
register_function(s, &prefixnot<T>, "!");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic_modify_pod(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, ×equal_pod<T>, "*=");
|
||||
register_function(s, ÷sequal_pod<T>, "/=");
|
||||
register_function(s, &subtractsequal_pod<T>, "-=");
|
||||
register_function(s, &addsequal_pod<T>, "+=");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_basic_constructors(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), type);
|
||||
s.register_function(build_constructor<T, const T &>(), "clone");
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
void add_constructor_overload(Dispatch_Engine &s, const std::string &type)
|
||||
{
|
||||
s.register_function(build_constructor<T, const U &>(), type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void add_opers_arithmetic(Dispatch_Engine &s)
|
||||
{
|
||||
add_opers_arithmetic_overload<T, T, T>(s);
|
||||
|
||||
}
|
||||
|
||||
class bad_boxed_value_cast : public std::bad_cast
|
||||
{
|
||||
public:
|
||||
bad_boxed_value_cast(const std::string &val) throw()
|
||||
: m_val(val)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~bad_boxed_value_cast() throw()
|
||||
{
|
||||
}
|
||||
|
||||
virtual const char * what() const throw()
|
||||
{
|
||||
return m_val.c_str();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_val;
|
||||
};
|
||||
|
||||
|
||||
|
||||
//Built in to_string operator
|
||||
template<typename Input>
|
||||
std::string to_string(Input i)
|
||||
{
|
||||
return boost::lexical_cast<std::string>(i);
|
||||
}
|
||||
|
||||
template<> std::string to_string(bool b)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
return "true";
|
||||
} else {
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void bootstrap_pod_type(Dispatch_Engine &s, const std::string &name)
|
||||
{
|
||||
s.register_type<T>(name);
|
||||
add_basic_constructors<T>(s, name);
|
||||
add_oper_assign<T>(s);
|
||||
add_oper_assign_pod<T>(s);
|
||||
add_opers_arithmetic<T>(s);
|
||||
add_opers_arithmetic_modify_pod<T>(s);
|
||||
register_function(s, &to_string<T>, "to_string");
|
||||
}
|
||||
|
||||
struct Bootstrap
|
||||
{
|
||||
static Boxed_Value unknown_assign(Boxed_Value lhs, Boxed_Value rhs)
|
||||
{
|
||||
if (lhs.is_unknown())
|
||||
{
|
||||
return (lhs.assign(rhs));
|
||||
} else {
|
||||
throw bad_boxed_value_cast("boxed_value has a set type alread");
|
||||
}
|
||||
}
|
||||
|
||||
static void print(const std::string &s)
|
||||
{
|
||||
std::cout << s << std::endl;
|
||||
}
|
||||
|
||||
static void bootstrap(Dispatch_Engine &s)
|
||||
{
|
||||
s.register_type<void>("void");
|
||||
|
||||
s.register_type<std::string>("string");
|
||||
|
||||
add_basic_constructors<bool>(s, "bool");
|
||||
add_basic_constructors<std::string>(s, "string");
|
||||
add_oper_assign<std::string>(s);
|
||||
|
||||
register_function(s, &to_string<const std::string &>, "to_string");
|
||||
register_function(s, &to_string<bool>, "to_string");
|
||||
register_function(s, &unknown_assign, "=");
|
||||
|
||||
bootstrap_pod_type<double>(s, "double");
|
||||
bootstrap_pod_type<int>(s, "int");
|
||||
bootstrap_pod_type<size_t>(s, "size_t");
|
||||
bootstrap_pod_type<char>(s, "char");
|
||||
bootstrap_pod_type<int64_t>(s, "int64_t");
|
||||
|
||||
|
||||
|
||||
Pod_Bootstrap::add_opers_comparison(s);
|
||||
Pod_Bootstrap::add_opers_arithmetic(s);
|
||||
|
||||
add_oper_add<std::string>(s);
|
||||
add_oper_add_equals <std::string>(s);
|
||||
|
||||
register_function(s, &print, "print_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)), "dump_object");
|
||||
|
||||
register_function(s, &bool_and<bool, bool>, "&&");
|
||||
register_function(s, &bool_or<bool, bool>, "||");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -4,77 +4,79 @@
|
||||
|
||||
#include "register_function.hpp"
|
||||
|
||||
|
||||
struct Pod_Bootstrap
|
||||
namespace dispatchkit
|
||||
{
|
||||
static Boxed_Value add(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
struct Pod_Bootstrap
|
||||
{
|
||||
return l + r;
|
||||
}
|
||||
static Boxed_Value add(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l + r;
|
||||
}
|
||||
|
||||
static Boxed_Value subtract(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l - r;
|
||||
}
|
||||
static Boxed_Value subtract(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l - r;
|
||||
}
|
||||
|
||||
static Boxed_Value divide(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l / r;
|
||||
}
|
||||
static Boxed_Value divide(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l / r;
|
||||
}
|
||||
|
||||
static Boxed_Value multiply(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l * r;
|
||||
}
|
||||
static Boxed_Value multiply(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l * r;
|
||||
}
|
||||
|
||||
static bool equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l == r;
|
||||
}
|
||||
static bool equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l == r;
|
||||
}
|
||||
|
||||
static bool not_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l != r;
|
||||
}
|
||||
static bool not_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l != r;
|
||||
}
|
||||
|
||||
static bool less_than(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l < r;
|
||||
}
|
||||
static bool less_than(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l < r;
|
||||
}
|
||||
|
||||
static bool greater_than(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l > r;
|
||||
}
|
||||
static bool greater_than(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l > r;
|
||||
}
|
||||
|
||||
static bool less_than_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l <= r;
|
||||
}
|
||||
static bool less_than_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l <= r;
|
||||
}
|
||||
|
||||
static bool greater_than_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l >= r;
|
||||
}
|
||||
static bool greater_than_equals(Boxed_POD_Value l, Boxed_POD_Value r)
|
||||
{
|
||||
return l >= r;
|
||||
}
|
||||
|
||||
static void add_opers_comparison(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals, "==");
|
||||
register_function(s, ¬_equals, "!=");
|
||||
register_function(s, &less_than, "<");
|
||||
register_function(s, &greater_than, ">");
|
||||
register_function(s, &less_than_equals, "<=");
|
||||
register_function(s, &greater_than_equals, ">=");
|
||||
}
|
||||
static void add_opers_comparison(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &equals, "==");
|
||||
register_function(s, ¬_equals, "!=");
|
||||
register_function(s, &less_than, "<");
|
||||
register_function(s, &greater_than, ">");
|
||||
register_function(s, &less_than_equals, "<=");
|
||||
register_function(s, &greater_than_equals, ">=");
|
||||
}
|
||||
|
||||
static void add_opers_arithmetic(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add, "+");
|
||||
register_function(s, &subtract, "-");
|
||||
register_function(s, ÷, "/");
|
||||
register_function(s, &multiply, "*");
|
||||
}
|
||||
};
|
||||
static void add_opers_arithmetic(Dispatch_Engine &s)
|
||||
{
|
||||
register_function(s, &add, "+");
|
||||
register_function(s, &subtract, "-");
|
||||
register_function(s, ÷, "/");
|
||||
register_function(s, &multiply, "*");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -3,82 +3,85 @@
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_reversible_container(Dispatch_Engine &system, const std::string &type)
|
||||
namespace dispatchkit
|
||||
{
|
||||
}
|
||||
template<typename ContainerType>
|
||||
void bootstrap_reversible_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_random_access_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_reversible_container<ContainerType>(system, type);
|
||||
template<typename ContainerType>
|
||||
void bootstrap_random_access_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_reversible_container<ContainerType>(system, type);
|
||||
|
||||
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
|
||||
typedef typename ContainerType::reference(ContainerType::*indexoper)(size_t);
|
||||
|
||||
//In the interest of runtime safety for the system, we prefer the at() method for [] access,
|
||||
//to throw an exception in an out of bounds condition.
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at)), "[]");
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[])), "at");
|
||||
}
|
||||
//In the interest of runtime safety for the system, we prefer the at() method for [] access,
|
||||
//to throw an exception in an out of bounds condition.
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::at)), "[]");
|
||||
system.register_function(
|
||||
boost::function<typename ContainerType::reference (ContainerType *, int)>(indexoper(&ContainerType::operator[])), "at");
|
||||
}
|
||||
|
||||
template<typename Assignable>
|
||||
void bootstrap_assignable(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_function(
|
||||
boost::function<Assignable &(Assignable*, const Assignable&)>(&Assignable::operator=), "=");
|
||||
}
|
||||
template<typename Assignable>
|
||||
void bootstrap_assignable(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_function(
|
||||
boost::function<Assignable &(Assignable*, const Assignable&)>(&Assignable::operator=), "=");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_assignable<ContainerType>(system, type);
|
||||
template<typename ContainerType>
|
||||
void bootstrap_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_assignable<ContainerType>(system, type);
|
||||
|
||||
system.register_function(
|
||||
boost::function<size_t (ContainerType *)>(&ContainerType::size), "size");
|
||||
system.register_function(
|
||||
boost::function<size_t (ContainerType *)>(&ContainerType::size), "maxsize");
|
||||
}
|
||||
system.register_function(
|
||||
boost::function<size_t (ContainerType *)>(&ContainerType::size), "size");
|
||||
system.register_function(
|
||||
boost::function<size_t (ContainerType *)>(&ContainerType::size), "maxsize");
|
||||
}
|
||||
|
||||
template<typename ContainerType>
|
||||
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_container<ContainerType>(system, type);
|
||||
}
|
||||
template<typename ContainerType>
|
||||
void bootstrap_forward_container(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_container<ContainerType>(system, type);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_function(build_constructor<Type>(), type);
|
||||
}
|
||||
template<typename Type>
|
||||
void bootstrap_default_constructible(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_function(build_constructor<Type>(), type);
|
||||
}
|
||||
|
||||
template<typename SequenceType>
|
||||
void bootstrap_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_forward_container<SequenceType>(system, type);
|
||||
bootstrap_default_constructible<SequenceType>(system, type);
|
||||
}
|
||||
template<typename SequenceType>
|
||||
void bootstrap_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_forward_container<SequenceType>(system, type);
|
||||
bootstrap_default_constructible<SequenceType>(system, type);
|
||||
}
|
||||
|
||||
template<typename SequenceType>
|
||||
void bootstrap_back_insertion_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_sequence<SequenceType>(system, type);
|
||||
template<typename SequenceType>
|
||||
void bootstrap_back_insertion_sequence(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
bootstrap_sequence<SequenceType>(system, type);
|
||||
|
||||
|
||||
typedef typename SequenceType::reference (SequenceType::*backptr)();
|
||||
typedef typename SequenceType::reference (SequenceType::*backptr)();
|
||||
|
||||
system.register_function(boost::function<typename SequenceType::reference (SequenceType *)>(backptr(&SequenceType::back)), "back");
|
||||
system.register_function(boost::function<void (SequenceType *,typename SequenceType::value_type)>(&SequenceType::push_back), "push_back");
|
||||
system.register_function(boost::function<void (SequenceType *)>(&SequenceType::pop_back), "pop_back");
|
||||
}
|
||||
system.register_function(boost::function<typename SequenceType::reference (SequenceType *)>(backptr(&SequenceType::back)), "back");
|
||||
system.register_function(boost::function<void (SequenceType *,typename SequenceType::value_type)>(&SequenceType::push_back), "push_back");
|
||||
system.register_function(boost::function<void (SequenceType *)>(&SequenceType::pop_back), "pop_back");
|
||||
}
|
||||
|
||||
template<typename VectorType>
|
||||
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<VectorType>(type);
|
||||
bootstrap_random_access_container<VectorType>(system, type);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
||||
template<typename VectorType>
|
||||
void bootstrap_vector(Dispatch_Engine &system, const std::string &type)
|
||||
{
|
||||
system.register_type<VectorType>(type);
|
||||
bootstrap_random_access_container<VectorType>(system, type);
|
||||
bootstrap_back_insertion_sequence<VectorType>(system, type);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -8,28 +8,30 @@
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
class Boxed_Value
|
||||
namespace dispatchkit
|
||||
{
|
||||
public:
|
||||
struct Void_Type
|
||||
{
|
||||
};
|
||||
|
||||
private:
|
||||
struct Data
|
||||
{
|
||||
struct Shared_Ptr_Proxy
|
||||
class Boxed_Value
|
||||
{
|
||||
public:
|
||||
struct Void_Type
|
||||
{
|
||||
virtual ~Shared_Ptr_Proxy()
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool unique(boost::any *) = 0;
|
||||
virtual long use_count(boost::any *) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Shared_Ptr_Proxy_Impl : Shared_Ptr_Proxy
|
||||
private:
|
||||
struct Data
|
||||
{
|
||||
struct Shared_Ptr_Proxy
|
||||
{
|
||||
virtual ~Shared_Ptr_Proxy()
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool unique(boost::any *) = 0;
|
||||
virtual long use_count(boost::any *) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Shared_Ptr_Proxy_Impl : Shared_Ptr_Proxy
|
||||
{
|
||||
virtual ~Shared_Ptr_Proxy_Impl()
|
||||
{
|
||||
@ -46,454 +48,452 @@ class Boxed_Value
|
||||
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(a);
|
||||
return ptr->use_count();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
Data(const Type_Info &ti,
|
||||
const boost::any &to,
|
||||
bool tr,
|
||||
const boost::shared_ptr<Shared_Ptr_Proxy> &t_proxy = boost::shared_ptr<Shared_Ptr_Proxy>())
|
||||
: m_type_info(ti), m_obj(to),
|
||||
Data(const Type_Info &ti,
|
||||
const boost::any &to,
|
||||
bool tr,
|
||||
const boost::shared_ptr<Shared_Ptr_Proxy> &t_proxy = boost::shared_ptr<Shared_Ptr_Proxy>())
|
||||
: m_type_info(ti), m_obj(to),
|
||||
m_is_ref(tr), m_ptr_proxy(t_proxy)
|
||||
{
|
||||
}
|
||||
|
||||
Data &operator=(const Data &rhs)
|
||||
{
|
||||
m_type_info = rhs.m_type_info;
|
||||
m_obj = rhs.m_obj;
|
||||
m_is_ref = rhs.m_is_ref;
|
||||
m_ptr_proxy = rhs.m_ptr_proxy;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
static bool get_false()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Type_Info m_type_info;
|
||||
boost::any m_obj;
|
||||
bool m_is_ref;
|
||||
boost::shared_ptr<Shared_Ptr_Proxy> m_ptr_proxy;
|
||||
};
|
||||
|
||||
struct Object_Cache
|
||||
{
|
||||
boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
|
||||
{
|
||||
return boost::shared_ptr<Data> (new Data(
|
||||
Get_Type_Info<void>::get(),
|
||||
boost::any(),
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(boost::shared_ptr<T> obj)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(obj),
|
||||
false,
|
||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||
);
|
||||
|
||||
std::map<void *, Data >::iterator itr
|
||||
= m_ptrs.find(obj.get());
|
||||
|
||||
if (itr != m_ptrs.end())
|
||||
{
|
||||
(*data) = (itr->second);
|
||||
} else {
|
||||
m_ptrs.insert(std::make_pair(obj.get(), *data));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(obj),
|
||||
true)
|
||||
);
|
||||
|
||||
std::map<void *, Data >::iterator itr
|
||||
= m_ptrs.find(obj.get_pointer());
|
||||
|
||||
if (itr != m_ptrs.end())
|
||||
{
|
||||
std::cout << "Reference wrapper ptr found, using it" << std::endl;
|
||||
(*data) = (itr->second);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(const T& t)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(boost::shared_ptr<T>(new T(t))),
|
||||
false,
|
||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||
);
|
||||
|
||||
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(&data->m_obj);
|
||||
|
||||
m_ptrs.insert(std::make_pair(ptr->get(), *data));
|
||||
return data;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Data> get()
|
||||
{
|
||||
return boost::shared_ptr<Data> (new Data(
|
||||
Type_Info(),
|
||||
boost::any(),
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
void cull()
|
||||
{
|
||||
std::map<void *, Data >::iterator itr = m_ptrs.begin();
|
||||
|
||||
while (itr != m_ptrs.end())
|
||||
{
|
||||
if (itr->second.m_ptr_proxy->unique(&itr->second.m_obj) == 1)
|
||||
{
|
||||
std::map<void *, Data >::iterator todel = itr;
|
||||
// std::cout << "Releasing unique ptr " << std::endl;
|
||||
++itr;
|
||||
m_ptrs.erase(todel);
|
||||
} else {
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
// std::cout << "References held: " << m_ptrs.size() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::map<void *, Data > m_ptrs;
|
||||
};
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
explicit Boxed_Value(T t)
|
||||
: m_data(get_object_cache().get(t))
|
||||
{
|
||||
}
|
||||
|
||||
Boxed_Value(const Boxed_Value &t_so)
|
||||
: m_data(t_so.m_data)
|
||||
{
|
||||
}
|
||||
|
||||
Data &operator=(const Data &rhs)
|
||||
Boxed_Value()
|
||||
: m_data(get_object_cache().get())
|
||||
{
|
||||
m_type_info = rhs.m_type_info;
|
||||
m_obj = rhs.m_obj;
|
||||
m_is_ref = rhs.m_is_ref;
|
||||
m_ptr_proxy = rhs.m_ptr_proxy;
|
||||
}
|
||||
|
||||
~Boxed_Value()
|
||||
{
|
||||
get_object_cache().cull();
|
||||
}
|
||||
|
||||
|
||||
Object_Cache &get_object_cache()
|
||||
{
|
||||
static Object_Cache oc;
|
||||
return oc;
|
||||
}
|
||||
|
||||
|
||||
Boxed_Value assign(const Boxed_Value &rhs)
|
||||
{
|
||||
(*m_data) = (*rhs.m_data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
static bool get_false()
|
||||
Boxed_Value &operator=(const Boxed_Value &rhs)
|
||||
{
|
||||
return false;
|
||||
m_data = rhs.m_data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Type_Info m_type_info;
|
||||
boost::any m_obj;
|
||||
bool m_is_ref;
|
||||
boost::shared_ptr<Shared_Ptr_Proxy> m_ptr_proxy;
|
||||
const Type_Info &get_type_info() const
|
||||
{
|
||||
return m_data->m_type_info;
|
||||
}
|
||||
|
||||
bool is_unknown() const
|
||||
{
|
||||
return m_data->m_type_info.m_is_unknown;
|
||||
}
|
||||
|
||||
boost::any get() const
|
||||
{
|
||||
return m_data->m_obj;
|
||||
}
|
||||
|
||||
bool is_ref() const
|
||||
{
|
||||
return m_data->m_is_ref;
|
||||
}
|
||||
|
||||
private:
|
||||
boost::shared_ptr<Data> m_data;
|
||||
};
|
||||
|
||||
|
||||
//cast_help specializations
|
||||
template<typename Result>
|
||||
struct Cast_Helper
|
||||
{
|
||||
typename boost::reference_wrapper<typename boost::add_const<Result>::type > operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct Object_Cache
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result &>
|
||||
{
|
||||
boost::shared_ptr<Data> get(Boxed_Value::Void_Type)
|
||||
typename boost::reference_wrapper<typename boost::add_const<Result>::type > operator()(Boxed_Value ob)
|
||||
{
|
||||
return boost::shared_ptr<Data> (new Data(
|
||||
Get_Type_Info<void>::get(),
|
||||
boost::any(),
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(boost::shared_ptr<T> obj)
|
||||
if (ob.is_ref())
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(obj),
|
||||
false,
|
||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||
);
|
||||
|
||||
std::map<void *, Data >::iterator itr
|
||||
= m_ptrs.find(obj.get());
|
||||
|
||||
if (itr != m_ptrs.end())
|
||||
{
|
||||
(*data) = (itr->second);
|
||||
} else {
|
||||
m_ptrs.insert(std::make_pair(obj.get(), *data));
|
||||
}
|
||||
|
||||
return data;
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(boost::reference_wrapper<T> obj)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(obj),
|
||||
true)
|
||||
);
|
||||
|
||||
std::map<void *, Data >::iterator itr
|
||||
= m_ptrs.find(obj.get_pointer());
|
||||
|
||||
if (itr != m_ptrs.end())
|
||||
{
|
||||
std::cout << "Reference wrapper ptr found, using it" << std::endl;
|
||||
(*data) = (itr->second);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
boost::shared_ptr<Data> get(const T& t)
|
||||
{
|
||||
boost::shared_ptr<Data> data(new Data(
|
||||
Get_Type_Info<T>::get(),
|
||||
boost::any(boost::shared_ptr<T>(new T(t))),
|
||||
false,
|
||||
boost::shared_ptr<Data::Shared_Ptr_Proxy>(new Data::Shared_Ptr_Proxy_Impl<T>()))
|
||||
);
|
||||
|
||||
boost::shared_ptr<T> *ptr = boost::any_cast<boost::shared_ptr<T> >(&data->m_obj);
|
||||
|
||||
m_ptrs.insert(std::make_pair(ptr->get(), *data));
|
||||
return data;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Data> get()
|
||||
{
|
||||
return boost::shared_ptr<Data> (new Data(
|
||||
Type_Info(),
|
||||
boost::any(),
|
||||
false)
|
||||
);
|
||||
}
|
||||
|
||||
void cull()
|
||||
{
|
||||
std::map<void *, Data >::iterator itr = m_ptrs.begin();
|
||||
|
||||
while (itr != m_ptrs.end())
|
||||
{
|
||||
if (itr->second.m_ptr_proxy->unique(&itr->second.m_obj) == 1)
|
||||
{
|
||||
std::map<void *, Data >::iterator todel = itr;
|
||||
// std::cout << "Releasing unique ptr " << std::endl;
|
||||
++itr;
|
||||
m_ptrs.erase(todel);
|
||||
} else {
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
// std::cout << "References held: " << m_ptrs.size() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::map<void *, Data > m_ptrs;
|
||||
};
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
explicit Boxed_Value(T t)
|
||||
: m_data(get_object_cache().get(t))
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result *>
|
||||
{
|
||||
const Result *operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result *>
|
||||
{
|
||||
Result *operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result &>
|
||||
{
|
||||
typename boost::reference_wrapper<Result> operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::any_cast<boost::reference_wrapper<Result> >(ob.get());
|
||||
} else {
|
||||
return boost::ref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<typename boost::shared_ptr<Result> >
|
||||
{
|
||||
typename boost::shared_ptr<Result> operator()(Boxed_Value ob)
|
||||
{
|
||||
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<Boxed_Value>
|
||||
{
|
||||
Boxed_Value operator()(Boxed_Value ob)
|
||||
{
|
||||
return ob;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct Boxed_POD_Value
|
||||
{
|
||||
Boxed_POD_Value(const Boxed_Value &v)
|
||||
: d(0), i(0), m_isfloat(false)
|
||||
{
|
||||
const int inp_ = int(v.get_type_info().m_type_info);
|
||||
|
||||
const int char_ = int(&typeid(char));
|
||||
const int bool_ = int(&typeid(bool));
|
||||
|
||||
const int double_ = int(&typeid(double));
|
||||
const int float_ = int(&typeid(float));
|
||||
|
||||
const int long_ = int(&typeid(long));
|
||||
const int unsigned_long_ = int(&typeid(unsigned long));
|
||||
const int int_ = int(&typeid(int));
|
||||
const int unsigned_int_ = int(&typeid(unsigned int));
|
||||
|
||||
const int uint8_t_ = int(&typeid(uint8_t));
|
||||
const int uint16_t_ = int(&typeid(uint16_t));
|
||||
const int uint32_t_ = int(&typeid(uint32_t));
|
||||
// const int uint64_t_ = int(&typeid(uint64_t));
|
||||
|
||||
const int int8_t_ = int(&typeid(int8_t));
|
||||
const int int16_t_ = int(&typeid(int16_t));
|
||||
const int int32_t_ = int(&typeid(int32_t));
|
||||
const int int64_t_ = int(&typeid(int64_t));
|
||||
|
||||
if (inp_ == double_)
|
||||
{
|
||||
d = Cast_Helper<double>()(v);
|
||||
m_isfloat = true;
|
||||
} else if (inp_ == float_) {
|
||||
d = Cast_Helper<float>()(v);
|
||||
m_isfloat = true;
|
||||
} else if (inp_ == bool_ ) {
|
||||
i = Cast_Helper<bool>()(v);
|
||||
} else if (inp_ == char_) {
|
||||
i = Cast_Helper<char>()(v);
|
||||
} else if (inp_ == int_) {
|
||||
i = Cast_Helper<int>()(v);
|
||||
} else if (inp_ == unsigned_int_) {
|
||||
i = Cast_Helper<unsigned int>()(v);
|
||||
} else if (inp_ == long_) {
|
||||
i = Cast_Helper<long>()(v);
|
||||
} else if (inp_ == unsigned_long_) {
|
||||
i = Cast_Helper<unsigned long>()(v);
|
||||
} else if (inp_ == int8_t_) {
|
||||
i = Cast_Helper<int8_t>()(v);
|
||||
} else if (inp_ == int16_t_) {
|
||||
i = Cast_Helper<int16_t>()(v);
|
||||
} else if (inp_ == int32_t_) {
|
||||
i = Cast_Helper<int32_t>()(v);
|
||||
} else if (inp_ == int64_t_) {
|
||||
i = Cast_Helper<int64_t>()(v);
|
||||
} else if (inp_ == uint8_t_) {
|
||||
i = Cast_Helper<uint8_t>()(v);
|
||||
} else if (inp_ == uint16_t_) {
|
||||
i = Cast_Helper<uint16_t>()(v);
|
||||
} else if (inp_ == uint32_t_) {
|
||||
i = Cast_Helper<uint32_t>()(v);
|
||||
} else {
|
||||
throw boost::bad_any_cast();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) == ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator<(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) < ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator>(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) > ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator>=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) >= ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator<=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) <= ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator!=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) != ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
Boxed_Value operator+(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i + r.i);
|
||||
}
|
||||
|
||||
Boxed_Value(const Boxed_Value &t_so)
|
||||
: m_data(t_so.m_data)
|
||||
{
|
||||
return Boxed_Value(((m_isfloat)?d:i) + ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value()
|
||||
: m_data(get_object_cache().get())
|
||||
Boxed_Value operator*(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i * r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) * ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
~Boxed_Value()
|
||||
Boxed_Value operator/(const Boxed_POD_Value &r) const
|
||||
{
|
||||
get_object_cache().cull();
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i / r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) / ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
|
||||
Object_Cache &get_object_cache()
|
||||
Boxed_Value operator-(const Boxed_POD_Value &r) const
|
||||
{
|
||||
static Object_Cache oc;
|
||||
return oc;
|
||||
}
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i - r.i);
|
||||
}
|
||||
|
||||
|
||||
Boxed_Value assign(const Boxed_Value &rhs)
|
||||
{
|
||||
(*m_data) = (*rhs.m_data);
|
||||
return *this;
|
||||
return Boxed_Value(((m_isfloat)?d:i) - ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value &operator=(const Boxed_Value &rhs)
|
||||
double d;
|
||||
int64_t i;
|
||||
|
||||
bool m_isfloat;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<Boxed_POD_Value>
|
||||
{
|
||||
m_data = rhs.m_data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const Type_Info &get_type_info() const
|
||||
{
|
||||
return m_data->m_type_info;
|
||||
}
|
||||
|
||||
bool is_unknown() const
|
||||
{
|
||||
return m_data->m_type_info.m_is_unknown;
|
||||
}
|
||||
|
||||
boost::any get() const
|
||||
{
|
||||
return m_data->m_obj;
|
||||
}
|
||||
|
||||
bool is_ref() const
|
||||
{
|
||||
return m_data->m_is_ref;
|
||||
}
|
||||
|
||||
private:
|
||||
boost::shared_ptr<Data> m_data;
|
||||
};
|
||||
|
||||
|
||||
//cast_help specializations
|
||||
template<typename Result>
|
||||
struct Cast_Helper
|
||||
{
|
||||
typename boost::reference_wrapper<typename boost::add_const<Result>::type > operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result &>
|
||||
{
|
||||
typename boost::reference_wrapper<typename boost::add_const<Result>::type > operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::cref((boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get());
|
||||
} else {
|
||||
return boost::cref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<const Result *>
|
||||
{
|
||||
const Result *operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result *>
|
||||
{
|
||||
Result *operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return (boost::any_cast<boost::reference_wrapper<Result> >(ob.get())).get_pointer();
|
||||
} else {
|
||||
return (boost::any_cast<boost::shared_ptr<Result> >(ob.get())).get();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<Result &>
|
||||
{
|
||||
typename boost::reference_wrapper<Result> operator()(Boxed_Value ob)
|
||||
{
|
||||
if (ob.is_ref())
|
||||
{
|
||||
return boost::any_cast<boost::reference_wrapper<Result> >(ob.get());
|
||||
} else {
|
||||
return boost::ref(*(boost::any_cast<boost::shared_ptr<Result> >(ob.get())));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Result>
|
||||
struct Cast_Helper<typename boost::shared_ptr<Result> >
|
||||
{
|
||||
typename boost::shared_ptr<Result> operator()(Boxed_Value ob)
|
||||
{
|
||||
return boost::any_cast<boost::shared_ptr<Result> >(ob.get());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<Boxed_Value>
|
||||
{
|
||||
Boxed_Value operator()(Boxed_Value ob)
|
||||
{
|
||||
return ob;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
struct Boxed_POD_Value
|
||||
{
|
||||
Boxed_POD_Value(const Boxed_Value &v)
|
||||
: d(0), i(0), m_isfloat(false)
|
||||
{
|
||||
const int inp_ = int(v.get_type_info().m_type_info);
|
||||
|
||||
const int char_ = int(&typeid(char));
|
||||
const int bool_ = int(&typeid(bool));
|
||||
|
||||
const int double_ = int(&typeid(double));
|
||||
const int float_ = int(&typeid(float));
|
||||
|
||||
const int long_ = int(&typeid(long));
|
||||
const int unsigned_long_ = int(&typeid(unsigned long));
|
||||
const int int_ = int(&typeid(int));
|
||||
const int unsigned_int_ = int(&typeid(unsigned int));
|
||||
|
||||
const int uint8_t_ = int(&typeid(uint8_t));
|
||||
const int uint16_t_ = int(&typeid(uint16_t));
|
||||
const int uint32_t_ = int(&typeid(uint32_t));
|
||||
// const int uint64_t_ = int(&typeid(uint64_t));
|
||||
|
||||
const int int8_t_ = int(&typeid(int8_t));
|
||||
const int int16_t_ = int(&typeid(int16_t));
|
||||
const int int32_t_ = int(&typeid(int32_t));
|
||||
const int int64_t_ = int(&typeid(int64_t));
|
||||
|
||||
if (inp_ == double_)
|
||||
{
|
||||
d = Cast_Helper<double>()(v);
|
||||
m_isfloat = true;
|
||||
} else if (inp_ == float_) {
|
||||
d = Cast_Helper<float>()(v);
|
||||
m_isfloat = true;
|
||||
} else if (inp_ == bool_ ) {
|
||||
i = Cast_Helper<bool>()(v);
|
||||
} else if (inp_ == char_) {
|
||||
i = Cast_Helper<char>()(v);
|
||||
} else if (inp_ == int_) {
|
||||
i = Cast_Helper<int>()(v);
|
||||
} else if (inp_ == unsigned_int_) {
|
||||
i = Cast_Helper<unsigned int>()(v);
|
||||
} else if (inp_ == long_) {
|
||||
i = Cast_Helper<long>()(v);
|
||||
} else if (inp_ == unsigned_long_) {
|
||||
i = Cast_Helper<unsigned long>()(v);
|
||||
} else if (inp_ == int8_t_) {
|
||||
i = Cast_Helper<int8_t>()(v);
|
||||
} else if (inp_ == int16_t_) {
|
||||
i = Cast_Helper<int16_t>()(v);
|
||||
} else if (inp_ == int32_t_) {
|
||||
i = Cast_Helper<int32_t>()(v);
|
||||
} else if (inp_ == int64_t_) {
|
||||
i = Cast_Helper<int64_t>()(v);
|
||||
} else if (inp_ == uint8_t_) {
|
||||
i = Cast_Helper<uint8_t>()(v);
|
||||
} else if (inp_ == uint16_t_) {
|
||||
i = Cast_Helper<uint16_t>()(v);
|
||||
} else if (inp_ == uint32_t_) {
|
||||
i = Cast_Helper<uint32_t>()(v);
|
||||
} else {
|
||||
throw boost::bad_any_cast();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) == ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator<(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) < ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator>(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) > ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator>=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) >= ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator<=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) <= ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
bool operator!=(const Boxed_POD_Value &r) const
|
||||
{
|
||||
return ((m_isfloat)?d:i) != ((r.m_isfloat)?r.d:r.i);
|
||||
}
|
||||
|
||||
Boxed_Value operator+(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i + r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) + ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value operator*(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i * r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) * ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value operator/(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i / r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) / ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
Boxed_Value operator-(const Boxed_POD_Value &r) const
|
||||
{
|
||||
if (!m_isfloat && !r.m_isfloat)
|
||||
{
|
||||
return Boxed_Value(i - r.i);
|
||||
}
|
||||
|
||||
return Boxed_Value(((m_isfloat)?d:i) - ((r.m_isfloat)?r.d:r.i));
|
||||
}
|
||||
|
||||
double d;
|
||||
int64_t i;
|
||||
|
||||
bool m_isfloat;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Cast_Helper<Boxed_POD_Value>
|
||||
{
|
||||
Boxed_POD_Value operator()(Boxed_Value ob)
|
||||
{
|
||||
return Boxed_POD_Value(ob);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Boxed_POD_Value operator()(Boxed_Value ob)
|
||||
{
|
||||
return Boxed_POD_Value(ob);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -16,194 +16,196 @@
|
||||
#include "proxy_functions.hpp"
|
||||
#include "proxy_constructors.hpp"
|
||||
|
||||
class Dispatch_Engine
|
||||
namespace dispatchkit
|
||||
{
|
||||
public:
|
||||
typedef std::multimap<std::string, boost::shared_ptr<Proxy_Function> > Function_Map;
|
||||
typedef std::map<std::string, Type_Info> Type_Name_Map;
|
||||
typedef std::map<std::string, Boxed_Value> Scope;
|
||||
typedef std::deque<Scope> Stack;
|
||||
class Dispatch_Engine
|
||||
{
|
||||
public:
|
||||
typedef std::multimap<std::string, boost::shared_ptr<Proxy_Function> > Function_Map;
|
||||
typedef std::map<std::string, Type_Info> Type_Name_Map;
|
||||
typedef std::map<std::string, Boxed_Value> Scope;
|
||||
typedef std::deque<Scope> Stack;
|
||||
|
||||
Dispatch_Engine()
|
||||
{
|
||||
m_scopes.push_back(Scope());
|
||||
}
|
||||
|
||||
void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, f));
|
||||
}
|
||||
|
||||
|
||||
template<typename Function>
|
||||
void register_function(const Function &func, const std::string &name)
|
||||
Dispatch_Engine()
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func))));
|
||||
m_scopes.push_back(Scope());
|
||||
}
|
||||
|
||||
void register_function(const boost::shared_ptr<Proxy_Function> &f, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, f));
|
||||
}
|
||||
|
||||
|
||||
template<typename Class>
|
||||
void set_object(const std::string &name, const Class &obj)
|
||||
template<typename Function>
|
||||
void register_function(const Function &func, const std::string &name)
|
||||
{
|
||||
m_functions.insert(std::make_pair(name, boost::shared_ptr<Proxy_Function>(new Proxy_Function_Impl<Function>(func))));
|
||||
}
|
||||
|
||||
|
||||
template<typename Class>
|
||||
void set_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
for (int i = m_scopes.size()-1; i >= 0; --i)
|
||||
{
|
||||
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
|
||||
if (itr != m_scopes[i].end())
|
||||
{
|
||||
m_scopes[i][name] = Boxed_Value(obj);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
add_object(name, obj);
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
void add_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
m_scopes.back()[name] = Boxed_Value(obj);
|
||||
}
|
||||
|
||||
void new_scope()
|
||||
{
|
||||
m_scopes.push_back(Scope());
|
||||
}
|
||||
|
||||
void pop_scope()
|
||||
{
|
||||
if (m_scopes.size() > 1)
|
||||
{
|
||||
m_scopes.pop_back();
|
||||
} else {
|
||||
throw std::range_error("Unable to pop global stack");
|
||||
}
|
||||
}
|
||||
|
||||
Stack get_stack()
|
||||
{
|
||||
return m_scopes;
|
||||
}
|
||||
|
||||
Stack set_stack(Stack s)
|
||||
{
|
||||
swap(s, m_scopes);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
Boxed_Value get_object(const std::string &name) const
|
||||
{
|
||||
for (int i = m_scopes.size()-1; i >= 0; --i)
|
||||
{
|
||||
std::map<std::string, Boxed_Value>::const_iterator itr = m_scopes[i].find(name);
|
||||
if (itr != m_scopes[i].end())
|
||||
{
|
||||
m_scopes[i][name] = Boxed_Value(obj);
|
||||
return;
|
||||
return itr->second;
|
||||
}
|
||||
}
|
||||
|
||||
add_object(name, obj);
|
||||
throw std::range_error("Object not known: " + name);
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
void add_object(const std::string &name, const Class &obj)
|
||||
{
|
||||
m_scopes.back()[name] = Boxed_Value(obj);
|
||||
}
|
||||
|
||||
void new_scope()
|
||||
{
|
||||
m_scopes.push_back(Scope());
|
||||
}
|
||||
|
||||
void pop_scope()
|
||||
{
|
||||
if (m_scopes.size() > 1)
|
||||
{
|
||||
m_scopes.pop_back();
|
||||
} else {
|
||||
throw std::range_error("Unable to pop global stack");
|
||||
}
|
||||
}
|
||||
|
||||
Stack get_stack()
|
||||
{
|
||||
return m_scopes;
|
||||
}
|
||||
|
||||
Stack set_stack(Stack s)
|
||||
{
|
||||
swap(s, m_scopes);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
Boxed_Value get_object(const std::string &name) const
|
||||
{
|
||||
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())
|
||||
template<typename Type>
|
||||
void register_type(const std::string &name)
|
||||
{
|
||||
return itr->second;
|
||||
m_types.insert(std::make_pair(name, Get_Type_Info<Type>::get()));
|
||||
}
|
||||
}
|
||||
|
||||
throw std::range_error("Object not known: " + name);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void register_type(const std::string &name)
|
||||
std::vector<Type_Name_Map::value_type> get_types() const
|
||||
{
|
||||
m_types.insert(std::make_pair(name, Get_Type_Info<Type>::get()));
|
||||
return std::vector<Type_Name_Map::value_type>(m_types.begin(), m_types.end());
|
||||
}
|
||||
|
||||
std::vector<Type_Name_Map::value_type> get_types() const
|
||||
{
|
||||
return std::vector<Type_Name_Map::value_type>(m_types.begin(), m_types.end());
|
||||
}
|
||||
std::vector<std::pair<std::string, Function_Map::mapped_type> >
|
||||
get_function(const std::string &t_name) const
|
||||
{
|
||||
std::vector<std::pair<std::string, Function_Map::mapped_type> > funcs;
|
||||
|
||||
std::vector<std::pair<std::string, Function_Map::mapped_type> >
|
||||
get_function(const std::string &t_name) const
|
||||
{
|
||||
std::vector<std::pair<std::string, Function_Map::mapped_type> > funcs;
|
||||
try {
|
||||
funcs.insert(funcs.end(),
|
||||
Function_Map::value_type(
|
||||
t_name,
|
||||
Cast_Helper<Function_Map::mapped_type>()(get_object(t_name)))
|
||||
);
|
||||
} catch (const std::bad_cast &) {
|
||||
} catch (const std::range_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
funcs.insert(funcs.end(),
|
||||
Function_Map::value_type(
|
||||
t_name,
|
||||
Cast_Helper<Function_Map::mapped_type>()(get_object(t_name)))
|
||||
);
|
||||
} catch (const std::bad_cast &) {
|
||||
} catch (const std::range_error &) {
|
||||
std::pair<Function_Map::const_iterator, Function_Map::const_iterator> range
|
||||
= m_functions.equal_range(t_name);
|
||||
|
||||
funcs.insert(funcs.end(), range.first, range.second);
|
||||
return funcs;
|
||||
}
|
||||
|
||||
std::pair<Function_Map::const_iterator, Function_Map::const_iterator> range
|
||||
= m_functions.equal_range(t_name);
|
||||
std::vector<Function_Map::value_type> get_functions() const
|
||||
{
|
||||
return std::vector<Function_Map::value_type>(m_functions.begin(), m_functions.end());
|
||||
}
|
||||
|
||||
funcs.insert(funcs.end(), range.first, range.second);
|
||||
return funcs;
|
||||
}
|
||||
private:
|
||||
std::deque<Scope> m_scopes;
|
||||
|
||||
std::vector<Function_Map::value_type> get_functions() const
|
||||
{
|
||||
return std::vector<Function_Map::value_type>(m_functions.begin(), m_functions.end());
|
||||
}
|
||||
Function_Map m_functions;
|
||||
Type_Name_Map m_types;
|
||||
};
|
||||
|
||||
private:
|
||||
std::deque<Scope> m_scopes;
|
||||
|
||||
Function_Map m_functions;
|
||||
Type_Name_Map m_types;
|
||||
};
|
||||
|
||||
void dump_object(Boxed_Value o)
|
||||
{
|
||||
std::cout << o.get_type_info().m_type_info->name() << std::endl;
|
||||
}
|
||||
|
||||
void dump_type(const Type_Info &type)
|
||||
{
|
||||
std::cout << type.m_bare_type_info->name();
|
||||
}
|
||||
|
||||
void dump_function(const Dispatch_Engine::Function_Map::value_type &f)
|
||||
{
|
||||
std::vector<Type_Info> params = f.second->get_param_types();
|
||||
|
||||
dump_type(params.front());
|
||||
std::cout << " " << f.first << "(";
|
||||
|
||||
for (std::vector<Type_Info>::const_iterator itr = params.begin() + 1;
|
||||
itr != params.end();
|
||||
++itr)
|
||||
void dump_object(Boxed_Value o)
|
||||
{
|
||||
dump_type(*itr);
|
||||
std::cout << ", ";
|
||||
std::cout << o.get_type_info().m_type_info->name() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << ")" << std::endl;
|
||||
}
|
||||
|
||||
void dump_system(const Dispatch_Engine &s)
|
||||
{
|
||||
std::cout << "Registered Types: " << std::endl;
|
||||
std::vector<Dispatch_Engine::Type_Name_Map::value_type> types = s.get_types();
|
||||
for (std::vector<Dispatch_Engine::Type_Name_Map::value_type>::const_iterator itr = types.begin();
|
||||
itr != types.end();
|
||||
++itr)
|
||||
void dump_type(const Type_Info &type)
|
||||
{
|
||||
std::cout << itr->first << ": ";
|
||||
dump_type(itr->second);
|
||||
std::cout << type.m_bare_type_info->name();
|
||||
}
|
||||
|
||||
void dump_function(const Dispatch_Engine::Function_Map::value_type &f)
|
||||
{
|
||||
std::vector<Type_Info> params = f.second->get_param_types();
|
||||
|
||||
dump_type(params.front());
|
||||
std::cout << " " << f.first << "(";
|
||||
|
||||
for (std::vector<Type_Info>::const_iterator itr = params.begin() + 1;
|
||||
itr != params.end();
|
||||
++itr)
|
||||
{
|
||||
dump_type(*itr);
|
||||
std::cout << ", ";
|
||||
}
|
||||
|
||||
std::cout << ")" << std::endl;
|
||||
}
|
||||
|
||||
void dump_system(const Dispatch_Engine &s)
|
||||
{
|
||||
std::cout << "Registered Types: " << std::endl;
|
||||
std::vector<Dispatch_Engine::Type_Name_Map::value_type> types = s.get_types();
|
||||
for (std::vector<Dispatch_Engine::Type_Name_Map::value_type>::const_iterator itr = types.begin();
|
||||
itr != types.end();
|
||||
++itr)
|
||||
{
|
||||
std::cout << itr->first << ": ";
|
||||
dump_type(itr->second);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::cout << std::endl; std::vector<Dispatch_Engine::Function_Map::value_type> funcs = s.get_functions();
|
||||
|
||||
std::cout << "Functions: " << std::endl;
|
||||
for (std::vector<Dispatch_Engine::Function_Map::value_type>::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
dump_function(*itr);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
std::cout << std::endl; std::vector<Dispatch_Engine::Function_Map::value_type> funcs = s.get_functions();
|
||||
|
||||
std::cout << "Functions: " << std::endl;
|
||||
for (std::vector<Dispatch_Engine::Function_Map::value_type>::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
dump_function(*itr);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -8,17 +8,20 @@
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/function.hpp>
|
||||
|
||||
template<typename Class>
|
||||
boost::shared_ptr<Class> constructor()
|
||||
namespace dispatchkit
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class());
|
||||
}
|
||||
template<typename Class>
|
||||
boost::shared_ptr<Class> constructor()
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class());
|
||||
}
|
||||
|
||||
template<typename Class>
|
||||
boost::function<boost::shared_ptr<Class> ()> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)();
|
||||
return boost::function<boost::shared_ptr<Class> ()>(func(&(constructor<Class>)));
|
||||
template<typename Class>
|
||||
boost::function<boost::shared_ptr<Class> ()> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)();
|
||||
return boost::function<boost::shared_ptr<Class> ()>(func(&(constructor<Class>)));
|
||||
}
|
||||
}
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 1, 10 )
|
||||
@ -28,18 +31,20 @@ boost::function<boost::shared_ptr<Class> ()> build_constructor()
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
|
||||
template<typename Class, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::shared_ptr<Class> constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
||||
namespace dispatchkit
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
|
||||
}
|
||||
template<typename Class, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::shared_ptr<Class> constructor( BOOST_PP_ENUM_BINARY_PARAMS(n, Param, p) )
|
||||
{
|
||||
return boost::shared_ptr<Class>(new Class( BOOST_PP_ENUM_PARAMS(n, p) ));
|
||||
}
|
||||
|
||||
template<typename Class, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)(BOOST_PP_ENUM_PARAMS(n, Param));
|
||||
return boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor<Class, BOOST_PP_ENUM_PARAMS(n, Param)>)));
|
||||
template<typename Class, BOOST_PP_ENUM_PARAMS(n, typename Param) >
|
||||
boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))> build_constructor()
|
||||
{
|
||||
typedef boost::shared_ptr<Class> (*func)(BOOST_PP_ENUM_PARAMS(n, Param));
|
||||
return boost::function<boost::shared_ptr<Class> (BOOST_PP_ENUM_PARAMS(n, Param))>(func(&(constructor<Class, BOOST_PP_ENUM_PARAMS(n, Param)>)));
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -16,212 +16,220 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
// handle_return implementations
|
||||
template<typename Ret>
|
||||
struct Handle_Return
|
||||
namespace dispatchkit
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret ()> &f)
|
||||
{
|
||||
return Boxed_Value(f());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret &()> &f)
|
||||
{
|
||||
return Boxed_Value(boost::ref(f()));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<Boxed_Value>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Boxed_Value ()> &f)
|
||||
{
|
||||
return f();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<Boxed_Value &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Boxed_Value &()> &f)
|
||||
{
|
||||
return f();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<void>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<void ()> &f)
|
||||
{
|
||||
f();
|
||||
return Boxed_Value(Boxed_Value::Void_Type());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Build param type list (variadic)
|
||||
template<typename Ret>
|
||||
std::vector<Type_Info> build_param_type_list(const boost::function<Ret ()> &f)
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>::get());
|
||||
return ti;
|
||||
}
|
||||
|
||||
// call_func implementations (variadic)
|
||||
template<typename Ret>
|
||||
Boxed_Value call_func(const boost::function<Ret ()> &f, const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != 0)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(f);
|
||||
}
|
||||
}
|
||||
|
||||
struct Param_List_Builder
|
||||
{
|
||||
Param_List_Builder &operator<<(const Boxed_Value &so)
|
||||
{
|
||||
objects.push_back(so);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Param_List_Builder &operator<<(T t)
|
||||
// handle_return implementations
|
||||
template<typename Ret>
|
||||
struct Handle_Return
|
||||
{
|
||||
objects.push_back(Boxed_Value(t));
|
||||
Boxed_Value operator()(const boost::function<Ret ()> &f)
|
||||
{
|
||||
return Boxed_Value(f());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Ret>
|
||||
struct Handle_Return<Ret &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Ret &()> &f)
|
||||
{
|
||||
return Boxed_Value(boost::ref(f()));
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<Boxed_Value>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Boxed_Value ()> &f)
|
||||
{
|
||||
return f();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<Boxed_Value &>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<Boxed_Value &()> &f)
|
||||
{
|
||||
return f();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Handle_Return<void>
|
||||
{
|
||||
Boxed_Value operator()(const boost::function<void ()> &f)
|
||||
{
|
||||
f();
|
||||
return Boxed_Value(Boxed_Value::Void_Type());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Build param type list (variadic)
|
||||
template<typename Ret>
|
||||
std::vector<Type_Info> build_param_type_list(const boost::function<Ret ()> &f)
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>::get());
|
||||
return ti;
|
||||
}
|
||||
|
||||
// call_func implementations (variadic)
|
||||
template<typename Ret>
|
||||
Boxed_Value call_func(const boost::function<Ret ()> &f, const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != 0)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(f);
|
||||
}
|
||||
}
|
||||
|
||||
struct Param_List_Builder
|
||||
{
|
||||
Param_List_Builder &operator<<(const Boxed_Value &so)
|
||||
{
|
||||
objects.push_back(so);
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator const std::vector<Boxed_Value> &() const
|
||||
{
|
||||
return objects;
|
||||
}
|
||||
template<typename T>
|
||||
Param_List_Builder &operator<<(T t)
|
||||
{
|
||||
objects.push_back(Boxed_Value(t));
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::vector<Boxed_Value> objects;
|
||||
};
|
||||
operator const std::vector<Boxed_Value> &() const
|
||||
{
|
||||
return objects;
|
||||
}
|
||||
|
||||
std::vector<Boxed_Value> objects;
|
||||
};
|
||||
}
|
||||
|
||||
#define BOOST_PP_ITERATION_LIMITS ( 1, 10 )
|
||||
#define BOOST_PP_FILENAME_1 "proxy_functions.hpp"
|
||||
#include BOOST_PP_ITERATE()
|
||||
|
||||
class Proxy_Function
|
||||
namespace dispatchkit
|
||||
{
|
||||
public:
|
||||
virtual ~Proxy_Function() {}
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms) = 0;
|
||||
virtual std::vector<Type_Info> get_param_types() = 0;
|
||||
|
||||
};
|
||||
|
||||
class Dynamic_Proxy_Function : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Dynamic_Proxy_Function(const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f, int arity=-1)
|
||||
: m_f(t_f), m_arity(arity)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Proxy_Function() {}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (m_arity < 0 || params.size() == size_t(m_arity))
|
||||
{
|
||||
return m_f(params);
|
||||
} else {
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
}
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types()
|
||||
{
|
||||
return build_param_type_list(m_f);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
||||
int m_arity;
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
class Proxy_Function_Impl : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Proxy_Function_Impl(const Func &f)
|
||||
: m_f(f)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Proxy_Function_Impl() {}
|
||||
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return call_func(m_f, params);
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types()
|
||||
{
|
||||
return build_param_type_list(m_f);
|
||||
}
|
||||
|
||||
private:
|
||||
Func m_f;
|
||||
};
|
||||
|
||||
Boxed_Value dispatch(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs,
|
||||
const std::vector<Boxed_Value> &plist)
|
||||
{
|
||||
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
class Proxy_Function
|
||||
{
|
||||
try {
|
||||
return (*itr->second)(plist);
|
||||
} catch (const std::bad_cast &) {
|
||||
//try again
|
||||
} catch (const std::range_error &) {
|
||||
//invalid num params, try again
|
||||
}
|
||||
}
|
||||
public:
|
||||
virtual ~Proxy_Function() {}
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms) = 0;
|
||||
virtual std::vector<Type_Info> get_param_types() = 0;
|
||||
|
||||
throw std::runtime_error("No matching function to dispatch to");
|
||||
};
|
||||
|
||||
class Dynamic_Proxy_Function : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Dynamic_Proxy_Function(const boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> &t_f, int arity=-1)
|
||||
: m_f(t_f), m_arity(arity)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Dynamic_Proxy_Function() {}
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (m_arity < 0 || params.size() == size_t(m_arity))
|
||||
{
|
||||
return m_f(params);
|
||||
} else {
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
}
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types()
|
||||
{
|
||||
return build_param_type_list(m_f);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::function<Boxed_Value (const std::vector<Boxed_Value> &)> m_f;
|
||||
int m_arity;
|
||||
};
|
||||
|
||||
template<typename Func>
|
||||
class Proxy_Function_Impl : public Proxy_Function
|
||||
{
|
||||
public:
|
||||
Proxy_Function_Impl(const Func &f)
|
||||
: m_f(f)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Proxy_Function_Impl() {}
|
||||
|
||||
|
||||
virtual Boxed_Value operator()(const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
return call_func(m_f, params);
|
||||
}
|
||||
|
||||
virtual std::vector<Type_Info> get_param_types()
|
||||
{
|
||||
return build_param_type_list(m_f);
|
||||
}
|
||||
|
||||
private:
|
||||
Func m_f;
|
||||
};
|
||||
|
||||
Boxed_Value dispatch(const std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > > &funcs,
|
||||
const std::vector<Boxed_Value> &plist)
|
||||
{
|
||||
for (std::vector<std::pair<std::string, boost::shared_ptr<Proxy_Function> > >::const_iterator itr = funcs.begin();
|
||||
itr != funcs.end();
|
||||
++itr)
|
||||
{
|
||||
try {
|
||||
return (*itr->second)(plist);
|
||||
} catch (const std::bad_cast &) {
|
||||
//try again
|
||||
} catch (const std::range_error &) {
|
||||
//invalid num params, try again
|
||||
}
|
||||
}
|
||||
|
||||
throw std::runtime_error("No matching function to dispatch to");
|
||||
}
|
||||
}
|
||||
|
||||
# endif
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
template<typename Ret, 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))> &f)
|
||||
namespace dispatchkit
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>::get());
|
||||
template<typename Ret, 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))> &f)
|
||||
{
|
||||
std::vector<Type_Info> ti;
|
||||
ti.push_back(Get_Type_Info<Ret>::get());
|
||||
|
||||
BOOST_PP_REPEAT(n, gettypeinfo, ~)
|
||||
BOOST_PP_REPEAT(n, gettypeinfo, ~)
|
||||
|
||||
return ti;
|
||||
}
|
||||
return ti;
|
||||
}
|
||||
|
||||
template<typename Ret, BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
Boxed_Value call_func(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != n)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~)));
|
||||
}
|
||||
template<typename Ret, BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
Boxed_Value call_func(const boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))> &f,
|
||||
const std::vector<Boxed_Value> ¶ms)
|
||||
{
|
||||
if (params.size() != n)
|
||||
{
|
||||
throw std::range_error("Incorrect number of parameters");
|
||||
} else {
|
||||
return Handle_Return<Ret>()(boost::bind(f BOOST_PP_REPEAT(n, casthelper, ~)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -18,18 +18,20 @@
|
||||
#else
|
||||
# define n BOOST_PP_ITERATION()
|
||||
|
||||
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)
|
||||
namespace dispatchkit
|
||||
{
|
||||
s.register_function(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f), 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)
|
||||
{
|
||||
s.register_function(boost::function<Ret (BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
|
||||
}
|
||||
|
||||
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
|
||||
{
|
||||
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
|
||||
template<typename Ret, typename Class BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, typename Param)>
|
||||
void register_function(Dispatch_Engine &s, Ret (Class::*f)(BOOST_PP_ENUM_PARAMS(n, Param)), const std::string &name)
|
||||
{
|
||||
s.register_function(boost::function<Ret (Class* BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM_PARAMS(n, Param))>(f), name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -8,6 +8,8 @@
|
||||
#include "bootstrap.hpp"
|
||||
#include "bootstrap_stl.hpp"
|
||||
|
||||
using namespace dispatchkit;
|
||||
|
||||
struct Test
|
||||
{
|
||||
Test(const std::string &s)
|
||||
|
@ -3,78 +3,83 @@
|
||||
|
||||
#include <boost/type_traits.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
struct Type_Info
|
||||
|
||||
namespace dispatchkit
|
||||
{
|
||||
Type_Info(bool t_is_const, bool t_is_reference, bool t_is_pointer, bool t_is_void,
|
||||
const std::type_info *t_ti, const std::type_info *t_bareti)
|
||||
: m_is_const(t_is_const), m_is_reference(t_is_reference), m_is_pointer(t_is_pointer),
|
||||
struct Type_Info
|
||||
{
|
||||
Type_Info(bool t_is_const, bool t_is_reference, bool t_is_pointer, bool t_is_void,
|
||||
const std::type_info *t_ti, const std::type_info *t_bareti)
|
||||
: m_is_const(t_is_const), m_is_reference(t_is_reference), m_is_pointer(t_is_pointer),
|
||||
m_type_info(t_ti), m_bare_type_info(t_bareti),
|
||||
m_is_unknown(false)
|
||||
{
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
Type_Info()
|
||||
: m_is_const(false), m_is_reference(false), m_is_pointer(false),
|
||||
Type_Info()
|
||||
: m_is_const(false), m_is_reference(false), m_is_pointer(false),
|
||||
m_is_void(false), m_type_info(0), m_bare_type_info(0),
|
||||
m_is_unknown(true)
|
||||
{
|
||||
}
|
||||
{
|
||||
}
|
||||
|
||||
Type_Info &operator=(const Type_Info &ti)
|
||||
{
|
||||
m_is_const = ti.m_is_const;
|
||||
m_is_reference = ti.m_is_reference;
|
||||
m_is_pointer = ti.m_is_pointer;
|
||||
m_is_void = ti.m_is_void;
|
||||
m_type_info = ti.m_type_info;
|
||||
m_bare_type_info = ti.m_bare_type_info;
|
||||
m_is_unknown = ti.m_is_unknown;
|
||||
return *this;
|
||||
}
|
||||
Type_Info &operator=(const Type_Info &ti)
|
||||
{
|
||||
m_is_const = ti.m_is_const;
|
||||
m_is_reference = ti.m_is_reference;
|
||||
m_is_pointer = ti.m_is_pointer;
|
||||
m_is_void = ti.m_is_void;
|
||||
m_type_info = ti.m_type_info;
|
||||
m_bare_type_info = ti.m_bare_type_info;
|
||||
m_is_unknown = ti.m_is_unknown;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool m_is_const;
|
||||
bool m_is_reference;
|
||||
bool m_is_pointer;
|
||||
bool m_is_void;
|
||||
const std::type_info *m_type_info;
|
||||
const std::type_info *m_bare_type_info;
|
||||
bool m_is_unknown;
|
||||
};
|
||||
bool m_is_const;
|
||||
bool m_is_reference;
|
||||
bool m_is_pointer;
|
||||
bool m_is_void;
|
||||
const std::type_info *m_type_info;
|
||||
const std::type_info *m_bare_type_info;
|
||||
bool m_is_unknown;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(T),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct Get_Type_Info
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(T),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::shared_ptr<T> >
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::shared_ptr<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::shared_ptr<T> >
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::shared_ptr<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::reference_wrapper<T> >
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::reference_wrapper<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct Get_Type_Info<boost::reference_wrapper<T> >
|
||||
{
|
||||
static Type_Info get()
|
||||
{
|
||||
return Type_Info(boost::is_const<T>::value, boost::is_reference<T>::value, boost::is_pointer<T>::value,
|
||||
boost::is_void<T>::value,
|
||||
&typeid(boost::reference_wrapper<T> ),
|
||||
&typeid(typename boost::remove_const<typename boost::remove_pointer<typename boost::remove_reference<T>::type>::type>::type));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -7,6 +7,8 @@
|
||||
|
||||
BOOST_AUTO_TEST_CASE( add_operators )
|
||||
{
|
||||
using namespace dispatchkit;
|
||||
|
||||
Dispatch_Engine ss;
|
||||
Bootstrap::bootstrap(ss);
|
||||
dump_system(ss);
|
||||
|
Loading…
x
Reference in New Issue
Block a user