A Discrete-Event Network Simulator
API
timer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef TIMER_H
20 #define TIMER_H
21 
22 #include "event-id.h"
23 #include "fatal-error.h"
24 #include "int-to-type.h"
25 #include "nstime.h"
26 
33 namespace ns3
34 {
35 
53 class TimerImpl;
54 
73 class Timer
74 {
75  public:
87  {
93  CANCEL_ON_DESTROY = (1 << 3),
99  REMOVE_ON_DESTROY = (1 << 4),
104  CHECK_ON_DESTROY = (1 << 5)
105  };
106 
108  enum State
109  {
113  };
114 
119  Timer();
124  Timer(DestroyPolicy destroyPolicy);
125  ~Timer();
126 
133  template <typename FN>
134  void SetFunction(FN fn);
135 
145  template <typename MEM_PTR, typename OBJ_PTR>
146  void SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr);
147 
154  template <typename... Ts>
155  void SetArguments(Ts... args);
156 
162  void SetDelay(const Time& delay);
166  Time GetDelay() const;
172  Time GetDelayLeft() const;
177  void Cancel();
182  void Remove();
187  bool IsExpired() const;
192  bool IsRunning() const;
197  bool IsSuspended() const;
201  Timer::State GetState() const;
206  void Schedule();
213  void Schedule(Time delay);
214 
227  void Suspend();
233  void Resume();
234 
235  private:
237  static constexpr auto TIMER_SUSPENDED{1 << 7};
238 
248  int m_flags;
260 };
261 
262 } // namespace ns3
263 
264 /********************************************************************
265  * Implementation of the templates declared above.
266  ********************************************************************/
267 
268 #include "timer-impl.h"
269 
270 namespace ns3
271 {
272 
273 template <typename FN>
274 void
276 {
277  delete m_impl;
278  m_impl = MakeTimerImpl(fn);
279 }
280 
281 template <typename MEM_PTR, typename OBJ_PTR>
282 void
283 Timer::SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr)
284 {
285  delete m_impl;
286  m_impl = MakeTimerImpl(memPtr, objPtr);
287 }
288 
289 template <typename... Ts>
290 void
292 {
293  if (m_impl == nullptr)
294  {
295  NS_FATAL_ERROR("You cannot set the arguments of a Timer before setting its function.");
296  return;
297  }
298  m_impl->SetArgs(args...);
299 }
300 
301 } // namespace ns3
302 
303 #endif /* TIMER_H */
An identifier for simulation events.
Definition: event-id.h:55
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A simple virtual Timer class.
Definition: timer.h:74
void SetDelay(const Time &delay)
Definition: timer.cc:76
void SetFunction(FN fn)
Definition: timer.h:275
~Timer()
Definition: timer.cc:54
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:257
bool IsExpired() const
Definition: timer.cc:122
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:36
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:252
Time GetDelayLeft() const
Definition: timer.cc:90
Timer::State GetState() const
Definition: timer.cc:143
void SetArguments(Ts... args)
Definition: timer.h:291
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:248
DestroyPolicy
The policy to use to manager the internal timer when an instance of the Timer class is destroyed or s...
Definition: timer.h:87
@ CANCEL_ON_DESTROY
This policy cancels the event from the destructor of the Timer or from Suspend().
Definition: timer.h:93
@ CHECK_ON_DESTROY
This policy enforces a check from the destructor of the Timer to verify that the timer has already ex...
Definition: timer.h:104
@ REMOVE_ON_DESTROY
This policy removes the event from the simulation event list when the destructor of the Timer is invo...
Definition: timer.h:99
State
The possible states of the Timer.
Definition: timer.h:109
@ RUNNING
Timer is currently running.
Definition: timer.h:110
@ EXPIRED
Timer has already expired.
Definition: timer.h:111
@ SUSPENDED
Timer is suspended.
Definition: timer.h:112
static constexpr auto TIMER_SUSPENDED
Internal bit marking the suspended timer state.
Definition: timer.h:237
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:108
Time GetDelay() const
Definition: timer.cc:83
bool IsSuspended() const
Definition: timer.cc:136
Time m_delay
The delay configured for this Timer.
Definition: timer.h:250
void Remove()
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:162
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:259
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:198
bool IsRunning() const
Definition: timer.cc:129
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:181
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:43
void SetArgs(T1 a1)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:1085
ns3::EventId declarations.
NS_FATAL_x macro definitions.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
TimerImpl * MakeTimerImpl(FN fn)
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:254
ns3::IntToType template class.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::TimerImpl declaration and implementation.