A Discrete-Event Network Simulator
API
node-list.cc
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  * Authors:
18  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>,
19  */
20 
21 #include "node-list.h"
22 
23 #include "node.h"
24 
25 #include "ns3/assert.h"
26 #include "ns3/config.h"
27 #include "ns3/log.h"
28 #include "ns3/object-vector.h"
29 #include "ns3/simulator.h"
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("NodeList");
35 
40 class NodeListPriv : public Object
41 {
42  public:
47  static TypeId GetTypeId();
48  NodeListPriv();
49  ~NodeListPriv() override;
50 
58  uint32_t Add(Ptr<Node> node);
59 
64  NodeList::Iterator Begin() const;
65 
70  NodeList::Iterator End() const;
71 
76  Ptr<Node> GetNode(uint32_t n);
77 
81  uint32_t GetNNodes();
82 
87  static Ptr<NodeListPriv> Get();
88 
89  private:
94  static Ptr<NodeListPriv>* DoGet();
95 
99  static void Delete();
100 
104  void DoDispose() override;
105 
106  std::vector<Ptr<Node>> m_nodes;
107 };
108 
110 
111 TypeId
113 {
114  static TypeId tid = TypeId("ns3::NodeListPriv")
115  .SetParent<Object>()
116  .SetGroupName("Network")
117  .AddAttribute("NodeList",
118  "The list of all nodes created during the simulation.",
121  MakeObjectVectorChecker<Node>());
122  return tid;
123 }
124 
127 {
129  return *DoGet();
130 }
131 
134 {
136  static Ptr<NodeListPriv> ptr = nullptr;
137  if (!ptr)
138  {
139  ptr = CreateObject<NodeListPriv>();
142  }
143  return &ptr;
144 }
145 
146 void
148 {
151  (*DoGet()) = nullptr;
152 }
153 
155 {
156  NS_LOG_FUNCTION(this);
157 }
158 
160 {
161  NS_LOG_FUNCTION(this);
162 }
163 
164 void
166 {
167  NS_LOG_FUNCTION(this);
168  for (auto i = m_nodes.begin(); i != m_nodes.end(); i++)
169  {
170  Ptr<Node> node = *i;
171  node->Dispose();
172  *i = nullptr;
173  }
174  m_nodes.erase(m_nodes.begin(), m_nodes.end());
176 }
177 
178 uint32_t
180 {
181  NS_LOG_FUNCTION(this << node);
182  uint32_t index = m_nodes.size();
183  m_nodes.push_back(node);
184  Simulator::ScheduleWithContext(index, TimeStep(0), &Node::Initialize, node);
185  return index;
186 }
187 
190 {
191  NS_LOG_FUNCTION(this);
192  return m_nodes.begin();
193 }
194 
197 {
198  NS_LOG_FUNCTION(this);
199  return m_nodes.end();
200 }
201 
202 uint32_t
204 {
205  NS_LOG_FUNCTION(this);
206  return m_nodes.size();
207 }
208 
209 Ptr<Node>
211 {
212  NS_LOG_FUNCTION(this << n);
213  NS_ASSERT_MSG(n < m_nodes.size(),
214  "Node index " << n << " is out of range (only have " << m_nodes.size()
215  << " nodes).");
216  return m_nodes[n];
217 }
218 
219 } // namespace ns3
220 
226 namespace ns3
227 {
228 
229 uint32_t
231 {
232  NS_LOG_FUNCTION(node);
233  return NodeListPriv::Get()->Add(node);
234 }
235 
238 {
240  return NodeListPriv::Get()->Begin();
241 }
242 
245 {
247  return NodeListPriv::Get()->End();
248 }
249 
250 Ptr<Node>
251 NodeList::GetNode(uint32_t n)
252 {
253  NS_LOG_FUNCTION(n);
254  return NodeListPriv::Get()->GetNode(n);
255 }
256 
257 uint32_t
259 {
261  return NodeListPriv::Get()->GetNNodes();
262 }
263 
264 } // namespace ns3
static Iterator Begin()
Definition: node-list.cc:237
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition: node-list.h:44
static uint32_t GetNNodes()
Definition: node-list.cc:258
static Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:251
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:230
static Iterator End()
Definition: node-list.cc:244
private implementation detail of the NodeList API.
Definition: node-list.cc:41
NodeList::Iterator End() const
Definition: node-list.cc:196
~NodeListPriv() override
Definition: node-list.cc:159
Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:210
static void Delete()
Delete the nodes list object.
Definition: node-list.cc:147
NodeList::Iterator Begin() const
Definition: node-list.cc:189
static Ptr< NodeListPriv > * DoGet()
Get the node list object.
Definition: node-list.cc:133
std::vector< Ptr< Node > > m_nodes
node objects container
Definition: node-list.cc:106
static Ptr< NodeListPriv > Get()
Get the node list object.
Definition: node-list.cc:126
uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:179
static TypeId GetTypeId()
Get the type ID.
Definition: node-list.cc:112
uint32_t GetNNodes()
Definition: node-list.cc:203
void DoDispose() override
Dispose the nodes in the list.
Definition: node-list.cc:165
A base class which provides memory management and object aggregation.
Definition: object.h:89
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
void Dispose()
Dispose of this Object.
Definition: object.cc:219
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:352
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
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:622
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#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:86
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1012
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1005
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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 ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ObjectPtrContainerValue ObjectVectorValue
ObjectVectorValue is an alias for ObjectPtrContainerValue.
Definition: object-vector.h:40
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:76