TaskUtil.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX
19 * @author Mirko Waechter( mirko.waechter at kit dot edu)
20 * @date 2017
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <functional>
27
28#include "PeriodicTask.h"
29#include "RunningTask.h"
30
31namespace armarx
32{
33
34 /**
35 * Usage:
36 \verbatim
37 SimplePeriodicTask<>::pointer_type task = new SimplePeriodicTask<>([&]
38 {
39 ARMARX_INFO << "Hello world from another thread";
40 }, 100);
41 task->start();
42 \endverbatim
43 */
44 template <class Functor = std::function<void(void)>>
45 class SimplePeriodicTask : public PeriodicTask<SimplePeriodicTask<Functor>>
46 {
47 public:
49 int periodMs,
50 bool assureMeanInterval = false,
51 std::string name = "",
52 bool forceSystemTime = true) :
53 PeriodicTask<SimplePeriodicTask<Functor>>(this,
55 periodMs,
56 assureMeanInterval,
57 name,
58 forceSystemTime),
59 f(f)
60 {
61 }
62
63 void
65 {
66 f();
67 }
68
69 Functor f;
70 };
71 template <class... Ts>
72 SimplePeriodicTask(Ts...) -> SimplePeriodicTask<std::function<void(void)>>;
73
74 /**
75 * Usage:
76 \verbatim
77 SimpleRunningTask<>::pointer_type task = new SimpleRunningTask<>([&]
78 {
79 ARMARX_INFO << "Hello world from another thread";
80 });
81 task->start();
82 \endverbatim
83 */
84 template <class Functor = std::function<void(void)>>
85 class SimpleRunningTask : public RunningTask<SimpleRunningTask<Functor>>
86 {
87 public:
88 SimpleRunningTask(Functor f, std::string name = "") :
89 RunningTask<SimpleRunningTask<Functor>>(this, &SimpleRunningTask::runningFn, name = ""),
90 f(f)
91 {
92 }
93
94 void
96 {
97 f();
98 }
99
100 Functor f;
101 };
102 template <class... Ts>
103 SimpleRunningTask(Ts...) -> SimpleRunningTask<std::function<void(void)>>;
104
105} // namespace armarx
PeriodicTask(T *parent, method_type periodicFn, int periodMs, bool assureMeanInterval=false, std::string name="", bool forceSystemTime=true)
Constructs a periodic task within the class parent which calls the peridoicFn in a new thread.
RunningTask(T *parent, method_type runningFn, const std::string &name="")
Constructs a running task within the class parent which calls the runningFn in a new thread.
SimplePeriodicTask(Functor f, int periodMs, bool assureMeanInterval=false, std::string name="", bool forceSystemTime=true)
Definition TaskUtil.h:48
SimpleRunningTask(Functor f, std::string name="")
Definition TaskUtil.h:88
This file offers overloads of toIce() and fromIce() functions for STL container types.
SimplePeriodicTask(Ts...) -> SimplePeriodicTask< std::function< void(void)> >
SimpleRunningTask(Ts...) -> SimpleRunningTask< std::function< void(void)> >