A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
names.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
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
18#ifndef OBJECT_NAMES_H
19#define OBJECT_NAMES_H
20
21#include "object.h"
22#include "ptr.h"
23
24/**
25 * \file
26 * \ingroup config
27 * Declaration of class ns3::Names.
28 */
29
30namespace ns3
31{
32
33/**
34 * \ingroup config
35 * \brief A directory of name and Ptr<Object> associations that allows
36 * us to give any ns3 Object a name.
37 */
38class Names
39{
40 public:
41 /**
42 * \brief Add the association between the string "name" and the
43 * Ptr<Object> obj.
44 *
45 * The name may begin either with "/Names" to explicitly call out
46 * the fact that the name provided is installed under the root of
47 * the name space, or it may begin with the name of the first object
48 * in the path. For example, Names::Add ("/Names/client", obj) and
49 * Names::Add ("client", obj) accomplish exactly the same thing. A
50 * name at a given level in the name space path must be unique. In
51 * the case of the example above, it would be illegal to try and
52 * associate a different object with the same name: "client" at the
53 * same level ("/Names") in the path.
54 *
55 * As well as specifying a name at the root of the "/Names"
56 * namespace, the name parameter can contain a path that fully
57 * qualifies the name to be added. For example, if you previously
58 * have named an object "client" in the root namespace as above, you
59 * could name an object "under" that name by making a call like
60 * Names::Add ("/Names/client/eth0", obj). This will define the
61 * name "eth0" and make it reachable using the path specified. Note
62 * that Names::Add ("client/eth0", obj) would accomplish exactly the
63 * same thing.
64 *
65 * Duplicate names are not allowed at the same level in a path,
66 * however you may associate similar names with different paths.
67 * For example, if you define "/Names/Client", you may not define
68 * another "/Names/Client" just as you may not have two files with
69 * the same name in a classical filesystem. However, you may have
70 * "/Names/Client/eth0" and "/Names/Server/eth0" defined at the same
71 * time just as you might have different files of the same name
72 * under different directories.
73 *
74 * \param [in] name The name of the object you want to associate; which may be
75 * prepended with a path to that object.
76 * \param [in] object A smart pointer to the object itself.
77 */
78 static void Add(std::string name, Ptr<Object> object);
79
80 /**
81 * \brief An intermediate form of Names::Add allowing you to provide
82 * a path to the parent object (under which you want this name to be
83 * defined) in the form of a name path string.
84 *
85 * In some cases, it is desirable to break up the path used to
86 * describe an item in the names namespace into a path and a name.
87 * This is analogous to a file system operation in which you provide
88 * a directory name and a file name.
89 *
90 * For example, consider a situation where you have previously named
91 * an object "/Names/server". If you further want to create an
92 * association for between a Ptr<Object> object that you want to
93 * live "under" the server in the name space -- perhaps "eth0" --
94 * you could do this in two ways, depending on which was more
95 * convenient: Names::Add ("/Names/server/eth0", object) or, using
96 * the split path and name approach, Names::Add ("/Names/server",
97 * "eth0", object).
98 *
99 * Duplicate names are not allowed at the same level in a path,
100 * however you may associate similar names with different paths.
101 * For example, if you define "/Names/Client", you may not define
102 * another "/Names/Client" just as you may not have two files with
103 * the same name in a classical filesystem. However, you may have
104 * "/Names/Client/eth0" and "/Names/Server/eth0" defined at the same
105 * time just as you might have different files of the same name
106 * under different directories.
107 *
108 * \param [in] path A path name describing a previously named object
109 * under which you want this new name to be defined.
110 * \param [in] name The name of the object you want to associate.
111 * \param [in] object A smart pointer to the object itself.
112 *
113 * \see Names::Add (Ptr<Object>,std::string,Ptr<Object>);
114 */
115 static void Add(std::string path, std::string name, Ptr<Object> object);
116
117 /**
118 * \brief A low-level form of Names::Add allowing you to specify the
119 * path to the parent object (under which you want this name to be
120 * defined) in the form of a previously named object.
121 *
122 * In some use cases, it is desirable to break up the path in the
123 * names name space into a path and a name. This is analogous to a
124 * file system operation in which you provide a directory name and a
125 * file name. Recall that the path string actually refers to a
126 * previously named object, "under" which you want to accomplish
127 * some naming action.
128 *
129 * However, the path is sometimes not available, and you only have
130 * the object that is represented by the path in the names name
131 * space. To support this use-case in a reasonably high-performance
132 * way, the path string is can be replaced by the object pointer to
133 * which that path would refer. In the spirit of the Config code
134 * where this use-case is most prominent, we refer to this object as
135 * the "context" for the names operation.
136 *
137 * You can think of the context roughly as the inode number of a
138 * directory file in Unix. The inode number can be used to look up
139 * the directory file which contains the list of file names defined
140 * at that directory level. Similarly the context is used to look
141 * up an internal name service entry which contains the names
142 * defined for that context.
143 *
144 * For example, consider a situation where you have previously named
145 * an object "/Names/server". If you further want to create an
146 * association for between a Ptr<Object> object that you want to
147 * live "under" the server in the name space -- perhaps "eth0" --
148 * you could do this by providing a complete path to the new name:
149 * Names::Add ("/Names/server/eth0", object). If, however,
150 * somewhere in your code you only had a pointer to the server, say
151 * Ptr<Node> node, and not a handy path string, you could also
152 * accomplish this by Names::Add (node, "eth0", object).
153 *
154 * Duplicate names are not allowed at the same level in a path. In
155 * the case of this method, the context object gives the same
156 * information as a path string. You may associate similar names
157 * with different paths. For example, if you define"/Names/Client",
158 * you may not define another "/Names/Client" just as you may not
159 * have two files with the same name in a classical filesystem.
160 * However, you may have "/Names/Client/eth0" and
161 * "/Names/Server/eth0" defined at the same time just as you might
162 * have different files of the same name under different
163 * directories.
164 *
165 * \param [in] context A smart pointer to an object that is used
166 * in place of the path under which you want this new
167 * name to be defined.
168 * \param [in] name The name of the object you want to associate.
169 * \param [in] object A smart pointer to the object itself.
170 */
171 static void Add(Ptr<Object> context, std::string name, Ptr<Object> object);
172
173 /**
174 * \brief Rename a previously associated name.
175 *
176 * The name may begin either with "/Names" to explicitly call out
177 * the fact that the name provided is installed under the root of
178 * the name space, or it may begin with the name of the first object
179 * in the path. For example, Names::Rename ("/Names/client",
180 * "server") and Names::Rename ("client", "server") accomplish
181 * exactly the same thing. Names at a given level in the name space
182 * path must be unique. In the case of the example above, it would
183 * be illegal to try and rename a different object to the same name:
184 * "server" at the same level ("/Names") in the path.
185 *
186 * As well as specifying a name at the root of the "/Names"
187 * namespace, the name parameter can contain a path that fully
188 * qualifies the name to be changed. For example, if you previously
189 * have (re)named an object "server" in the root namespace as above,
190 * you could then rename an object "under" that name by making a
191 * call like Names::Rename ("/Names/server/csma", "eth0"). This
192 * will rename the object previously associated with
193 * "/Names/server/csma" to "eth0" and make leave it reachable using
194 * the path "/Names/server/eth0". Note that Names::Rename
195 * ("server/csma", "eth0") would accomplish exactly the same thing.
196 *
197 * \param [in] oldpath The current path name to the object you want
198 * to change.
199 * \param [in] newname The new name of the object you want to change.
200 *
201 * \see Names::Add (std::string name, Ptr<Object> obj)
202 */
203 static void Rename(std::string oldpath, std::string newname);
204
205 /**
206 * \brief An intermediate form of Names::Rename allowing you to
207 * provide a path to the parent object (under which you want this
208 * name to be changed) in the form of a name path string.
209 *
210 * In some cases, it is desirable to break up the path used to
211 * describe an item in the names namespace into a path and a name.
212 * This is analogous to a file system operation in which you provide
213 * a directory name and a file name.
214 *
215 * For example, consider a situation where you have previously named
216 * an object "/Names/server/csma". If you want to change the name
217 * "csma" to "eth0", you could do this in two ways, depending on
218 * which was more convenient: Names::Rename ("/Names/server/csma",
219 * "eth0") or, using the split path and name approach, Names::Rename
220 * ("/Names/server", "csma", "eth0").
221 *
222 * \param [in] path A path name describing a previously named object
223 * under which you want this name change to occur
224 * (cf. directory).
225 * \param [in] oldname The currently defined name of the object.
226 * \param [in] newname The new name you want the object to have.
227 */
228 static void Rename(std::string path, std::string oldname, std::string newname);
229
230 /**
231 * \brief A low-level form of Names::Rename allowing you to specify
232 * the path to the parent object (under which you want this name to
233 * be changed) in the form of a previously named object.
234 *
235 * In some use cases, it is desirable to break up the path in the
236 * names name space into a path and a name. This is analogous to a
237 * file system operation in which you provide a directory name and a
238 * file name. Recall that the path string actually refers to a
239 * previously named object, "under" which you want to accomplish
240 * some naming action.
241 *
242 * However, the path is sometimes not available, and you only have
243 * the object that is represented by the path in the names name
244 * space. To support this use-case in a reasonably high-performance
245 * way, the path string is can be replaced by the object pointer to
246 * which that path would refer. In the spirit of the Config code
247 * where this use-case is most prominent, we refer to this object as
248 * the "context" for the names operation.
249 *
250 * You can think of the context roughly as the inode number of a
251 * directory file in Unix. The inode number can be used to look up
252 * the directory file which contains the list of file names defined
253 * at that directory level. Similarly the context is used to look
254 * up an internal name service entry which contains the names
255 * defined for that context.
256 *
257 * For example, consider a situation where you have previously named
258 * an object "/Names/server/csma". If you later decide to rename
259 * the csma object to say "eth0" -- you could do this by providing a
260 * complete path as in Names::Rename ("/Names/server/csma", "eth0").
261 * If, however, somewhere in your code you only had a pointer to the
262 * server, and not a handy path string, say Ptr<Node> node, you
263 * could also accomplish this by Names::Rename (node, "csma",
264 * "eth0").
265 *
266 * \param [in] context A smart pointer to an object that is used
267 * in place of the path under which you want this
268 * new name to be defined.
269 * \param [in] oldname The current shortname of the object you want
270 * to change.
271 * \param [in] newname The new shortname of the object you want
272 * to change.
273 */
274 static void Rename(Ptr<Object> context, std::string oldname, std::string newname);
275
276 /**
277 * \brief Given a pointer to an object, look to see if that object
278 * has a name associated with it and, if so, return the name of the
279 * object otherwise return an empty string.
280 *
281 * An object can be referred to in two ways. Either you can talk
282 * about it using its fully qualified path name, for example,
283 * "/Names/client/eth0" or you can refer to it by its name, in this
284 * case "eth0".
285 *
286 * This method returns the name of the object, e.g., "eth0".
287 *
288 * \param [in] object A smart pointer to an object for which you want
289 * to find its name.
290 *
291 * \returns A string containing the name of the object if found,
292 * otherwise the empty string.
293 */
294 static std::string FindName(Ptr<Object> object);
295
296 /**
297 * \brief Given a pointer to an object, look to see if that object
298 * has a name associated with it and return the fully qualified name
299 * path of the object otherwise return an empty string.
300 *
301 * An object can be referred to in two ways. Either you can talk
302 * about it using its fully qualified path name, for example,
303 * "/Names/client/eth0" or you can refer to it by its name, in this
304 * case "eth0".
305 *
306 * This method returns the name path of the object, e.g.,
307 * "Names/client/eth0".
308 *
309 * \param [in] object A smart pointer to an object for which you
310 * want to find its fullname.
311 *
312 * \returns A string containing the name path of the object,
313 * otherwise the empty string.
314 */
315 static std::string FindPath(Ptr<Object> object);
316
317 /**
318 * \brief Clear the list of objects associated with names.
319 */
320
321 static void Clear();
322
323 /**
324 * \brief Given a name path string, look to see if there's an object
325 * in the system with that associated to it. If there is, do a
326 * GetObject on the resulting object to convert it to the requested
327 * typename and return it.
328 *
329 * An object can be referred to in two ways. Either you can talk
330 * about it using its fully qualified path name, for example,
331 * "/Names/client/eth0" or you can refer to it by its name, in this
332 * case "eth0".
333 *
334 * This method requires that the name path of the object be
335 * provided, e.g., "Names/client/eth0".
336 *
337 * \param [in] path A string containing a name space path used
338 * to locate the object.
339 *
340 * \returns A smart pointer to the named object converted to
341 * the requested type.
342 */
343 template <typename T>
344 static Ptr<T> Find(std::string path);
345
346 /**
347 * \brief Given a path to an object and an object name, look through
348 * the names defined under the path to see if there's an object
349 * there with the given name.
350 *
351 * In some cases, it is desirable to break up the path used to
352 * describe an item in the names namespace into a path and a name.
353 * This is analogous to a file system operation in which you provide
354 * a directory name and a file name.
355 *
356 * For example, consider a situation where you have previously named
357 * an object "/Names/server/eth0". If you want to discover the
358 * object which you associated with this path, you could do this in
359 * two ways, depending on which was more convenient: Names::Find
360 * ("/Names/server/eth0") or, using the split path and name
361 * approach, Names::Find ("/Names/server", "eth0").
362 *
363 * \param [in] path A path name describing a previously named object
364 * under which you want to look for the specified name.
365 * \param [in] name A string containing a name to search for.
366 *
367 * \returns A smart pointer to the named object converted to
368 * the requested type.
369 */
370 template <typename T>
371 static Ptr<T> Find(std::string path, std::string name);
372
373 /**
374 * \brief Given a path to an object and an object name, look through
375 * the names defined under the path to see if there's an object
376 * there with the given name.
377 *
378 * In some cases, it is desirable to break up the path used to
379 * describe an item in the names namespace into a path and a name.
380 * This is analogous to a file system operation in which you provide
381 * a directory name and a file name.
382 *
383 * For example, consider a situation where you have previously named
384 * an object "/Names/server/eth0". If you want to discover the
385 * object which you associated with this path, you could do this in
386 * two ways, depending on which was more convenient: Names::Find
387 * ("/Names/server/eth0") or, using the split path and name
388 * approach, Names::Find ("/Names/server", "eth0").
389 *
390 * However, the path is sometimes not available, and you only have
391 * the object that is represented by the path in the names name
392 * space. To support this use-case in a reasonably high-performance
393 * way, the path string is can be replaced by the object pointer to
394 * which that path would refer. In the spirit of the Config code
395 * where this use-case is most prominent, we refer to this object as
396 * the "context" for the names operation.
397 *
398 * You can think of the context roughly as the inode number of a
399 * directory file in Unix. The inode number can be used to look up
400 * the directory file which contains the list of file names defined
401 * at that directory level. Similarly the context is used to look
402 * up an internal name service entry which contains the names
403 * defined for that context.
404 *
405 * \param [in] context A smart pointer to an object that is used
406 * in place of the path under which you want this
407 * new name to be defined.
408 * \param [in] name A string containing a name to search for.
409 *
410 * \returns A smart pointer to the named object converted to
411 * the requested type.
412 */
413 template <typename T>
414 static Ptr<T> Find(Ptr<Object> context, std::string name);
415
416 private:
417 /**
418 * \brief Non-templated internal version of Names::Find
419 *
420 * \param [in] path A string containing the path of the object
421 * to look for.
422 *
423 * \returns A smart pointer to the named object.
424 */
425 static Ptr<Object> FindInternal(std::string path);
426
427 /**
428 * \brief Non-templated internal version of Names::Find
429 *
430 * \param [in] path A string containing the path to search
431 * for the object in.
432 * \param [in] name A string containing the name of the object
433 * to look for.
434 *
435 * \returns A smart pointer to the named object.
436 */
437 static Ptr<Object> FindInternal(std::string path, std::string name);
438
439 /**
440 * \brief Non-templated internal version of Names::Find
441 *
442 * \param [in] context A smart pointer to an object under which
443 * you want to look for the provided name.
444 * \param [in] name A string containing the name to look for.
445 *
446 * \returns A smart pointer to the named object.
447 */
448 static Ptr<Object> FindInternal(Ptr<Object> context, std::string name);
449};
450
451template <typename T>
452/* static */
453Ptr<T>
454Names::Find(std::string path)
455{
456 Ptr<Object> obj = FindInternal(path);
457 if (obj)
458 {
459 return obj->GetObject<T>();
460 }
461 else
462 {
463 return nullptr;
464 }
465}
466
467template <typename T>
468/* static */
469Ptr<T>
470Names::Find(std::string path, std::string name)
471{
472 Ptr<Object> obj = FindInternal(path, name);
473 if (obj)
474 {
475 return obj->GetObject<T>();
476 }
477 else
478 {
479 return nullptr;
480 }
481}
482
483template <typename T>
484/* static */
485Ptr<T>
486Names::Find(Ptr<Object> context, std::string name)
487{
488 Ptr<Object> obj = FindInternal(context, name);
489 if (obj)
490 {
491 return obj->GetObject<T>();
492 }
493 else
494 {
495 return nullptr;
496 }
497}
498
499} // namespace ns3
500
501#endif /* OBJECT_NAMES_H */
A directory of name and Ptr<Object> associations that allows us to give any ns3 Object a name.
Definition: names.h:39
static void Rename(std::string oldpath, std::string newname)
Rename a previously associated name.
Definition: names.cc:783
static Ptr< Object > FindInternal(std::string path)
Non-templated internal version of Names::Find.
Definition: names.cc:850
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:775
static Ptr< T > Find(std::string path)
Given a name path string, look to see if there's an object in the system with that associated to it.
Definition: names.h:454
static void Clear()
Clear the list of objects associated with names.
Definition: names.cc:843
static std::string FindName(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and,...
Definition: names.cc:829
static std::string FindPath(Ptr< Object > object)
Given a pointer to an object, look to see if that object has a name associated with it and return the...
Definition: names.cc:836
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.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::Ptr smart pointer declaration and implementation.