Boost logo

Boost :

From: Rene Jager (renej_at_[hidden])
Date: 2000-10-16 17:32:59


Gary Powell wrote:

> > >I've used the ones at
http://www.primenet.com/~jukubik/callback.html
> > paper
> > >and source at this site. (Nice discussion on the patterns used to
create
> > the
> > >code.)
> >
> > Do you have another URL? That one seems to be broken.
> Sorry, I should test these each and every time I type it in.
> http://www.primenet.com/~jakubik/callback.html
>
> Anyway I found the paper he wrote very interesting in that like Meyers
he
> started with what he wanted and worked through each step to finally
get it.
> (That's not to say its what you want, but the process was cool.)
>
> -gary-
>
> gary.powell_at_[hidden]

in the apps i work on we make extensive use of callbacks and after
using/implementing
it in the way(s) as show in previous posts, we came down to something
like the
following:

class SomeObjType
{
public:
    void its_method (int i, double d) { ...}
};

class CallbackCaller
{
 // ...
    template <class CB>
    void attach_callback (const CB & cb)
    {
        _callbacks.push_back(shared_ptr<abstract_unary_function<void,
double> >(
            new concrete_unary_function<void, double, CB>(cb));
    }
    void call_callbacks (double d)
    {
        // call all callbacks with 'd' as arg
    }
 private:
    std::vector<shared_ptr<abstract_unary_function<void, double> > >
_callbacks;
};

int
main ()
{
    CallbackCaller cc;
    SomeObjType obj;
    cc.attach_callback(bl::bind(&SomeObjType::its_method, &obj,
bl::free1));
    cc.call_callbacks;
}

where:

bl::bind, bl::free1, etc are from the binder library (now lambda library
and
already
part of boost?),
and the abstract- and concrete- function structs as follows (also for
nullary,
binary, ..., etc):

template <class Arg, class Result>
struct abstract_unary_function : public unary_function<Arg, Result>
{
    virtual Result operator () (boost::call_traits<Arg>::param_type x)
const =
0;
}

template <class Arg, class Result, class F>
struct concrete_unary_function : public abstract_unary_function<Arg,
Result>
{
    virtual Result operator () (boost::call_traits<Arg>::param_type x)
const
    {
        return f(x);
    }
private:
    F f;
}

template <class Arg, class F>
struct concrete_unary_function<Arg, void, F> : public
abstract_unary_function<Arg, void>
{
    virtual void operator () (boost::call_traits<Arg>::param_type x)
const
    {
        f(x);
    }
private:
    F f;
}

the advantage is that the application programmers have nothing to do
whatsoever with
special functions, classes, etc; just hand over something that can be
called using '(...)';
unneeded result types are ignored (or converted)
with the function adapters, binder library and some extra relaxN(...)
template functions
(allowing to except more arguments than actually used) almost everything
can be turned
into a callback...

renej


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk