A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
node-list.cc
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 
37 class NodeListPriv : public Object
38 {
39 public:
40  static TypeId GetTypeId (void);
41  NodeListPriv ();
42  ~NodeListPriv ();
43 
44  uint32_t Add (Ptr<Node> node);
45  NodeList::Iterator Begin (void) const;
46  NodeList::Iterator End (void) const;
47  Ptr<Node> GetNode (uint32_t n);
48  uint32_t GetNNodes (void);
49 
50  static Ptr<NodeListPriv> Get (void);
51 
52 private:
53  virtual void DoDispose (void);
54  static Ptr<NodeListPriv> *DoGet (void);
55  static void Delete (void);
56  std::vector<Ptr<Node> > m_nodes;
57 };
58 
59 NS_OBJECT_ENSURE_REGISTERED (NodeListPriv);
60 
61 TypeId
62 NodeListPriv::GetTypeId (void)
63 {
64  static TypeId tid = TypeId ("ns3::NodeListPriv")
65  .SetParent<Object> ()
66  .AddAttribute ("NodeList", "The list of all nodes created during the simulation.",
68  MakeObjectVectorAccessor (&NodeListPriv::m_nodes),
69  MakeObjectVectorChecker<Node> ())
70  ;
71  return tid;
72 }
73 
74 Ptr<NodeListPriv>
75 NodeListPriv::Get (void)
76 {
78  return *DoGet ();
79 }
80 Ptr<NodeListPriv> *
81 NodeListPriv::DoGet (void)
82 {
84  static Ptr<NodeListPriv> ptr = 0;
85  if (ptr == 0)
86  {
87  ptr = CreateObject<NodeListPriv> ();
89  Simulator::ScheduleDestroy (&NodeListPriv::Delete);
90  }
91  return &ptr;
92 }
93 void
94 NodeListPriv::Delete (void)
95 {
98  (*DoGet ()) = 0;
99 }
100 
101 
102 NodeListPriv::NodeListPriv ()
103 {
104  NS_LOG_FUNCTION (this);
105 }
106 NodeListPriv::~NodeListPriv ()
107 {
108  NS_LOG_FUNCTION (this);
109 }
110 void
112 {
113  NS_LOG_FUNCTION (this);
114  for (std::vector<Ptr<Node> >::iterator i = m_nodes.begin ();
115  i != m_nodes.end (); i++)
116  {
117  Ptr<Node> node = *i;
118  node->Dispose ();
119  *i = 0;
120  }
121  m_nodes.erase (m_nodes.begin (), m_nodes.end ());
123 }
124 
125 
126 uint32_t
127 NodeListPriv::Add (Ptr<Node> node)
128 {
129  NS_LOG_FUNCTION (this << node);
130  uint32_t index = m_nodes.size ();
131  m_nodes.push_back (node);
132  Simulator::ScheduleWithContext (index, TimeStep (0), &Node::Initialize, node);
133  return index;
134 
135 }
136 NodeList::Iterator
137 NodeListPriv::Begin (void) const
138 {
139  NS_LOG_FUNCTION (this);
140  return m_nodes.begin ();
141 }
142 NodeList::Iterator
143 NodeListPriv::End (void) const
144 {
145  NS_LOG_FUNCTION (this);
146  return m_nodes.end ();
147 }
148 uint32_t
149 NodeListPriv::GetNNodes (void)
150 {
151  NS_LOG_FUNCTION (this);
152  return m_nodes.size ();
153 }
154 
155 Ptr<Node>
156 NodeListPriv::GetNode (uint32_t n)
157 {
158  NS_LOG_FUNCTION (this << n);
159  NS_ASSERT_MSG (n < m_nodes.size (), "Node index " << n <<
160  " is out of range (only have " << m_nodes.size () << " nodes).");
161  return m_nodes[n];
162 }
163 
164 }
165 
171 namespace ns3 {
172 
173 uint32_t
175 {
176  NS_LOG_FUNCTION (node);
177  return NodeListPriv::Get ()->Add (node);
178 }
179 NodeList::Iterator
181 {
183  return NodeListPriv::Get ()->Begin ();
184 }
185 NodeList::Iterator
187 {
189  return NodeListPriv::Get ()->End ();
190 }
191 Ptr<Node>
192 NodeList::GetNode (uint32_t n)
193 {
194  NS_LOG_FUNCTION (n);
195  return NodeListPriv::Get ()->GetNode (n);
196 }
197 uint32_t
199 {
201  return NodeListPriv::Get ()->GetNNodes ();
202 }
203 
204 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
static uint32_t GetNNodes(void)
Definition: node-list.cc:198
static Ptr< Node > GetNode(uint32_t n)
Definition: node-list.cc:192
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
virtual void DoDispose(void)
Definition: object.cc:335
void UnregisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:751
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
static Iterator End(void)
Definition: node-list.cc:186
static uint32_t Add(Ptr< Node > node)
Definition: node-list.cc:174
static void ScheduleWithContext(uint32_t context, Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:900
private implementation detail of the NodeList API.
Definition: node-list.cc:37
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:745
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
static Iterator Begin(void)
Definition: node-list.cc:180
virtual void DoDispose(void)
Definition: node-list.cc:111
void Initialize(void)
Definition: object.cc:179
static EventId ScheduleDestroy(MEM mem_ptr, OBJ obj)
Definition: simulator.h:1072
a base class which provides memory management and object aggregation
Definition: object.h:63
contain a set of ns3::Object pointers.
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471
void Dispose(void)
Definition: object.cc:204