blob: 4a479acf4fb22af1840cd5c311976993867c976d [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"
33
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070034#include <boost/ref.hpp>
35
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070036namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037namespace ndn {
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070038
Ilya Moiseenko332add02011-12-24 17:21:25 -080039/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040 * \ingroup ndn
41 * \brief Hierarchical NDN name
42 * A Name element represents a hierarchical name for Ndn content.
Ilya Moiseenko332add02011-12-24 17:21:25 -080043 * It simply contains a sequence of Component elements.
44 * Each Component element contains a sequence of zero or more bytes.
45 * There are no restrictions on what byte sequences may be used.
46 * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
47 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048class NameComponents : public SimpleRefCount<NameComponents>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070049{
50public:
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -070051 typedef std::list<std::string>::iterator iterator;
52 typedef std::list<std::string>::const_iterator const_iterator;
53
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070054 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080055 * \brief Constructor
56 * Creates a prefix with zero components (can be looked as root "/")
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070057 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 NameComponents ();
Ilya Moiseenko332add02011-12-24 17:21:25 -080059
60 /**
61 * \brief Constructor
62 * Creates a prefix from a list of strings where every string represents a prefix component
63 * @param[in] components A list of strings
64 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 NameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080066
Ilya Moiseenko332add02011-12-24 17:21:25 -080067 /**
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070068 * @brief Constructor
69 * Creates a prefix from the string (string is parsed using operator>>)
70 * @param[in] prefix A string representation of a prefix
71 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072 NameComponents (const std::string &prefix);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070073
74 /**
75 * @brief Constructor
76 * Creates a prefix from the string (string is parsed using operator>>)
77 * @param[in] prefix A string representation of a prefix
78 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070079 NameComponents (const char *prefix);
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070080
81 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080082 * \brief Generic Add method
83 * Appends object of type T to the list of components
84 * @param[in] value The object to be appended
85 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070087 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080088 Add (const T &value);
89
Ilya Moiseenko332add02011-12-24 17:21:25 -080090 /**
91 * \brief Generic constructor operator
92 * The object of type T will be appended to the list of components
93 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080094 template<class T>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 inline NameComponents&
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070097
Ilya Moiseenko332add02011-12-24 17:21:25 -080098 /**
99 * \brief Get a name
100 * Returns a list of components (strings)
101 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700102 const std::list<std::string> &
103 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700104
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700105 /**
106 * @brief Helper call to get the last component of the name
107 */
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800108 std::string
109 GetLastComponent () const;
110
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700111 /**
112 * \brief Get subcomponents of the name, starting with first component
Ilya Moiseenko332add02011-12-24 17:21:25 -0800113 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700114 */
115 std::list<boost::reference_wrapper<const std::string> >
116 GetSubComponents (size_t num) const;
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700117
118 /**
119 * @brief Get prefix of the name, containing less minusComponents right components
120 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700121 NameComponents
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700122 cut (size_t minusComponents) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700123
Ilya Moiseenko332add02011-12-24 17:21:25 -0800124 /**
125 * \brief Print name
126 * @param[in] os Stream to print
127 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700128 void Print (std::ostream &os) const;
129
Ilya Moiseenko332add02011-12-24 17:21:25 -0800130 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700131 * \brief Returns the size of NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800132 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700133 inline size_t
134 size () const;
135
Ilya Moiseenko332add02011-12-24 17:21:25 -0800136 /**
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700137 * @brief Get read-write begin() iterator
138 */
139 inline iterator
140 begin ();
141
142 /**
143 * @brief Get read-only begin() iterator
144 */
145 inline const_iterator
146 begin () const;
147
148 /**
149 * @brief Get read-write end() iterator
150 */
151 inline iterator
152 end ();
153
154 /**
155 * @brief Get read-only end() iterator
156 */
157 inline const_iterator
158 end () const;
159
160 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700161 * \brief Equality operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800162 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700163 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700164 operator== (const NameComponents &prefix) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700165
Ilya Moiseenko332add02011-12-24 17:21:25 -0800166 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700167 * \brief Less than operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800168 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700169 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700170 operator< (const NameComponents &prefix) const;
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700171
172 typedef std::string partial_type;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700173
174private:
Ilya Moiseenko332add02011-12-24 17:21:25 -0800175 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700176};
177
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700178/**
179 * \brief Print out name components separated by slashes, e.g., /first/second/third
180 */
181std::ostream &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700182operator << (std::ostream &os, const NameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700183
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700184/**
185 * \brief Read components from input and add them to components. Will read input stream till eof
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700186 * Substrings separated by slashes will become separate components
187 */
188std::istream &
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700189operator >> (std::istream &is, NameComponents &components);
Ilya Moiseenko332add02011-12-24 17:21:25 -0800190
191/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192 * \brief Returns the size of NameComponents object
Ilya Moiseenko332add02011-12-24 17:21:25 -0800193 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700194size_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700195NameComponents::size () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700196{
197 return m_prefix.size ();
198}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800199
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700200NameComponents::iterator
201NameComponents::begin ()
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700202{
203 return m_prefix.begin ();
204}
205
206/**
207 * @brief Get read-only begin() iterator
208 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700209NameComponents::const_iterator
210NameComponents::begin () const
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700211{
212 return m_prefix.begin ();
213}
214
215/**
216 * @brief Get read-write end() iterator
217 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700218NameComponents::iterator
219NameComponents::end ()
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700220{
221 return m_prefix.end ();
222}
223
224/**
225 * @brief Get read-only end() iterator
226 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700227NameComponents::const_iterator
228NameComponents::end () const
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700229{
230 return m_prefix.end ();
231}
232
233
Ilya Moiseenko332add02011-12-24 17:21:25 -0800234/**
235 * \brief Generic constructor operator
236 * The object of type T will be appended to the list of components
237 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800238template<class T>
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700239NameComponents&
240NameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700241{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800242 Add (value);
243 return *this;
244}
245
Ilya Moiseenko332add02011-12-24 17:21:25 -0800246/**
247 * \brief Generic Add method
248 * Appends object of type T to the list of components
249 * @param[in] value The object to be appended
250 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800251template<class T>
252void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700253NameComponents::Add (const T &value)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800254{
255 std::ostringstream os;
256 os << value;
257 m_prefix.push_back (os.str ());
258}
259
Ilya Moiseenko332add02011-12-24 17:21:25 -0800260/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700261 * \brief Equality operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800262 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700263bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264NameComponents::operator== (const NameComponents &prefix) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700265{
266 if (m_prefix.size () != prefix.m_prefix.size ())
267 return false;
268
269 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
270}
271
Ilya Moiseenko332add02011-12-24 17:21:25 -0800272/**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700273 * \brief Less than operator for NameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800274 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700275bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700276NameComponents::operator< (const NameComponents &prefix) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700277{
278 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
279 prefix.m_prefix.begin (), prefix.m_prefix.end ());
280}
281
282
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700283ATTRIBUTE_HELPER_HEADER (NameComponents);
284
285} // namespace ndn
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700286} // namespace ns3
287
288#endif // _NDN_NAME_COMPONENTS_H_
289