libzypp: zypp/Callback.h Source File

libzypp 17.38.14

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-core/base/NonCopyable.h>
16#include <zypp-core/UserData.h>
17
19namespace zypp
20{
21
23 namespace HACK {
24 class Callback
25 {
26 };
27 } // namespace HACK
28
30
145 namespace callback
146 {
147
149 struct ZYPP_API ReportBase
150 {
151 typedef callback::UserData UserData;
152 typedef UserData::ContentType ContentType;
153
155 virtual void report( const UserData & userData_r = UserData() )
156 {}
157
158 virtual ~ReportBase()
159 {}
160 };
161
163 template<class TReport>
164 struct DistributeReport;
165
167 template<class TReport>
168 struct ZYPP_API ReceiveReport : public TReport
169 {
170 typedef TReport ReportType;
171 typedef ReceiveReport<TReport> Receiver;
172 typedef DistributeReport<TReport> Distributor;
173
174 virtual ~ReceiveReport()
175 { disconnect(); }
176
177 ReceiveReport * whoIsConnected() const
178 { return Distributor::instance().getReceiver(); }
179
180 bool connected() const
181 { return whoIsConnected() == this; }
182
183 void connect()
184 { Distributor::instance().setReceiver( *this ); }
185
186 void disconnect()
187 { Distributor::instance().unsetReceiver( *this ); }
188
189 virtual void reportbegin()
190 {}
191 virtual void reportend()
192 {}
193 };
194
196 template<class TReport>
197 struct ZYPP_API DistributeReport
198 {
199 public:
200 typedef TReport ReportType;
201 typedef ReceiveReport<TReport> Receiver;
202 typedef DistributeReport<TReport> Distributor;
203
204 static DistributeReport & instance()
205 {
206 static DistributeReport _self;
207 return _self;
208 }
209
210 Receiver * getReceiver() const
211 { return _receiver == &_noReceiver ? 0 : _receiver; }
212
213 void setReceiver( Receiver & rec_r )
214 { _receiver = &rec_r; }
215
216 void unsetReceiver( Receiver & rec_r )
217 { if ( _receiver == &rec_r ) noReceiver(); }
218
219 void noReceiver()
220 { _receiver = &_noReceiver; }
221
222 public:
223 Receiver * operator->()
224 { return _receiver; }
225
226 private:
227 DistributeReport()
228 : _receiver( &_noReceiver )
229 {}
230 Receiver _noReceiver;
231 Receiver * _receiver;
232 };
233
235 template<class TReport>
236 struct ZYPP_API SendReport : private zypp::base::NonCopyable
237 {
238 typedef TReport ReportType;
239 typedef ReceiveReport<TReport> Receiver;
240 typedef DistributeReport<TReport> Distributor;
241
242 SendReport()
243 { Distributor::instance()->reportbegin(); }
244
245 ~SendReport()
246 { Distributor::instance()->reportend(); }
247
248 static Receiver * whoIsConnected()
249 { return Distributor::instance().getReceiver(); }
250
251 static bool connected()
252 { return whoIsConnected(); }
253
254 Distributor & operator->()
255 { return Distributor::instance(); }
256 };
257
283 template<class TReport>
284 struct ZYPP_API TempConnect
285 {
286 typedef TReport ReportType;
287 typedef ReceiveReport<TReport> Receiver;
288 typedef DistributeReport<TReport> Distributor;
289
290 TempConnect()
291 : _oldRec( Distributor::instance().getReceiver() )
292 {
293 Distributor::instance().noReceiver();
294 }
295
296 TempConnect( Receiver & rec_r )
297 : _oldRec( Distributor::instance().getReceiver() )
298 {
299 rec_r.connect();
300 }
301
302 ~TempConnect()
303 {
304 if ( _oldRec )
305 Distributor::instance().setReceiver( *_oldRec );
306 else
307 Distributor::instance().noReceiver();
308 }
309 private:
310 Receiver * _oldRec;
311 };
312
314 } // namespace callback
315
317} // namespace zypp
319#endif // ZYPP_CALLBACK_H
NonCopyable.h
UserData.h
TReport
zypp::HACK::Callback
Definition Callback.h:25
zypp::callback::UserData
Typesafe passing of user data via callbacks.
Definition UserData.h:40
zypp::callback::UserData::ContentType
zypp::ContentType ContentType
Definition UserData.h:51
zypp::HACK
Definition Callback.h:23
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::callback
Callbacks light.
Definition Callback.h:146
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::callback::DistributeReport
Definition Callback.h:198
zypp::callback::DistributeReport::noReceiver
void noReceiver()
Definition Callback.h:219
zypp::callback::DistributeReport::Receiver
ReceiveReport< TReport > Receiver
Definition Callback.h:201
zypp::callback::DistributeReport::getReceiver
Receiver * getReceiver() const
Definition Callback.h:210
zypp::callback::DistributeReport::_noReceiver
Receiver _noReceiver
Definition Callback.h:230
zypp::callback::DistributeReport::instance
static DistributeReport & instance()
Definition Callback.h:204
zypp::callback::DistributeReport::_receiver
Receiver * _receiver
Definition Callback.h:231
zypp::callback::DistributeReport::ReportType
TReport ReportType
Definition Callback.h:200
zypp::callback::DistributeReport::DistributeReport
DistributeReport()
Definition Callback.h:227
zypp::callback::DistributeReport::Distributor
DistributeReport< TReport > Distributor
Definition Callback.h:202
zypp::callback::DistributeReport::operator->
Receiver * operator->()
Definition Callback.h:223
zypp::callback::DistributeReport::unsetReceiver
void unsetReceiver(Receiver &rec_r)
Definition Callback.h:216
zypp::callback::DistributeReport::setReceiver
void setReceiver(Receiver &rec_r)
Definition Callback.h:213
zypp::callback::ReceiveReport
Definition Callback.h:169
zypp::callback::ReceiveReport::~ReceiveReport
virtual ~ReceiveReport()
Definition Callback.h:174
zypp::callback::ReceiveReport::reportend
virtual void reportend()
Definition Callback.h:191
zypp::callback::ReceiveReport::connect
void connect()
Definition Callback.h:183
zypp::callback::ReceiveReport::whoIsConnected
ReceiveReport * whoIsConnected() const
Definition Callback.h:177
zypp::callback::ReceiveReport::reportbegin
virtual void reportbegin()
Definition Callback.h:189
zypp::callback::ReceiveReport::ReportType
TReport ReportType
Definition Callback.h:170
zypp::callback::ReceiveReport::Distributor
DistributeReport< TReport > Distributor
Definition Callback.h:172
zypp::callback::ReceiveReport::Receiver
ReceiveReport< TReport > Receiver
Definition Callback.h:171
zypp::callback::ReceiveReport< ReportType >::disconnect
void disconnect()
Definition Callback.h:186
zypp::callback::ReceiveReport::connected
bool connected() const
Definition Callback.h:180
zypp::callback::ReportBase
Definition Callback.h:150
zypp::callback::ReportBase::~ReportBase
virtual ~ReportBase()
Definition Callback.h:158
zypp::callback::ReportBase::ContentType
UserData::ContentType ContentType
Definition Callback.h:152
zypp::callback::ReportBase::report
virtual void report(const UserData &userData_r=UserData())
The most generic way of sending/receiving data.
Definition Callback.h:155
zypp::callback::ReportBase::UserData
callback::UserData UserData
Definition Callback.h:151
zypp::callback::SendReport::SendReport
SendReport()
Definition Callback.h:242
zypp::callback::SendReport::connected
static bool connected()
Definition Callback.h:251
zypp::callback::SendReport::ReportType
TReport ReportType
Definition Callback.h:238
zypp::callback::SendReport::Distributor
DistributeReport< TReport > Distributor
Definition Callback.h:240
zypp::callback::SendReport::operator->
Distributor & operator->()
Definition Callback.h:254
zypp::callback::SendReport::~SendReport
~SendReport()
Definition Callback.h:245
zypp::callback::SendReport::whoIsConnected
static Receiver * whoIsConnected()
Definition Callback.h:248
zypp::callback::SendReport::Receiver
ReceiveReport< TReport > Receiver
Definition Callback.h:239
zypp::callback::TempConnect::Distributor
DistributeReport< TReport > Distributor
Definition Callback.h:288
zypp::callback::TempConnect::Receiver
ReceiveReport< TReport > Receiver
Definition Callback.h:287
zypp::callback::TempConnect::TempConnect
TempConnect(Receiver &rec_r)
Definition Callback.h:296
zypp::callback::TempConnect::~TempConnect
~TempConnect()
Definition Callback.h:302
zypp::callback::TempConnect::_oldRec
Receiver * _oldRec
Definition Callback.h:310
zypp::callback::TempConnect::TempConnect
TempConnect()
Definition Callback.h:290
zypp::callback::TempConnect::ReportType
TReport ReportType
Definition Callback.h:286