RunningTask.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, 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 ArmarXCore::core
19 * @author Kai Welke (welke at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 #include <ArmarXCore/interface/core/ThreadingIceBase.h>
30 
31 #include <IceUtil/Thread.h>
32 
33 #include <functional>
34 #include <memory>
35 
36 namespace armarx
37 {
38  class ThreadList;
40 
42  : virtual public IceUtil::Thread
43  , virtual protected RunningTaskIceBase
44  {
45  public:
46  using CallbackT = std::function<void()>;
47 
49 
50  RunningTaskBase(std::string const& name);
51 
59  template <typename T>
60  RunningTaskBase(T* parent, void (T::* runningFn)(), const std::string& name = "")
61  : RunningTaskBase(name.empty() ? GetTypeString<T>() : name)
62  {
63  callback = [parent, runningFn]()
64  {
65  return (parent->*runningFn)();
66  };
67  }
68 
72  ~RunningTaskBase() override;
73 
74  void setName(const std::string& name);
75 
76  void setThreadList(ThreadListPtr threadList);
77 
81  void start();
82 
88  void stop(bool waitForJoin = true);
89 
93  void join();
94 
100  bool isRunning() const;
101 
107  bool isFinished() const;
108 
114  bool waitForFinished(int timeoutMS = -1);
115 
121  bool isStopped();
122 
126  void waitForStop();
127 
128  std::string getName() const;
129 
130  private:
131  void run() override;
132 
133  struct Impl;
134  std::unique_ptr<Impl> impl;
135  };
136 
163  template <class T>
164  class ARMARXCORE_IMPORT_EXPORT RunningTask
165  : public RunningTaskBase
166  {
167  public:
173  typedef void (T::*method_type)(void);
174 
179 
187  RunningTask(T* parent, method_type runningFn, const std::string& name = "")
188  : RunningTaskBase(parent, runningFn, name)
189  {
190  }
191  };
192 
193 
194 
195 }
196 
197 
GetTypeString.h
armarx::RunningTask::RunningTask
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.
Definition: RunningTask.h:187
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:255
armarx::RunningTaskBase::callback
CallbackT callback
Definition: RunningTask.h:48
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::RunningTaskBase::CallbackT
std::function< void()> CallbackT
Definition: RunningTask.h:46
armarx::TaskStatus::isRunning
bool isRunning(Status status)
Returns whether the given task status describes a state where a path is planned.
Definition: PlanningUtil.cpp:67
armarx::RunningTaskBase
Definition: RunningTask.h:41
armarx::GetTypeString
std::string GetTypeString(const std::type_info &tinf, bool withoutNamespaceSpecifier=false)
Definition: GetTypeString.h:36
armarx::RunningTaskBase::RunningTaskBase
RunningTaskBase(T *parent, void(T::*runningFn)(), const std::string &name="")
Constructs a running task within the class parent which calls the runningFn in a new thread.
Definition: RunningTask.h:60
IceUtil::Handle
Definition: forward_declarations.h:29
ImportExport.h
ARMARXCORE_IMPORT_EXPORT
#define ARMARXCORE_IMPORT_EXPORT
Definition: ImportExport.h:38
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28