Amir Taaki
2012-02-25 14:34:44 UTC
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
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