User Manual, Developers Guide and API Documentation

MultipleTimeout.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_EVENTS_MULTIPLETIMEOUT_HPP
00029 #define WNS_EVENTS_MULTIPLETIMEOUT_HPP
00030 
00031 #include <WNS/events/scheduler/IEvent.hpp>
00032 
00033 #include <WNS/simulator/ISimulator.hpp>
00034 #include <WNS/events/scheduler/Interface.hpp>
00035 
00036 #include <WNS/Assure.hpp>
00037 
00038 #include <map>
00039 
00040 namespace wns { namespace events {
00041 
00049     template <typename T>
00050     class MultipleTimeout
00051     {
00052         typedef MultipleTimeout<T> MultipleTimeoutConcretion;
00053         typedef std::map<T, wns::events::scheduler::IEventPtr> EventMap;
00054 
00055         friend class TimeoutEvent;
00056 
00057         class TimeoutEvent
00058         {
00059         public:
00060             TimeoutEvent(MultipleTimeoutConcretion* _dest, const T& _t) :
00061                 dest(_dest),
00062                 t(_t)
00063             {} // TimeoutEvent
00064 
00065             virtual void operator()()
00066             {
00067                 assure(dest->hasTimeoutSet(t), "Invalid MultipleTimeout event.");
00068                 typename EventMap::iterator evIt = dest->events.find(t);
00069                 assure(evIt != dest->events.end(), "Queued event does not exist in MultipleTimeout event list.");
00070                 dest->events.erase(evIt);
00071                 dest->onTimeout(t);
00072             } // execute
00073 
00074 
00075             virtual
00076             ~TimeoutEvent()
00077             {}
00078 
00079         private:
00080             MultipleTimeoutConcretion* dest;
00081             T t;
00082 
00083             friend class MultipleTimeout<T>;
00084         };
00085 
00086     public:
00087         MultipleTimeout() :
00088             events()
00089         {} // MultipleTimeout
00090 
00091         virtual
00092         ~MultipleTimeout()
00093         {
00094             cancelAllTimeouts();
00095         } // ~MultipleTimeout
00096 
00108         void
00109         setTimeout(const T& t, double delay)
00110         {
00111             assure(!hasTimeoutSet(t), "A timer for this object instance has been set already.");
00112             TimeoutEvent toEvent = TimeoutEvent(this, t);
00113             scheduler::IEventPtr ev = wns::simulator::getEventScheduler()->scheduleDelay(toEvent, delay);
00114             events.insert(std::make_pair(t, ev));
00115         } // setTimeout
00116 
00130         void
00131         setNewTimeout(const T& t, double delay)
00132         {
00133             if (hasTimeoutSet(t))
00134                 cancelTimeout(t);
00135 
00136                 setTimeout(t, delay);
00137         } // setNewTimeout
00138 
00142         bool
00143         hasTimeoutSet(const T& t) const
00144         {
00145             return events.find(t) != events.end();
00146         } // hasTimeoutSet
00147 
00151         size_t
00152         numberOfTimeoutsSet() const
00153         {
00154             return events.size();
00155         } // numberOfTimeoutsSet
00156 
00162         void
00163         cancelTimeout(const T& t)
00164         {
00165             assure(hasTimeoutSet(t), "No timer has been set for this object instance.");
00166 
00167             typename EventMap::iterator evIt = events.find(t);
00168             assure(evIt != events.end(), "Event does not exist in MultipleTimeout event list.");
00169             wns::simulator::getEventScheduler()->cancelEvent(evIt->second);
00170             events.erase(evIt);
00171         } // cancelTimeout
00172 
00176         void
00177         cancelAllTimeouts()
00178         {
00179             while(!events.empty())
00180             {
00181                 wns::simulator::getEventScheduler()->cancelEvent(events.begin()->second);
00182                 events.erase(events.begin());
00183             }
00184         } // cancelAllTimeouts
00185 
00192         virtual void
00193         onTimeout(const T& t) = 0;
00194 
00195     private:
00196         EventMap events;
00197     };
00198 
00199 } // events
00200 } // wns
00201 
00202 #endif // WNS_EVENTS_MULTIPLETIMEOUT_HPP
00203 
00204 

Generated on Fri May 25 03:31:38 2012 for openWNS by  doxygen 1.5.5