A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
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 
60 
61 TypeId
63 {
64  static TypeId tid = TypeId ("ns3::NodeListPriv")
65  .SetParent<Object> ()
66  .AddAttribute ("NodeList", "The list of all nodes created during the simulation.",
69  MakeObjectVectorChecker<Node> ())
70  ;
71  return tid;
72 }
73 
76 {
77  return *DoGet ();
78 }
81 {
82  static Ptr<NodeListPriv> ptr = 0;
83  if (ptr == 0)
84  {
85  ptr = CreateObject<NodeListPriv> ();
88  }
89  return &ptr;
90 }
91 void
93 {
96  (*DoGet ()) = 0;
97 }
98 
99 
101 {
103 }
105 {
106 }
107 void
109 {
111  for (std::vector<Ptr<Node> >::iterator i = m_nodes.begin ();
112  i != m_nodes.end (); i++)
113  {
114  Ptr<Node> node = *i;
115  node->Dispose ();
116  *i = 0;
117  }
118  m_nodes.erase (m_nodes.begin (), m_nodes.end ());
120 }
121 
122 
123 uint32_t
125 {
126  uint32_t index = m_nodes.size ();
127  m_nodes.push_back (node);
129  return index;
130 
131 }
134 {
135  return m_nodes.begin ();
136 }
138 NodeListPriv::End (void) const
139 {
140  return m_nodes.end ();
141 }
142 uint32_t
144 {
145  return m_nodes.size ();
146 }
147 
148 Ptr<Node>
150 {
151  NS_ASSERT_MSG (n < m_nodes.size (), "Node index " << n <<
152  " is out of range (only have " << m_nodes.size () << " nodes).");
153  return m_nodes[n];
154 }
155 
156 }
157 
163 namespace ns3 {
164 
165 uint32_t
167 {
168  return NodeListPriv::Get ()->Add (node);
169 }
172 {
173  return NodeListPriv::Get ()->Begin ();
174 }
177 {
178  return NodeListPriv::Get ()->End ();
179 }
180 Ptr<Node>
181 NodeList::GetNode (uint32_t n)
182 {
183  return NodeListPriv::Get ()->GetNode (n);
184 }
185 uint32_t
187 {
188  return NodeListPriv::Get ()->GetNNodes ();
189 }
190 
191 } // namespace ns3