46 #include <unordered_map>
50 #include <std_msgs/Time.h>
51 #include <std_msgs/Header.h>
55 namespace cafer_core {
60 template<
typename Msg,
typename DataContainer,
typename DerivedClass>
77 ManagerBase(std::string type, std::string name, std::string description =
"") :
82 std::seed_seq seed = {std::time(0)};
119 static_cast<DerivedClass *
>(
this)->add(*msg);
128 if (type_io ==
ADD) {
139 size_t _container_size;
143 return _container_size;
195 template<
typename Msg,
typename DataContainer>
201 template<
typename Msg>
202 class Manager<Msg, std::unordered_map<u_int32_t, Msg>>
203 :
public ManagerBase<Msg, std::unordered_map<u_int32_t, Msg>, Manager<Msg, std::unordered_map<u_int32_t, Msg>>> {
216 Base::_container_mutex.lock();
217 Base::_data_set.emplace(msg.header.seq, msg);
218 Base::_container_mutex.unlock();
227 std::uniform_int_distribution<> dist(0., Base::_data_set.size() - 1);
228 Base::_container_mutex.lock();
229 auto random_it = std::next(std::begin(Base::_data_set), dist(Base::_gen));
230 Base::_container_mutex.unlock();
231 Msg res = random_it->second;
240 size_t remove(
const u_int32_t& h)
243 Base::_container_mutex.lock();
244 return_val = Base::_data_set.erase(h);
245 Base::_container_mutex.unlock();
256 Base::_container_mutex.lock();
257 return_msg = Base::_data_set.find(
id)->second;
258 Base::_container_mutex.unlock();
264 template<
typename Msg>
265 class Manager<Msg, std::deque<Msg>> :
public ManagerBase<Msg, std::deque<Msg>, Manager<Msg, std::deque<Msg>>> {
280 Base::_container_mutex.lock();
281 Base::_data_set.push_back(msg);
282 Base::_container_mutex.unlock();
294 Base::_container_mutex.lock();
295 msg = Base::_data_set.front();
296 Base::_data_set.pop_front();
297 Base::_container_mutex.unlock();
307 template<
typename Msg>
313 template<
typename Msg>
317 #endif //_MANAGER_HPP
size_t data_size()
Returns the number of elements in the data container.
std::unique_ptr< ros::Publisher > _publisher
std::unique_ptr< ros::Subscriber > _subcriber
subscriber to retrieve data
void disconnect_from_ros()
ros::Subscriber Subscriber
void add_cb(const shared_ptr< Msg > &msg)
The callback function used to process messages from the listened topic.
void add(const Msg &msg)
add a msg to the container of Manager
shared_ptr< ros::NodeHandle > ros_nh
boost::shared_ptr< T > shared_ptr
std::mutex _container_mutex
Mutex to protect the _data_set from concurrent access.
io
The io enum type of traitement to do with incoming data.
Msg search(const u_int32_t &id)
search a precise msg by is identifier
DataContainer _data_set
the data container. All messages are stored in this container.
void add(const Msg &msg)
add a msg to the container of Manager. Add the new message in the back of the queue.
Base class for manager component. A manager component handle ROS messages (like images, features or policies).
void listen_to(const std::string &topic, io type_io=ADD)
Connects to a specific topic and listen to it.
ManagerBase(std::string type, std::string name, std::string description="")
Manager constructor.