A Discrete-Event Network Simulator
API
node-list.cc
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  * Authors:
19  * Mathieu Lacage <mathieu.lacage@sophia.inria.fr>,
20  */
21 
22 #include "ns3/simulator.h"
23 #include "ns3/object-vector.h"
24 #include "ns3/config.h"
25 #include "ns3/log.h"
26 #include "ns3/assert.h"
27 #include "node-list.h"
28 #include "node.h"
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("NodeList");
33 
38 class NodeListPriv : public Object
39 {
40 public:
45  static TypeId GetTypeId (void);
46  NodeListPriv ();
47  ~NodeListPriv ();
48 
56  uint32_t Add (Ptr<Node> node);
57 
62  NodeList::Iterator Begin (void) const;
63 
68  NodeList::Iterator End (void) const;
69 
74  Ptr<Node> GetNode (uint32_t n);
75 
79  uint32_t GetNNodes (void);
80 
85  static Ptr<NodeListPriv> Get (void);
86 
87 private:
92  static Ptr<NodeListPriv> *DoGet (void);
93 
97  static void Delete (void);
98 
102  virtual void DoDispose (void);
103 
104  std::vector<Ptr<Node> > m_nodes;
105 };
106 
108 
109 TypeId
111 {
112  static TypeId tid = TypeId ("ns3::NodeListPriv")
113  .SetParent<Object> ()
114  .SetGroupName("Network")
115  .AddAttribute ("NodeList", "The list of all nodes created during the simulation.",
118  MakeObjectVectorChecker<Node> ())
119  ;
120  return tid;
121 }
122 
125 {
127  return *DoGet ();
128 }
131 {
133  static Ptr<NodeListPriv> ptr = 0;
134  if (ptr == 0)
135  {
136  ptr = CreateObject<NodeListPriv> ();
139  }
140  return &ptr;
141 }
142 void
144 {
147  (*DoGet ()) = 0;
148 }
149 
150 
152 {
153  NS_LOG_FUNCTION (this);
154 }
156 {
157  NS_LOG_FUNCTION (this);
158 }
159 void
161 {
162  NS_LOG_FUNCTION (this);
163  for (std::vector<Ptr<Node> >::iterator i = m_nodes.begin ();
164  i != m_nodes.end (); i++)
165  {
166  Ptr<Node> node = *i;
167  node->Dispose ();
168  *i = 0;
169  }
170  m_nodes.erase (m_nodes.begin (), m_nodes.end ());
172 }
173 
174 
175 uint32_t
177 {
178  NS_LOG_FUNCTION (this << node);
179  uint32_t index = m_nodes.size ();
180  m_nodes.push_back (node);
181  Simulator::ScheduleWithContext (index, TimeStep (0), &Node::Initialize, node);
182  return index;
183 
184 }
187 {
188  NS_LOG_FUNCTION (this);
189  return m_nodes.begin ();
190 }
192 NodeListPriv::End (void) const
193 {
194  NS_LOG_FUNCTION (this);
195  return m_nodes.end ();
196 }
197 uint32_t
199 {
200  NS_LOG_FUNCTION (this);
201  return m_nodes.size ();
202 }
203 
204 Ptr<Node>
206 {
207  NS_LOG_FUNCTION (this << n);
208  NS_ASSERT_MSG (n < m_nodes.size (), "Node index " << n <<
209  " is out of range (only have " << m_nodes.size () << " nodes).");
210  return m_nodes[n];
211 }
212 
213 }
214 
220 namespace ns3 {
221 
222 uint32_t
224 {
225  NS_LOG_FUNCTION (node);
226  return NodeListPriv::Get ()->Add (node);
227 }
230 {
232  return NodeListPriv::Get ()->Begin ();
233 }
236 {
238  return NodeListPriv::Get ()->End ();
239 }
240 Ptr<Node>
242 {
243  NS_LOG_FUNCTION (n);
244  return NodeListPriv::Get ()->GetNode (n);
245 }
246 uint32_t
248 {
250  return NodeListPriv::Get ()->GetNNodes ();
251 }
252 
253 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::NodeListPriv::Begin
NodeList::Iterator Begin(void) const
Definition: node-list.cc:186
ns3::Object::Dispose
void Dispose(void)
Dispose of this Object.
Definition: object.cc:214
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::NodeListPriv::Get
static Ptr< NodeListPriv > Get(void)
Get the node list object.
Definition: node-list.cc:124
ns3::NodeListPriv::DoDispose
virtual void DoDispose(void)
Dispose the nodes in the list.
Definition: node-list.cc:160
ns3::Simulator::ScheduleWithContext
static void ScheduleWithContext(uint32_t context, Time const &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:571
ns3::NodeListPriv::m_nodes
std::vector< Ptr< Node > > m_nodes
node objects container
Definition: node-list.cc:104
ns3::Simulator::ScheduleDestroy
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:605
ns3::NodeList::Begin
static Iterator Begin(void)
Definition: node-list.cc:229
ns3::NodeListPriv::NodeListPriv
NodeListPriv()
Definition: node-list.cc:151
ns3::MakeObjectVectorAccessor
Ptr< const AttributeAccessor > MakeObjectVectorAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-vector.h:81
ns3::NodeListPriv::DoGet
static Ptr< NodeListPriv > * DoGet(void)
Get the node list object.
Definition: node-list.cc:130
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::NodeListPriv::Add
uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:176
ns3::NodeListPriv
private implementation detail of the NodeList API.
Definition: node-list.cc:39
ns3::NodeListPriv::End
NodeList::Iterator End(void) const
Definition: node-list.cc:192
ns3::NodeList::GetNNodes
static uint32_t GetNNodes(void)
Definition: node-list.cc:247
ns3::Config::RegisterRootNamespaceObject
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:946
ns3::ObjectPtrContainerValue
Container for a set of ns3::Object pointers.
Definition: object-ptr-container.h:46
node-list.h
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::Ptr< Node >
ns3::NodeList::Iterator
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Definition: node-list.h:44
ns3::NodeListPriv::GetNNodes
uint32_t GetNNodes(void)
Definition: node-list.cc:198
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
node.h
ns3::NodeListPriv::GetNode
Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:205
ns3::Config::UnregisterRootNamespaceObject
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:952
NS_ASSERT_MSG
#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:88
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
ns3::NodeList::End
static Iterator End(void)
Definition: node-list.cc:235
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::NodeListPriv::~NodeListPriv
~NodeListPriv()
Definition: node-list.cc:155
ns3::NodeList::Add
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:223
ns3::Object::Initialize
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
ns3::NodeListPriv::Delete
static void Delete(void)
Delete the nodes list object.
Definition: node-list.cc:143
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::NodeList::GetNode
static Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:241
sample-rng-plot.n
n
Definition: sample-rng-plot.py:37
ns3::NodeListPriv::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: node-list.cc:110