A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
node-container.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 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 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef NODE_CONTAINER_H
20#define NODE_CONTAINER_H
21
22#include "ns3/node.h"
23
24#include <type_traits>
25#include <vector>
26
27namespace ns3
28{
29
30/**
31 * \brief keep track of a set of node pointers.
32 *
33 * Typically ns-3 helpers operate on more than one node at a time. For example
34 * a device helper may want to install devices on a large number of similar
35 * nodes. The helper Install methods usually take a NodeContainer as a
36 * parameter. NodeContainers hold the multiple Ptr<Node> which are used
37 * to refer to the nodes.
38 */
40{
41 public:
42 /// Node container iterator
43 typedef std::vector<Ptr<Node>>::const_iterator Iterator;
44
45 /**
46 * \brief Create a NodeContainer that contains a list of _all_ nodes
47 * created through NodeContainer::Create() and stored in the
48 * ns3::NodeList.
49 *
50 * Whenever a Node is created, a Ptr<Node> is added to a global list of all
51 * nodes in the system. It is sometimes useful to be able to get to all
52 * nodes in one place. This method creates a NodeContainer that is
53 * initialized to contain all of the simulation nodes,
54 *
55 * \returns a NodeContainer which contains a list of all Nodes.
56 */
57 static NodeContainer GetGlobal();
58
59 /**
60 * Create an empty NodeContainer.
61 */
63
64 /**
65 * Create a NodeContainer with exactly one node which has been previously
66 * instantiated. The single Node is specified by a smart pointer.
67 *
68 * \param node The Ptr<Node> to add to the container.
69 */
71
72 /**
73 * Create a NodeContainer with exactly one node which has been previously
74 * instantiated and assigned a name using the Object Name Service. This
75 * Node is then specified by its assigned name.
76 *
77 * \param nodeName The name of the Node Object to add to the container.
78 */
79 NodeContainer(std::string nodeName);
80
81 /**
82 * Create a NodeContainer with the requested number of Nodes.
83 *
84 * This is syntactic sugar for
85 *
86 * \code
87 * NodeContainer nodes;
88 * nodes.Create (size);
89 * // or nodes.Create (size, systemId);
90 * \endcode
91 *
92 * \param [in] n The number of nodes to create.
93 * \param [in] systemId The system id or rank associated with this node
94 */
95 explicit NodeContainer(uint32_t n, uint32_t systemId = 0);
96
97 /**
98 * Create a node container which is a concatenation of multiple input
99 * NodeContainers.
100 *
101 * \tparam Ts \deduced Template type parameter pack for the multiple
102 * NodeContainers
103 * \param nc The first NodeContainer
104 * \param args The remaining NodeContainers
105 *
106 * \note A frequently seen idiom that uses these constructors involves the
107 * implicit conversion by constructor of Ptr<Node>. When used, Ptr<Node>
108 * will be passed to this constructor instead of NodeContainer&.
109 * C++ will notice the implicit conversion path that goes through the
110 * NodeContainer (Ptr<Node> node) constructor above. Using this conversion
111 * one may optionally provide arguments of Ptr<Node> to these constructors.
112 */
113 template <typename... Ts>
114 NodeContainer(const NodeContainer& nc, Ts&&... args);
115
116 /**
117 * \brief Get an iterator which refers to the first Node in the
118 * container.
119 *
120 * Nodes can be retrieved from the container in two ways. First,
121 * directly by an index into the container, and second, using an iterator.
122 * This method is used in the iterator method and is typically used in a
123 * for-loop to run through the Nodes
124 *
125 * \code
126 * NodeContainer::Iterator i;
127 * for (i = container.Begin (); i != container.End (); ++i)
128 * {
129 * (*i)->method (); // some Node method
130 * }
131 * \endcode
132 *
133 * \returns an iterator which refers to the first Node in the container.
134 */
135 Iterator Begin() const;
136
137 /**
138 * \brief Get an iterator which indicates past-the-last Node in the
139 * container.
140 *
141 * Nodes can be retrieved from the container in two ways. First,
142 * directly by an index into the container, and second, using an iterator.
143 * This method is used in the iterator method and is typically used in a
144 * for-loop to run through the Nodes
145 *
146 * \code
147 * NodeContainer::Iterator i;
148 * for (i = container.Begin (); i != container.End (); ++i)
149 * {
150 * (*i)->method (); // some Node method
151 * }
152 * \endcode
153 *
154 * \returns an iterator which indicates an ending condition for a loop.
155 */
156 Iterator End() const;
157
158 /**
159 * \brief Get the number of Ptr<Node> stored in this container.
160 *
161 * Nodes can be retrieved from the container in two ways. First,
162 * directly by an index into the container, and second, using an iterator.
163 * This method is used in the direct method and is typically used to
164 * define an ending condition in a for-loop that runs through the stored
165 * Nodes
166 *
167 * \code
168 * uint32_t nNodes = container.GetN ();
169 * for (uint32_t i = 0; i < nNodes; ++i)
170 * {
171 * Ptr<Node> p = container.Get (i);
172 * i->method (); // some Node method
173 * }
174 * \endcode
175 *
176 * \returns the number of Ptr<Node> stored in this container.
177 */
178 uint32_t GetN() const;
179
180 /**
181 * \brief Get the Ptr<Node> stored in this container at a given
182 * index.
183 *
184 * Nodes can be retrieved from the container in two ways. First,
185 * directly by an index into the container, and second, using an iterator.
186 * This method is used in the direct method and is used to retrieve the
187 * indexed Ptr<Node>.
188 *
189 * \code
190 * uint32_t nNodes = container.GetN ();
191 * for (uint32_t i = 0; i < nNodes; ++i)
192 * {
193 * Ptr<Node> p = container.Get (i);
194 * i->method (); // some Node method
195 * }
196 * \endcode
197 *
198 * \param i the index of the requested node pointer.
199 * \returns the requested node pointer.
200 */
201 Ptr<Node> Get(uint32_t i) const;
202
203 /**
204 * \brief Create n nodes and append pointers to them to the end of this
205 * NodeContainer.
206 *
207 * Nodes are at the heart of any ns-3 simulation. One of the first tasks that
208 * any simulation needs to do is to create a number of nodes. This method
209 * automates that task.
210 *
211 * \param n The number of Nodes to create
212 */
213 void Create(uint32_t n);
214
215 /**
216 * \brief Create n nodes with specified systemId for distributed simulations
217 * and append pointers to them to the end of this NodeContainer.
218 *
219 * Nodes are at the heart of any ns-3 simulation. One of the first tasks that
220 * any simulation needs to do is to create a number of nodes. This method
221 * automates that task, and adds the ability to specify systemId for
222 * distributed simulations.
223 *
224 * \param n The number of Nodes to create
225 * \param systemId The system id or rank associated with this node
226 */
227 void Create(uint32_t n, uint32_t systemId);
228
229 /**
230 * \brief Append the contents of another NodeContainer to the end of
231 * this container.
232 *
233 * \param nc The NodeContainer to append.
234 */
235 void Add(const NodeContainer& nc);
236
237 /**
238 * \brief Append the contents of another NodeContainer to the end of
239 * this container.
240 *
241 * \tparam Ts \deduced Template type parameter pack for the multiple
242 * NodeContainer
243 * \param nc The NodeContainer to append
244 * \param args The remaining NodeContainers to append
245 */
246 template <typename... Ts>
247 void Add(const NodeContainer& nc, Ts&&... args);
248
249 /**
250 * \brief Append a single Ptr<Node> to this container.
251 *
252 * \param node The Ptr<Node> to append.
253 */
254 void Add(Ptr<Node> node);
255
256 /**
257 * \brief Append to this container the single Ptr<Node> referred to
258 * via its object name service registered name.
259 *
260 * \param nodeName The name of the Node Object to add to the container.
261 */
262 void Add(std::string nodeName);
263
264 /**
265 * \brief Return true if container contains a Node with index id
266 *
267 * \param id Node Id
268 * \return whether the NodeContainer contains a node with index id
269 */
270 bool Contains(uint32_t id) const;
271
272 private:
273 std::vector<Ptr<Node>> m_nodes; //!< Nodes smart pointers
274};
275
276} // namespace ns3
277
278///////////////////////////////////////////////////////////
279// Implementation of the templates declared above
280///////////////////////////////////////////////////////////
281
282namespace ns3
283{
284
285template <typename... Ts>
287{
288 static_assert(std::conjunction_v<std::is_convertible<Ts, NodeContainer>...>,
289 "Variable types are not convertible to NodeContainer");
290
291 Add(nc, std::forward<Ts>(args)...);
292}
293
294template <typename... Ts>
295void
296NodeContainer::Add(const NodeContainer& nc, Ts&&... args)
297{
298 static_assert(std::conjunction_v<std::is_convertible<Ts, NodeContainer>...>,
299 "Variable types are not convertible to NodeContainer");
300
301 Add(nc);
302 Add(std::forward<Ts>(args)...);
303}
304
305} // namespace ns3
306
307#endif /* NODE_CONTAINER_H */
keep track of a set of node pointers.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
std::vector< Ptr< Node > > m_nodes
Nodes smart pointers.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
static NodeContainer GetGlobal()
Create a NodeContainer that contains a list of all nodes created through NodeContainer::Create() and ...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
bool Contains(uint32_t id) const
Return true if container contains a Node with index id.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
NodeContainer()
Create an empty NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Every class exported by the ns3 library is enclosed in the ns3 namespace.