OpendTect-6_4  6.4
callback.h
Go to the documentation of this file.
1 #ifndef callback_h
2 #define callback_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: A.H.Bril
9  Date: 8-11-1995
10  Contents: Notification and Callbacks
11  RCS: $Id$
12 ________________________________________________________________________
13 
14 -*/
15 
16 #include "basicmod.h"
17 #include "refcount.h"
18 #include "sets.h"
19 #include "threadlock.h"
20 
35 class CallBacker;
36 
37 
39 #define mCBFn(clss,fn) ((CallBackFunction)(&clss::fn))
40 
42 #define mCB(obj,clss,fn) CallBack( static_cast<clss*>(obj), mCBFn(clss,fn))
43 
45 #define mSCB(fn) CallBack( ((StaticCallBackFunction)(&fn)) )
46 
47 
58 {
59 public:
60  static void initClass();
61 
63  : obj_( o ), fn_( f ), sfn_( 0 ) {}
65  : obj_( 0 ), fn_( 0 ), sfn_( f ) {}
66  inline int operator==( const CallBack& c ) const
67  { return obj_==c.obj_ && fn_==c.fn_ && sfn_==c.sfn_; }
68  inline int operator!=( const CallBack& cb ) const
69  { return !(*this==cb); }
70 
71  inline bool willCall() const
72  { return (obj_ && fn_) || sfn_; }
73  void doCall(CallBacker*) const;
74 
75  inline CallBacker* cbObj() { return obj_; }
76  inline const CallBacker* cbObj() const { return obj_; }
77  inline CallBackFunction cbFn() const { return fn_; }
78  inline StaticCallBackFunction scbFn() const { return sfn_; }
79 
80  static bool addToMainThread(const CallBack&, CallBacker* =0);
85  static bool callInMainThread(const CallBack&, CallBacker* =0);
93  static void removeFromMainThread(const CallBacker*);
94 
95 
96  // See also mEnsureExecutedInMainThread macro
97 
98 protected:
99 
103 
104 public:
105 
106  // Usually only called from mEnsureExecutedInMainThread:
107 
108  static bool queueIfNotInMainThread(CallBack,
109  CallBacker* =0);
113 };
114 
115 #define mMainThreadCall( func ) \
116 CallBack::callInMainThread( CallBack( this, ((CallBackFunction)(&func) ) ), 0)
117 
118 #define mEnsureExecutedInMainThread( func ) \
119  if ( CallBack::queueIfNotInMainThread( \
120  CallBack( this, ((CallBackFunction)(&func) ) ), 0 ) ) \
121  return
122 
123 
130 public:
131  CallBackSet();
132  CallBackSet(const CallBackSet&);
133  CallBackSet& operator=(const CallBackSet&);
134 
135  void doCall(CallBacker*,CallBacker* exclude=0);
140  void removeWith(CallBacker*);
142  void removeWith(CallBackFunction);
144  void removeWith(StaticCallBackFunction);
146 
147  inline bool isEnabled() const { return enabled_; }
148  bool doEnable( bool yn=true );
150 
152 private:
153  bool enabled_;
154 };
155 
156 
162 {
163 
164  friend class NotifyStopper;
165  friend class CallBacker;
166 
167 public:
168 
169  NotifierAccess(const NotifierAccess&);
170  NotifierAccess();
171  virtual ~NotifierAccess();
172 
173  void notify(const CallBack&,bool first=false);
174  bool notifyIfNotNotified(const CallBack&);
176  void remove(const CallBack&);
177  bool removeWith(CallBacker*,bool wait=true);
179 
180  inline bool isEnabled() const { return cbs_.isEnabled(); }
181  inline bool enable( bool yn=true ) { return cbs_.doEnable(yn); }
182  inline bool disable() { return cbs_.doEnable(false); }
183 
184  inline bool isEmpty() const { return cbs_.isEmpty(); }
185  bool willCall(CallBacker*) const;
192 
193  bool isShutdownSubscribed(CallBacker*) const;
195 protected:
196  static void doTrigger(CallBackSet&,CallBacker* c,
197  CallBacker* exclude);
198  void addShutdownSubscription(CallBacker*);
199  bool removeShutdownSubscription(CallBacker*, bool wait);
201 
206 };
207 
208 
253 template <class T>
255 {
256 public:
257 
258  void trigger( T& t ) { trigger(&t); }
259 
260  // Following functions are usually used by T class only:
261  Notifier( T* c ) { cber_ = c; }
262 
263  inline void trigger( CallBacker* c=0, CallBacker* exclude=0 )
264  { doTrigger( cbs_, c ? c : cber_, exclude ); }
265 };
266 
267 
273 {
274  friend class NotifierAccess;
275 public:
276  CallBacker();
277  CallBacker(const CallBacker&);
278  virtual ~CallBacker();
279 
280  bool attachCB(NotifierAccess&,const CallBack&,
281  bool onlyifnew=false);
286  bool attachCB(NotifierAccess* notif,const CallBack& cb,
287  bool onlyifnew=false)
288  { return notif ? attachCB(*notif,cb,onlyifnew):false; }
291  void detachCB(NotifierAccess&,const CallBack&);
294  void detachCB(NotifierAccess* notif,const CallBack& cb)
295  { if ( notif ) detachCB( *notif, cb ); }
298  bool isNotifierAttached(NotifierAccess*) const;
300 
301 protected:
302  void detachAllNotifiers();
304 private:
305 
306  bool notifyShutdown(NotifierAccess*,bool wait);
308 
311 };
312 
313 
314 #define mAttachCB( notifier, func ) \
315 attachCB( notifier, CallBack( this, ((CallBackFunction)(&func) ) ), false )
316 
317 #define mAttachCBIfNotAttached( notifier, func ) \
318 attachCB( notifier, CallBack( this, ((CallBackFunction)(&func) ) ), true )
319 
320 #define mDetachCB( notifier, func ) \
321 detachCB( notifier, CallBack( this, ((CallBackFunction)(&func) ) ) )
322 
334 template <class T>
335 mClass(Basic) CBCapsule : public CallBacker
336 {
337 public:
338  CBCapsule( T d, CallBacker* c )
339  : data(d), caller(c) {}
340 
341  T data;
342  CallBacker* caller;
343 };
344 
345 
369 #define mCBCapsuleGet(T,var,cb) \
370 CBCapsule<T>* var = dynamic_cast< CBCapsule<T>* >( cb );
371 
372 #define mCBCapsuleUnpack(T,var,cb) \
373 mCBCapsuleGet(T,cb##caps,cb) \
374 T var = cb##caps->data
375 
376 #define mCBCapsuleUnpackWithCaller(T,var,cber,cb) \
377 mCBCapsuleGet(T,cb##caps,cb) \
378 T var = cb##caps->data; \
379 CallBacker* cber = cb##caps->caller
380 
381 
394 template <class T,class C>
396 {
397 public:
398 
399  void trigger( C c, T& t ) { trigger(c,&t); }
400 
401 // Following functions are usually used by T class only:
402 
403  CNotifier( T* cb ) { cber_ = cb; }
404 
405  inline void trigger( C c, CallBacker* cb=0 )
406  {
407  CBCapsule<C> caps( c, cb ? cb : cber_ );
408  doTrigger( cbs_, &caps, 0 );
409  }
410 };
411 
412 
430 {
431 public:
433  ~NotifyStopper();
434 
435  void enable();
436  void disable();
437  void restore();
438 
439 protected:
440 
442  bool oldst_;
443 
444 };
445 
446 
447 // Set of macros to add an instanceCreated() notifier
448 // This can provide a notification of any instance of a class being produced
449 
450 #define mDeclInstanceCreatedNotifierAccess(clss) \
451  static Notifier<clss>& instanceCreated()
452 
453 #define mDefineInstanceCreatedNotifierAccess(clss) \
454 Notifier<clss>& clss::instanceCreated() \
455 { \
456  mDefineStaticLocalObject( Notifier<clss>, theNotif, (0)); \
457  return theNotif; \
458 }
459 
460 #define mTriggerInstanceCreatedNotifier() \
461  instanceCreated().trigger( this )
462 
463 
464 #endif
#define mRefCountImpl(ClassName)
Macro to setup a class with destructor for reference counting.
Definition: refcount.h:163
#define mExpClass(module)
Definition: commondefs.h:160
Capsule class to wrap any class into a CallBacker.
Definition: callback.h:335
To be able to send and/or receive CallBacks, inherit from this class.
Definition: callback.h:272
Temporarily disables a Notifier.
Definition: callback.h:429
bool enable(bool yn=true)
Definition: callback.h:181
bool isEmpty() const
Definition: callback.h:184
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:53
bool enabled_
Definition: callback.h:153
NotifierAccess & thenotif_
Definition: callback.h:441
void(* StaticCallBackFunction)(CallBacker *)
Definition: callback.h:44
void trigger(C c, T &t)
Definition: callback.h:399
bool disable()
Definition: callback.h:182
Interface class for Notifier. See comments there.
Definition: callback.h:161
friend class CallBacker
Definition: callback.h:165
Threads::Lock shutdownsubscriberlock_
Definition: callback.h:205
void(CallBacker::* CallBackFunction)(CallBacker *)
Definition: callback.h:38
bool willCall() const
Definition: callback.h:71
CallBacker * caller
Definition: callback.h:342
int operator==(const CallBack &c) const
Definition: callback.h:66
CallBack(CallBacker *o=0, CallBackFunction f=0)
Definition: callback.h:62
friend class NotifierAccess
Definition: callback.h:274
Definition: callback.h:254
T data
Definition: callback.h:341
Set of (small) copyable elements.
Definition: commontypes.h:30
int operator!=(const CallBack &cb) const
Definition: callback.h:68
bool isEnabled() const
Definition: callback.h:147
void trigger(C c, CallBacker *cb=0)
Definition: callback.h:405
void trigger(T &t)
Definition: callback.h:258
CNotifier(T *cb)
Definition: callback.h:403
StaticCallBackFunction scbFn() const
Definition: callback.h:78
bool isEnabled() const
Definition: callback.h:180
ObjectSet< NotifierAccess > attachednotifiers_
Definition: callback.h:309
CallBacker * obj_
Definition: callback.h:100
bool attachCB(NotifierAccess *notif, const CallBack &cb, bool onlyifnew=false)
Definition: callback.h:286
ObjectSet< CallBacker > shutdownsubscribers_
Definition: callback.h:204
Notifier(T *c)
Definition: callback.h:261
CallBack(StaticCallBackFunction f)
Definition: callback.h:64
void trigger(CallBacker *c=0, CallBacker *exclude=0)
Definition: callback.h:263
StaticCallBackFunction sfn_
Definition: callback.h:102
CallBacker * cbObj()
Definition: callback.h:75
Threads::Lock attachednotifierslock_
Definition: callback.h:310
bool oldst_
Definition: callback.h:442
CallBackFunction cbFn() const
Definition: callback.h:77
static void doTrigger(CallBackSet &, CallBacker *c, CallBacker *exclude)
void detachCB(NotifierAccess *notif, const CallBack &cb)
Definition: callback.h:294
CallBackFunction fn_
Definition: callback.h:101
TypeSet of CallBacks with a few extras.
Definition: callback.h:128
Notifier with automatic capsule creation.
Definition: callback.h:395
CallBackSet & cbs_
Definition: callback.h:190
friend class NotifyStopper
Definition: callback.h:164
#define mClass(module)
Definition: commondefs.h:164
CallBacks object-oriented (object + method).
Definition: callback.h:57
CBCapsule(T d, CallBacker *c)
Definition: callback.h:338
Threads::Lock lock_
Definition: callback.h:151
static void initClass()
const CallBacker * cbObj() const
Definition: callback.h:76
CallBacker * cber_
Definition: callback.h:191

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019