libzypp  13.10.6
Callback.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_CALLBACK_H
13 #define ZYPP_CALLBACK_H
14 
15 #include "zypp/base/NonCopyable.h"
16 
18 namespace zypp
19 {
20 
22  namespace HACK {
23  class Callback
24  {
25  };
26  } // namespace HACK
27 
29 
129  namespace callback
130  {
131 
133  struct ReportBase
134  {
135  virtual ~ReportBase()
136  {}
137  };
138 
140  template<class _Report>
142 
144  template<class _Report>
145  struct ReceiveReport : public _Report
146  {
150 
151  virtual ~ReceiveReport()
152  { disconnect(); }
153 
155  { return Distributor::instance().getReceiver(); }
156 
157  bool connected() const
158  { return whoIsConnected() == this; }
159 
160  void connect()
161  { Distributor::instance().setReceiver( *this ); }
162 
163  void disconnect()
164  { Distributor::instance().unsetReceiver( *this ); }
165 
166  virtual void reportbegin()
167  {}
168  virtual void reportend()
169  {}
170  };
171 
173  template<class _Report>
174  struct DistributeReport
175  {
176  public:
180 
182  {
183  static DistributeReport _self;
184  return _self;
185  }
186 
188  { return _receiver == &_noReceiver ? 0 : _receiver; }
189 
190  void setReceiver( Receiver & rec_r )
191  { _receiver = &rec_r; }
192 
193  void unsetReceiver( Receiver & rec_r )
194  { if ( _receiver == &rec_r ) noReceiver(); }
195 
196  void noReceiver()
197  { _receiver = &_noReceiver; }
198 
199  public:
201  { return _receiver; }
202 
203  private:
205  : _receiver( &_noReceiver )
206  {}
209  };
210 
212  template<class _Report>
214  {
218 
220  { Distributor::instance()->reportbegin(); }
221 
223  { Distributor::instance()->reportend(); }
224 
226  { return Distributor::instance().getReceiver(); }
227 
228  static bool connected()
229  { return whoIsConnected(); }
230 
232  { return Distributor::instance(); }
233  };
234 
260  template<class _Report>
261  struct TempConnect
262  {
266 
268  : _oldRec( Distributor::instance().getReceiver() )
269  {
271  }
272 
274  : _oldRec( Distributor::instance().getReceiver() )
275  {
276  rec_r.connect();
277  }
278 
280  {
281  if ( _oldRec )
283  else
285  }
286  private:
288  };
289 
291  } // namespace callback
294 } // namespace zypp
296 #endif // ZYPP_CALLBACK_H
virtual void reportend()
Definition: Callback.h:168
virtual void reportbegin()
Definition: Callback.h:166
DistributeReport< _Report > Distributor
Definition: Callback.h:217
ReceiveReport * whoIsConnected() const
Definition: Callback.h:154
Receiver * getReceiver() const
Definition: Callback.h:187
static DistributeReport & instance()
Definition: Callback.h:181
Temporarily connect a ReceiveReport then restore the previous one.
Definition: Callback.h:261
ReceiveReport< _Report > Receiver
Definition: Callback.h:216
TempConnect(Receiver &rec_r)
Definition: Callback.h:273
void setReceiver(Receiver &rec_r)
Definition: Callback.h:190
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
ReceiveReport< _Report > Receiver
Definition: Callback.h:178
DistributeReport< _Report > Distributor
Definition: Callback.h:179
DistributeReport< _Report > Distributor
Definition: Callback.h:265
static bool connected()
Definition: Callback.h:228
void unsetReceiver(Receiver &rec_r)
Definition: Callback.h:193
ReceiveReport< _Report > Receiver
Definition: Callback.h:264
Distributor & operator->()
Definition: Callback.h:231
DistributeReport< _Report > Distributor
Definition: Callback.h:149
static Receiver * whoIsConnected()
Definition: Callback.h:225
ReceiveReport< _Report > Receiver
Definition: Callback.h:148