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