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