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 ();
114  Timer (enum DestroyPolicy destroyPolicy);
115  ~Timer ();
116 
122  template <typename FN>
123  void SetFunction (FN fn);
124 
132  template <typename MEM_PTR, typename OBJ_PTR>
133  void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr);
134 
135 
141  template <typename T1>
142  void SetArguments (T1 a1);
149  template <typename T1, typename T2>
150  void SetArguments (T1 a1, T2 a2);
158  template <typename T1, typename T2, typename T3>
159  void SetArguments (T1 a1, T2 a2, T3 a3);
168  template <typename T1, typename T2, typename T3, typename T4>
169  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4);
179  template <typename T1, typename T2, typename T3, typename T4, typename T5>
180  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
191  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
192  void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
193 
199  void SetDelay (const Time &delay);
203  Time GetDelay (void) const;
209  Time GetDelayLeft (void) const;
214  void Cancel (void);
219  void Remove (void);
224  bool IsExpired (void) const;
229  bool IsRunning (void) const;
234  bool IsSuspended (void) const;
238  enum Timer::State GetState (void) const;
243  void Schedule (void);
250  void Schedule (Time delay);
251 
257  void Suspend (void);
263  void Resume (void);
264 
265 private:
268  {
269  TIMER_SUSPENDED = (1 << 7)
270  };
271 
281  int m_flags;
293 };
294 
295 } // namespace ns3
296 
297 
298 /********************************************************************
299  * Implementation of the templates declared above.
300  ********************************************************************/
301 
302 #include "timer-impl.h"
303 
304 namespace ns3 {
305 
306 
307 template <typename FN>
308 void
310 {
311  delete m_impl;
312  m_impl = MakeTimerImpl (fn);
313 }
314 template <typename MEM_PTR, typename OBJ_PTR>
315 void
316 Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr)
317 {
318  delete m_impl;
319  m_impl = MakeTimerImpl (memPtr, objPtr);
320 }
321 
322 template <typename T1>
323 void
325 {
326  if (m_impl == 0)
327  {
328  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
329  return;
330  }
331  m_impl->SetArgs (a1);
332 }
333 template <typename T1, typename T2>
334 void
335 Timer::SetArguments (T1 a1, T2 a2)
336 {
337  if (m_impl == 0)
338  {
339  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
340  return;
341  }
342  m_impl->SetArgs (a1, a2);
343 }
344 
345 template <typename T1, typename T2, typename T3>
346 void
347 Timer::SetArguments (T1 a1, T2 a2, T3 a3)
348 {
349  if (m_impl == 0)
350  {
351  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
352  return;
353  }
354  m_impl->SetArgs (a1, a2, a3);
355 }
356 
357 template <typename T1, typename T2, typename T3, typename T4>
358 void
359 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4)
360 {
361  if (m_impl == 0)
362  {
363  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
364  return;
365  }
366  m_impl->SetArgs (a1, a2, a3, a4);
367 }
368 
369 template <typename T1, typename T2, typename T3, typename T4, typename T5>
370 void
371 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
372 {
373  if (m_impl == 0)
374  {
375  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
376  return;
377  }
378  m_impl->SetArgs (a1, a2, a3, a4, a5);
379 }
380 
381 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
382 void
383 Timer::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
384 {
385  if (m_impl == 0)
386  {
387  NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
388  return;
389  }
390  m_impl->SetArgs (a1, a2, a3, a4, a5, a6);
391 }
392 
393 } // namespace ns3
394 
395 #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
Timer is currently running.
Definition: timer.h:102
Time GetDelayLeft(void) const
Definition: timer.cc:87
bool IsSuspended(void) const
Definition: timer.cc:133
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Time m_delay
The delay configured for this Timer.
Definition: timer.h:283
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:96
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:309
InternalSuspended
Internal bit marking the suspended state.
Definition: timer.h:267
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:292
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:281
This policy cancels the event from the destructor of the Timer to verify that the event has already e...
Definition: timer.h:86
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:91
void SetArguments(T1 a1)
Definition: timer.h:324
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:187
State
The possible states of the Timer.
Definition: timer.h:99
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:42
bool IsRunning(void) const
Definition: timer.cc:127
Timer has already expired.
Definition: timer.h:103
~Timer()
Definition: timer.cc:53
An identifier for simulation events.
Definition: event-id.h:53
enum Timer::State GetState(void) const
Definition: timer.cc:139
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:290
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:285
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed...
Definition: timer.h:80
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
Time GetDelay(void) const
Definition: timer.cc:81
ns3::TimerImpl declaration and implementation.
bool IsExpired(void) const
Definition: timer.cc:121
ns3::EventId declarations.