blob: c2076d1b7cb59b8cf10b84faa59ff76f915cad15 [file] [log] [blame]
Ilya Moiseenkod26e6822011-08-23 17:48:38 -07001/* -*- 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 *
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070020 */
21
22#ifndef _NDN_NAME_COMPONENTS_H_
23#define _NDN_NAME_COMPONENTS_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"
Alexander Afanasyev5d79e682012-11-19 14:12:23 -080033#include "ns3/buffer.h"
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070034
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070035#include <boost/ref.hpp>
36
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070037namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038namespace ndn {
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070039
Ilya Moiseenko332add02011-12-24 17:21:25 -080040/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070041 * \ingroup ndn
42 * \brief Hierarchical NDN name
43 * A Name element represents a hierarchical name for Ndn content.
Ilya Moiseenko332add02011-12-24 17:21:25 -080044 * 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 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049class NameComponents : public SimpleRefCount<NameComponents>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070050{
51public:
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -070052 typedef std::list<std::string>::iterator iterator;
53 typedef std::list<std::string>::const_iterator const_iterator;
54
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070055 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080056 * \brief Constructor
57 * Creates a prefix with zero components (can be looked as root "/")
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070058 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 NameComponents ();
Ilya Moiseenko332add02011-12-24 17:21:25 -080060
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 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070066 NameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067
Ilya Moiseenko332add02011-12-24 17:21:25 -080068 /**
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070069 * @brief Constructor
70 * Creates a prefix from the string (string is parsed using operator>>)
71 * @param[in] prefix A string representation of a prefix
72 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073 NameComponents (const std::string &prefix);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070074
75 /**
76 * @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 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070080 NameComponents (const char *prefix);
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070081
82 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080083 * \brief Generic Add method
84 * Appends object of type T to the list of components
85 * @param[in] value The object to be appended
86 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080087 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070088 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080089 Add (const T &value);
90
Ilya Moiseenko332add02011-12-24 17:21:25 -080091 /**
92 * \brief Generic constructor operator
93 * The object of type T will be appended to the list of components
94 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 template<class T>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 inline NameComponents&
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070098
Ilya Moiseenko332add02011-12-24 17:21:25 -080099 /**
100 * \brief Get a name
101 * Returns a list of components (strings)
102 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700103 const std::list<std::string> &
104 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700105
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700106 /**
107 * @brief Helper call to get the last component of the name
108 */
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800109 std::string
110 GetLastComponent () const;
111
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700112 /**
113 * \brief Get subcomponents of the name, starting with first component
Ilya Moiseenko332add02011-12-24 17:21:25 -0800114 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700115 */
116 std::list<boost::reference_wrapper<const std::string> >
117 GetSubComponents (size_t num) const;
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700118
119 /**
120 * @brief Get prefix of the name, containing less minusComponents right components
121 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700122 NameComponents
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700123 cut (size_t minusComponents) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700124
Ilya Moiseenko332add02011-12-24 17:21:25 -0800125 /**
126 * \brief Print name
127 * @param[in] os Stream to print
128 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700129 void Print (std::ostream &os) const;
130
Ilya Moiseenko332add02011-12-24 17:21:25 -0800131 /**
Alexander Afanasyev5d79e682012-11-19 14:12:23 -0800132 * @brief Get serialized size for ndnSIM packet encoding
133 */
134 size_t
135 GetSerializedSize () const;
136
137 /**
138 * @brief Serialize Name in ndnSIM packet encoding
139 * @param[in] start buffer to contain serialized name
140 */
141 uint32_t
142 Serialize (Buffer::Iterator start) const;
143
144 /**
145 * \brief Deserialize Name in ndnSIM packet encoding
146 * @param[in] start buffer that contains serialized name
147 */
148 uint32_t
149 Deserialize (Buffer::Iterator start);
150
151 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700152 * \brief Returns the size of NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800153 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700154 inline size_t
155 size () const;
156
Ilya Moiseenko332add02011-12-24 17:21:25 -0800157 /**
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700158 * @brief Get read-write begin() iterator
159 */
160 inline iterator
161 begin ();
162
163 /**
164 * @brief Get read-only begin() iterator
165 */
166 inline const_iterator
167 begin () const;
168
169 /**
170 * @brief Get read-write end() iterator
171 */
172 inline iterator
173 end ();
174
175 /**
176 * @brief Get read-only end() iterator
177 */
178 inline const_iterator
179 end () const;
180
181 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700182 * \brief Equality operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800183 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700184 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700185 operator== (const NameComponents &prefix) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700186
Ilya Moiseenko332add02011-12-24 17:21:25 -0800187 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700188 * \brief Less than operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800189 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700190 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700191 operator< (const NameComponents &prefix) const;
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700192
193 typedef std::string partial_type;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700194
195private:
Ilya Moiseenko332add02011-12-24 17:21:25 -0800196 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700197};
198
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700199/**
200 * \brief Print out name components separated by slashes, e.g., /first/second/third
201 */
202std::ostream &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700203operator << (std::ostream &os, const NameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700204
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700205/**
206 * \brief Read components from input and add them to components. Will read input stream till eof
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700207 * Substrings separated by slashes will become separate components
208 */
209std::istream &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700210operator >> (std::istream &is, NameComponents &components);
Ilya Moiseenko332add02011-12-24 17:21:25 -0800211
212/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700213 * \brief Returns the size of NameComponents object
Ilya Moiseenko332add02011-12-24 17:21:25 -0800214 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700215size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700216NameComponents::size () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700217{
218 return m_prefix.size ();
219}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800220
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700221NameComponents::iterator
222NameComponents::begin ()
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700223{
224 return m_prefix.begin ();
225}
226
227/**
228 * @brief Get read-only begin() iterator
229 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230NameComponents::const_iterator
231NameComponents::begin () const
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700232{
233 return m_prefix.begin ();
234}
235
236/**
237 * @brief Get read-write end() iterator
238 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700239NameComponents::iterator
240NameComponents::end ()
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700241{
242 return m_prefix.end ();
243}
244
245/**
246 * @brief Get read-only end() iterator
247 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700248NameComponents::const_iterator
249NameComponents::end () const
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700250{
251 return m_prefix.end ();
252}
253
254
Ilya Moiseenko332add02011-12-24 17:21:25 -0800255/**
256 * \brief Generic constructor operator
257 * The object of type T will be appended to the list of components
258 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800259template<class T>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700260NameComponents&
261NameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700262{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800263 Add (value);
264 return *this;
265}
266
Ilya Moiseenko332add02011-12-24 17:21:25 -0800267/**
268 * \brief Generic Add method
269 * Appends object of type T to the list of components
270 * @param[in] value The object to be appended
271 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800272template<class T>
273void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700274NameComponents::Add (const T &value)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800275{
276 std::ostringstream os;
277 os << value;
278 m_prefix.push_back (os.str ());
279}
280
Ilya Moiseenko332add02011-12-24 17:21:25 -0800281/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700282 * \brief Equality operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800283 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700284bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700285NameComponents::operator== (const NameComponents &prefix) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700286{
287 if (m_prefix.size () != prefix.m_prefix.size ())
288 return false;
289
290 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
291}
292
Ilya Moiseenko332add02011-12-24 17:21:25 -0800293/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700294 * \brief Less than operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800295 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700296bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700297NameComponents::operator< (const NameComponents &prefix) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700298{
299 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
300 prefix.m_prefix.begin (), prefix.m_prefix.end ());
301}
302
303
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700304ATTRIBUTE_HELPER_HEADER (NameComponents);
305
306} // namespace ndn
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700307} // namespace ns3
308
309#endif // _NDN_NAME_COMPONENTS_H_
310