blob: 553c140590666c6c8c8633fb070aed34b7677ca6 [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 {
37
Ilya Moiseenko332add02011-12-24 17:21:25 -080038/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070039 * \ingroup ndn
40 * \brief Hierarchical NDN name
41 * A Name element represents a hierarchical name for Ndn content.
Ilya Moiseenko332add02011-12-24 17:21:25 -080042 * It simply contains a sequence of Component elements.
43 * Each Component element contains a sequence of zero or more bytes.
44 * There are no restrictions on what byte sequences may be used.
45 * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
46 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070047class NdnNameComponents : public SimpleRefCount<NdnNameComponents>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070048{
49public:
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -070050 typedef std::list<std::string>::iterator iterator;
51 typedef std::list<std::string>::const_iterator const_iterator;
52
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070053 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080054 * \brief Constructor
55 * Creates a prefix with zero components (can be looked as root "/")
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070056 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070057 NdnNameComponents ();
Ilya Moiseenko332add02011-12-24 17:21:25 -080058
59 /**
60 * \brief Constructor
61 * Creates a prefix from a list of strings where every string represents a prefix component
62 * @param[in] components A list of strings
63 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070064 NdnNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080065
Ilya Moiseenko332add02011-12-24 17:21:25 -080066 /**
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070067 * @brief Constructor
68 * Creates a prefix from the string (string is parsed using operator>>)
69 * @param[in] prefix A string representation of a prefix
70 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070071 NdnNameComponents (const std::string &prefix);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070072
73 /**
74 * @brief Constructor
75 * Creates a prefix from the string (string is parsed using operator>>)
76 * @param[in] prefix A string representation of a prefix
77 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070078 NdnNameComponents (const char *prefix);
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070079
80 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080081 * \brief Generic Add method
82 * Appends object of type T to the list of components
83 * @param[in] value The object to be appended
84 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080085 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070086 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080087 Add (const T &value);
88
Ilya Moiseenko332add02011-12-24 17:21:25 -080089 /**
90 * \brief Generic constructor operator
91 * The object of type T will be appended to the list of components
92 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093 template<class T>
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070094 inline NdnNameComponents&
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070096
Ilya Moiseenko332add02011-12-24 17:21:25 -080097 /**
98 * \brief Get a name
99 * Returns a list of components (strings)
100 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700101 const std::list<std::string> &
102 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700103
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700104 /**
105 * @brief Helper call to get the last component of the name
106 */
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800107 std::string
108 GetLastComponent () const;
109
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700110 /**
111 * \brief Get subcomponents of the name, starting with first component
Ilya Moiseenko332add02011-12-24 17:21:25 -0800112 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700113 */
114 std::list<boost::reference_wrapper<const std::string> >
115 GetSubComponents (size_t num) const;
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700116
117 /**
118 * @brief Get prefix of the name, containing less minusComponents right components
119 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700120 NdnNameComponents
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700121 cut (size_t minusComponents) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700122
Ilya Moiseenko332add02011-12-24 17:21:25 -0800123 /**
124 * \brief Print name
125 * @param[in] os Stream to print
126 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700127 void Print (std::ostream &os) const;
128
Ilya Moiseenko332add02011-12-24 17:21:25 -0800129 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700130 * \brief Returns the size of NdnNameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800131 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700132 inline size_t
133 size () const;
134
Ilya Moiseenko332add02011-12-24 17:21:25 -0800135 /**
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700136 * @brief Get read-write begin() iterator
137 */
138 inline iterator
139 begin ();
140
141 /**
142 * @brief Get read-only begin() iterator
143 */
144 inline const_iterator
145 begin () const;
146
147 /**
148 * @brief Get read-write end() iterator
149 */
150 inline iterator
151 end ();
152
153 /**
154 * @brief Get read-only end() iterator
155 */
156 inline const_iterator
157 end () const;
158
159 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700160 * \brief Equality operator for NdnNameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800161 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700162 inline bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700163 operator== (const NdnNameComponents &prefix) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700164
Ilya Moiseenko332add02011-12-24 17:21:25 -0800165 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700166 * \brief Less than operator for NdnNameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800167 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700168 inline bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700169 operator< (const NdnNameComponents &prefix) const;
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700170
171 typedef std::string partial_type;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700172
173private:
Ilya Moiseenko332add02011-12-24 17:21:25 -0800174 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700175};
176
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700177/**
178 * \brief Print out name components separated by slashes, e.g., /first/second/third
179 */
180std::ostream &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700181operator << (std::ostream &os, const NdnNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700182
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700183/**
184 * \brief Read components from input and add them to components. Will read input stream till eof
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700185 * Substrings separated by slashes will become separate components
186 */
187std::istream &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700188operator >> (std::istream &is, NdnNameComponents &components);
Ilya Moiseenko332add02011-12-24 17:21:25 -0800189
190/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700191 * \brief Returns the size of NdnNameComponents object
Ilya Moiseenko332add02011-12-24 17:21:25 -0800192 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700193size_t
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700194NdnNameComponents::size () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700195{
196 return m_prefix.size ();
197}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800198
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700199NdnNameComponents::iterator
200NdnNameComponents::begin ()
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700201{
202 return m_prefix.begin ();
203}
204
205/**
206 * @brief Get read-only begin() iterator
207 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700208NdnNameComponents::const_iterator
209NdnNameComponents::begin () const
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700210{
211 return m_prefix.begin ();
212}
213
214/**
215 * @brief Get read-write end() iterator
216 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700217NdnNameComponents::iterator
218NdnNameComponents::end ()
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700219{
220 return m_prefix.end ();
221}
222
223/**
224 * @brief Get read-only end() iterator
225 */
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700226NdnNameComponents::const_iterator
227NdnNameComponents::end () const
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700228{
229 return m_prefix.end ();
230}
231
232
Ilya Moiseenko332add02011-12-24 17:21:25 -0800233/**
234 * \brief Generic constructor operator
235 * The object of type T will be appended to the list of components
236 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800237template<class T>
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700238NdnNameComponents&
239NdnNameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700240{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800241 Add (value);
242 return *this;
243}
244
Ilya Moiseenko332add02011-12-24 17:21:25 -0800245/**
246 * \brief Generic Add method
247 * Appends object of type T to the list of components
248 * @param[in] value The object to be appended
249 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800250template<class T>
251void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700252NdnNameComponents::Add (const T &value)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800253{
254 std::ostringstream os;
255 os << value;
256 m_prefix.push_back (os.str ());
257}
258
Ilya Moiseenko332add02011-12-24 17:21:25 -0800259/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700260 * \brief Equality operator for NdnNameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800261 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700262bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700263NdnNameComponents::operator== (const NdnNameComponents &prefix) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700264{
265 if (m_prefix.size () != prefix.m_prefix.size ())
266 return false;
267
268 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
269}
270
Ilya Moiseenko332add02011-12-24 17:21:25 -0800271/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700272 * \brief Less than operator for NdnNameComponents
Ilya Moiseenko332add02011-12-24 17:21:25 -0800273 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700274bool
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700275NdnNameComponents::operator< (const NdnNameComponents &prefix) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700276{
277 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
278 prefix.m_prefix.begin (), prefix.m_prefix.end ());
279}
280
281
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700282ATTRIBUTE_HELPER_HEADER (NdnNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700283} // namespace ns3
284
285#endif // _NDN_NAME_COMPONENTS_H_
286