1 | #ifndef PERIODICACTION
|
---|
2 | #define PERIODICACTION
|
---|
3 | /**
|
---|
4 | * Class for periodically calls to a slot: a global function or a method of a
|
---|
5 | * given object.
|
---|
6 | *
|
---|
7 | * Connects the slot to a PeriodicSignal, that wraps a thread, which
|
---|
8 | * periodically will call the doWhile[doWhileNot] slot until this returns
|
---|
9 | * false[true]. If a FinallyDo action is registered, when the PeriodicAction
|
---|
10 | * has finished it will call this finalizing slot
|
---|
11 | *
|
---|
12 | * To wrap the doWhile, doWhileNot or FinallyDo action properly (type safer
|
---|
13 | * than pointer to functions, better object oriented), call the slot factory:
|
---|
14 | *
|
---|
15 | * slot(global_function)
|
---|
16 | * slot(anObject,&Object::Method)
|
---|
17 | *
|
---|
18 | * EXAMPLE: -----------------------------------------------------------------
|
---|
19 | *
|
---|
20 | * class Counter: public Object{
|
---|
21 | * Counter(int max, int incr);
|
---|
22 | * bool isIncrementPossible();
|
---|
23 | * void PrintIAmDone();
|
---|
24 | * };
|
---|
25 | *
|
---|
26 | * bool end;void endMe(){end=true;}
|
---|
27 | *
|
---|
28 | * int main(){
|
---|
29 | * end=false;
|
---|
30 | * Counter byTwo(100,2), byThree(100,3);
|
---|
31 | *
|
---|
32 | * PeriodicAction byTwoEachSec(1000000);
|
---|
33 | * byTwoEachSec.DoWhile(slot(byTwo,&Counter::isIncrementPossible));
|
---|
34 | * byTwoEachSec.FinallyDo(slot(byTwo,&Counter::PrintIAmDone));
|
---|
35 | * byTwoEachSec.Start();
|
---|
36 | *
|
---|
37 | * PeriodicAction byThreeEachTwoSec(2000000);
|
---|
38 | * byThreeEachTwoSec.DoWhile(slot(byThree,&Counter::isIncrementPossible));
|
---|
39 | * byThreeEachTwoSec.FinallyDo(slot(endMe));
|
---|
40 | * byThreeEachTwoSec.Start();
|
---|
41 | *
|
---|
42 | * while(!end){
|
---|
43 | * }
|
---|
44 | * }
|
---|
45 | * ----------------------------------------------------------------------------
|
---|
46 | *
|
---|
47 | * Pending: Define behaviour of synchronization of diferent periodicactions
|
---|
48 | *
|
---|
49 | * Implementation Pending: Check the periodicaction is not running before
|
---|
50 | * starting, and it is running before stopping it. (With
|
---|
51 | * PeriodicSignal::isRunning)
|
---|
52 | *
|
---|
53 | * Implementation Pending: Not thread safe when two periodic actions may
|
---|
54 | * access the same variables
|
---|
55 | */
|
---|
56 |
|
---|
57 |
|
---|
58 | #include "PeriodicSignal.H"
|
---|
59 | //for signals, and Object
|
---|
60 | #include <sigc++/signal_system.h>
|
---|
61 | using namespace SigC;
|
---|
62 |
|
---|
63 | class PeriodicAction: public Object {
|
---|
64 |
|
---|
65 | public:
|
---|
66 | /** Aproximate period (+-500ms) in microsec (so, greater than 500000)
|
---|
67 | * to repeat doWhile or doWhileNot actions
|
---|
68 | * @see #doWhile
|
---|
69 | * @see #doWhileNot
|
---|
70 | */
|
---|
71 | unsigned int period;
|
---|
72 | public:
|
---|
73 | PeriodicAction( unsigned int period_);
|
---|
74 |
|
---|
75 | PeriodicAction ( unsigned int period_, const Slot0<bool> & doWhileData,
|
---|
76 | const Slot0<void> & finallyData );
|
---|
77 | void DoWhileNot( const Slot0<bool> & s);
|
---|
78 | void DoWhile(const Slot0<bool> &s);
|
---|
79 | void FinallyDo( const Slot0<void> & s);
|
---|
80 | void Start();
|
---|
81 | void Stop();
|
---|
82 | // void SetPeriod(unsigned long int period_);
|
---|
83 | private:
|
---|
84 | PeriodicSignal periodic;
|
---|
85 | Connection actionConn,periodConn;
|
---|
86 |
|
---|
87 | Signal0<bool> doWhile;
|
---|
88 | Signal0<bool> doWhileNot;
|
---|
89 |
|
---|
90 | Signal0<void> finally;
|
---|
91 |
|
---|
92 | void DoItOnceAndCheckContinuance();
|
---|
93 | void DoItOnceAndCheckContinuance_Not();
|
---|
94 | };
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 | #endif
|
---|