signal.h
Go to the documentation of this file.
1 #ifndef SIMPLE_SIGNAL_H
2 #define SIMPLE_SIGNAL_H
3 
4 #include <memory>
5 #include <functional>
6 #include <vector>
7 
8 namespace BT
9 {
14 template <typename... CallableArgs>
15 class Signal
16 {
17  public:
18  using CallableFunction = std::function<void(CallableArgs...)>;
19  using Subscriber = std::shared_ptr<CallableFunction>;
20 
21  void notify(CallableArgs... args)
22  {
23  for (size_t i = 0; i < subscribers_.size();)
24  {
25  if (auto sub = subscribers_[i].lock())
26  {
27  (*sub)(args...);
28  i++;
29  }
30  else
31  {
32  subscribers_.erase(subscribers_.begin() + i);
33  }
34  }
35  }
36 
38  {
39  Subscriber sub = std::make_shared<CallableFunction>(std::move(func));
40  subscribers_.emplace_back(sub);
41  return sub;
42  }
43 
44  private:
45  std::vector<std::weak_ptr<CallableFunction>> subscribers_;
46 };
47 }
48 
49 #endif // SIMPLE_SIGNAL_H
std::function< void(CallableArgs...)> CallableFunction
Definition: signal.h:18
std::vector< std::weak_ptr< CallableFunction > > subscribers_
Definition: signal.h:45
void notify(CallableArgs...args)
Definition: signal.h:21
Subscriber subscribe(CallableFunction func)
Definition: signal.h:37


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32