Discussion:
[asio-users] Decorator code for you all
Amir Taaki
2012-02-25 14:34:44 UTC
Permalink
Hi,

I created this useful decorator code that you might all find useful to use in boost::asio,

 * Defines a function decorator ala Python
 *
 *   void foo(int x, int y);
 *   function<void ()> wrapper(function<void (int)> f);
 *
 *   auto f = decorator(wrapper, bind(foo, 110, _1));
 *   f();

So the wrapper is a user defined class that takes the function f in and transforms it (using bind) into a function signature of the user's choosing (in this example function<void ()>). The wrapper is invocated when the decorated functor is called.

Hope you find this useful. Before I was misusing this to call strand_->post when you pass callbacks to other threads, before realising that strand.wrap(...) which calls dispatch will do the same thing - oops. But I'm sure there's other uses for it anyway. Dynamic polymorphic functions (or something).

Source:

http://gitorious.org/libbitcoin/libbitcoin/blobs/master/include/bitcoin/utility/decorator.hpp
Igor R
2012-02-25 16:53:46 UTC
Permalink
Post by Amir Taaki
I created this useful decorator code that you might all find useful to use in boost::asio,
all those, who don't use MSVC, you mean ;-)
Post by Amir Taaki
 * Defines a function decorator ala Python
 *
 *   void foo(int x, int y);
 *   function<void ()> wrapper(function<void (int)> f);
 *
 *   auto f = decorator(wrapper, bind(foo, 110, _1));
 *   f();
So the wrapper is a user defined class that takes the function f in and transforms it (using bind) into a function signature of the user's choosing (in this example function<void ()>).
You mean function<void(int)>?
Anyway, excuse my ignorance, but why is it better than just auto f =
bind(foo, 110, _1); ?

Loading...