WorkerManager.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <functional> // for std::bind
10 #include <mutex>
11 #include <string>
12 #include <unordered_map>
13 
14 #include "bota_worker/Worker.hpp"
16 
17 namespace bota_worker
18 {
20 {
21 public:
22  WorkerManager();
23  virtual ~WorkerManager();
24 
25  template <class T>
26  inline bool addWorker(const std::string& name, const double timestep, bool (T::*fp)(const WorkerEvent&), T* obj,
27  const int priority = 0, const bool autostart = true)
28  {
29  return addWorker(WorkerOptions(name, timestep, std::bind(fp, obj, std::placeholders::_1), priority), autostart);
30  }
31 
32  template <class T>
33  inline bool addWorker(const std::string& name, const double timestep, bool (T::*cfp)(const WorkerEvent&),
34  void (T::*rfp)(), T* obj, const int priority = 0, const bool autostart = true)
35  {
36  return addWorker(
37  WorkerOptions(name, timestep, std::bind(cfp, obj, std::placeholders::_1), std::bind(rfp, obj), priority),
38  autostart);
39  }
40 
41  inline bool addWorker(const std::string& name, const double timestep, const WorkerCallback& callback,
42  const int priority = 0, const bool autostart = true)
43  {
44  return addWorker(WorkerOptions(name, timestep, callback, priority), autostart);
45  }
46 
47  inline bool addWorker(const std::string& name, const double timestep, const WorkerCallback& callback,
48  const WorkerCallbackFailureReaction& callbackFailureReaction, const int priority = 0,
49  const bool autostart = true)
50  {
51  return addWorker(WorkerOptions(name, timestep, callback, callbackFailureReaction, priority), autostart);
52  }
53 
54  bool addWorker(const WorkerOptions& options, const bool autostart = true);
55 
56  // the addWorker variant below is commented out because it can lead to strange behaviour. E.g. when the user wants to
57  // move a worker from
58  // one workerManager to
59  // another, the old owner may have a unordered_map<std::string, Worker>-entry with an invalid worker.
60  // bool addWorker(Worker&& worker);
61 
62  void startWorker(const std::string& name, const int priority = 0);
63 
64  void startWorkers();
65 
66  bool hasWorker(const std::string& name);
67 
68  void stopWorker(const std::string& name, const bool wait = true);
69 
70  void stopWorkers(const bool wait = true);
71 
72  void cancelWorker(const std::string& name, const bool wait = true);
73 
77  void cancelWorkers(const bool wait = true);
78 
79  void setWorkerTimestep(const std::string& name, const double timeStep);
80 
85 
86 private:
87  std::unordered_map<std::string, Worker> workers_;
88  std::mutex mutexWorkers_;
89 };
90 
91 } // namespace bota_worker
bool hasWorker(const std::string &name)
bool addWorker(const std::string &name, const double timestep, bool(T::*cfp)(const WorkerEvent &), void(T::*rfp)(), T *obj, const int priority=0, const bool autostart=true)
void stopWorker(const std::string &name, const bool wait=true)
bool addWorker(const std::string &name, const double timestep, const WorkerCallback &callback, const int priority=0, const bool autostart=true)
std::function< void(void)> WorkerCallbackFailureReaction
std::function< bool(const WorkerEvent &)> WorkerCallback
bool addWorker(const std::string &name, const double timestep, const WorkerCallback &callback, const WorkerCallbackFailureReaction &callbackFailureReaction, const int priority=0, const bool autostart=true)
void setWorkerTimestep(const std::string &name, const double timeStep)
void stopWorkers(const bool wait=true)
std::unordered_map< std::string, Worker > workers_
void cancelWorkers(const bool wait=true)
void startWorker(const std::string &name, const int priority=0)
bool addWorker(const std::string &name, const double timestep, bool(T::*fp)(const WorkerEvent &), T *obj, const int priority=0, const bool autostart=true)
void cancelWorker(const std::string &name, const bool wait=true)


bota_worker
Author(s):
autogenerated on Wed Mar 3 2021 03:09:10