-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_PeriodicFunction.cpp
54 lines (49 loc) · 1.63 KB
/
test_PeriodicFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <chrono>
#include "PeriodicFunction.h"
int main() {
//Create a lambda and plcae into execution
//every 200 millis over a duration of 1 second
using namespace std::chrono;
steady_clock::time_point startTime { steady_clock::now() };
steady_clock::time_point execTime { startTime };
auto lambda = [&] { //We don't need the ()
using namespace std::chrono;
execTime = steady_clock::now();
std::cout << "lambda executed at T="
<< duration_cast<milliseconds>(execTime - startTime).count()
<< std::endl;
startTime = execTime;
};
pf::PeriodicFunction<decltype(lambda)> pf1 { 200, 1000, lambda };
while (!pf1.hasExpired()) ;
std::cout << std::endl;
//Re-run the lambda every half second for a second
startTime = steady_clock::now();
execTime = startTime;
pf1.restart(500, 1500);
while (!pf1.hasExpired()) ;
std::cout << std::endl;
//Define a stateful Functor class
class Functor {
public:
Functor() :
startTime_ { steady_clock::now() }, execTime_ { startTime_ } {
}
void operator()() {
execTime_ = steady_clock::now();
std::cout << "Functor::operator() executed at T="
<< duration_cast<milliseconds>(execTime_ - startTime_).count()
<< std::endl;
startTime_ = execTime_;
}
private:
steady_clock::time_point startTime_;
steady_clock::time_point execTime_;
};
//Create a Functor object and place into execution
//every 50 millis over a duration of 250 millis
Functor myFunction { };
pf::PeriodicFunction<Functor> pf2 { 50, 250, myFunction, 0 };
while (!pf2.hasExpired()) ;
}