A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
node-list.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors:
7 * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>,
8 */
9
10#include "node-list.h"
11
12#include "node.h"
13
14#include "ns3/assert.h"
15#include "ns3/config.h"
16#include "ns3/log.h"
17#include "ns3/object-vector.h"
18#include "ns3/simulator.h"
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("NodeList");
24
25/**
26 * \ingroup network
27 * \brief private implementation detail of the NodeList API.
28 */
29class NodeListPriv : public Object
30{
31 public:
32 /**
33 * \brief Get the type ID.
34 * \return the object TypeId
35 */
36 static TypeId GetTypeId();
38 ~NodeListPriv() override;
39
40 /**
41 * \param node node to add
42 * \returns index of node in list.
43 *
44 * This method is called automatically from Node::Node so
45 * the user has little reason to call it himself.
46 */
48
49 /**
50 * \returns a C++ iterator located at the beginning of this
51 * list.
52 */
54
55 /**
56 * \returns a C++ iterator located at the end of this
57 * list.
58 */
59 NodeList::Iterator End() const;
60
61 /**
62 * \param n index of requested node.
63 * \returns the Node associated to index n.
64 */
66
67 /**
68 * \returns the number of nodes currently in the list.
69 */
71
72 /**
73 * \brief Get the node list object
74 * \returns the node list
75 */
76 static Ptr<NodeListPriv> Get();
77
78 private:
79 /**
80 * \brief Get the node list object
81 * \returns the node list
82 */
83 static Ptr<NodeListPriv>* DoGet();
84
85 /**
86 * \brief Delete the nodes list object
87 */
88 static void Delete();
89
90 /**
91 * \brief Dispose the nodes in the list
92 */
93 void DoDispose() override;
94
95 std::vector<Ptr<Node>> m_nodes; //!< node objects container
96};
97
99
100TypeId
102{
103 static TypeId tid = TypeId("ns3::NodeListPriv")
104 .SetParent<Object>()
105 .SetGroupName("Network")
106 .AddAttribute("NodeList",
107 "The list of all nodes created during the simulation.",
111 return tid;
112}
113
116{
118 return *DoGet();
119}
120
123{
125 static Ptr<NodeListPriv> ptr = nullptr;
126 if (!ptr)
127 {
131 }
132 return &ptr;
133}
134
135void
142
147
152
153void
155{
156 NS_LOG_FUNCTION(this);
157 for (auto i = m_nodes.begin(); i != m_nodes.end(); i++)
158 {
159 Ptr<Node> node = *i;
160 node->Dispose();
161 *i = nullptr;
162 }
163 m_nodes.erase(m_nodes.begin(), m_nodes.end());
165}
166
169{
170 NS_LOG_FUNCTION(this << node);
171 uint32_t index = m_nodes.size();
172 m_nodes.push_back(node);
173 Simulator::ScheduleWithContext(index, TimeStep(0), &Node::Initialize, node);
174 return index;
175}
176
179{
180 NS_LOG_FUNCTION(this);
181 return m_nodes.begin();
182}
183
186{
187 NS_LOG_FUNCTION(this);
188 return m_nodes.end();
189}
190
193{
194 NS_LOG_FUNCTION(this);
195 return m_nodes.size();
196}
197
200{
201 NS_LOG_FUNCTION(this << n);
202 NS_ASSERT_MSG(n < m_nodes.size(),
203 "Node index " << n << " is out of range (only have " << m_nodes.size()
204 << " nodes).");
205 return m_nodes[n];
206}
207
208} // namespace ns3
209
210/**
211 * The implementation of the public static-based API
212 * which calls into the private implementation through
213 * the simulation singleton.
214 */
215namespace ns3
216{
217
220{
221 NS_LOG_FUNCTION(node);
222 return NodeListPriv::Get()->Add(node);
223}
224
227{
229 return NodeListPriv::Get()->Begin();
230}
231
234{
236 return NodeListPriv::Get()->End();
237}
238
241{
243 return NodeListPriv::Get()->GetNode(n);
244}
245
248{
250 return NodeListPriv::Get()->GetNNodes();
251}
252
253} // namespace ns3
static Iterator Begin()
Definition node-list.cc:226
static uint32_t GetNNodes()
Definition node-list.cc:247
static Ptr< Node > GetNode(uint32_t n)
Definition node-list.cc:240
static uint32_t Add(Ptr< Node > node)
Definition node-list.cc:219
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition node-list.h:33
static Iterator End()
Definition node-list.cc:233
private implementation detail of the NodeList API.
Definition node-list.cc:30
NodeList::Iterator End() const
Definition node-list.cc:185
~NodeListPriv() override
Definition node-list.cc:148
Ptr< Node > GetNode(uint32_t n)
Definition node-list.cc:199
static void Delete()
Delete the nodes list object.
Definition node-list.cc:136
NodeList::Iterator Begin() const
Definition node-list.cc:178
static Ptr< NodeListPriv > * DoGet()
Get the node list object.
Definition node-list.cc:122
std::vector< Ptr< Node > > m_nodes
node objects container
Definition node-list.cc:95
static Ptr< NodeListPriv > Get()
Get the node list object.
Definition node-list.cc:115
uint32_t Add(Ptr< Node > node)
Definition node-list.cc:168
static TypeId GetTypeId()
Get the type ID.
Definition node-list.cc:101
uint32_t GetNNodes()
Definition node-list.cc:192
void DoDispose() override
Dispose the nodes in the list.
Definition node-list.cc:154
A base class which provides memory management and object aggregation.
Definition object.h:78
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition object.cc:203
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
Smart pointer class similar to boost::intrusive_ptr.
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static EventId ScheduleDestroy(FUNC f, Ts &&... args)
Schedule an event to run at the end of the simulation, when Simulator::Destroy() is called.
Definition simulator.h:611
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition config.cc:1005
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition config.cc:998
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeObjectVectorChecker()
ObjectPtrContainerValue ObjectVectorValue
ObjectVectorValue is an alias for ObjectPtrContainerValue.
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.