signal.h
Go to the documentation of this file.
00001 #ifndef SIMPLE_SIGNAL_H
00002 #define SIMPLE_SIGNAL_H
00003 
00004 #include <memory>
00005 #include <functional>
00006 #include <vector>
00007 
00008 namespace BT
00009 {
00014 template <typename... CallableArgs>
00015 class Signal
00016 {
00017   public:
00018     using CallableFunction = std::function<void(CallableArgs...)>;
00019     using Subscriber = std::shared_ptr<CallableFunction>;
00020 
00021     void notify(CallableArgs... args)
00022     {
00023         for (size_t i = 0; i < subscribers_.size();)
00024         {
00025             if (auto sub = subscribers_[i].lock())
00026             {
00027                 (*sub)(args...);
00028                 i++;
00029             }
00030             else
00031             {
00032                 subscribers_.erase(subscribers_.begin() + i);
00033             }
00034         }
00035     }
00036 
00037     Subscriber subscribe(CallableFunction func)
00038     {
00039         Subscriber sub = std::make_shared<CallableFunction>(std::move(func));
00040         subscribers_.emplace_back(sub);
00041         return sub;
00042     }
00043 
00044   private:
00045     std::vector<std::weak_ptr<CallableFunction>> subscribers_;
00046 };
00047 }
00048 
00049 #endif   // SIMPLE_SIGNAL_H


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10