Home » U++ Library support » U++ Callbacks and Timers » Callbacks with 'Disable' feature
Callbacks with 'Disable' feature [message #29138] |
Thu, 07 October 2010 09:43  |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
hi all
Callbacks so far only have the Clear() behaviour. sometimes it is good to simply 'pause' its function, but later be able to reactivate them.. (withoud need to know where the callback goes to..class independant 
so what about a Enable() / Disable() methodpair..??
[Updated on: Thu, 07 October 2010 09:44] Report message to a moderator
|
|
|
Re: Callbacks with 'Disable' feature [message #29140 is a reply to message #29138] |
Thu, 07 October 2010 11:39   |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
maybe sth as simple as this..
template <class P1>
class Callback1 : Moveable< Callback1<P1> > {
Callback1Action<P1> *action;
bool act;
void Retain() const { if(action ) AtomicInc(action->count); }
void Release() { if(action && AtomicDec(action->count) == 0) delete action; }
bool operator==(const Callback1&);
bool operator!=(const Callback1&);
public:
typedef Callback1 CLASSNAME;
Callback1& operator=(const Callback1& c);
Callback1(const Callback1& c);
void Clear() { Release(); action = NULL; }
void Enable(bool b = true) { act = b; }
void Disable() { Enable(false); }
bool Enabled() const { return act; }
operator bool() const { return action && action->IsValid(); }
void Execute(P1 p1) const { if(act && action) action->Execute(p1); }
void operator()(P1 p1) const { Execute(p1); }
explicit Callback1(Callback1Action <P1> *newaction) { act = true; action = newaction; }
Callback1() { act = true; action = NULL; }
Callback1(_CNULL) { act = true; action = NULL; }
~Callback1();
static Callback1 Empty() { return CNULL; }
};
[Updated on: Thu, 07 October 2010 11:39] Report message to a moderator
|
|
|
Re: Callbacks with 'Disable' feature [message #29142 is a reply to message #29140] |
Thu, 07 October 2010 11:44   |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
BTW: in Callback.h:161 is missing the Execute() definition for Gate4<>. here comes with act already placed..
template <class P1, class P2, class P3, class P4>
bool Gate4<P1, P2, P3, P4>::Execute(P1 p1, P2 p2, P3 p3, P4 p4) const {
return (void *)action == (void *)1 ? true : (act && action) ? action->Execute(p1, p2, p3, p4) : false;
}
|
|
|
|
Re: Callbacks with 'Disable' feature [message #29221 is a reply to message #29220] |
Mon, 11 October 2010 19:34   |
|
Hi kohait,
The only comment I can give you is that I actually never needed such thing. I think there are two possible cases: 1) You need to disable some specific callbacks, or 2) you need to disable callback on a specific place of your code. Both are very easily solvable even without Callback::Disable(). For 1) it is just adding "if(DoNotExecuteNow) return;" on the beginning of the callback functions you might need to disable. For the second case, it would be similar, but the if would be outside of the call, something like "if(!DoNotExecuteNow) WhenSomething();".
IMHO there is not much need for changes. Actually I never even used the Clear() method 
Best regards,
Honza
|
|
|
|
Re: Callbacks with 'Disable' feature [message #29249 is a reply to message #29229] |
Wed, 13 October 2010 09:44   |
 |
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
what about the missing fix? i removed the act && action..Quote: |
Callback.h:161 is missing the Execute() definition for Gate4<>
|
i'm wondering that noone has needed it so far 
template <class P1, class P2, class P3, class P4>
bool Gate4<P1, P2, P3, P4>::Execute(P1 p1, P2 p2, P3 p3, P4 p4) const {
return (void *)action == (void *)1 ? true : action ? action->Execute(p1, p2, p3, p4) : false;
}
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 25 19:09:02 CEST 2025
Total time taken to generate the page: 0.00666 seconds
|