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 } // namespace BT
48 
49 #endif // SIMPLE_SIGNAL_H
BT
Definition: ex01_wrap_legacy.cpp:29
BT::Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus >::Subscriber
std::shared_ptr< CallableFunction > Subscriber
Definition: signal.h:19
BT::Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus >::CallableFunction
std::function< void(CallableArgs...)> CallableFunction
Definition: signal.h:18
detail::void
j template void())
Definition: json.hpp:4893
BT::Signal::subscribe
Subscriber subscribe(CallableFunction func)
Definition: signal.h:37
BT::Signal::notify
void notify(CallableArgs... args)
Definition: signal.h:21
BT::Signal::subscribers_
std::vector< std::weak_ptr< CallableFunction > > subscribers_
Definition: signal.h:45
BT::Signal
Definition: signal.h:15


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17