source: trunk/MagicSoft/Control/SubsystemIO/mainForPeriodicAction.cxx@ 9488

Last change on this file since 9488 was 1054, checked in by casaldaliga, 23 years ago
changed .H to .hxx in includes to work with new naming
File size: 1.8 KB
Line 
1#include <iostream>
2#include "PeriodicAction.hxx"
3//the next class derives from sigc++ Object, so its methods are callable as slots
4class Counter:public Object
5{
6public:
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};
28bool end;
29
30void endMe()
31{
32 end=true;
33}
34
35int 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}
Note: See TracBrowser for help on using the repository browser.