1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Callback.h
12 #ifndef ZYPP_CALLBACK_H
13 #define ZYPP_CALLBACK_H
15 #include "zypp/base/NonCopyable.h"
17 ///////////////////////////////////////////////////////////////////
19 { /////////////////////////////////////////////////////////////////
21 /** \todo Eliminate this! */
28 ///////////////////////////////////////////////////////////////////
31 * \par The task report structure (SENDER SIDE).
33 * A default constructible struct derived from callback::ReportBase.
34 * It \b must \b not conatin any data, just virtual methods.
36 * These are the functions the sender invokes, and which will be forwarded
37 * to some receiver. If no receiver is present, the defined default
38 * implementations are invoked.
40 * For methods returning non-void, define a reasonable return value,
41 * because this is what you get back in case no receiver is listening.
43 * That way the sending side does not need to know whether some receiver
44 * is listening. And it enables the receiver to return a reasonable value,
45 * in case he's got no idea, what to else to return.
48 * struct Foo : public callback::ReportBase
50 * virtual void ping( int i )
58 * \par Sending a Task report (SENDER SIDE).
60 * Simply create a callback::SendReport<_Report>, where _Report
61 * is your task report structure. Invoke the callback functions
62 * as needed. That's it.
64 * \note Even creation and destruction of a callback::SendReport
65 * are indicated to a receiver. So even in case of an Exception,
66 * the receiver is able to recognize, that the task ended.
67 * So don't create it without need.
71 * callback::SendReport<Foo> report;
73 * int response = report->pong();
77 * \par Receiving Task reports (RECEIVER SIDE).
79 * To receive task reports of type \c Foo the recipient class
80 * derives from callback::ReceiveReport\<Foo\>. callback::ReceiveReport
81 * inherits \c Foo and provides two additional methods:
84 * virtual void reportbegin() {}
85 * virtual void reportend() {}
88 * These two are automatically invoked, whenever the sender
89 * creates a callback::SendReport instance, and when it gets
90 * destructed. So even if the sending task is aborted without
91 * sending an explicit notification, the reciever may notice it,
92 * by overloading \c reportend.
94 * Overload the methods you're interested in.
96 * \note In case you must return some value and don't know which,
97 * return the task structures default. The author of the task
98 * structure had to provide this value, so it's probabely better
99 * than anything you \e invent.
103 * ...// don't know what to return?
104 * return Foo::somefunktion();
110 { /////////////////////////////////////////////////////////////////
115 virtual ~ReportBase()
120 template<class _Report>
121 class DistributeReport;
124 template<class _Report>
125 struct ReceiveReport : public _Report
127 typedef DistributeReport<_Report> Distributor;
129 virtual void reportbegin()
131 virtual void reportend()
136 template<class _Report>
137 struct DistributeReport
140 typedef ReceiveReport<_Report> Receiver;
142 static DistributeReport & instance()
144 static DistributeReport _self;
148 void setReceiver( Receiver & _rec )
149 { _receiver = &_rec; }
152 { _receiver = &_noReceiver; }
155 Receiver * operator->()
156 { return _receiver; }
160 : _receiver( &_noReceiver )
162 Receiver _noReceiver;
163 Receiver * _receiver;
167 template<class _Report>
168 struct SendReport : private zypp::base::NonCopyable
170 typedef DistributeReport<_Report> Distributor;
173 { Distributor::instance()->reportbegin(); }
176 { Distributor::instance()->reportend(); }
178 Distributor & operator->()
179 { return Distributor::instance(); }
182 /////////////////////////////////////////////////////////////////
183 } // namespace callback
184 ///////////////////////////////////////////////////////////////////
185 /////////////////////////////////////////////////////////////////
187 ///////////////////////////////////////////////////////////////////
188 #endif // ZYPP_CALLBACK_H