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