test_faninout.cxx
Go to the documentation of this file.
1 #include "WireCellUtil/Testing.h"
3 
4 #include <boost/signals2.hpp>
5 #include <boost/optional.hpp>
6 
7 #include <iostream>
8 #include <vector>
9 
10 using namespace std;
11 
12 struct Counter {
13  int count;
14  Counter(int c=0) : count(c) {}
15  int operator()(){
16  return count++;
17  }
18 };
19 
20 struct Echoer {
21  int operator()(int x) {
22  return x;
23  }
24 };
25 
26 /// moved to templated version in Faninout.h
27 // struct Addresser {
28 // int addr;
29 // typedef boost::signals2::signal<int (int)> signal;
30 // typedef signal::slot_type slot_type;
31 // signal sig;
32 // Addresser(int address) : addr(address) {}
33 // int operator()() {
34 // int val = *sig(addr);
35 // cerr << "Address "<<addr<<" returns " << val << endl;
36 // return val;
37 // }
38 // };
39 
40 
41 struct Consumer {
42  typedef boost::signals2::signal<int ()> signal;
43  typedef signal::slot_type slot_type;
44  signal sig;
45  int operator()() {
46  int val = *sig();
47  cerr << "Consumed: " << val << endl;
48  return val;
49  }
50 };
51 
52 using namespace std;
53 using namespace WireCell;
54 
56 {
57  Counter counter;
58  Fanout<int> fanout;
59  fanout.connect(counter);
60 
61  int addresses[] = {1,2,3,0,0,0,2,4,4,4,4,4,1,1,2,1,1,-1};
62  int want_val[] = {0,1,2,3,4,5,2,0,1,2,3,4,1,2,3,3,4,-1};
63 
64  // pre-register
65  fanout.address(4);
66 
67  for (int ind=0; addresses[ind] >= 0; ++ind) {
68  int addr = addresses[ind];
69  if (addr < 0) { break; }
70  int want = want_val[ind];
71  int got = fanout(addr);
72  cerr << "addr=" << addr << " got:" << got << " want:" << want << endl;
73  Assert(want == got);
74  }
75 
76 }
77 
78 void test_ref()
79 {
80  Echoer echo;
81 
82  Addresser<int> addr0(0), addr1(1);
83  addr0.connect(echo);
84  addr1.connect(echo);
85 
86  Consumer consumer0, consumer1;
87  consumer0.sig.connect(boost::ref(addr0));
88  consumer1.sig.connect(boost::ref(addr1));
89 
90  cerr << consumer0() << endl;
91  cerr << consumer1() << endl;
92 }
93 
95 {
96  Counter counter;
97  Fanout<int> fanout;
98  fanout.address(0);
99  fanout.address(1);
100  fanout.connect(counter);
101 
102 
103  Addresser<int> addr0(0), addr1(1);
104  addr0.connect(boost::ref(fanout));
105  addr1.connect(boost::ref(fanout));
106 
107  Consumer consumer0, consumer1;
108  consumer0.sig.connect(boost::ref(addr0));
109  consumer1.sig.connect(boost::ref(addr1));
110 
111  cerr << consumer1() << endl;
112  cerr << consumer0() << endl;
113  cerr << consumer0() << endl;
114  cerr << consumer0() << endl;
115  cerr << consumer1() << endl;
116  cerr << consumer0() << endl;
117 
118 }
119 
120 
122 {
123  Counter c1(10),c2(20),c3(30);
124 
125  // Fan-in concept is inherent in boost::signals2, but does require
126  // some "combiner" to enact whatever fan-in policy is desired.
127  // This one synchronizes input into a collection.
128  boost::signals2::signal< int (), Fanin< std::vector<int> > > sig;
129  sig.connect(c1);
130  sig.connect(c2);
131  sig.connect(c3);
132 
133  for (int ind=0; ind<10; ++ind) {
134  auto vec = sig();
135  cerr << ind;
136  for (auto x: vec) {
137  cerr << " " << x;
138  }
139  cerr << endl;
140  }
141 
142  return 0;
143 }
144 
145 int main()
146 {
148  test_ref();
150  test_fanin();
151  return 0;
152 }
int main()
int operator()(int x)
int operator()()
STL namespace.
moved to templated version in Faninout.h
#define Assert
Definition: Testing.h:7
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:285
signal sig
int test_fanin()
void test_fanout_addressing()
Definition: Main.h:22
void address(const address_type &addr)
Register an address.
Definition: Faninout.h:31
void connect(const slot &s)
Definition: Faninout.h:74
int operator()()
signal::slot_type slot_type
void test_plug_and_play()
list x
Definition: train.py:276
boost::signals2::signal< int()> signal
void test_ref()
constexpr std::enable_if_t< are_cv_compatible< TO, FROM >::value, std::add_pointer_t< std::remove_pointer_t< TO > > > addr(FROM &from)
Definition: ensurePointer.h:35
Counter(int c=0)
void connect(const slot &s)
Definition: Faninout.h:28
QTextStream & endl(QTextStream &s)