OpendTect  6.3
notify.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*+
4 ________________________________________________________________________
5 
6  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
7  Author: Bert / Kris
8  Date: Nov 1995 / April 2016
9  Contents: Notifications
10 ________________________________________________________________________
11 
12 -*/
13 
14 #include "basicmod.h"
15 #include "callback.h"
16 
17 
21 {
22 
23  friend class NotifyStopper;
24  friend class CallBacker;
25 
26 public:
27 
28  NotifierAccess(const NotifierAccess&);
30  virtual ~NotifierAccess();
31 
32  void notify(const CallBack&,bool first=false) const;
33  bool notifyIfNotNotified(const CallBack&) const;
35  void remove(const CallBack&) const;
36  bool removeWith(const CallBacker*,bool wait=true) const;
38  void transferCBSTo(const NotifierAccess&,
39  const CallBacker* only_for) const;
40 
41  inline bool isEnabled() const { return !cbs_.hasAnyDisabled(); }
42  inline void enable( bool yn=true ) { return cbs_.disableAll(!yn); }
43  inline void disable() { return cbs_.disableAll(true); }
44 
45  inline bool isEmpty() const { return cbs_.isEmpty(); }
46  bool willCall(const CallBacker*) const;
53 
54  bool isShutdownSubscribed(const CallBacker*) const;
56 
57 protected:
58 
59  static void doTrigger(CallBackSet&,const CallBacker*);
60  void addShutdownSubscription(const CallBacker*) const;
61  bool removeShutdownSubscription(const CallBacker*,
62  bool wait) const;
64 
68 
69 };
70 
71 
120 template <class T>
122 {
123 public:
124 
125  // Following functions are usually used by T class only:
126  Notifier( T* c ) { cber_ = c; }
127 
128  inline void trigger()
129  { if ( !cbs_.isEmpty() ) doTrigger( cbs_, cber_ ); }
130  inline void trigger( T& t )
131  { if ( !cbs_.isEmpty() ) doTrigger( cbs_, &t ); }
132  inline void trigger( CallBacker* c )
133  { if ( !cbs_.isEmpty() ) doTrigger( cbs_, c ); }
134 
135 };
136 
137 
138 #define mAttachObjCB( objptr, notifier, func, chk ) \
139  attachCB( notifier, CallBack( objptr, mCBFn(func) ), chk )
140 #define mDetachObjCB( objptr, notifier, func ) \
141  detachCB( notifier, CallBack( objptr, mCBFn(func) ) )
142 
143 #define mAttachCB( notifier, func ) \
144  mAttachObjCB( this, notifier, func, false )
145 
146 #define mAttachCBIfNotAttached( notifier, func ) \
147  mAttachObjCB( this, notifier, func, true )
148 
149 #define mDetachCB( notifier, func ) \
150  mDetachObjCB( this, notifier, func )
151 
152 
165 template <class T,class PayLoad>
167 {
168 public:
169  CNotifier( T* cb ) { cber_ = cb; }
170 
171  inline void trigger( PayLoad pl )
172  {
173  CBCapsule<PayLoad> caps( pl, cber_ );
174  doTrigger( cbs_, &caps );
175  }
176 
177  inline void trigger( PayLoad pl, CallBacker* cb )
178  {
179  if ( !cb )
180  trigger( pl );
181  else
182  {
183  CBCapsule<PayLoad> caps( pl, cb );
184  doTrigger( cbs_, &caps );
185  }
186  }
187  inline void trigger( PayLoad pl, T& t )
188  { trigger(pl,&t); }
189 
190 };
191 
192 
213 {
214 public:
215  NotifyStopper(NotifierAccess&,const CallBacker* only_for=0);
216  ~NotifyStopper();
217 
218  void enableNotification();
219  void disableNotification();
220 
221 protected:
222 
226 
227  void setDisabled(bool);
228 
229 };
230 
231 
234 
235 #define mDeclInstanceCreatedNotifierAccess(clss) \
236  static Notifier<clss>& instanceCreated()
237 
238 #define mDefineInstanceCreatedNotifierAccess(clss) \
239 Notifier<clss>& clss::instanceCreated() \
240 { \
241  mDefineStaticLocalObject( Notifier<clss>, theNotif, (0)); \
242  return theNotif; \
243 }
244 
245 #define mTriggerInstanceCreatedNotifier() \
246  instanceCreated().trigger( this )
#define mExpClass(module)
Definition: commondefs.h:157
Capsule class to wrap any class into a CallBacker.
Definition: callback.h:257
Inherit from this class to be able to send and/or receive CallBacks.
Definition: callback.h:182
Temporarily disables a Notifier.
Definition: notify.h:212
bool isEmpty() const
Definition: notify.h:45
A lock of a type that (hopefully) suits your needs. To use it, you need the Locker class...
Definition: threadlock.h:51
NotifierAccess & thenotif_
Definition: notify.h:223
Interface class for Notifier. See comments there.
Definition: notify.h:20
Threads::Lock shutdownsubscriberlock_
Definition: notify.h:67
void trigger()
Definition: notify.h:128
void trigger(PayLoad pl)
Definition: notify.h:171
void disable()
Definition: notify.h:43
friend class NotifierAccess
Definition: callback.h:184
const CallBacker * onlyfor_
Definition: notify.h:225
void trigger(CallBacker *c)
Definition: notify.h:132
Class to help setup a callback handling.
Definition: notify.h:121
void enable(bool yn=true)
Definition: notify.h:42
void trigger(T &t)
Definition: notify.h:130
CNotifier(T *cb)
Definition: notify.h:169
void trigger(PayLoad pl, T &t)
Definition: notify.h:187
bool isEnabled() const
Definition: notify.h:41
Notifier(T *c)
Definition: notify.h:126
TypeSet of CallBacks with a few extras.
Definition: callback.h:149
Notifier with automatic capsule creation.
Definition: notify.h:166
CallBackSet & cbs_
Definition: notify.h:51
ObjectSet< const CallBacker > shutdownsubscribers_
Definition: notify.h:66
#define mClass(module)
Definition: commondefs.h:161
bool isdisabled_
Definition: notify.h:224
void trigger(PayLoad pl, CallBacker *cb)
Definition: notify.h:177
CallBacks object-oriented (object + method).
Definition: callback.h:62
CallBacker * cber_
Definition: notify.h:52

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