00001 /************************************************************************************************** 00002 Software License Agreement (BSD License) 00003 00004 Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt 00005 All rights reserved. 00006 00007 Redistribution and use in source and binary forms, with or without modification, are permitted 00008 provided that the following conditions are met: 00009 00010 *Redistributions of source code must retain the above copyright notice, this list of 00011 conditions and the following disclaimer. 00012 *Redistributions in binary form must reproduce the above copyright notice, this list of 00013 conditions and the following disclaimer in the documentation and/or other materials provided 00014 with the distribution. 00015 *Neither the name of the University of Aveiro nor the names of its contributors may be used to 00016 endorse or promote products derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 00019 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00021 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00024 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00025 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 ***************************************************************************************************/ 00032 #ifndef _TOPIC_PRIORITY_H_ 00033 #define _TOPIC_PRIORITY_H_ 00034 00035 #include <ros/ros.h> 00036 00044 template <class cmd_type> 00045 class TopicQueuePriority 00046 { 00048 typename std::map<int,cmd_type> message_map; 00050 typename std::map<int,cmd_type>::iterator it; 00051 00052 public: 00071 void push_msg(cmd_type msg) 00072 { 00073 msg->lifetime=ros::Time::now().toSec()+msg->lifetime; 00074 00075 it=message_map.find(msg->priority); 00076 00077 if(it==message_map.end()) 00078 message_map[msg->priority]=msg; 00079 else 00080 { 00081 message_map.erase(it); 00082 message_map[msg->priority]=msg; 00083 } 00084 } 00085 00092 cmd_type top_msg(void) 00093 { 00094 maintenance(); 00095 00096 int max_priority=-1; 00097 typename std::map<int,cmd_type>::iterator it_max; 00098 00099 for(it=message_map.begin();it!=message_map.end();it++) 00100 if((*it).second->priority > max_priority) 00101 { 00102 max_priority=(*it).second->priority; 00103 it_max=it; 00104 } 00105 00106 if(message_map.size()) 00107 return (*it_max).second; 00108 00109 cmd_type null_ptr; 00110 return null_ptr; 00111 } 00115 private: 00121 void maintenance(void) 00122 { 00123 for(it=message_map.begin();it!=message_map.end();it++) 00124 if(ros::Time::now().toSec() > (*it).second->lifetime) 00125 message_map.erase(it); 00126 } 00127 }; 00128 00129 #endif