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
34namespace ns3 {
35
53class TimerImpl;
54
73class Timer
74{
75public:
87 {
104 CHECK_ON_DESTROY = (1 << 5)
105 };
107 enum State
108 {
112 };
117 Timer ();
122 Timer (enum DestroyPolicy destroyPolicy);
123 ~Timer ();
124
131 template <typename FN>
132 void SetFunction (FN fn);
133
143 template <typename MEM_PTR, typename OBJ_PTR>
144 void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr);
145
146
153 template <typename... Ts>
154 void SetArguments (Ts... args);
155
161 void SetDelay (const Time &delay);
165 Time GetDelay (void) const;
171 Time GetDelayLeft (void) const;
176 void Cancel (void);
181 void Remove (void);
186 bool IsExpired (void) const;
191 bool IsRunning (void) const;
196 bool IsSuspended (void) const;
200 enum Timer::State GetState (void) const;
205 void Schedule (void);
212 void Schedule (Time delay);
213
226 void Suspend (void);
232 void Resume (void);
233
234private:
237 {
238 TIMER_SUSPENDED = (1 << 7)
239 };
240
262};
263
264} // namespace ns3
265
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}
283template <typename MEM_PTR, typename OBJ_PTR>
284void
285Timer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr)
286{
287 delete m_impl;
288 m_impl = MakeTimerImpl (memPtr, objPtr);
289}
290
291template <typename... Ts>
292void
294{
295 if (m_impl == 0)
296 {
297 NS_FATAL_ERROR ("You cannot set the arguments of a Timer before setting its function.");
298 return;
299 }
300 m_impl->SetArgs (args...);
301}
302
303} // namespace ns3
304
305#endif /* TIMER_H */
An identifier for simulation events.
Definition: event-id.h:54
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
A simple virtual Timer class.
Definition: timer.h:74
void SetDelay(const Time &delay)
Definition: timer.cc:75
void SetFunction(FN fn)
Definition: timer.h:278
void Suspend(void)
Pause the timer and save the amount of time left until it was set to expire.
Definition: timer.cc:177
bool IsExpired(void) const
Definition: timer.cc:121
void Remove(void)
Remove from the simulation event-list the currently-running event if there is one.
Definition: timer.cc:115
~Timer()
Definition: timer.cc:53
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: timer.h:259
Timer()
Create a timer with a default event lifetime management policy:
Definition: timer.cc:35
EventId m_event
The future event scheduled to expire the timer.
Definition: timer.h:254
bool IsRunning(void) const
Definition: timer.cc:127
InternalSuspended
Internal bit marking the suspended state.
Definition: timer.h:237
@ TIMER_SUSPENDED
Definition: timer.h:238
void SetArguments(Ts... args)
Definition: timer.h:293
int m_flags
Bitfield for Timer State, DestroyPolicy and InternalSuspended.
Definition: timer.h:250
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:108
@ RUNNING
Timer is currently running.
Definition: timer.h:109
@ EXPIRED
Timer has already expired.
Definition: timer.h:110
@ SUSPENDED
Timer is suspended.
Definition: timer.h:111
enum Timer::State GetState(void) const
Definition: timer.cc:139
Time GetDelayLeft(void) const
Definition: timer.cc:87
void Resume(void)
Restart the timer to expire within the amount of time left saved during Suspend.
Definition: timer.cc:194
void Schedule(void)
Schedule a new event using the currently-configured delay, function, and arguments.
Definition: timer.cc:158
void Cancel(void)
Cancel the currently-running event if there is one.
Definition: timer.cc:109
Time GetDelay(void) const
Definition: timer.cc:81
Time m_delay
The delay configured for this Timer.
Definition: timer.h:252
Time m_delayLeft
The amount of time left on the Timer while it is suspended.
Definition: timer.h:261
bool IsSuspended(void) const
Definition: timer.cc:133
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:959
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:165
TimerImpl * MakeTimerImpl(FN fn)
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:252
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.