00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2007 INESC Porto 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Gustavo J. A. M. Carneiro <gjc@inescporto.pt> 00019 */ 00020 #ifndef EVENT_GARBAGE_COLLECTOR_H 00021 #define EVENT_GARBAGE_COLLECTOR_H 00022 00023 #include <set> 00024 #include "ns3/event-id.h" 00025 #include "ns3/simulator.h" 00026 00027 namespace ns3 { 00028 00029 /** 00030 * \brief An object that tracks scheduled events and automatically 00031 * cancels them when it is destroyed. It is useful in situations 00032 * where multiple instances of the same type of event can 00033 * simultaneously be scheduled, and when the events should be limited 00034 * to the lifetime of a container object. 00035 */ 00036 class EventGarbageCollector 00037 { 00038 public: 00039 00040 EventGarbageCollector (); 00041 00042 /** 00043 * \brief Tracks a new event 00044 */ 00045 void Track (EventId event); 00046 00047 ~EventGarbageCollector (); 00048 00049 private: 00050 00051 struct EventIdLessThanTs 00052 { 00053 bool operator () (const EventId &a, const EventId &b) const 00054 { 00055 return (a.GetTs () < b.GetTs ()); 00056 } 00057 }; 00058 00059 typedef std::multiset<EventId, EventIdLessThanTs> EventList; 00060 00061 EventList::size_type m_nextCleanupSize; 00062 EventList m_events; 00063 00064 void Cleanup (); 00065 void Grow (); 00066 void Shrink (); 00067 }; 00068 00069 }; // namespace ns3 00070 00071 #endif /* EVENT_GARBAGE_COLLECTOR_H */