Update all instances of a class with a single funtion?
First of all, let me say that while I do know about classes and such, stuff like inheritance, templates, and other complex topics is pretty much beyond my level.
My Timer class structure looks like so:
Code:
class Timer
{
public:
double time;
void Update(void);
};
Update() increases time by however much time has passed since the last time Update() was called, normally every frame. time is public, so you can easilly set it to 0 to reset it, or grab it's current value directly, or even set it to a specific value if you need.
What I want is to call Update() only once, instead of once for every single instance of the class. This not only would clean up my code significantly, but it would speed it up too (less calls to QueryPerformanceCounter(), etc.)
Any advice?
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by candlemaster
Update() increases time by however much time has passed since the last time Update() was called, normally every frame. time is public, so you can easilly set it to 0 to reset it, or grab it's current value directly, or even set it to a specific value if you need.
What I want is to call Update() only once, instead of once for every single instance of the class. This not only would clean up my code significantly, but it would speed it up too (less calls to QueryPerformanceCounter(), etc.)
Is that all that Update does? It might help if you gave us more information on what this Timer class is for, how it will be used, etc.
At the moment, it sounds like you are asking for the impossible, at least if you can only process sequentially: all the Timer objects are separate, but somehow they must be updated simultaneously, in the same instant of time.
If you do want them to be updated like that, it seems to me they must all share a singleton time member. Perhaps they can keep a time member that is added to this singleton time member to produce the resulting time, but whether this is feasible depends on what you are trying to do.
On the other hand, if you will be satisfied with just calling a function once and having all the Timer objects updated (in sequence), then a possible solution is to have a registry of Timer objects. Upon construction, each Timer object will register itself with this registry. You can then just call a function to update each Timer object.
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by
laserlight
On the other hand, if you will be satisfied with just calling a function once and having all the Timer objects updated (in sequence), then a possible solution is to have a registry of Timer objects. Upon construction, each Timer object will register itself with this registry. You can then just call a function to update each Timer object.
That pretty much describes what I want to do, yeah. How would I go about making a registry?
Re: Update all instances of a class with a single funtion?
Code:
#include <vector>
class TheRegister
{
public:
void register(Timer* timer)
{
m_timers.push_back(timer);
}
void update()
{
//loop for all Timer* in m_timers and call update
}
private:
std::vector<Timer*> m_timers;
};
keep the register somewhere 'central' or make it into a singleton.
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by
Amleto
keep the register somewhere 'central' or make it into a singleton.
I think I'd probably make the vector of timer pointers a static member of the timer class itself, most likely a private one. The "update all timers" function would then be a (public) static member of that class as well. And timer objects would register themselves in their constructor. That way the whole thing would be pretty self-contained...
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by
Eri523
I think I'd probably make the vector of timer pointers a static member of the timer class itself, most likely a private one. The "update all timers" function would then be a (public) static member of that class as well. And timer objects would register themselves in their constructor. That way the whole thing would be pretty self-contained...
in effect, that's a singleton then ...
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by
Amleto
in effect, that's a singleton then ...
Not quite... A singleton would have a single instance pointer instead of a vector of instance pointers and the instance it points to is reused over and over. Also, the timer objects here would probably get constructed using a public constructor (and register themselves in that process) instead of the instance accessor function that's common to singleton implementations. And from the client code perspective the timer objects would look much more like usual instances rather than a singleton.
Actually, the design I proposed for the timer class happens to have some similarity with what I like to call a "multi-singleton" which probably is better described as a hybrid of the Singleton pattern and the Named Constructor idiom. It's one of my favorite toys and I'm currently using it in one of my real-life apps and something quite similar in a library module used by many of my apps. In a construct like that, there's a registry of existing objects that get reused when the matching parameters are passed to the named constructor.
Re: Update all instances of a class with a single funtion?
actually, ( unless I'm missing something :) ) there's no need of linearly updating each timer from a centralized timer; you can write something like
Code:
class Timer
{
public:
Timer(): time_() {}
void Update() { time_ += 0.1; }
double GetTime() const { return time_; }
private:
double time_;
};
class FreeTimer
{
public:
FreeTimer( Timer& timer ): timer_(&timer), last_abs_time_( timer.GetTime() ), local_time_( timer.GetTime() ) {}
double PopTime()
{
local_time_ += timer_->GetTime() - last_abs_time_;
last_abs_time_ = timer_->GetTime();
return local_time_;
}
void PushTime( double time )
{
local_time_ = 0.;
last_abs_time_ = timer_->GetTime();
}
private:
double last_abs_time_;
double local_time_;
Timer* timer_; // or a smart pointer in real code ...
};
int main()
{
Timer timer;
FreeTimer free_timer1( timer );
FreeTimer free_timer2( timer );
while(-1)
{
timer.Update();
std::cout << "absolute timer: " << timer.GetTime() << std::endl;
if( /*whatever*/ ) std::cout << "timer 1: " << free_timer1.PopTime() << std::endl;
if( /*whatever*/ ) std::cout << "timer 2: " << free_timer2.PopTime() << std::endl;
if( /*whatever*/ ) free_timer1.PushTime( 0. );
if( /*whatever*/ ) free_timer2.PushTime( 0. );
}
}
of course, the code above will accumulate numerical errors on free timer values ... but I suppose the OP can live with that given his requirements ...
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by superbonzo
actually, ( unless I'm missing something ) there's no need of linearly updating each timer from a centralized timer; you can write something like
Dunno, I mentioned a similiar option in my post #2, but for some reason candlemaster decided that the registry idea would be more appropriate.
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by
laserlight
Dunno, I mentioned a similiar option in my post #2,[...]
so, I did miss something then ... :)
Re: Update all instances of a class with a single funtion?
Sounds like this could be a job for the Observer Pattern
Re: Update all instances of a class with a single funtion?
I said, in effect, which is true. You have one possible accessor, and one possible update method.
Quote:
Originally Posted by
Eri523
Not quite... A singleton would have a single instance pointer instead of a vector of instance pointers and the instance it points to is reused over and over.
That doesn't work if the timers are meant to be used independently. Since the op wanted multiple timers, it doesnt make sense to me to 'reuse' one.
Quote:
Originally Posted by
Eri523
Also, the timer objects here would probably get constructed using a public constructor (and register themselves in that process) instead of the instance accessor funtion that's common to singleton implementations. And from the client code perspective the timer objects would look much more like usual instances rather than a singleton.
sure, it might look a bit different syntactically, but in effect they are the same.
Re: Update all instances of a class with a single funtion?
No an observer is much different than a singleton.
Re: Update all instances of a class with a single funtion?
Quote:
Originally Posted by
ahoodin
No an observer is much different than a singleton.
:confused:
nobody said otherwise.
Re: Update all instances of a class with a single funtion?
Aaah well here is a good reference on these things:
http://www.codeguru.com/forum/showthread.php?t=327982
HTH,