blob: db39ccf8228642d9aa420a3f70afd5ba53c84367 [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 /**
99 * \brief Generic constructor operator
100 * The object of type T will be appended to the list of components
101 */
102 template<class T>
103 inline Name&
104 operator () (const T &value);
105
106 /**
107 * \brief Get a name
108 * Returns a list of components (strings)
109 */
110 const std::list<std::string> &
111 GetComponents () const;
112
113 /**
114 * @brief Helper call to get the last component of the name
115 */
116 std::string
117 GetLastComponent () const;
118
119 /**
120 * \brief Get subcomponents of the name, starting with first component
121 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
122 */
123 std::list<boost::reference_wrapper<const std::string> >
124 GetSubComponents (size_t num) const;
125
126 /**
127 * @brief Get prefix of the name, containing less minusComponents right components
128 */
129 Name
130 cut (size_t minusComponents) const;
131
132 /**
133 * \brief Print name
134 * @param[in] os Stream to print
135 */
136 void Print (std::ostream &os) const;
137
138 /**
139 * @brief Get serialized size for ndnSIM packet encoding
140 */
141 size_t
142 GetSerializedSize () const;
143
144 /**
145 * @brief Serialize Name in ndnSIM packet encoding
146 * @param[in] start buffer to contain serialized name
147 */
148 uint32_t
149 Serialize (Buffer::Iterator start) const;
150
151 /**
152 * \brief Deserialize Name in ndnSIM packet encoding
153 * @param[in] start buffer that contains serialized name
154 */
155 uint32_t
156 Deserialize (Buffer::Iterator start);
157
158 /**
159 * \brief Returns the size of Name
160 */
161 inline size_t
162 size () const;
163
164 /**
165 * @brief Get read-write begin() iterator
166 */
167 inline iterator
168 begin ();
169
170 /**
171 * @brief Get read-only begin() iterator
172 */
173 inline const_iterator
174 begin () const;
175
176 /**
177 * @brief Get read-write end() iterator
178 */
179 inline iterator
180 end ();
181
182 /**
183 * @brief Get read-only end() iterator
184 */
185 inline const_iterator
186 end () const;
187
188 /**
189 * \brief Equality operator for Name
190 */
191 inline bool
192 operator== (const Name &prefix) const;
193
194 /**
195 * \brief Less than operator for Name
196 */
197 inline bool
198 operator< (const Name &prefix) const;
199
200 typedef std::string partial_type;
201
202private:
203 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
204};
205
206/**
207 * \brief Print out name components separated by slashes, e.g., /first/second/third
208 */
209std::ostream &
210operator << (std::ostream &os, const Name &components);
211
212/**
213 * \brief Read components from input and add them to components. Will read input stream till eof
214 * Substrings separated by slashes will become separate components
215 */
216std::istream &
217operator >> (std::istream &is, Name &components);
218
219/**
220 * \brief Returns the size of Name object
221 */
222size_t
223Name::size () const
224{
225 return m_prefix.size ();
226}
227
228Name::iterator
229Name::begin ()
230{
231 return m_prefix.begin ();
232}
233
234/**
235 * @brief Get read-only begin() iterator
236 */
237Name::const_iterator
238Name::begin () const
239{
240 return m_prefix.begin ();
241}
242
243/**
244 * @brief Get read-write end() iterator
245 */
246Name::iterator
247Name::end ()
248{
249 return m_prefix.end ();
250}
251
252/**
253 * @brief Get read-only end() iterator
254 */
255Name::const_iterator
256Name::end () const
257{
258 return m_prefix.end ();
259}
260
261
262/**
263 * \brief Generic constructor operator
264 * The object of type T will be appended to the list of components
265 */
266template<class T>
267Name&
268Name::operator () (const T &value)
269{
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700270 return Add (value);
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800271}
272
273/**
274 * \brief Generic Add method
275 * Appends object of type T to the list of components
276 * @param[in] value The object to be appended
277 */
278template<class T>
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700279Name&
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800280Name::Add (const T &value)
281{
282 std::ostringstream os;
283 os << value;
284 m_prefix.push_back (os.str ());
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700285
286 return *this;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800287}
288
289/**
290 * \brief Equality operator for Name
291 */
292bool
293Name::operator== (const Name &prefix) const
294{
295 if (m_prefix.size () != prefix.m_prefix.size ())
296 return false;
297
298 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
299}
300
301/**
302 * \brief Less than operator for Name
303 */
304bool
305Name::operator< (const Name &prefix) const
306{
307 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
308 prefix.m_prefix.begin (), prefix.m_prefix.end ());
309}
310
311ATTRIBUTE_HELPER_HEADER (Name);
312
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700313// for backwards compatibility
314typedef Name NameComponents;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800315
316} // namespace ndn
317} // namespace ns3
318
319#endif // _NDN_NAME_H_
320