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