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
33namespace ns3
34{
35
53class TimerImpl;
54
73class Timer
74{
75 public:
87 {
104 CHECK_ON_DESTROY = (1 << 5)
105 };
106
108 enum State
109 {
113 };
114
119 Timer();
124 Timer(enum 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 enum Timer::State GetState() const;
206 void Schedule();
213 void Schedule(Time delay);
214
227 void Suspend();
233 void Resume();
234
235 private:
238 {
239 TIMER_SUSPENDED = (1 << 7)
240 };
241
263};
264
265} // namespace ns3
266
267/********************************************************************
268 * Implementation of the templates declared above.
269 ********************************************************************/
270
271#include "timer-impl.h"
272
273namespace ns3
274{
275
276template <typename FN>
277void
279{
280 delete m_impl;
281 m_impl = MakeTimerImpl(fn);
282}
283
284template <typename MEM_PTR, typename OBJ_PTR>
285void
286Timer::SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr)
287{
288 delete m_impl;
289 m_impl = MakeTimerImpl(memPtr, objPtr);
290}
291
292template <typename... Ts>
293void
295{
296 if (m_impl == nullptr)
297 {
298 NS_FATAL_ERROR("You cannot set the arguments of a Timer before setting its function.");
299 return;
300 }
301 m_impl->SetArgs(args...);
302}
303
304} // namespace ns3
305
306#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:278
~Timer()
Definition: timer.cc:54
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:260
bool IsExpired() const
Definition: timer.cc:126
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:255
Time GetDelayLeft() const
Definition: timer.cc:90
InternalSuspended
Internal bit marking the suspended state.
Definition: timer.h:238
@ TIMER_SUSPENDED
Definition: timer.h:239
void SetArguments(Ts... args)
Definition: timer.h:294
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:251
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
enum Timer::State GetState() const
Definition: timer.cc:147
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
void Cancel()
Cancel the currently-running event if there is one.
Definition: timer.cc:112
Time GetDelay() const
Definition: timer.cc:83
bool IsSuspended() const
Definition: timer.cc:140
Time m_delay
The delay configured for this Timer.
Definition: timer.h:253
void Remove()
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:119
void Schedule()
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:166
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:262
void Resume()
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:202
bool IsRunning() const
Definition: timer.cc:133
void Suspend()
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:185
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:160
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.