blob: 54f999856b23b08416dda37d9121cc657fc7d7a6 [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 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#ifndef _NDN_NAME_COMPONENTS_H_
22#define _NDN_NAME_COMPONENTS_H_
23
24#include "ns3/simple-ref-count.h"
25#include "ns3/attribute.h"
26#include "ns3/attribute-helper.h"
27
28#include <string>
29#include <algorithm>
30#include <list>
31#include "ns3/object.h"
32
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070033#include <boost/ref.hpp>
34
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070035namespace ns3 {
36
Ilya Moiseenko332add02011-12-24 17:21:25 -080037/**
38 * \ingroup ccnx
39 * \brief Hierarchical CCNX name
40 * A Name element represents a hierarchical name for CCNx content.
41 * It simply contains a sequence of Component elements.
42 * Each Component element contains a sequence of zero or more bytes.
43 * There are no restrictions on what byte sequences may be used.
44 * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
45 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070046class CcnxNameComponents : public SimpleRefCount<CcnxNameComponents>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070047{
48public:
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -070049 typedef std::list<std::string>::iterator iterator;
50 typedef std::list<std::string>::const_iterator const_iterator;
51
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070052 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080053 * \brief Constructor
54 * Creates a prefix with zero components (can be looked as root "/")
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070055 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070056 CcnxNameComponents ();
Ilya Moiseenko332add02011-12-24 17:21:25 -080057
58 /**
59 * \brief Constructor
60 * Creates a prefix from a list of strings where every string represents a prefix component
61 * @param[in] components A list of strings
62 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070063 CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080064
Ilya Moiseenko332add02011-12-24 17:21:25 -080065 /**
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070066 * @brief Constructor
67 * Creates a prefix from the string (string is parsed using operator>>)
68 * @param[in] prefix A string representation of a prefix
69 */
70 CcnxNameComponents (const std::string &prefix);
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070071
72 /**
73 * @brief Constructor
74 * Creates a prefix from the string (string is parsed using operator>>)
75 * @param[in] prefix A string representation of a prefix
76 */
77 CcnxNameComponents (const char *prefix);
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070078
79 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080080 * \brief Generic Add method
81 * Appends object of type T to the list of components
82 * @param[in] value The object to be appended
83 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080084 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070085 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086 Add (const T &value);
87
Ilya Moiseenko332add02011-12-24 17:21:25 -080088 /**
89 * \brief Generic constructor operator
90 * The object of type T will be appended to the list of components
91 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080092 template<class T>
93 inline CcnxNameComponents&
94 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070095
Ilya Moiseenko332add02011-12-24 17:21:25 -080096 /**
97 * \brief Get a name
98 * Returns a list of components (strings)
99 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700100 const std::list<std::string> &
101 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700102
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700103 /**
104 * @brief Helper call to get the last component of the name
105 */
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -0800106 std::string
107 GetLastComponent () const;
108
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700109 /**
110 * \brief Get subcomponents of the name, starting with first component
Ilya Moiseenko332add02011-12-24 17:21:25 -0800111 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700112 */
113 std::list<boost::reference_wrapper<const std::string> >
114 GetSubComponents (size_t num) const;
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -0700115
116 /**
117 * @brief Get prefix of the name, containing less minusComponents right components
118 */
119 CcnxNameComponents
120 cut (size_t minusComponents) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700121
Ilya Moiseenko332add02011-12-24 17:21:25 -0800122 /**
123 * \brief Print name
124 * @param[in] os Stream to print
125 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700126 void Print (std::ostream &os) const;
127
Ilya Moiseenko332add02011-12-24 17:21:25 -0800128 /**
129 * \brief Returns the size of CcnxNameComponents
130 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700131 inline size_t
132 size () const;
133
Ilya Moiseenko332add02011-12-24 17:21:25 -0800134 /**
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700135 * @brief Get read-write begin() iterator
136 */
137 inline iterator
138 begin ();
139
140 /**
141 * @brief Get read-only begin() iterator
142 */
143 inline const_iterator
144 begin () const;
145
146 /**
147 * @brief Get read-write end() iterator
148 */
149 inline iterator
150 end ();
151
152 /**
153 * @brief Get read-only end() iterator
154 */
155 inline const_iterator
156 end () const;
157
158 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -0800159 * \brief Equality operator for CcnxNameComponents
160 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700161 inline bool
162 operator== (const CcnxNameComponents &prefix) const;
163
Ilya Moiseenko332add02011-12-24 17:21:25 -0800164 /**
165 * \brief Less than operator for CcnxNameComponents
166 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700167 inline bool
168 operator< (const CcnxNameComponents &prefix) const;
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700169
170 typedef std::string partial_type;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700171
172private:
Ilya Moiseenko332add02011-12-24 17:21:25 -0800173 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700174};
175
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700176/**
177 * \brief Print out name components separated by slashes, e.g., /first/second/third
178 */
179std::ostream &
180operator << (std::ostream &os, const CcnxNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700181
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700182/**
183 * \brief Read components from input and add them to components. Will read input stream till eof
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700184 * Substrings separated by slashes will become separate components
185 */
186std::istream &
187operator >> (std::istream &is, CcnxNameComponents &components);
Ilya Moiseenko332add02011-12-24 17:21:25 -0800188
189/**
190 * \brief Returns the size of CcnxNameComponents object
191 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700192size_t
193CcnxNameComponents::size () const
194{
195 return m_prefix.size ();
196}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800197
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700198CcnxNameComponents::iterator
199CcnxNameComponents::begin ()
200{
201 return m_prefix.begin ();
202}
203
204/**
205 * @brief Get read-only begin() iterator
206 */
207CcnxNameComponents::const_iterator
208CcnxNameComponents::begin () const
209{
210 return m_prefix.begin ();
211}
212
213/**
214 * @brief Get read-write end() iterator
215 */
216CcnxNameComponents::iterator
217CcnxNameComponents::end ()
218{
219 return m_prefix.end ();
220}
221
222/**
223 * @brief Get read-only end() iterator
224 */
225CcnxNameComponents::const_iterator
226CcnxNameComponents::end () const
227{
228 return m_prefix.end ();
229}
230
231
Ilya Moiseenko332add02011-12-24 17:21:25 -0800232/**
233 * \brief Generic constructor operator
234 * The object of type T will be appended to the list of components
235 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800236template<class T>
237CcnxNameComponents&
238CcnxNameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700239{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800240 Add (value);
241 return *this;
242}
243
Ilya Moiseenko332add02011-12-24 17:21:25 -0800244/**
245 * \brief Generic Add method
246 * Appends object of type T to the list of components
247 * @param[in] value The object to be appended
248 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800249template<class T>
250void
251CcnxNameComponents::Add (const T &value)
252{
253 std::ostringstream os;
254 os << value;
255 m_prefix.push_back (os.str ());
256}
257
Ilya Moiseenko332add02011-12-24 17:21:25 -0800258/**
259 * \brief Equality operator for CcnxNameComponents
260 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700261bool
262CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const
263{
264 if (m_prefix.size () != prefix.m_prefix.size ())
265 return false;
266
267 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
268}
269
Ilya Moiseenko332add02011-12-24 17:21:25 -0800270/**
271 * \brief Less than operator for CcnxNameComponents
272 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700273bool
274CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const
275{
276 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
277 prefix.m_prefix.begin (), prefix.m_prefix.end ());
278}
279
280
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700281ATTRIBUTE_HELPER_HEADER (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700282} // namespace ns3
283
284#endif // _NDN_NAME_COMPONENTS_H_
285