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 | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 49 | /** |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 50 | * \brief Constructor |
| 51 | * Creates a prefix with zero components (can be looked as root "/") |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 52 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 53 | CcnxNameComponents (); |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * \brief Constructor |
| 57 | * Creates a prefix from a list of strings where every string represents a prefix component |
| 58 | * @param[in] components A list of strings |
| 59 | */ |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 60 | CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 61 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 62 | /** |
| 63 | * \brief Generic Add method |
| 64 | * Appends object of type T to the list of components |
| 65 | * @param[in] value The object to be appended |
| 66 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 67 | template<class T> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 68 | inline void |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 69 | Add (const T &value); |
| 70 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 71 | /** |
| 72 | * \brief Generic constructor operator |
| 73 | * The object of type T will be appended to the list of components |
| 74 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 75 | template<class T> |
| 76 | inline CcnxNameComponents& |
| 77 | operator () (const T &value); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 78 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 79 | /** |
| 80 | * \brief Get a name |
| 81 | * Returns a list of components (strings) |
| 82 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 83 | const std::list<std::string> & |
| 84 | GetComponents () const; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 86 | /** |
| 87 | * @brief Helper call to get the last component of the name |
| 88 | */ |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 89 | std::string |
| 90 | GetLastComponent () const; |
| 91 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 92 | /** |
| 93 | * \brief Get subcomponents of the name, starting with first component |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 94 | * @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] | 95 | */ |
| 96 | std::list<boost::reference_wrapper<const std::string> > |
| 97 | GetSubComponents (size_t num) const; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 98 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 99 | /** |
| 100 | * \brief Print name |
| 101 | * @param[in] os Stream to print |
| 102 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 103 | void Print (std::ostream &os) const; |
| 104 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 105 | /** |
| 106 | * \brief Returns the size of CcnxNameComponents |
| 107 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 108 | inline size_t |
| 109 | size () const; |
| 110 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 111 | /** |
| 112 | * \brief Equality operator for CcnxNameComponents |
| 113 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 114 | inline bool |
| 115 | operator== (const CcnxNameComponents &prefix) const; |
| 116 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 117 | /** |
| 118 | * \brief Less than operator for CcnxNameComponents |
| 119 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 120 | inline bool |
| 121 | operator< (const CcnxNameComponents &prefix) const; |
| 122 | |
| 123 | private: |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 124 | std::list<std::string> m_prefix; ///< \brief a list of strings (components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 125 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 126 | typedef std::list<std::string>::iterator iterator; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 127 | typedef std::list<std::string>::const_iterator const_iterator; |
| 128 | }; |
| 129 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 130 | /** |
| 131 | * \brief Print out name components separated by slashes, e.g., /first/second/third |
| 132 | */ |
| 133 | std::ostream & |
| 134 | operator << (std::ostream &os, const CcnxNameComponents &components); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 136 | /** |
| 137 | * \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] | 138 | * Substrings separated by slashes will become separate components |
| 139 | */ |
| 140 | std::istream & |
| 141 | operator >> (std::istream &is, CcnxNameComponents &components); |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * \brief Returns the size of CcnxNameComponents object |
| 145 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 146 | size_t |
| 147 | CcnxNameComponents::size () const |
| 148 | { |
| 149 | return m_prefix.size (); |
| 150 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 151 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 152 | /** |
| 153 | * \brief Generic constructor operator |
| 154 | * The object of type T will be appended to the list of components |
| 155 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 156 | template<class T> |
| 157 | CcnxNameComponents& |
| 158 | CcnxNameComponents::operator () (const T &value) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 159 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 160 | Add (value); |
| 161 | return *this; |
| 162 | } |
| 163 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 164 | /** |
| 165 | * \brief Generic Add method |
| 166 | * Appends object of type T to the list of components |
| 167 | * @param[in] value The object to be appended |
| 168 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 169 | template<class T> |
| 170 | void |
| 171 | CcnxNameComponents::Add (const T &value) |
| 172 | { |
| 173 | std::ostringstream os; |
| 174 | os << value; |
| 175 | m_prefix.push_back (os.str ()); |
| 176 | } |
| 177 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 178 | /** |
| 179 | * \brief Equality operator for CcnxNameComponents |
| 180 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 181 | bool |
| 182 | CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const |
| 183 | { |
| 184 | if (m_prefix.size () != prefix.m_prefix.size ()) |
| 185 | return false; |
| 186 | |
| 187 | return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ()); |
| 188 | } |
| 189 | |
Ilya Moiseenko | 332add0 | 2011-12-24 17:21:25 -0800 | [diff] [blame] | 190 | /** |
| 191 | * \brief Less than operator for CcnxNameComponents |
| 192 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 193 | bool |
| 194 | CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const |
| 195 | { |
| 196 | return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (), |
| 197 | prefix.m_prefix.begin (), prefix.m_prefix.end ()); |
| 198 | } |
| 199 | |
| 200 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 201 | ATTRIBUTE_HELPER_HEADER (CcnxNameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 202 | } // namespace ns3 |
| 203 | |
| 204 | #endif // _NDN_NAME_COMPONENTS_H_ |
| 205 | |