topic_priority.h
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #ifndef _TOPIC_PRIORITY_H_
33 #define _TOPIC_PRIORITY_H_
34 
35 #include <ros/ros.h>
36 
44 template <class cmd_type>
46 {
48  typename std::map<int,cmd_type> message_map;
50  typename std::map<int,cmd_type>::iterator it;
51 
52  public:
71  void push_msg(cmd_type msg)
72  {
73  msg->lifetime=ros::Time::now().toSec()+msg->lifetime;
74 
75  it=message_map.find(msg->priority);
76 
77  if(it==message_map.end())
78  message_map[msg->priority]=msg;
79  else
80  {
81  message_map.erase(it);
82  message_map[msg->priority]=msg;
83  }
84  }
85 
92  cmd_type top_msg(void)
93  {
94  maintenance();
95 
96  int max_priority=-1;
97  typename std::map<int,cmd_type>::iterator it_max;
98 
99  for(it=message_map.begin();it!=message_map.end();it++)
100  if((*it).second->priority > max_priority)
101  {
102  max_priority=(*it).second->priority;
103  it_max=it;
104  }
105 
106  if(message_map.size())
107  return (*it_max).second;
108 
109  cmd_type null_ptr;
110  return null_ptr;
111  }
115  private:
121  void maintenance(void)
122  {
123  for(it=message_map.begin();it!=message_map.end();it++)
124  if(ros::Time::now().toSec() > (*it).second->lifetime)
125  message_map.erase(it);
126  }
127 };
128 
129 #endif
Topic priority sorting class.
cmd_type top_msg(void)
Return the current command message.
std::map< int, cmd_type >::iterator it
Iterator for the messages.
void push_msg(cmd_type msg)
Add new command message.
std::map< int, cmd_type > message_map
Map of all the messages received.
void maintenance(void)
Do maintenance on the message map.


topic_priority
Author(s): Jorge Almeida
autogenerated on Mon Mar 2 2015 01:32:54