1 | #include <iostream>
|
---|
2 | #include "PeriodicAction.H"
|
---|
3 | //the next class derives from sigc++ Object, so its methods are callable as slots
|
---|
4 | class Counter:public Object
|
---|
5 | {
|
---|
6 | public:
|
---|
7 | int max;
|
---|
8 | int count;
|
---|
9 | int incr;
|
---|
10 |
|
---|
11 | Counter(int max_, int incr_)
|
---|
12 | :max(max_),incr(incr_),count(0)
|
---|
13 | {};
|
---|
14 | bool isIncrementPossible()
|
---|
15 | {
|
---|
16 | count+=incr;
|
---|
17 | cout<<"Counter by "<<incr<<" to "<<max<<" is at "<<count<<"\n";
|
---|
18 | //be careful on blocking here by putting endl or flushing cout!!!! It spoils the whole thing, because blocks the internal thread
|
---|
19 | if(count<=max) return true;
|
---|
20 | else return false;
|
---|
21 | };
|
---|
22 | void PrintIAmDone()
|
---|
23 | {
|
---|
24 | cout<<"Counter by "<<incr<<" to "<<max<<" is done!\n";
|
---|
25 | };
|
---|
26 |
|
---|
27 | };
|
---|
28 | bool end;
|
---|
29 |
|
---|
30 | void endMe()
|
---|
31 | {
|
---|
32 | end=true;
|
---|
33 | }
|
---|
34 |
|
---|
35 | int main()
|
---|
36 | {
|
---|
37 | end=false;
|
---|
38 | Counter byTwo(100,2), byThree(100,3);
|
---|
39 |
|
---|
40 | PeriodicAction byTwoEachSec(1000000);//in microsec
|
---|
41 | //byTwoEachSec will increment byTwo counter until isIncrementPossible returns false (will DoWhile(isIncrementPossible) ).
|
---|
42 | byTwoEachSec.DoWhile(slot(byTwo,&Counter::isIncrementPossible));
|
---|
43 | //DoWhile action has been set to the slot (callback) method isIncrementPossible, member of Counter class,
|
---|
44 | byTwoEachSec.FinallyDo(slot(byTwo,&Counter::PrintIAmDone));
|
---|
45 | //finally, when PeriodicAction finishes, that is, when isIncrementPossible returns false, will call byTwo.PrintIAmDone
|
---|
46 | byTwoEachSec.Start();
|
---|
47 |
|
---|
48 | PeriodicAction byThreeEachTwoSec(2000000);
|
---|
49 | byThreeEachTwoSec.DoWhile(slot(byThree,&Counter::isIncrementPossible));
|
---|
50 | byThreeEachTwoSec.FinallyDo(slot(endMe));
|
---|
51 | //in these case FinallyDo is not initialized to a method of an object, but to a global function. How can one provide this flexible interface without signal/slots
|
---|
52 | byThreeEachTwoSec.Start();
|
---|
53 |
|
---|
54 | while(!end){
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | return 0;
|
---|
59 | }
|
---|