28#include <easy3d/util/timer.h>
29#include <easy3d/core/signal.h>
43 virtual void start() {
44 std::lock_guard<std::mutex> guard(mutex);
45 std::cout <<
"do nothing\n";
49class Car :
public Vehicle {
51 Car(
int speed) : speed_(speed) {}
54 int speed()
const {
return speed_; }
56 void start()
override {
57 std::lock_guard<std::mutex> guard(mutex);
58 std::cout <<
"started\n";
61 void start(
int new_speed) {
62 std::lock_guard<std::mutex> guard(mutex);
63 std::cout <<
"speed changed from " << speed_ <<
" to " << new_speed <<
"\n";
67 void stop(
int hours,
const std::string &msg)
const {
68 std::lock_guard<std::mutex> guard(mutex);
69 std::cout << msg <<
". After driving for " << hours <<
" hours\n";
77void test_timer_for_members(Car *car) {
105 t.
single_shot(33, car, &Car::stop, 6,
"I have to stop");
106 t.
set_interval(33, car, &Car::stop, 6,
"I have to stop");
107 t.
set_timeout(33, car, &Car::stop, 6,
"I have to stop");
113void timer_func_start() {
114 std::lock_guard<std::mutex> guard(mutex);
115 std::cout <<
"started\n";
118void timer_func_start(Car *car) {
119 std::lock_guard<std::mutex> guard(mutex);
120 std::cout <<
"speed is " << car->speed() <<
"\n";
123void timer_func_report_speed(
int max_allow_speed,
const Car *car) {
124 std::lock_guard<std::mutex> guard(mutex);
125 std::cout <<
"max allowed is " << max_allow_speed <<
". I am at " << car->speed() <<
"\n";
128void timer_func_stop(
const Car *car,
int hours,
const std::string &msg) {
129 std::lock_guard<std::mutex> guard(mutex);
130 std::cout << msg <<
" after driving for " << hours <<
" hours. My speed was " << car->speed() <<
"\n";
134void test_timer_for_functions(Car *car) {
153 t.
single_shot(33,
static_cast<void (*)(
void)
> (timer_func_start));
156 t.
set_interval(33,
static_cast<void (*)(
void)
> (timer_func_start));
159 t.
set_timeout(33,
static_cast<void (*)(
void)
> (timer_func_start));
167 t.
single_shot(33,
static_cast<void (*)(Car *)
> (timer_func_start), car);
168 t.
single_shot(33, overload<Car*> (timer_func_start), car);
170 t.
set_interval(33,
static_cast<void (*)(Car *)
> (timer_func_start), car);
171 t.
set_interval(33, overload<Car*> (timer_func_start), car);
173 t.
set_timeout(33,
static_cast<void (*)(Car *)
> (timer_func_start), car);
174 t.
set_timeout(33, overload<Car*> (timer_func_start), car);
181 t.
single_shot(33, timer_func_report_speed, 120, car);
183 t.
set_timeout(33, timer_func_report_speed, 120, car);
189 t.
single_shot(333, timer_func_stop, car, 6,
"I have to stop");
190 t.
set_interval(333, timer_func_stop, car, 6,
"I have to stop");
191 t.
set_timeout(333, timer_func_stop, car, 6,
"I have to stop");
197void test_timer_for_lambda_functions(Car *car) {
198 auto lambda_start = []() ->
void {
199 std::lock_guard<std::mutex> guard(mutex);
200 std::cout <<
"started\n";
203 auto lambda_start_1arg = [](Car *car) ->
void {
204 std::lock_guard<std::mutex> guard(mutex);
205 std::cout <<
"speed is " << car->speed() <<
"\n";
208 auto lambda_report_speed = [](
int max_allow_speed,
const Car *car) ->
void {
209 std::lock_guard<std::mutex> guard(mutex);
210 std::cout <<
"max allowed is " << max_allow_speed <<
". I am at " << car->speed() <<
"\n";
213 auto lambda_stop = [](
const Car *car,
int hours,
const std::string &msg) ->
void {
214 std::lock_guard<std::mutex> guard(mutex);
215 std::cout << msg <<
" after driving for " << hours <<
" hours. My speed was " << car->speed() <<
"\n";
256 t.
single_shot(333, lambda_stop, car, 6,
"I have to stop");
257 t.
set_interval(333, lambda_stop, car, 6,
"I have to stop");
258 t.
set_timeout(333, lambda_stop, car, 6,
"I have to stop");
267 std::cout <<
"triggers a class member ------------------------------------------------------------------\n";
268 test_timer_for_members(&car);
269 std::this_thread::sleep_for(std::chrono::seconds(1));
271 std::cout <<
"\ntriggers a function ------------------------------------------------------------------\n";
272 test_timer_for_functions(&car);
273 std::this_thread::sleep_for(std::chrono::seconds(1));
275 std::cout <<
"\ntriggers a lambda function -----------------------------------------------------------\n";
276 test_timer_for_lambda_functions(&car);
277 std::this_thread::sleep_for(std::chrono::seconds(1));
A light-weight implementation of the timer mechanism.
Definition: timer.h:49
void set_interval(int interval, std::function< void(Args...)> const &func, Args... args) const
Executes function func for every interval milliseconds.
Definition: timer.h:268
static void single_shot(int delay, std::function< void(Args...)> const &func, Args... args)
Executes function func after delay milliseconds.
Definition: timer.h:195
void set_timeout(int delay, std::function< void(Args...)> const &func, Args... args) const
Executes function func after delay milliseconds.
Definition: timer.h:227
Definition: collider.cpp:182