a463ed300c89dd3cd4e9ca9d3b936e3e55d3da3e
[platform/upstream/libzypp.git] / devel / devel.ma / MaTest.cc
1 #include <iostream>
2
3 #include <boost/signal.hpp>
4 #include <boost/bind.hpp>
5
6 #include <zypp/base/LogTools.h>
7 #include <zypp/base/Easy.h>
8
9 using std::endl;
10 using std::cout;
11
12 namespace boost
13 {
14   template<class Tp>
15       std::ostream & operator<<( std::ostream & str, const signal<Tp> & obj )
16   {
17     return str << "Connected slots: " << obj.num_slots();
18   }
19
20   namespace signals
21   {
22     std::ostream & operator<<( std::ostream & str, const connection & obj )
23     {
24       return str << "Connection: "
25           << ( obj.connected() ? '*' : '_' )
26           << ( obj.blocked()   ? 'B' : '_' )
27           ;
28     }
29   }
30 }
31
32 using namespace zypp;
33
34 using boost::signal;
35 using boost::signals::connection;
36 using boost::signals::trackable;
37
38 struct HelloWorld
39 {
40   HelloWorld() {++i;}
41   HelloWorld(const HelloWorld &) {++i;}
42   ~HelloWorld() { --i;}
43
44   void operator()(unsigned) const
45   {
46     USR << "Hello, World! " << i << std::endl;
47   }
48
49   static int i;
50 };
51
52 int HelloWorld::i = 0;
53
54 struct M
55 {
56   void ping() const
57   {
58     static unsigned i = 0;
59     ++i;
60     MIL << i << " -> " << _sigA << endl;
61     _sigA( i );
62   }
63
64   typedef signal<void(unsigned)> SigA;
65
66   SigA & siga() const { return _sigA; }
67
68   mutable SigA _sigA;
69 };
70
71 struct X : public trackable
72 {
73   X() {_s=++s;}
74   X( const X & ) {_s=++s;}
75   X& operator=( const X & ) { return *this; }
76   ~X() {_s=-_s;}
77   static int s;
78   int _s;
79
80   void pong( unsigned i ) const
81   {
82     DBG << _s << ' ' << i << endl;
83   }
84 };
85
86 int X::s;
87
88 /******************************************************************
89 **
90 **      FUNCTION NAME : main
91 **      FUNCTION TYPE : int
92 */
93 int main( int argc, const char * argv[] )
94 {
95   --argc; ++argv; // skip arg 0
96
97   M m;
98   m.ping();
99   X xx;
100   m.siga().connect( boost::bind( &X::pong, &xx, _1 ) );
101   m.ping();
102
103   {
104     X x;
105     m.siga().connect( boost::bind( &X::pong, &x, _1 ) );
106     m.ping();
107
108     X y;
109     m.siga().connect( boost::bind( &X::pong, &y, _1 ) );
110     m.ping();
111
112   }
113
114   m.ping();
115
116   ///////////////////////////////////////////
117
118   INT << "---STOP" << endl;
119   return 0;
120 }
121