test_sigslot.cxx
Go to the documentation of this file.
1 #include <boost/signals2.hpp>
2 
3 #include <iostream>
4 #include <memory>
5 
6 
7 
8 // http://stackoverflow.com/a/20429450
9 
10 typedef boost::signals2::signal<int ()> IntSig;
11 typedef std::shared_ptr<IntSig> IntSigPtr;
12 typedef IntSig::slot_type IntSlot;
13 
14 using namespace std;
15 
16 struct A {
17  int x;
18  double dd;
19  A(int n, double d) : x(n), dd(d) {}
20  int operator()() {
21  cout << "A @ " << x << " " << dd << endl;
22  dd *= x;
23  return x++;
24  }
25 };
26 
27 struct B {
28  int y;
29  double s;
31 
32  B(double scale) : y(0), s(scale), sig(new IntSig) {}
33  void connect(const IntSlot& slot) { sig->connect(slot); }
34  int operator()() {
35  y = s * *(*sig)();
36  cout << "B @ " << y << endl;
37  return y;
38  }
39 };
40 
42 {
43  A a(42, 1.0);
44  B b(1.0);
45  b.connect(a);
46  B b2(2.0);
47  b2.connect(b);
48  B b3(3.0);
49  b3.connect(b2);
50 
51  b3();
52  b3();
53 }
54 
55 struct MyData {
56  typedef std::shared_ptr<MyData> pointer;
57  int x;
58  MyData(int n=0) : x(n) {}
59  int next() { return ++x; }
60  int get() { return x; }
61 
62  typedef boost::signals2::signal<pointer ()> source_signal;
63  typedef typename source_signal::slot_type source_slot;
64 };
65 
66 struct SlotA {
68  SlotA() : data(new MyData) {}
70  data->next();
71  return data;
72  }
73 };
74 
75 struct SigA {
76  SigA() {}
77 
78  MyData::pointer operator()() { return *m_input(); }
79 
80  void connect(const MyData::source_slot& s) { m_input.connect(s); }
82 };
83 
85 {
86  SlotA slota;
87  SigA siga, sigaa;
88  siga.connect(slota);
89  sigaa.connect(boost::ref(siga));
90  cout << sigaa()->get() << endl;;
91  cout << sigaa()->get() << endl;;
92  cout << sigaa()->get() << endl;;
93 }
94 
95 
96 int main()
97 {
98  test_simple();
99  test_isignal();
100  return 0;
101 }
int next()
std::shared_ptr< IntSig > IntSigPtr
A(int n, double d)
int y
source_signal::slot_type source_slot
void test_isignal()
STL namespace.
MyData::source_signal m_input
void test_simple()
basic_data data
Definition: format.h:764
int x
MyData(int n=0)
double y
int main()
int operator()()
MyData::pointer operator()()
IntSig::slot_type IntSlot
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
std::shared_ptr< MyData > pointer
void scale(Sequence< Val > &seq, Val scalar)
Scale (multiply) sequence values by scalar.
Definition: Waveform.h:146
double dd
double s
void connect(const IntSlot &slot)
MyData::pointer data
#define A
Definition: memgrp.cpp:38
static bool * b
Definition: config.cpp:1043
IntSigPtr sig
list x
Definition: train.py:276
boost::signals2::signal< int()> IntSig
int operator()()
boost::signals2::signal< pointer()> source_signal
std::size_t n
Definition: format.h:3399
MyData::pointer operator()()
static QCString * s
Definition: config.cpp:1042
void connect(const MyData::source_slot &s)
QTextStream & endl(QTextStream &s)
B(double scale)