blob: 0694f0be41defe56d4b8fbcef0b0c3e5111c618e [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 /**
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800139 * \brief Returns the size of Name
140 */
141 inline size_t
142 size () const;
143
144 /**
145 * @brief Get read-write begin() iterator
146 */
147 inline iterator
148 begin ();
149
150 /**
151 * @brief Get read-only begin() iterator
152 */
153 inline const_iterator
154 begin () const;
155
156 /**
157 * @brief Get read-write end() iterator
158 */
159 inline iterator
160 end ();
161
162 /**
163 * @brief Get read-only end() iterator
164 */
165 inline const_iterator
166 end () const;
167
168 /**
169 * \brief Equality operator for Name
170 */
171 inline bool
172 operator== (const Name &prefix) const;
173
174 /**
175 * \brief Less than operator for Name
176 */
177 inline bool
178 operator< (const Name &prefix) const;
179
180 typedef std::string partial_type;
181
182private:
183 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
184};
185
186/**
187 * \brief Print out name components separated by slashes, e.g., /first/second/third
188 */
189std::ostream &
190operator << (std::ostream &os, const Name &components);
191
192/**
193 * \brief Read components from input and add them to components. Will read input stream till eof
194 * Substrings separated by slashes will become separate components
195 */
196std::istream &
197operator >> (std::istream &is, Name &components);
198
199/**
200 * \brief Returns the size of Name object
201 */
202size_t
203Name::size () const
204{
205 return m_prefix.size ();
206}
207
208Name::iterator
209Name::begin ()
210{
211 return m_prefix.begin ();
212}
213
214/**
215 * @brief Get read-only begin() iterator
216 */
217Name::const_iterator
218Name::begin () const
219{
220 return m_prefix.begin ();
221}
222
223/**
224 * @brief Get read-write end() iterator
225 */
226Name::iterator
227Name::end ()
228{
229 return m_prefix.end ();
230}
231
232/**
233 * @brief Get read-only end() iterator
234 */
235Name::const_iterator
236Name::end () const
237{
238 return m_prefix.end ();
239}
240
241
242/**
243 * \brief Generic constructor operator
244 * The object of type T will be appended to the list of components
245 */
246template<class T>
247Name&
248Name::operator () (const T &value)
249{
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700250 return Add (value);
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800251}
252
253/**
254 * \brief Generic Add method
255 * Appends object of type T to the list of components
256 * @param[in] value The object to be appended
257 */
258template<class T>
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700259Name&
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800260Name::Add (const T &value)
261{
262 std::ostringstream os;
263 os << value;
264 m_prefix.push_back (os.str ());
Alexander Afanasyev8e436fa2013-03-17 14:16:32 -0700265
266 return *this;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800267}
268
269/**
270 * \brief Equality operator for Name
271 */
272bool
273Name::operator== (const Name &prefix) const
274{
275 if (m_prefix.size () != prefix.m_prefix.size ())
276 return false;
277
278 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
279}
280
281/**
282 * \brief Less than operator for Name
283 */
284bool
285Name::operator< (const Name &prefix) const
286{
287 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
288 prefix.m_prefix.begin (), prefix.m_prefix.end ());
289}
290
291ATTRIBUTE_HELPER_HEADER (Name);
292
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700293// for backwards compatibility
294typedef Name NameComponents;
Alexander Afanasyev32c07562013-02-01 12:58:43 -0800295
296} // namespace ndn
297} // namespace ns3
298
299#endif // _NDN_NAME_H_
300