Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 20 | */ |
| 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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 34 | #include <boost/ref.hpp> |
| 35 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 36 | namespace ns3 { |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 37 | namespace ndn { |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 38 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 39 | /** |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 40 | * \ingroup ndn |
| 41 | * \brief Hierarchical NDN name |
| 42 | * A Name element represents a hierarchical name for Ndn content. |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 43 | * 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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 48 | class NameComponents : public SimpleRefCount<NameComponents> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 49 | { |
| 50 | public: |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 51 | typedef std::list<std::string>::iterator iterator; |
| 52 | typedef std::list<std::string>::const_iterator const_iterator; |
| 53 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 54 | /** |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 55 | * \brief Constructor |
| 56 | * Creates a prefix with zero components (can be looked as root "/") |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 57 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 58 | NameComponents (); |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 59 | |
| 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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 65 | NameComponents (const std::list<boost::reference_wrapper<const std::string> > &components); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 66 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 67 | /** |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 68 | * @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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 72 | NameComponents (const std::string &prefix); |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 73 | |
| 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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 79 | NameComponents (const char *prefix); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 82 | * \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 Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 86 | template<class T> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 87 | inline void |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 88 | Add (const T &value); |
| 89 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 90 | /** |
| 91 | * \brief Generic constructor operator |
| 92 | * The object of type T will be appended to the list of components |
| 93 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 94 | template<class T> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 95 | inline NameComponents& |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 96 | operator () (const T &value); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 97 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 98 | /** |
| 99 | * \brief Get a name |
| 100 | * Returns a list of components (strings) |
| 101 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 102 | const std::list<std::string> & |
| 103 | GetComponents () const; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 105 | /** |
| 106 | * @brief Helper call to get the last component of the name |
| 107 | */ |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 108 | std::string |
| 109 | GetLastComponent () const; |
| 110 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 111 | /** |
| 112 | * \brief Get subcomponents of the name, starting with first component |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 113 | * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()] |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 114 | */ |
| 115 | std::list<boost::reference_wrapper<const std::string> > |
| 116 | GetSubComponents (size_t num) const; |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * @brief Get prefix of the name, containing less minusComponents right components |
| 120 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 121 | NameComponents |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 122 | cut (size_t minusComponents) const; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 123 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 124 | /** |
| 125 | * \brief Print name |
| 126 | * @param[in] os Stream to print |
| 127 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 128 | void Print (std::ostream &os) const; |
| 129 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 130 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 131 | * \brief Returns the size of NameComponents |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 132 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 133 | inline size_t |
| 134 | size () const; |
| 135 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 136 | /** |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 137 | * @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 Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 161 | * \brief Equality operator for NameComponents |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 162 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 163 | inline bool |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 164 | operator== (const NameComponents &prefix) const; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 165 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 166 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 167 | * \brief Less than operator for NameComponents |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 168 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 169 | inline bool |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 170 | operator< (const NameComponents &prefix) const; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 171 | |
| 172 | typedef std::string partial_type; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 173 | |
| 174 | private: |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 175 | std::list<std::string> m_prefix; ///< \brief a list of strings (components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 178 | /** |
| 179 | * \brief Print out name components separated by slashes, e.g., /first/second/third |
| 180 | */ |
| 181 | std::ostream & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 182 | operator << (std::ostream &os, const NameComponents &components); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 184 | /** |
| 185 | * \brief Read components from input and add them to components. Will read input stream till eof |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 186 | * Substrings separated by slashes will become separate components |
| 187 | */ |
| 188 | std::istream & |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 189 | operator >> (std::istream &is, NameComponents &components); |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 190 | |
| 191 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 192 | * \brief Returns the size of NameComponents object |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 193 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 194 | size_t |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 195 | NameComponents::size () const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 196 | { |
| 197 | return m_prefix.size (); |
| 198 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 199 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 200 | NameComponents::iterator |
| 201 | NameComponents::begin () |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 202 | { |
| 203 | return m_prefix.begin (); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * @brief Get read-only begin() iterator |
| 208 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 209 | NameComponents::const_iterator |
| 210 | NameComponents::begin () const |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 211 | { |
| 212 | return m_prefix.begin (); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @brief Get read-write end() iterator |
| 217 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 218 | NameComponents::iterator |
| 219 | NameComponents::end () |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 220 | { |
| 221 | return m_prefix.end (); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @brief Get read-only end() iterator |
| 226 | */ |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 227 | NameComponents::const_iterator |
| 228 | NameComponents::end () const |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 229 | { |
| 230 | return m_prefix.end (); |
| 231 | } |
| 232 | |
| 233 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 234 | /** |
| 235 | * \brief Generic constructor operator |
| 236 | * The object of type T will be appended to the list of components |
| 237 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 238 | template<class T> |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 239 | NameComponents& |
| 240 | NameComponents::operator () (const T &value) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 241 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 242 | Add (value); |
| 243 | return *this; |
| 244 | } |
| 245 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 246 | /** |
| 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 Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 251 | template<class T> |
| 252 | void |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 253 | NameComponents::Add (const T &value) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 254 | { |
| 255 | std::ostringstream os; |
| 256 | os << value; |
| 257 | m_prefix.push_back (os.str ()); |
| 258 | } |
| 259 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 260 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 261 | * \brief Equality operator for NameComponents |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 262 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 263 | bool |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 264 | NameComponents::operator== (const NameComponents &prefix) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 265 | { |
| 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 Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 272 | /** |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 273 | * \brief Less than operator for NameComponents |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 274 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 275 | bool |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 276 | NameComponents::operator< (const NameComponents &prefix) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 277 | { |
| 278 | return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (), |
| 279 | prefix.m_prefix.begin (), prefix.m_prefix.end ()); |
| 280 | } |
| 281 | |
| 282 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 283 | ATTRIBUTE_HELPER_HEADER (NameComponents); |
| 284 | |
| 285 | } // namespace ndn |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 286 | } // namespace ns3 |
| 287 | |
| 288 | #endif // _NDN_NAME_COMPONENTS_H_ |
| 289 | |