-
September 3rd, 2011, 01:07 AM
#1
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?
Last edited by candlemaster; September 3rd, 2011 at 01:09 AM.
-
September 3rd, 2011, 04:10 AM
#2
Re: Update all instances of a class with a single funtion?
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.
-
September 3rd, 2011, 02:53 PM
#3
Re: Update all instances of a class with a single funtion?
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?
-
September 3rd, 2011, 04:32 PM
#4
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.
-
September 4th, 2011, 05:55 PM
#5
Re: Update all instances of a class with a single funtion?
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...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
September 5th, 2011, 01:45 PM
#6
Re: Update all instances of a class with a single funtion?
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 ...
-
September 5th, 2011, 02:55 PM
#7
Re: Update all instances of a class with a single funtion?
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.
Last edited by Eri523; September 6th, 2011 at 06:46 PM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
September 6th, 2011, 02:37 AM
#8
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 ...
-
September 6th, 2011, 02:50 AM
#9
Re: Update all instances of a class with a single funtion?
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.
-
September 6th, 2011, 03:31 AM
#10
Re: Update all instances of a class with a single funtion?
Originally Posted by laserlight
Dunno, I mentioned a similiar option in my post #2,[...]
so, I did miss something then ...
-
September 6th, 2011, 04:17 AM
#11
Re: Update all instances of a class with a single funtion?
Sounds like this could be a job for the Observer Pattern
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
September 6th, 2011, 01:46 PM
#12
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.
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.
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.
-
September 6th, 2011, 02:00 PM
#13
Re: Update all instances of a class with a single funtion?
No an observer is much different than a singleton.
ahoodin
To keep the plot moving, that's why.
-
September 7th, 2011, 12:12 PM
#14
Re: Update all instances of a class with a single funtion?
Originally Posted by ahoodin
No an observer is much different than a singleton.
nobody said otherwise.
-
September 7th, 2011, 12:24 PM
#15
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,
ahoodin
To keep the plot moving, that's why.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|