Boost logo

Boost :

From: Rainer Deyke (root_at_[hidden])
Date: 2001-05-08 23:29:54


----- Original Message -----
From: "Jesse Jones" <jesjones_at_[hidden]>
To: "Rainer Deyke" <root_at_[hidden]>; <boost_at_[hidden]>
Sent: Tuesday, May 08, 2001 8:02 PM
Subject: Re: [boost] Re: about function.hpp

> >In the specific case of timers, you probably want a class to manage the
> >lifetime of a timer:
> >
> >class Timer {
> > Timer(const function<void>& g, int ms) : f(g) { whatever.add_timer(f,
> >ms); }
> > ~Timer() { whatever.remove_timer(f); }
> >private:
> > boost::function<void> f;
> >};
> >
> >Without such a class, your code is either not exception safe, or
exceedingly
> >difficult to read/maintain, or both. Given the necessity of such a
class,
> >it doesn't really matter if the underlying mechanism uses cookies or not.
>
> You seem to be talking about the RAII idiom which doesn't seem to
> really make sense for timers. Neither the Mac (with Carbon Event
> timers) nor Windows (with WM_TIMER) are going to support creating a
> timer on the stack inside a function which is where RAII shines. Not
> that trying to do that makes any sense to me...

I'll have to disagree on two grounds.

1. RAII is *not* just for stack variables, but for all objects which have a
clearly defined lifetime. If you want to register a (non-static) member
function of object A as a timer callback, you need to make sure that this
timer does not outlive object A. You can do this by unregistering the timer
callback in A's destructor, but this is error-prone. (Consider that A's
destructor never gets called if A's constructor throws an exception, which
might happen after the callback is already registered). RAII really is the
superior idiom here.

2. Even with an event-based timer architecture, creating the timer on the
stack inside a function makes sense so long as the function contains an
event loop. There is really no reason to limit yourself to a single event
loop in 'main'/'WinMain'.

--
Rainer Deyke (root_at_[hidden])
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor

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