A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
timer.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef TIMER_H
21 #define TIMER_H
22 
23 #include "fatal-error.h"
24 #include "nstime.h"
25 #include "event-id.h"
26 #include "int-to-type.h"
27 
28 namespace ns3 {
29 
30 class TimerImpl;
31 
45 class Timer
46 {
47 public:
53  {
58  CANCEL_ON_DESTROY = (1 << 3),
63  REMOVE_ON_DESTROY = (1 << 4),
68  CHECK_ON_DESTROY = (1 << 5)
69  };
70  enum State
71  {
75  };
80  Timer ();
84  Timer (enum DestroyPolicy destroyPolicy);
85  ~Timer ();
86 
92  template <typename FN>
93  void SetFunction (FN fn);
94 
101  template <typename MEM_PTR, typename OBJ_PTR>
102  void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr);
103 
104 
110  template <typename T1>
111  void SetArguments (T1 a1);
118  template <typename T1, typename T2>
119  void SetArguments (T1 a1, T2 a2);
127  template <typename T1, typename T2, typename T3>
128  void SetArguments (T1 a1, T2 a2, T3 a3);
137  template <typename T1, typename T2, typename T3, typename T4>
138  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4);
148  template <typename T1, typename T2, typename T3, typename T4, typename T5>
149  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
160  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
161  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
162 
168  void SetDelay (const Time &delay);
172  Time GetDelay (void) const;
178  Time GetDelayLeft (void) const;
183  void Cancel (void);
188  void Remove (void);
192  bool IsExpired (void) const;
196  bool IsRunning (void) const;
201  bool IsSuspended (void) const;
205  enum Timer::State GetState (void) const;
210  void Schedule (void);
217  void Schedule (Time delay);
218 
224  void Suspend (void);
230  void Resume (void);
231 
232 private:
233  enum
234  {
235  TIMER_SUSPENDED = (1 << 7)
236  };
237 
238  int m_flags;
243 };
244 
245 } // namespace ns3
246 
247 #include "timer-impl.h"
248 
249 namespace ns3 {
250 
251 
252 template <typename FN>
253 void
255 {
256  delete m_impl;
257  m_impl = MakeTimerImpl (fn);
258 }
259 template <typename MEM_PTR, typename OBJ_PTR>
260 void
261 Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr)
262 {
263  delete m_impl;
264  m_impl = MakeTimerImpl (memPtr, objPtr);
265 }
266 
267 template <typename T1>
268 void
270 {
271  if (m_impl == 0)
272  {
273  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
274  return;
275  }
276  m_impl->SetArgs (a1);
277 }
278 template <typename T1, typename T2>
279 void
280 Timer::SetArguments (T1 a1, T2 a2)
281 {
282  if (m_impl == 0)
283  {
284  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
285  return;
286  }
287  m_impl->SetArgs (a1, a2);
288 }
289 
290 template <typename T1, typename T2, typename T3>
291 void
292 Timer::SetArguments (T1 a1, T2 a2, T3 a3)
293 {
294  if (m_impl == 0)
295  {
296  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
297  return;
298  }
299  m_impl->SetArgs (a1, a2, a3);
300 }
301 
302 template <typename T1, typename T2, typename T3, typename T4>
303 void
304 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4)
305 {
306  if (m_impl == 0)
307  {
308  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
309  return;
310  }
311  m_impl->SetArgs (a1, a2, a3, a4);
312 }
313 
314 template <typename T1, typename T2, typename T3, typename T4, typename T5>
315 void
316 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
317 {
318  if (m_impl == 0)
319  {
320  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
321  return;
322  }
323  m_impl->SetArgs (a1, a2, a3, a4, a5);
324 }
325 
326 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
327 void
328 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
329 {
330  if (m_impl == 0)
331  {
332  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
333  return;
334  }
335  m_impl->SetArgs (a1, a2, a3, a4, a5, a6);
336 }
337 
338 } // namespace ns3
339 
340 #endif /* TIMER_H */
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
a simple Timer class
Definition: timer.h:45
Timer()
create a timer with a default event lifetime management policy:
Definition: timer.cc:29
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
bool IsRunning(void) const
Definition: timer.cc:121
Time m_delay
Definition: timer.h:239
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:68
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:152
void Remove(void)
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:109
void SetFunction(FN fn)
Definition: timer.h:254
Time m_delayLeft
Definition: timer.h:242
void SetDelay(const Time &delay)
Definition: timer.cc:69
int m_flags
Definition: timer.h:238
enum Timer::State GetState(void) const
Definition: timer.cc:133
This policy cancels the event from the destructor of the Timer to verify that the event has already e...
Definition: timer.h:58
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:63
void SetArguments(T1 a1)
Definition: timer.h:269
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:181
Time GetDelay(void) const
Definition: timer.cc:75
~Timer()
Definition: timer.cc:47
an identifier for simulation events.
Definition: event-id.h:46
TimerImpl * m_impl
Definition: timer.h:241
EventId m_event
Definition: timer.h:240
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:103
TimerImpl * MakeTimerImpl(FN fn)
Definition: timer-impl.h:100
DestroyPolicy
The policy to use to manager the internal timer when and instance of the Timer class is destroyed...
Definition: timer.h:52
bool IsExpired(void) const
Definition: timer.cc:115
void Suspend(void)
Cancel the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:171
void SetArgs(T1 a1)
Definition: timer-impl.h:725
bool IsSuspended(void) const
Definition: timer.cc:127
Time GetDelayLeft(void) const
Definition: timer.cc:81