|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2001-08-15 20:32:05
On Wednesday 15 August 2001 09:17, you wrote:
> > Well, the preferred method shortens the pattern a little:
> >
> > {
> > mutex::scoped_lock lock(m);
> > cv.wait(lock, pred);
> > action();
> > }
> >
> > It's not really possibly to shorten things beyond this.
>
> Well, you can write:
>
> template<class Action, class Pred> struct caction
> {
> void run() {
> mutax.scope_lock lock(m);
> cv.wait(lock, pred);
> action();
> }
> caction(mutex &m_a, Pred const &pred_a, Action &action_a)
>
> : m(m_a), pred(pred_a), action(action_a) { run(); }
>
> mutex &m;
> Pred const &pred;
> Action &action;
> };
>
> mutex m; ..
> Pred pred(..);
> Action action(..);
> cation<Action, Pred>(m, pred, action);
>
> Of course, the templatisation is basically useless due to the lack
> of nested function closures, which force you to declare the types
> Pred and Action out of line (instead of defining inline anonymous
> functions like you would in a functional programming language).
> But then, by this argument, STL is basically rendered useless
> by lack of core language support, so YMMV.
Boost.Function can eliminate the need for the template parameters:
typedef boost::function<void> Action;
typedef boost::function<bool> Pred;
A library like Lambda, Boost.Bind, or FACT would let you define the predicate
and action inline.
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk