yarp-devices
FutureTask.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __FUTURE_TASK_HPP__
4 #define __FUTURE_TASK_HPP__
5 
6 #include <functional>
7 #include <memory>
8 #include <vector>
9 
10 namespace roboticslab
11 {
12 
21 {
22 public:
24  virtual ~FutureTask() = default;
25 
27  template<typename Fn, typename... Args>
28  void add(Fn && fn, Args &&... args)
29  { deferreds.push_back([=](int) { return fn(args...); }); }
30 
32  template<typename T, typename Fn, typename... Args>
33  void add(T * p, Fn && fn, Args &&... args)
34  { deferreds.push_back([=](int) { return std::invoke(fn, p, args...); }); }
35 
37  virtual bool dispatch() = 0;
38 
40  unsigned int size() const
41  { return deferreds.size(); }
42 
44  void clear()
45  { deferreds.clear(); }
46 
47 protected:
48  std::vector<std::function<bool(int)>> deferreds;
49 };
50 
56 {
57 public:
59  virtual ~FutureTaskFactory() = default;
60 
62  virtual std::unique_ptr<FutureTask> createTask() = 0;
63 };
64 
70 {
71 public:
72  std::unique_ptr<FutureTask> createTask() override;
73 };
74 
80 {
81 public:
83  ParallelTaskFactory(int threads);
84 
85  ~ParallelTaskFactory() override;
86 
87  std::unique_ptr<FutureTask> createTask() override;
88 
89 private:
90  class Private;
91  std::unique_ptr<Private> priv;
92 };
93 
94 } // namespace roboticslab
95 
96 #endif // __FUTURE_TASK_HPP__
Definition: FutureTask.cpp:63
Base class for an abstract factory of FutureTask.
Definition: FutureTask.hpp:56
virtual std::unique_ptr< FutureTask > createTask()=0
Create a managed FutureTask instance.
virtual ~FutureTaskFactory()=default
Virtual destructor.
Base class for a deferred task executor.
Definition: FutureTask.hpp:21
virtual bool dispatch()=0
Dispatch the registered callbacks and returns their joint result.
virtual ~FutureTask()=default
Virtual destructor.
void clear()
Clear internal list of deferred callbacks.
Definition: FutureTask.hpp:44
void add(Fn &&fn, Args &&... args)
Register a deferred callback given a free function.
Definition: FutureTask.hpp:28
unsigned int size() const
Get the number of registered deferred callbacks.
Definition: FutureTask.hpp:40
void add(T *p, Fn &&fn, Args &&... args)
Register a deferred callback given a generic class instance.
Definition: FutureTask.hpp:33
Abstract factory of ParallelTask.
Definition: FutureTask.hpp:80
ParallelTaskFactory(int threads)
Constructor, creates a pool of threads.
Definition: FutureTask.cpp:76
std::unique_ptr< FutureTask > createTask() override
Create a managed FutureTask instance.
Definition: FutureTask.cpp:87
Abstract factory of SequentialTask.
Definition: FutureTask.hpp:70
std::unique_ptr< FutureTask > createTask() override
Create a managed FutureTask instance.
Definition: FutureTask.cpp:82
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6