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