blob: eb4f8c16f49b10f1daffa350f5a9f83b3c7a6fc1 [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);
71
72 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080073 * \brief Generic Add method
74 * Appends object of type T to the list of components
75 * @param[in] value The object to be appended
76 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070078 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080079 Add (const T &value);
80
Ilya Moiseenko332add02011-12-24 17:21:25 -080081 /**
82 * \brief Generic constructor operator
83 * The object of type T will be appended to the list of components
84 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080085 template<class T>
86 inline CcnxNameComponents&
87 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070088
Ilya Moiseenko332add02011-12-24 17:21:25 -080089 /**
90 * \brief Get a name
91 * Returns a list of components (strings)
92 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070093 const std::list<std::string> &
94 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070095
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070096 /**
97 * @brief Helper call to get the last component of the name
98 */
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080099 std::string
100 GetLastComponent () const;
101
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700102 /**
103 * \brief Get subcomponents of the name, starting with first component
Ilya Moiseenko332add02011-12-24 17:21:25 -0800104 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700105 */
106 std::list<boost::reference_wrapper<const std::string> >
107 GetSubComponents (size_t num) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700108
Ilya Moiseenko332add02011-12-24 17:21:25 -0800109 /**
110 * \brief Print name
111 * @param[in] os Stream to print
112 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700113 void Print (std::ostream &os) const;
114
Ilya Moiseenko332add02011-12-24 17:21:25 -0800115 /**
116 * \brief Returns the size of CcnxNameComponents
117 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700118 inline size_t
119 size () const;
120
Ilya Moiseenko332add02011-12-24 17:21:25 -0800121 /**
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700122 * @brief Get read-write begin() iterator
123 */
124 inline iterator
125 begin ();
126
127 /**
128 * @brief Get read-only begin() iterator
129 */
130 inline const_iterator
131 begin () const;
132
133 /**
134 * @brief Get read-write end() iterator
135 */
136 inline iterator
137 end ();
138
139 /**
140 * @brief Get read-only end() iterator
141 */
142 inline const_iterator
143 end () const;
144
145 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -0800146 * \brief Equality operator for CcnxNameComponents
147 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700148 inline bool
149 operator== (const CcnxNameComponents &prefix) const;
150
Ilya Moiseenko332add02011-12-24 17:21:25 -0800151 /**
152 * \brief Less than operator for CcnxNameComponents
153 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700154 inline bool
155 operator< (const CcnxNameComponents &prefix) const;
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700156
157 typedef std::string partial_type;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700158
159private:
Ilya Moiseenko332add02011-12-24 17:21:25 -0800160 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700161};
162
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700163/**
164 * \brief Print out name components separated by slashes, e.g., /first/second/third
165 */
166std::ostream &
167operator << (std::ostream &os, const CcnxNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700168
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700169/**
170 * \brief Read components from input and add them to components. Will read input stream till eof
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700171 * Substrings separated by slashes will become separate components
172 */
173std::istream &
174operator >> (std::istream &is, CcnxNameComponents &components);
Ilya Moiseenko332add02011-12-24 17:21:25 -0800175
176/**
177 * \brief Returns the size of CcnxNameComponents object
178 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700179size_t
180CcnxNameComponents::size () const
181{
182 return m_prefix.size ();
183}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800184
Alexander Afanasyevfd0c41c2012-06-11 22:15:49 -0700185CcnxNameComponents::iterator
186CcnxNameComponents::begin ()
187{
188 return m_prefix.begin ();
189}
190
191/**
192 * @brief Get read-only begin() iterator
193 */
194CcnxNameComponents::const_iterator
195CcnxNameComponents::begin () const
196{
197 return m_prefix.begin ();
198}
199
200/**
201 * @brief Get read-write end() iterator
202 */
203CcnxNameComponents::iterator
204CcnxNameComponents::end ()
205{
206 return m_prefix.end ();
207}
208
209/**
210 * @brief Get read-only end() iterator
211 */
212CcnxNameComponents::const_iterator
213CcnxNameComponents::end () const
214{
215 return m_prefix.end ();
216}
217
218
Ilya Moiseenko332add02011-12-24 17:21:25 -0800219/**
220 * \brief Generic constructor operator
221 * The object of type T will be appended to the list of components
222 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800223template<class T>
224CcnxNameComponents&
225CcnxNameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700226{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800227 Add (value);
228 return *this;
229}
230
Ilya Moiseenko332add02011-12-24 17:21:25 -0800231/**
232 * \brief Generic Add method
233 * Appends object of type T to the list of components
234 * @param[in] value The object to be appended
235 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800236template<class T>
237void
238CcnxNameComponents::Add (const T &value)
239{
240 std::ostringstream os;
241 os << value;
242 m_prefix.push_back (os.str ());
243}
244
Ilya Moiseenko332add02011-12-24 17:21:25 -0800245/**
246 * \brief Equality operator for CcnxNameComponents
247 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700248bool
249CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const
250{
251 if (m_prefix.size () != prefix.m_prefix.size ())
252 return false;
253
254 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
255}
256
Ilya Moiseenko332add02011-12-24 17:21:25 -0800257/**
258 * \brief Less than operator for CcnxNameComponents
259 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700260bool
261CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const
262{
263 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
264 prefix.m_prefix.begin (), prefix.m_prefix.end ());
265}
266
267
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700268ATTRIBUTE_HELPER_HEADER (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700269} // namespace ns3
270
271#endif // _NDN_NAME_COMPONENTS_H_
272