From 714e1c0cecfed8e8aadfc223785b01cb7355f933 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Mon, 29 Jun 2009 23:00:29 +0000 Subject: [PATCH] Support cleaner usage of bind, by adding a built in _ variable --- dispatchkit/bootstrap.hpp | 2 ++ dispatchkit/proxy_functions.hpp | 10 ++++++++-- samples/bind.chai | 13 ++----------- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/dispatchkit/bootstrap.hpp b/dispatchkit/bootstrap.hpp index 2ab934c..da32fa1 100644 --- a/dispatchkit/bootstrap.hpp +++ b/dispatchkit/bootstrap.hpp @@ -497,6 +497,8 @@ namespace dispatchkit s.register_function(boost::function(boost::bind(&dump_system, boost::ref(s))), "dump_system"); s.register_function(boost::function(boost::bind(&dump_object, _1)), "dump_object"); + s.add_object("_", Placeholder_Object()); + s.register_function(boost::shared_ptr(new Dynamic_Proxy_Function(boost::bind(&bind_function, _1))), "bind"); diff --git a/dispatchkit/proxy_functions.hpp b/dispatchkit/proxy_functions.hpp index 83f809f..06b1303 100644 --- a/dispatchkit/proxy_functions.hpp +++ b/dispatchkit/proxy_functions.hpp @@ -206,6 +206,10 @@ namespace dispatchkit boost::shared_ptr m_guard; }; + struct Placeholder_Object + { + }; + class Bound_Function : public Proxy_Function { public: @@ -244,7 +248,8 @@ namespace dispatchkit while (true) { - while (barg != m_args.end() && !barg->is_unknown()) + while (barg != m_args.end() + && !(barg->get_type_info() == Get_Type_Info::get())) { args.push_back(*barg); ++barg; @@ -256,7 +261,8 @@ namespace dispatchkit ++parg; } - if (barg != m_args.end() && barg->is_unknown()) + if (barg != m_args.end() + && barg->get_type_info() == Get_Type_Info::get()) { ++barg; } diff --git a/samples/bind.chai b/samples/bind.chai index 4054357..e7aea7e 100644 --- a/samples/bind.chai +++ b/samples/bind.chai @@ -1,19 +1,10 @@ -//This is a cheater function. The entire point of it is to return a variable -// with an unspecified type. -// The bind function will substitute any unknown object with those passed in -def _() -{ - var i; - return i; -} - def add(x, y) { return x + y; } -var b = bind(add, 2, _()); +var b = bind(add, 2, _); var c = bind(b, 3); print(b(4)); @@ -25,7 +16,7 @@ def concat(a,b,c,d) return to_string(a) + to_string(b) + to_string(c) + to_string(d); } -var d = bind(concat, _(), " Hello ", _(), " World "); +var d = bind(concat, _, " Hello ", _, " World "); print(d(1, 3)); //This checks to make sure arity is handled correctly: //print(d(1, 3, 4));