A Discrete-Event Network Simulator
API
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 
34 namespace ns3 {
35 
53 class TimerImpl;
54 
73 class Timer
74 {
75 public:
81  {
86  CANCEL_ON_DESTROY = (1 << 3),
91  REMOVE_ON_DESTROY = (1 << 4),
96  CHECK_ON_DESTROY = (1 << 5)
97  };
99  enum State
100  {
104  };
109  Timer ();
113  Timer (enum DestroyPolicy destroyPolicy);
114  ~Timer ();
115 
121  template <typename FN>
122  void SetFunction (FN fn);
123 
130  template <typename MEM_PTR, typename OBJ_PTR>
131  void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr);
132 
133 
139  template <typename T1>
140  void SetArguments (T1 a1);
147  template <typename T1, typename T2>
148  void SetArguments (T1 a1, T2 a2);
156  template <typename T1, typename T2, typename T3>
157  void SetArguments (T1 a1, T2 a2, T3 a3);
166  template <typename T1, typename T2, typename T3, typename T4>
167  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4);
177  template <typename T1, typename T2, typename T3, typename T4, typename T5>
178  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
189  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
190  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
191 
197  void SetDelay (const Time &delay);
201  Time GetDelay (void) const;
207  Time GetDelayLeft (void) const;
212  void Cancel (void);
217  void Remove (void);
221  bool IsExpired (void) const;
225  bool IsRunning (void) const;
230  bool IsSuspended (void) const;
234  enum Timer::State GetState (void) const;
239  void Schedule (void);
246  void Schedule (Time delay);
247 
253  void Suspend (void);
259  void Resume (void);
260 
261 private:
264  {
265  TIMER_SUSPENDED = (1 << 7)
266  };
267 
277  int m_flags;
289 };
290 
291 } // namespace ns3
292 
293 
294 /********************************************************************
295  * Implementation of the templates declared above.
296  ********************************************************************/
297 
298 #include "timer-impl.h"
299 
300 namespace ns3 {
301 
302 
303 template <typename FN>
304 void
306 {
307  delete m_impl;
308  m_impl = MakeTimerImpl (fn);
309 }
310 template <typename MEM_PTR, typename OBJ_PTR>
311 void
312 Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr)
313 {
314  delete m_impl;
315  m_impl = MakeTimerImpl (memPtr, objPtr);
316 }
317 
318 template <typename T1>
319 void
321 {
322  if (m_impl == 0)
323  {
324  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
325  return;
326  }
327  m_impl->SetArgs (a1);
328 }
329 template <typename T1, typename T2>
330 void
331 Timer::SetArguments (T1 a1, T2 a2)
332 {
333  if (m_impl == 0)
334  {
335  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
336  return;
337  }
338  m_impl->SetArgs (a1, a2);
339 }
340 
341 template <typename T1, typename T2, typename T3>
342 void
343 Timer::SetArguments (T1 a1, T2 a2, T3 a3)
344 {
345  if (m_impl == 0)
346  {
347  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
348  return;
349  }
350  m_impl->SetArgs (a1, a2, a3);
351 }
352 
353 template <typename T1, typename T2, typename T3, typename T4>
354 void
355 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4)
356 {
357  if (m_impl == 0)
358  {
359  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
360  return;
361  }
362  m_impl->SetArgs (a1, a2, a3, a4);
363 }
364 
365 template <typename T1, typename T2, typename T3, typename T4, typename T5>
366 void
367 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
368 {
369  if (m_impl == 0)
370  {
371  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
372  return;
373  }
374  m_impl->SetArgs (a1, a2, a3, a4, a5);
375 }
376 
377 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
378 void
379 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
380 {
381  if (m_impl == 0)
382  {
383  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
384  return;
385  }
386  m_impl->SetArgs (a1, a2, a3, a4, a5, a6);
387 }
388 
389 } // namespace ns3
390 
391 #endif /* TIMER_H */
NS_FATAL_x macro definitions.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
A simple Timer class.
Definition: timer.h:73
TimerImpl * MakeTimerImpl(FN fn)
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:253
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:35
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:96
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
bool IsRunning(void) const
Definition: timer.cc:127
Time m_delay
The delay configured for this Timer.
Definition: timer.h:279
Timer has already expired.
Definition: timer.h:103
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void Remove(void)
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
void SetFunction(FN fn)
Definition: timer.h:305
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:288
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes...
ns3::IntToType template class.
void SetDelay(const Time &delay)
Definition: timer.cc:75
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:277
InternalSuspended
Internal bit marking the suspended state.
Definition: timer.h:263
enum Timer::State GetState(void) const
Definition: timer.cc:139
void SetArguments(T1 a1)
Definition: timer.h:320
Every class exported by the ns3 library is enclosed in the ns3 namespace.
State
The possible states of the Timer.
Definition: timer.h:99
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:187
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:42
Time GetDelay(void) const
Definition: timer.cc:81
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:91
Timer is currently running.
Definition: timer.h:102
~Timer()
Definition: timer.cc:53
An identifier for simulation events.
Definition: event-id.h:53
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:286
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:281
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
bool IsExpired(void) const
Definition: timer.cc:121
void Suspend(void)
Cancel the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
void SetArgs(T1 a1)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:974
bool IsSuspended(void) const
Definition: timer.cc:133
Time GetDelayLeft(void) const
Definition: timer.cc:87
ns3::TimerImpl declaration and implementation.
This policy cancels the event from the destructor of the Timer to verify that the event has already e...
Definition: timer.h:86
ns3::EventId declarations.
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed...
Definition: timer.h:80