Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 23 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifndef NDN_NAME_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 27 | #define NDN_NAME_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 29 | #include "common.hpp" |
| 30 | #include "name-component.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 31 | #include "util/time.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 33 | #include "encoding/block.hpp" |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 34 | #include "encoding/encoding-buffer.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 36 | #include <boost/iterator/reverse_iterator.hpp> |
| 37 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 38 | namespace ndn { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 39 | |
Jeff Thompson | c7d6550 | 2013-11-06 17:22:26 -0800 | [diff] [blame] | 40 | /** |
| 41 | * A Name holds an array of Name::Component and represents an NDN name. |
| 42 | */ |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 43 | class Name : public enable_shared_from_this<Name> |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 44 | { |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 45 | public: |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 46 | /// @brief Error that can be thrown from Name |
| 47 | class Error : public name::Component::Error |
| 48 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 49 | public: |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 50 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 51 | Error(const std::string& what) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 52 | : name::Component::Error(what) |
| 53 | { |
| 54 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 55 | }; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 57 | typedef name::Component Component; |
| 58 | |
| 59 | typedef std::vector<Component> component_container; |
| 60 | |
| 61 | typedef Component value_type; |
| 62 | typedef void allocator_type; |
| 63 | typedef Component& reference; |
| 64 | typedef const Component const_reference; |
| 65 | typedef Component* pointer; |
| 66 | typedef const Component* const_pointer; |
| 67 | typedef Component* iterator; |
| 68 | typedef const Component* const_iterator; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 70 | typedef boost::reverse_iterator<iterator> reverse_iterator; |
| 71 | typedef boost::reverse_iterator<const_iterator> const_reverse_iterator; |
| 72 | |
| 73 | typedef component_container::difference_type difference_type; |
| 74 | typedef component_container::size_type size_type; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 75 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Create a new Name with no components. |
| 78 | */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 79 | Name() |
| 80 | : m_nameBlock(Tlv::Name) |
| 81 | { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 82 | } |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * @brief Create Name object from wire block |
| 86 | * |
| 87 | * This is a more efficient equivalent for |
| 88 | * @code |
| 89 | * Name name; |
| 90 | * name.wireDecode(wire); |
| 91 | * @endcode |
| 92 | */ |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 93 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 94 | Name(const Block& wire) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 95 | { |
| 96 | m_nameBlock = wire; |
| 97 | m_nameBlock.parse(); |
| 98 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 99 | |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 100 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 101 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 102 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 103 | */ |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 104 | Name(const char* uri) |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 105 | { |
| 106 | set(uri); |
| 107 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 108 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 109 | /** |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 110 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
| 111 | * @param uri The URI string. |
| 112 | */ |
| 113 | Name(const std::string& uri) |
| 114 | { |
| 115 | set(uri.c_str()); |
| 116 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 118 | /** |
| 119 | * @brief Fast encoding or block size estimation |
| 120 | */ |
| 121 | template<bool T> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 122 | size_t |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 123 | wireEncode(EncodingImpl<T>& block) const; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 124 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 125 | const Block& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 126 | wireEncode() const; |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 127 | |
| 128 | void |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 129 | wireDecode(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * @brief Check if already has wire |
| 133 | */ |
| 134 | bool |
| 135 | hasWire() const; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 136 | |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 137 | /** |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 138 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 139 | * @param uri The null-terminated URI string. |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 140 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 141 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 142 | set(const char* uri); |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 145 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
| 146 | * @param uri The URI string. |
| 147 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 148 | void |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 149 | set(const std::string& uri) |
| 150 | { |
| 151 | set(uri.c_str()); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 154 | /** |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 155 | * Append a new component, copying from value of length valueLength. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 156 | * @return This name so that you can chain calls to append. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 157 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 158 | Name& |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 159 | append(const uint8_t* value, size_t valueLength) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 160 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 161 | m_nameBlock.push_back(Component(value, valueLength)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 162 | return *this; |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 163 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 164 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Append a new component, copying from value of length valueLength. |
| 167 | * @return This name so that you can chain calls to append. |
| 168 | */ |
| 169 | template<class InputIterator> |
| 170 | Name& |
| 171 | append(InputIterator begin, InputIterator end) |
| 172 | { |
| 173 | m_nameBlock.push_back(Component(begin, end)); |
| 174 | return *this; |
| 175 | } |
| 176 | |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 177 | Name& |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 178 | append(const Component& value) |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 179 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 180 | m_nameBlock.push_back(value); |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 181 | return *this; |
| 182 | } |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 184 | /** |
| 185 | * @brief Append name component that represented as a string |
| 186 | * |
| 187 | * Note that this method is necessary to ensure correctness and unambiguity of |
| 188 | * ``append("string")`` operations (both Component and Name can be implicitly |
| 189 | * converted from string, each having different outcomes |
| 190 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 191 | Name& |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 192 | append(const char* value) |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 193 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 194 | m_nameBlock.push_back(Component(value)); |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 195 | return *this; |
| 196 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 197 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 198 | Name& |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 199 | append(const Block& value) |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 200 | { |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 201 | if (value.type() == Tlv::NameComponent) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 202 | m_nameBlock.push_back(value); |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 203 | else |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 204 | m_nameBlock.push_back(Block(Tlv::NameComponent, value)); |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 206 | return *this; |
| 207 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 209 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 210 | * Append the components of the given name to this name. |
| 211 | * @param name The Name with components to append. |
| 212 | * @return This name so that you can chain calls to append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 213 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 214 | Name& |
| 215 | append(const Name& name); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 216 | |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 217 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 218 | * Clear all the components. |
| 219 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 220 | void |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 221 | clear() |
| 222 | { |
| 223 | m_nameBlock = Block(Tlv::Name); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 224 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 225 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 226 | /** |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 227 | * Get a new name, constructed as a subset of components. |
| 228 | * @param iStartComponent The index if the first component to get. |
| 229 | * @param nComponents The number of components starting at iStartComponent. |
| 230 | * @return A new name. |
| 231 | */ |
| 232 | Name |
| 233 | getSubName(size_t iStartComponent, size_t nComponents) const; |
| 234 | |
| 235 | /** |
| 236 | * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name. |
| 237 | * @param iStartComponent The index if the first component to get. |
| 238 | * @return A new name. |
| 239 | */ |
| 240 | Name |
| 241 | getSubName(size_t iStartComponent) const; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 242 | |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 243 | /** |
| 244 | * Return a new Name with the first nComponents components of this Name. |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 245 | * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up |
| 246 | * to name.size() - N. For example getPrefix(-1) returns the name without the final component. |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 247 | * @return A new Name. |
| 248 | */ |
| 249 | Name |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 250 | getPrefix(int nComponents) const |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 251 | { |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 252 | if (nComponents < 0) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 253 | return getSubName(0, m_nameBlock.elements_size() + nComponents); |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 254 | else |
| 255 | return getSubName(0, nComponents); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 256 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 257 | |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 258 | /** |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 259 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 260 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 261 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 262 | std::string |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 263 | toUri() const; |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 264 | |
| 265 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 266 | * @brief Append a component with the number encoded as nonNegativeInteger |
| 267 | * |
| 268 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 269 | * |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 270 | * @param number The non-negative number |
| 271 | * @return This name so that you can chain calls to append. |
| 272 | */ |
| 273 | Name& |
| 274 | appendNumber(uint64_t number) |
| 275 | { |
| 276 | m_nameBlock.push_back(Component::fromNumber(number)); |
| 277 | return *this; |
| 278 | } |
| 279 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 280 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 281 | * @brief An alias for appendNumber(uint64_t) |
| 282 | */ |
| 283 | Name& |
| 284 | appendVersion(uint64_t number) |
| 285 | { |
| 286 | return appendNumber(number); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @brief Append a component with the encoded version number (current UNIX timestamp |
| 291 | * in milliseconds) |
| 292 | */ |
| 293 | Name& |
| 294 | appendVersion(); |
| 295 | |
| 296 | /** |
| 297 | * @brief An alias for appendNumber(uint64_t) |
| 298 | */ |
| 299 | Name& |
| 300 | appendSegment(uint64_t number) |
| 301 | { |
| 302 | return appendNumber(number); |
| 303 | } |
| 304 | |
| 305 | /** |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame^] | 306 | * @brief get the successor of a name |
| 307 | * successor of a name is defined that its last component is |
| 308 | * advanced next possible value of last component of original name |
| 309 | * @return a new name |
| 310 | */ |
| 311 | Name |
| 312 | getSuccessor() const; |
| 313 | |
| 314 | /** |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 315 | * Check if this name has the same component count and components as the given name. |
| 316 | * @param name The Name to check. |
| 317 | * @return true if the names are equal, otherwise false. |
| 318 | */ |
| 319 | bool |
| 320 | equals(const Name& name) const; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 321 | |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 322 | /** |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 323 | * Check if the N components of this name are the same as the first N components of the given name. |
| 324 | * @param name The Name to check. |
| 325 | * @return true if this matches the given name, otherwise false. This always returns true if this name is empty. |
| 326 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 327 | bool |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 328 | isPrefixOf(const Name& name) const; |
| 329 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 330 | // |
| 331 | // vector equivalent interface. |
| 332 | // |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 333 | |
| 334 | /** |
| 335 | * @brief Check if name is emtpy |
| 336 | */ |
| 337 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 338 | empty() const |
| 339 | { |
| 340 | return m_nameBlock.elements().empty(); |
| 341 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 342 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Get the number of components. |
| 345 | * @return The number of components. |
| 346 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 347 | size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 348 | size() const |
| 349 | { |
| 350 | return m_nameBlock.elements_size(); |
| 351 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 352 | |
| 353 | /** |
| 354 | * Get the component at the given index. |
| 355 | * @param i The index of the component, starting from 0. |
| 356 | * @return The name component at the index. |
| 357 | */ |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 358 | const Component& |
Alexander Afanasyev | 8f9aa8b | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 359 | get(ssize_t i) const |
| 360 | { |
| 361 | if (i >= 0) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 362 | return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]); |
Alexander Afanasyev | 8f9aa8b | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 363 | else |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 364 | return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]); |
| 365 | } |
| 366 | |
| 367 | const Component& |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 368 | operator[](ssize_t i) const |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 369 | { |
| 370 | return get(i); |
Alexander Afanasyev | 8f9aa8b | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 371 | } |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 372 | |
| 373 | /** |
| 374 | * @brief Get component at the specified index |
| 375 | * |
| 376 | * Unlike get() and operator[] methods, at() checks for out of bounds |
| 377 | * and will throw Name::Error when it happens |
| 378 | * |
| 379 | * @throws Name::Error if index out of bounds |
| 380 | */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 381 | const Component& |
| 382 | at(ssize_t i) const |
| 383 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 384 | if ((i >= 0 && static_cast<size_t>(i) >= size()) || |
| 385 | (i < 0 && static_cast<size_t>(-i) > size())) |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 386 | throw Error("Requested component does not exist (out of bounds)"); |
| 387 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 388 | return get(i); |
| 389 | } |
| 390 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 391 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 392 | * @brief Compare this to the other Name using NDN canonical ordering. |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 393 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 394 | * If the first components of each name are not equal, this returns -1 if the first comes |
| 395 | * before the second using the NDN canonical ordering for name components, or 1 if it |
| 396 | * comes after. If they are equal, this compares the second components of each name, etc. |
| 397 | * If both names are the same up to the size of the shorter name, this returns -1 if the |
| 398 | * first name is shorter than the second or 1 if it is longer. For example, if you |
| 399 | * std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names with |
| 400 | * the prefix /a are next to each other. But it may be also be counter-intuitive because |
| 401 | * /c comes before /bb according to NDN canonical ordering since it is shorter. @param |
| 402 | * other The other Name to compare with. @return 0 If they compare equal, -1 if *this |
| 403 | * comes before other in the canonical ordering, or 1 if *this comes after other in the |
| 404 | * canonical ordering. |
| 405 | * |
| 406 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 407 | */ |
| 408 | int |
| 409 | compare(const Name& other) const; |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 410 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 411 | /** |
| 412 | * Append the component |
| 413 | * @param component The component of type T. |
| 414 | */ |
| 415 | template<class T> void |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 416 | push_back(const T& component) |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 417 | { |
| 418 | append(component); |
| 419 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 420 | |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Check if this name has the same component count and components as the given name. |
| 423 | * @param name The Name to check. |
| 424 | * @return true if the names are equal, otherwise false. |
| 425 | */ |
| 426 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 427 | operator==(const Name& name) const |
| 428 | { |
| 429 | return equals(name); |
| 430 | } |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 431 | |
| 432 | /** |
| 433 | * Check if this name has the same component count and components as the given name. |
| 434 | * @param name The Name to check. |
| 435 | * @return true if the names are not equal, otherwise false. |
| 436 | */ |
| 437 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 438 | operator!=(const Name& name) const |
| 439 | { |
| 440 | return !equals(name); |
| 441 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 442 | |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 443 | /** |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 444 | * Return true if this is less than or equal to the other Name in the NDN canonical ordering. |
| 445 | * @param other The other Name to compare with. |
| 446 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 447 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 448 | */ |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 449 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 450 | operator<=(const Name& other) const |
| 451 | { |
| 452 | return compare(other) <= 0; |
| 453 | } |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 454 | |
| 455 | /** |
| 456 | * Return true if this is less than the other Name in the NDN canonical ordering. |
| 457 | * @param other The other Name to compare with. |
| 458 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 459 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 460 | */ |
| 461 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 462 | operator<(const Name& other) const |
| 463 | { |
| 464 | return compare(other) < 0; |
| 465 | } |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * Return true if this is less than or equal to the other Name in the NDN canonical ordering. |
| 469 | * @param other The other Name to compare with. |
| 470 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 471 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 472 | */ |
| 473 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 474 | operator>=(const Name& other) const |
| 475 | { |
| 476 | return compare(other) >= 0; |
| 477 | } |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 478 | |
| 479 | /** |
| 480 | * Return true if this is greater than the other Name in the NDN canonical ordering. |
| 481 | * @param other The other Name to compare with. |
| 482 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 483 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 484 | */ |
| 485 | bool |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 486 | operator>(const Name& other) const |
| 487 | { |
| 488 | return compare(other) > 0; |
| 489 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 490 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 491 | // |
| 492 | // Iterator interface to name components. |
| 493 | // |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * Begin iterator (const). |
| 497 | */ |
| 498 | const_iterator |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 499 | begin() const |
| 500 | { |
| 501 | return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin()); |
| 502 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 503 | |
| 504 | /** |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 505 | * End iterator (const). |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 506 | * |
| 507 | * @todo Check if this crash when there are no elements in the buffer |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 508 | */ |
| 509 | const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 510 | end() const |
| 511 | { |
| 512 | return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end()); |
| 513 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 514 | |
| 515 | /** |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 516 | * Reverse begin iterator (const). |
| 517 | */ |
| 518 | const_reverse_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 519 | rbegin() const |
| 520 | { |
| 521 | return const_reverse_iterator(end()); |
| 522 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 523 | |
| 524 | /** |
| 525 | * Reverse end iterator (const). |
| 526 | */ |
| 527 | const_reverse_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 528 | rend() const |
| 529 | { |
| 530 | return const_reverse_iterator(begin()); |
| 531 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 532 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 533 | private: |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 534 | mutable Block m_nameBlock; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 535 | }; |
| 536 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 537 | inline std::ostream& |
| 538 | operator<<(std::ostream& os, const Name& name) |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 539 | { |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 540 | if (name.empty()) |
| 541 | { |
| 542 | os << "/"; |
| 543 | } |
| 544 | else |
| 545 | { |
| 546 | for (Name::const_iterator i = name.begin(); i != name.end(); i++) { |
| 547 | os << "/"; |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 548 | i->toUri(os); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | return os; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | inline std::string |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 555 | Name::toUri() const |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 556 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 557 | std::ostringstream os; |
| 558 | os << *this; |
| 559 | return os.str(); |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Alexander Afanasyev | 0df2836 | 2014-03-20 17:31:43 -0700 | [diff] [blame] | 562 | inline std::istream& |
| 563 | operator>>(std::istream& is, Name& name) |
| 564 | { |
| 565 | std::string inputString; |
| 566 | is >> inputString; |
| 567 | name.set(inputString); |
| 568 | |
| 569 | return is; |
| 570 | } |
| 571 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 572 | |
| 573 | inline void |
| 574 | Name::set(const char* uri_cstr) |
| 575 | { |
| 576 | clear(); |
| 577 | |
| 578 | std::string uri = uri_cstr; |
| 579 | trim(uri); |
| 580 | if (uri.size() == 0) |
| 581 | return; |
| 582 | |
| 583 | size_t iColon = uri.find(':'); |
| 584 | if (iColon != std::string::npos) { |
| 585 | // Make sure the colon came before a '/'. |
| 586 | size_t iFirstSlash = uri.find('/'); |
| 587 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
| 588 | // Omit the leading protocol such as ndn: |
| 589 | uri.erase(0, iColon + 1); |
| 590 | trim(uri); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | // Trim the leading slash and possibly the authority. |
| 595 | if (uri[0] == '/') { |
| 596 | if (uri.size() >= 2 && uri[1] == '/') { |
| 597 | // Strip the authority following "//". |
| 598 | size_t iAfterAuthority = uri.find('/', 2); |
| 599 | if (iAfterAuthority == std::string::npos) |
| 600 | // Unusual case: there was only an authority. |
| 601 | return; |
| 602 | else { |
| 603 | uri.erase(0, iAfterAuthority + 1); |
| 604 | trim(uri); |
| 605 | } |
| 606 | } |
| 607 | else { |
| 608 | uri.erase(0, 1); |
| 609 | trim(uri); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | size_t iComponentStart = 0; |
| 614 | |
| 615 | // Unescape the components. |
| 616 | while (iComponentStart < uri.size()) { |
| 617 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 618 | if (iComponentEnd == std::string::npos) |
| 619 | iComponentEnd = uri.size(); |
| 620 | |
| 621 | Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd); |
| 622 | // Ignore illegal components. This also gets rid of a trailing '/'. |
| 623 | if (!component.empty()) |
| 624 | append(Component(component)); |
| 625 | |
| 626 | iComponentStart = iComponentEnd + 1; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | inline Name& |
| 631 | Name::append(const Name& name) |
| 632 | { |
| 633 | if (&name == this) |
| 634 | // Copying from this name, so need to make a copy first. |
| 635 | return append(Name(name)); |
| 636 | |
| 637 | for (size_t i = 0; i < name.size(); ++i) |
| 638 | append(name.at(i)); |
| 639 | |
| 640 | return *this; |
| 641 | } |
| 642 | |
| 643 | inline Name& |
| 644 | Name::appendVersion() |
| 645 | { |
| 646 | appendNumber(time::toUnixTimestamp(time::system_clock::now()).count()); |
| 647 | return *this; |
| 648 | } |
| 649 | |
| 650 | inline Name |
| 651 | Name::getSubName(size_t iStartComponent, size_t nComponents) const |
| 652 | { |
| 653 | Name result; |
| 654 | |
| 655 | size_t iEnd = iStartComponent + nComponents; |
| 656 | for (size_t i = iStartComponent; i < iEnd && i < size(); ++i) |
| 657 | result.append(at(i)); |
| 658 | |
| 659 | return result; |
| 660 | } |
| 661 | |
| 662 | inline Name |
| 663 | Name::getSubName(size_t iStartComponent) const |
| 664 | { |
| 665 | Name result; |
| 666 | |
| 667 | for (size_t i = iStartComponent; i < size(); ++i) |
| 668 | result.append(at(i)); |
| 669 | |
| 670 | return result; |
| 671 | } |
| 672 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame^] | 673 | inline Name |
| 674 | Name::getSuccessor() const |
| 675 | { |
| 676 | if (empty()) { |
| 677 | static uint8_t firstValue[] = { 0 }; |
| 678 | Name firstName; |
| 679 | firstName.append(firstValue, 1); |
| 680 | return firstName; |
| 681 | } |
| 682 | |
| 683 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 684 | } |
| 685 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 686 | inline bool |
| 687 | Name::equals(const Name& name) const |
| 688 | { |
| 689 | if (size() != name.size()) |
| 690 | return false; |
| 691 | |
| 692 | for (size_t i = 0; i < size(); ++i) { |
| 693 | if (at(i) != name.at(i)) |
| 694 | return false; |
| 695 | } |
| 696 | |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | inline bool |
| 701 | Name::isPrefixOf(const Name& name) const |
| 702 | { |
| 703 | // This name is longer than the name we are checking it against. |
| 704 | if (size() > name.size()) |
| 705 | return false; |
| 706 | |
| 707 | // Check if at least one of given components doesn't match. |
| 708 | for (size_t i = 0; i < size(); ++i) { |
| 709 | if (at(i) != name.at(i)) |
| 710 | return false; |
| 711 | } |
| 712 | |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | inline int |
| 718 | Name::compare(const Name& other) const |
| 719 | { |
| 720 | for (size_t i = 0; i < size() && i < other.size(); ++i) { |
| 721 | int comparison = at(i).compare(other.at(i)); |
| 722 | if (comparison == 0) |
| 723 | // The components at this index are equal, so check the next components. |
| 724 | continue; |
| 725 | |
| 726 | // Otherwise, the result is based on the components at this index. |
| 727 | return comparison; |
| 728 | } |
| 729 | |
| 730 | // The components up to min(this.size(), other.size()) are equal, so the shorter name is less. |
| 731 | if (size() < other.size()) |
| 732 | return -1; |
| 733 | else if (size() > other.size()) |
| 734 | return 1; |
| 735 | else |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 741 | template<bool T> |
| 742 | inline size_t |
| 743 | Name::wireEncode(EncodingImpl<T>& blk) const |
| 744 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 745 | size_t totalLength = 0; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 746 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 747 | for (const_reverse_iterator i = rbegin(); |
| 748 | i != rend(); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 749 | ++i) |
| 750 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 751 | totalLength += i->wireEncode(blk); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 752 | } |
| 753 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 754 | totalLength += blk.prependVarNumber(totalLength); |
| 755 | totalLength += blk.prependVarNumber(Tlv::Name); |
| 756 | return totalLength; |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 759 | inline const Block& |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 760 | Name::wireEncode() const |
| 761 | { |
| 762 | if (m_nameBlock.hasWire()) |
| 763 | return m_nameBlock; |
| 764 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 765 | EncodingEstimator estimator; |
| 766 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 767 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 768 | EncodingBuffer buffer(estimatedSize, 0); |
| 769 | wireEncode(buffer); |
| 770 | |
| 771 | m_nameBlock = buffer.block(); |
| 772 | m_nameBlock.parse(); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 773 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 774 | return m_nameBlock; |
| 775 | } |
| 776 | |
| 777 | inline void |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 778 | Name::wireDecode(const Block& wire) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 779 | { |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 780 | if (wire.type() != Tlv::Name) |
| 781 | throw Tlv::Error("Unexpected TLV type when decoding Name"); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 782 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 783 | m_nameBlock = wire; |
| 784 | m_nameBlock.parse(); |
| 785 | } |
| 786 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 787 | inline bool |
| 788 | Name::hasWire() const |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 789 | { |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 790 | return m_nameBlock.hasWire(); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 793 | } // namespace ndn |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 794 | |
| 795 | #endif |