blob: d69a75e66ab445b14c1310b70bb106a1daf24439 [file] [log] [blame]
Alexander Afanasyev32c07562013-02-01 12:58:43 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
20 */
21
22#ifndef _NDN_NAME_H_
23#define _NDN_NAME_H_
24
25#include "ns3/simple-ref-count.h"
26#include "ns3/attribute.h"
27#include "ns3/attribute-helper.h"
28
29#include <string>
30#include <algorithm>
31#include <list>
32#include "ns3/object.h"
33#include "ns3/buffer.h"
34
35#include <boost/ref.hpp>
36
37namespace ns3 {
38namespace ndn {
39
40/**
41 * \ingroup ndn
42 * \brief Hierarchical NDN name
43 * A Name element represents a hierarchical name for Ndn content.
44 * It simply contains a sequence of Component elements.
45 * Each Component element contains a sequence of zero or more bytes.
46 * There are no restrictions on what byte sequences may be used.
47 * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
48 */
49class Name : public SimpleRefCount<Name>
50{
51public:
52 typedef std::list<std::string>::iterator iterator;
53 typedef std::list<std::string>::const_iterator const_iterator;
54
55 /**
56 * \brief Constructor
57 * Creates a prefix with zero components (can be looked as root "/")
58 */
59 Name ();
60
61 /**
62 * \brief Constructor
63 * Creates a prefix from a list of strings where every string represents a prefix component
64 * @param[in] components A list of strings
65 */
66 Name (const std::list<boost::reference_wrapper<const std::string> > &components);
67
68 /**
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -070069 * \brief Constructor
70 * Creates a prefix from a list of strings where every string represents a prefix component
71 * @param[in] components A list of strings
72 */
73 Name (const std::list<std::string> &components);
74
75 /**
Alexander Afanasyev32c07562013-02-01 12:58:43 -080076 * @brief Constructor
77 * Creates a prefix from the string (string is parsed using operator>>)
78 * @param[in] prefix A string representation of a prefix
79 */
80 Name (const std::string &prefix);
81
82 /**
83 * @brief Constructor
84 * Creates a prefix from the string (string is parsed using operator>>)
85 * @param[in] prefix A string representation of a prefix
86 */
87 Name (const char *prefix);
88
89 /**
90 * \brief Generic Add method
91 * Appends object of type T to the list of components
92 * @param[in] value The object to be appended
93 */
94 template<class T>
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -070095 inline Name&
Alexander Afanasyev32c07562013-02-01 12:58:43 -080096 Add (const T &value);
97
98 /**
Alexander Afanasyev79606062013-07-11 00:57:28 -070099 * \brief Append components from another name
100 * @param otherName Name to add at the end
101 */
102 inline Name&
103 Append (const Name &otherName);
104
105 /**
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800106 * \brief Generic constructor operator
107 * The object of type T will be appended to the list of components
108 */
109 template<class T>
110 inline Name&
111 operator () (const T &value);
112
113 /**
114 * \brief Get a name
115 * Returns a list of components (strings)
116 */
117 const std::list<std::string> &
118 GetComponents () const;
119
120 /**
121 * @brief Helper call to get the last component of the name
122 */
123 std::string
124 GetLastComponent () const;
125
126 /**
127 * \brief Get subcomponents of the name, starting with first component
128 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
129 */
130 std::list<boost::reference_wrapper<const std::string> >
131 GetSubComponents (size_t num) const;
132
133 /**
134 * @brief Get prefix of the name, containing less minusComponents right components
135 */
136 Name
137 cut (size_t minusComponents) const;
138
139 /**
140 * \brief Print name
141 * @param[in] os Stream to print
142 */
143 void Print (std::ostream &os) const;
144
145 /**
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800146 * \brief Returns the size of Name
147 */
148 inline size_t
149 size () const;
150
151 /**
152 * @brief Get read-write begin() iterator
153 */
154 inline iterator
155 begin ();
156
157 /**
158 * @brief Get read-only begin() iterator
159 */
160 inline const_iterator
161 begin () const;
162
163 /**
164 * @brief Get read-write end() iterator
165 */
166 inline iterator
167 end ();
168
169 /**
170 * @brief Get read-only end() iterator
171 */
172 inline const_iterator
173 end () const;
174
175 /**
176 * \brief Equality operator for Name
177 */
178 inline bool
179 operator== (const Name &prefix) const;
180
181 /**
182 * \brief Less than operator for Name
183 */
184 inline bool
185 operator< (const Name &prefix) const;
186
187 typedef std::string partial_type;
188
189private:
190 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
191};
192
193/**
194 * \brief Print out name components separated by slashes, e.g., /first/second/third
195 */
196std::ostream &
197operator << (std::ostream &os, const Name &components);
198
199/**
200 * \brief Read components from input and add them to components. Will read input stream till eof
201 * Substrings separated by slashes will become separate components
202 */
203std::istream &
204operator >> (std::istream &is, Name &components);
205
206/**
207 * \brief Returns the size of Name object
208 */
209size_t
210Name::size () const
211{
212 return m_prefix.size ();
213}
214
215Name::iterator
216Name::begin ()
217{
218 return m_prefix.begin ();
219}
220
221/**
222 * @brief Get read-only begin() iterator
223 */
224Name::const_iterator
225Name::begin () const
226{
227 return m_prefix.begin ();
228}
229
230/**
231 * @brief Get read-write end() iterator
232 */
233Name::iterator
234Name::end ()
235{
236 return m_prefix.end ();
237}
238
239/**
240 * @brief Get read-only end() iterator
241 */
242Name::const_iterator
243Name::end () const
244{
245 return m_prefix.end ();
246}
247
248
249/**
250 * \brief Generic constructor operator
251 * The object of type T will be appended to the list of components
252 */
253template<class T>
254Name&
255Name::operator () (const T &value)
256{
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700257 return Add (value);
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800258}
259
260/**
261 * \brief Generic Add method
262 * Appends object of type T to the list of components
263 * @param[in] value The object to be appended
264 */
265template<class T>
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700266Name&
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800267Name::Add (const T &value)
268{
269 std::ostringstream os;
270 os << value;
271 m_prefix.push_back (os.str ());
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700272
273 return *this;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800274}
275
Alexander Afanasyev79606062013-07-11 00:57:28 -0700276inline Name&
277Name::Append (const Name &otherName)
278{
279 std::copy (otherName.begin (), otherName.end (), std::back_inserter (m_prefix));
280 return *this;
281}
282
283
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800284/**
285 * \brief Equality operator for Name
286 */
287bool
288Name::operator== (const Name &prefix) const
289{
290 if (m_prefix.size () != prefix.m_prefix.size ())
291 return false;
292
293 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
294}
295
296/**
297 * \brief Less than operator for Name
298 */
299bool
300Name::operator< (const Name &prefix) const
301{
302 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
303 prefix.m_prefix.begin (), prefix.m_prefix.end ());
304}
305
306ATTRIBUTE_HELPER_HEADER (Name);
307
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700308// for backwards compatibility
309typedef Name NameComponents;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800310
311} // namespace ndn
312} // namespace ns3
313
314#endif // _NDN_NAME_H_
315