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