Boost logo

Boost :

From: Doug Gregor (gregod_at_[hidden])
Date: 2001-05-03 09:35:15


On Wednesday 02 May 2001 09:15, you wrote:
> [Hit the wrong key while I was composing this so a half written draft
> slipped out. Sorry about that.]
>
> I haven't looked at the new stuff closely but I do have a couple of
> comments:
>
> 1) I really want to be able to use operator== to test if two
> callbacks have the same value. I have a lot of code that does stuff
> like this:
>
> class ITimer {
>
> public:
> virtual void AddTimer(function<void> callback, MilliSecond interval) =
> 0;
>
> virtual void RemoveTimer(function<void> callback) = 0;
> };
>
> This is nice and simple, but requires support for equality tests...

Requiring operator== of all function objects used with boost::function would
be way too strict, I think. We have to be very careful when adding
requirements for functors in boost::function because they are requirements
even if not used.

I'd argue that you should have one Timer object per callback, and then use a
static class member to keep track of all of the existing Timer objects to
call them; no equality test required.

> 2) The API is a bit more awkward than I would like. For example, I
> have lots of code that looks sort of like this:
>
> // About
> function<void> action(this, &CApp::DoAbout);
> handler->RegisterCommand(kAboutCmd, action, kDisabledIfDialog);
>
> // Delete Formula
> action = function<void> (this, &CApp::DoDeleteFormula);
> function<SCommandStatus> enabler(this,
> &CAppMenuHandler::DoEnableDeleteFormula);
> handler->RegisterCommand(kDeleteFormulaCmd, action, enabler);
>
> // Delete Palette
> action = function<void> this, &CApp::DoDeletePalette);
> enabler = function<SCommandStatus> (this,
> &CApp::DoEnableDeletePalette); handler->RegisterCommand(kDeletePaletteCmd,
> action, enabler);
>
> All of this boiler-plate is pretty annoying (and the real version
> would be worse since I'd have to create some temporary functors to
> bind the member function pointers). One thing that would help is to
> get rid of all those temporary function objects. If the API directly
> supported member functions you could do this with a set() method:
>
> // About
> function<void> action(this, &CApp::DoAbout);
> handler->RegisterCommand(kAboutCmd, action, kDisabledIfDialog);
>
> // Delete Formula
> action.set(this, &CApp::DoDeleteFormula);
> function<SCommandStatus> enabler(this,
> &CAppMenuHandler::DoEnableDeleteFormula);
> handler->RegisterCommand(kDeleteFormulaCmd, action, enabler);
>
> // Delete Palette
> action.set(this, &CApp::DoDeletePalette);
> enabler.set(this, &CApp::DoEnableDeletePalette);
> handler->RegisterCommand(kDeletePaletteCmd, action, enabler);
>
> -- Jesse

Why not just:
-------------------
// About
handle->RegisterCommand(kAboutCmd, bind(this, &CApp::DoAbout),
kDisabledIfDialog);

// Delete Formula
handler->RegisterCommand(kDeleteFormulaCmd,
        bind(this, &CApp::DoDeleteFormula),
        bind(this, &CAppMenuHandler::DoEnableDeleteFormula));

// Delete palette
handler->RegisterCommand(kDeletePaletteCmd,
        bind(this, &CApp::DoDeletePalette),
        bind(this, &CApp::DoEnableDeletePalette));
-------------------

Implicit conversion from any function object/function pointer to a
boost::function is supported to make all of this easier.

        Doug


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