A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
event-id.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef EVENT_ID_H
9#define EVENT_ID_H
10
11#include "event-impl.h"
12#include "nstime.h"
13#include "ptr.h"
14
15#include <stdint.h>
16
17/**
18 * @file
19 * @ingroup events
20 * ns3::EventId declarations.
21 */
22
23namespace ns3
24{
25
26class EventImpl;
27
28/**
29 * @ingroup events
30 * @brief An identifier for simulation events.
31 *
32 * Each EventId identifies a unique event scheduled with one
33 * of the many Simulator::Schedule() methods. This EventId can
34 * be used to cancel or remove events after they are scheduled
35 * with Cancel(), Remove(), or Simulator::Cancel() or Simulator::Remove().
36 *
37 * The important thing to remember about this class is that
38 * every variable of this type is _always_ in a valid state,
39 * even when it has not been assigned an EventId coming from a
40 * Simulator::Schedule() method: calling Simulator::Cancel(), IsPending(),
41 * IsExpired() or passing around instances of this object
42 * will not result in crashes or memory leaks.
43 */
45{
46 public:
47 /** Special values of the event UID. */
48 enum UID
49 {
50 /** Invalid UID value. */
52 /** ScheduleNow() events. */
53 NOW = 1,
54 /** ScheduleDestroy() events. */
56 /** Reserved UID. */
58 /** Schedule(), etc. events. */
60 };
61
62 /** Default constructor. This EventId does nothing. */
63 EventId();
64 /**
65 * Construct a real event.
66 *
67 * @param [in] impl The implementation of this event.
68 * @param [in] ts The virtual time stamp this event should occur.
69 * @param [in] context The execution context for this event.
70 * @param [in] uid The unique id for this EventId.
71 */
72 EventId(const Ptr<EventImpl>& impl, uint64_t ts, uint32_t context, uint32_t uid);
73 /**
74 * This method is syntactic sugar for the ns3::Simulator::Cancel
75 * method.
76 */
77 void Cancel();
78 /**
79 * This method is syntactic sugar for the ns3::Simulator::Remove
80 * method.
81 */
82 void Remove();
83 /**
84 * This method is syntactic sugar for the ns3::Simulator::IsExpired
85 * method.
86 * @returns \c true if the event has expired, \c false otherwise.
87 */
88 bool IsExpired() const;
89 /**
90 * This method is syntactic sugar for !IsExpired().
91 *
92 * @returns \c true if the event has not expired, \c false otherwise.
93 */
94 bool IsPending() const;
95
96 public:
97 /**
98 * @name Scheduler Helpers.
99 * @brief These methods are normally invoked only by
100 * subclasses of the Scheduler base class.
101 */
102 /**@{*/
103 /** @return The underlying EventImpl pointer. */
104 EventImpl* PeekEventImpl() const;
105 /** @return The virtual time stamp. */
106 uint64_t GetTs() const;
107 /** @return The event context. */
108 uint32_t GetContext() const;
109 /** @return The unique id. */
110 uint32_t GetUid() const;
111 /**@}*/
112
113 /**
114 * @name Comparison operators
115 * @{
116 */
117
118 /**
119 * Test if two EventId's are equal.
120 * @param [in] a The first EventId.
121 * @param [in] b The second EventId.
122 * @return \c true if the \pname{a} and \pname{b} represent the same event.
123 */
124 friend bool operator==(const EventId& a, const EventId& b);
125
126 /**
127 * Less than operator for two EventId's, based on time stamps.
128 * @param [in] a The first EventId.
129 * @param [in] b The second EventId.
130 * @return \c true if \pname{a} occurs before \pname{b}.
131 */
132 friend bool operator<(const EventId& a, const EventId& b);
133
134 /**
135 * Compare a Time to an EventId.
136 *
137 * This is useful when you have cached a previously scheduled event:
138 *
139 * m_event = Schedule (...);
140 *
141 * and later you want to know the relationship between that event
142 * and some other Time `when`:
143 *
144 * if (when < m_event) ...
145 *
146 * @param [in] time The Time operand.
147 * @param [in] event The EventId
148 * @returns \c true if \p time is before (less than) the
149 * time stamp of the EventId.
150 */
151 friend bool operator<(const Time& time, const EventId& event);
152
153 /**@}*/ // Comparison operators
154
155 private:
156 Ptr<EventImpl> m_eventImpl; /**< The underlying event implementation. */
157 uint64_t m_ts; /**< The virtual time stamp. */
158 uint32_t m_context; /**< The context. */
159 uint32_t m_uid; /**< The unique id. */
160};
161
162/*************************************************
163 ** Inline implementations
164 ************************************************/
165
166inline bool
167operator==(const EventId& a, const EventId& b)
168{
169 return a.m_uid == b.m_uid && a.m_context == b.m_context && a.m_ts == b.m_ts &&
171}
172
173inline bool
174operator<(const EventId& a, const EventId& b)
175{
176 return (a.GetTs() < b.GetTs());
177}
178
179inline bool
180operator<(const Time& time, const EventId& event)
181{
182 // Negative Time is less than any possible EventId, which are all >= 0.
183 if (time.IsStrictlyNegative())
184 {
185 return true;
186 }
187 // Time must be >= 0 so casting to unsigned is safe.
188 return static_cast<uint64_t>(time.GetTimeStep()) < event.GetTs();
189}
190} // namespace ns3
191
192#endif /* EVENT_ID_H */
An identifier for simulation events.
Definition event-id.h:45
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
uint32_t GetUid() const
Definition event-id.cc:93
UID
Special values of the event UID.
Definition event-id.h:49
@ INVALID
Invalid UID value.
Definition event-id.h:51
@ RESERVED
Reserved UID.
Definition event-id.h:57
@ VALID
Schedule(), etc.
Definition event-id.h:59
@ DESTROY
ScheduleDestroy() events.
Definition event-id.h:55
@ NOW
ScheduleNow() events.
Definition event-id.h:53
bool IsPending() const
This method is syntactic sugar for !IsExpired().
Definition event-id.cc:65
friend bool operator<(const EventId &a, const EventId &b)
Less than operator for two EventId's, based on time stamps.
Definition event-id.h:174
EventImpl * PeekEventImpl() const
Definition event-id.cc:72
friend bool operator==(const EventId &a, const EventId &b)
Test if two EventId's are equal.
Definition event-id.h:167
bool IsExpired() const
This method is syntactic sugar for the ns3::Simulator::IsExpired method.
Definition event-id.cc:58
uint64_t m_ts
The virtual time stamp.
Definition event-id.h:157
uint32_t m_uid
The unique id.
Definition event-id.h:159
void Remove()
This method is syntactic sugar for the ns3::Simulator::Remove method.
Definition event-id.cc:51
uint32_t GetContext() const
Definition event-id.cc:86
uint64_t GetTs() const
Definition event-id.cc:79
EventId()
Default constructor.
Definition event-id.cc:25
uint32_t m_context
The context.
Definition event-id.h:158
Ptr< EventImpl > m_eventImpl
The underlying event implementation.
Definition event-id.h:156
A simulation event.
Definition event-impl.h:35
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
bool IsStrictlyNegative() const
Exactly equivalent to t < 0.
Definition nstime.h:332
int64_t GetTimeStep() const
Get the raw time value, in the current resolution unit.
Definition nstime.h:440
ns3::EventImpl declarations.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:167
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:174
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::Ptr smart pointer declaration and implementation.