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 |
| 29 | struct Error : public name::Component::Error { Error(const std::string &what) : name::Component::Error(what) {} }; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 31 | typedef name::Component Component; |
| 32 | |
| 33 | typedef std::vector<Component> component_container; |
| 34 | |
| 35 | typedef Component value_type; |
| 36 | typedef void allocator_type; |
| 37 | typedef Component& reference; |
| 38 | typedef const Component const_reference; |
| 39 | typedef Component* pointer; |
| 40 | typedef const Component* const_pointer; |
| 41 | typedef Component* iterator; |
| 42 | typedef const Component* const_iterator; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 44 | typedef boost::reverse_iterator<iterator> reverse_iterator; |
| 45 | typedef boost::reverse_iterator<const_iterator> const_reverse_iterator; |
| 46 | |
| 47 | typedef component_container::difference_type difference_type; |
| 48 | typedef component_container::size_type size_type; |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 49 | |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Create a new Name with no components. |
| 52 | */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 53 | Name() |
| 54 | : m_nameBlock(Tlv::Name) |
| 55 | { |
Jeff Thompson | 016ed64 | 2013-07-02 14:39:06 -0700 | [diff] [blame] | 56 | } |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * @brief Create Name object from wire block |
| 60 | * |
| 61 | * This is a more efficient equivalent for |
| 62 | * @code |
| 63 | * Name name; |
| 64 | * name.wireDecode(wire); |
| 65 | * @endcode |
| 66 | */ |
| 67 | Name(const Block &wire) |
| 68 | { |
| 69 | m_nameBlock = wire; |
| 70 | m_nameBlock.parse(); |
| 71 | } |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 74 | * 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] | 75 | * @param uri The URI string. |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 76 | */ |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 77 | Name(const char* uri) |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 78 | { |
| 79 | set(uri); |
| 80 | } |
Jeff Thompson | 5a6b5ab | 2013-08-05 15:43:47 -0700 | [diff] [blame] | 81 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 82 | /** |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 83 | * Parse the uri according to the NDN URI Scheme and create the name with the components. |
| 84 | * @param uri The URI string. |
| 85 | */ |
| 86 | Name(const std::string& uri) |
| 87 | { |
| 88 | set(uri.c_str()); |
| 89 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 90 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 91 | /** |
| 92 | * @brief Fast encoding or block size estimation |
| 93 | */ |
| 94 | template<bool T> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 95 | size_t |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 96 | wireEncode(EncodingImpl<T> &block) const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 98 | const Block & |
| 99 | wireEncode() const; |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 100 | |
| 101 | void |
| 102 | wireDecode(const Block &wire); |
Jeff Thompson | b468c31 | 2013-07-01 17:50:14 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 105 | * 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] | 106 | * @param uri The null-terminated URI string. |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 107 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 108 | void |
| 109 | set(const char *uri); |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 112 | * Parse the uri according to the NDN URI Scheme and set the name with the components. |
| 113 | * @param uri The URI string. |
| 114 | */ |
| 115 | void |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 116 | set(const std::string& uri) |
| 117 | { |
| 118 | set(uri.c_str()); |
| 119 | } |
Jeff Thompson | 7781b39 | 2013-12-17 11:45:59 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 122 | * Append a new component, copying from value of length valueLength. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 123 | * @return This name so that you can chain calls to append. |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 124 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 125 | Name& |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 126 | append(const uint8_t *value, size_t valueLength) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 127 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 128 | m_nameBlock.push_back(Component(value, valueLength)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 129 | return *this; |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 130 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 131 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 132 | // /** |
| 133 | // * Append a new component, copying from value. |
| 134 | // * @return This name so that you can chain calls to append. |
| 135 | // */ |
| 136 | // Name& |
| 137 | // append(const Buffer& value) |
| 138 | // { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 139 | // m_nameBlock.push_back(value); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 140 | // return *this; |
| 141 | // } |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 142 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 143 | Name& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 144 | append(const ConstBufferPtr &value) |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 145 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 146 | m_nameBlock.push_back(value); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 147 | return *this; |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 148 | } |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 149 | |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 150 | Name& |
| 151 | append(const Component &value) |
| 152 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 153 | m_nameBlock.push_back(value); |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 154 | return *this; |
| 155 | } |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 157 | /** |
| 158 | * @brief Append name component that represented as a string |
| 159 | * |
| 160 | * Note that this method is necessary to ensure correctness and unambiguity of |
| 161 | * ``append("string")`` operations (both Component and Name can be implicitly |
| 162 | * converted from string, each having different outcomes |
| 163 | */ |
| 164 | Name& |
| 165 | append(const char *value) |
| 166 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 167 | m_nameBlock.push_back(Component(value)); |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 168 | return *this; |
| 169 | } |
| 170 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 171 | Name& |
| 172 | append(const Block &value) |
| 173 | { |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 174 | if (value.type() == Tlv::NameComponent) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 175 | m_nameBlock.push_back(value); |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 176 | else |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 177 | m_nameBlock.push_back(Block(Tlv::NameComponent, value)); |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 179 | return *this; |
| 180 | } |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 181 | |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 182 | /** |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 183 | * Append the components of the given name to this name. |
| 184 | * @param name The Name with components to append. |
| 185 | * @return This name so that you can chain calls to append. |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 186 | */ |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 187 | Name& |
| 188 | append(const Name& name); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 189 | |
Jeff Thompson | 0aa66f2 | 2013-09-23 13:02:13 -0700 | [diff] [blame] | 190 | /** |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 191 | * Clear all the components. |
| 192 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 193 | void |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 194 | clear() |
| 195 | { |
| 196 | m_nameBlock = Block(Tlv::Name); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 200 | * Get a new name, constructed as a subset of components. |
| 201 | * @param iStartComponent The index if the first component to get. |
| 202 | * @param nComponents The number of components starting at iStartComponent. |
| 203 | * @return A new name. |
| 204 | */ |
| 205 | Name |
| 206 | getSubName(size_t iStartComponent, size_t nComponents) const; |
| 207 | |
| 208 | /** |
| 209 | * Get a new name, constructed as a subset of components starting at iStartComponent until the end of the name. |
| 210 | * @param iStartComponent The index if the first component to get. |
| 211 | * @return A new name. |
| 212 | */ |
| 213 | Name |
| 214 | getSubName(size_t iStartComponent) const; |
| 215 | |
| 216 | /** |
| 217 | * Return a new Name with the first nComponents components of this Name. |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 218 | * @param nComponents The number of prefix components. If nComponents is -N then return the prefix up |
| 219 | * 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] | 220 | * @return A new Name. |
| 221 | */ |
| 222 | Name |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 223 | getPrefix(int nComponents) const |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 224 | { |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 225 | if (nComponents < 0) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 226 | return getSubName(0, m_nameBlock.elements_size() + nComponents); |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 227 | else |
| 228 | return getSubName(0, nComponents); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 232 | * Encode this name as a URI. |
Jeff Thompson | 3f2175b | 2013-07-31 17:12:47 -0700 | [diff] [blame] | 233 | * @return The encoded URI. |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 234 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 235 | std::string |
| 236 | toUri() const; |
Jeff Thompson | e606351 | 2013-07-01 15:11:28 -0700 | [diff] [blame] | 237 | |
Jeff Thompson | 21844fc | 2013-08-08 14:52:51 -0700 | [diff] [blame] | 238 | /** |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 239 | * Append a component with the encoded segment number. |
| 240 | * @param segment The segment number. |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 241 | * @return This name so that you can chain calls to append. |
| 242 | */ |
| 243 | Name& |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 244 | appendSegment(uint64_t segment) |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 245 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 246 | m_nameBlock.push_back(Component::fromNumberWithMarker(segment, 0x00)); |
Jeff Thompson | d129ac1 | 2013-10-11 14:30:12 -0700 | [diff] [blame] | 247 | return *this; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Append a component with the encoded version number. |
| 252 | * Note that this encodes the exact value of version without converting from a time representation. |
| 253 | * @param version The version number. |
| 254 | * @return This name so that you can chain calls to append. |
| 255 | */ |
| 256 | Name& |
| 257 | appendVersion(uint64_t version) |
| 258 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 259 | m_nameBlock.push_back(Component::fromNumberWithMarker(version, 0xFD)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 260 | return *this; |
Jeff Thompson | 8aac199 | 2013-08-12 17:26:02 -0700 | [diff] [blame] | 261 | } |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * @brief Append a component with the encoded version number. |
| 265 | * |
| 266 | * This version of the method creates version number based on the current timestamp |
| 267 | * @return This name so that you can chain calls to append. |
| 268 | */ |
| 269 | Name& |
| 270 | appendVersion(); |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 271 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 272 | /** |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 273 | * Check if this name has the same component count and components as the given name. |
| 274 | * @param name The Name to check. |
| 275 | * @return true if the names are equal, otherwise false. |
| 276 | */ |
| 277 | bool |
| 278 | equals(const Name& name) const; |
| 279 | |
| 280 | /** |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 281 | * Check if the N components of this name are the same as the first N components of the given name. |
| 282 | * @param name The Name to check. |
| 283 | * @return true if this matches the given name, otherwise false. This always returns true if this name is empty. |
| 284 | */ |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 285 | bool |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 286 | isPrefixOf(const Name& name) const; |
| 287 | |
| 288 | bool |
| 289 | match(const Name& name) const |
| 290 | { |
| 291 | return isPrefixOf(name); |
| 292 | } |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 293 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 294 | // |
| 295 | // vector equivalent interface. |
| 296 | // |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * @brief Check if name is emtpy |
| 300 | */ |
| 301 | bool |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 302 | empty() const { return m_nameBlock.elements().empty(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * Get the number of components. |
| 306 | * @return The number of components. |
| 307 | */ |
| 308 | size_t |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 309 | size() const { return m_nameBlock.elements_size(); } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * Get the component at the given index. |
| 313 | * @param i The index of the component, starting from 0. |
| 314 | * @return The name component at the index. |
| 315 | */ |
| 316 | const Component& |
Alexander Afanasyev | 8f9aa8bed | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 317 | get(ssize_t i) const |
| 318 | { |
| 319 | if (i >= 0) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 320 | return reinterpret_cast<const Component&>(m_nameBlock.elements()[i]); |
Alexander Afanasyev | 8f9aa8bed | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 321 | else |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 322 | return reinterpret_cast<const Component&>(m_nameBlock.elements()[size() + i]); |
| 323 | } |
| 324 | |
| 325 | const Component& |
| 326 | operator [] (ssize_t i) const |
| 327 | { |
| 328 | return get(i); |
Alexander Afanasyev | 8f9aa8bed | 2013-12-30 15:58:57 -0800 | [diff] [blame] | 329 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 330 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 331 | const Component& |
| 332 | at(ssize_t i) const |
| 333 | { |
| 334 | return get(i); |
| 335 | } |
| 336 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Compare this to the other Name using NDN canonical ordering. If the first components of each name are not equal, |
| 339 | * this returns -1 if the first comes before the second using the NDN canonical ordering for name components, or 1 if it comes after. |
| 340 | * If they are equal, this compares the second components of each name, etc. If both names are the same up to |
| 341 | * the size of the shorter name, this returns -1 if the first name is shorter than the second or 1 if it is longer. |
| 342 | * For example, if you std::sort gives: /a/b/d /a/b/cc /c /c/a /bb . This is intuitive because all names |
| 343 | * with the prefix /a are next to each other. But it may be also be counter-intuitive because /c comes before /bb |
| 344 | * according to NDN canonical ordering since it is shorter. |
| 345 | * @param other The other Name to compare with. |
| 346 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
| 347 | * 1 if *this comes after other in the canonical ordering. |
| 348 | * |
| 349 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 350 | */ |
| 351 | int |
| 352 | compare(const Name& other) const; |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 353 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 354 | /** |
| 355 | * Append the component |
| 356 | * @param component The component of type T. |
| 357 | */ |
| 358 | template<class T> void |
| 359 | push_back(const T &component) |
| 360 | { |
| 361 | append(component); |
| 362 | } |
| 363 | |
Jeff Thompson | 91737f5 | 2013-10-04 11:07:24 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Check if this name has the same component count and components as the given name. |
| 366 | * @param name The Name to check. |
| 367 | * @return true if the names are equal, otherwise false. |
| 368 | */ |
| 369 | bool |
| 370 | operator == (const Name &name) const { return equals(name); } |
| 371 | |
| 372 | /** |
| 373 | * Check if this name has the same component count and components as the given name. |
| 374 | * @param name The Name to check. |
| 375 | * @return true if the names are not equal, otherwise false. |
| 376 | */ |
| 377 | bool |
| 378 | operator != (const Name &name) const { return !equals(name); } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 379 | |
| 380 | /** |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 381 | * Return true if this is less than or equal to the other Name in the NDN canonical ordering. |
| 382 | * @param other The other Name to compare with. |
| 383 | * |
| 384 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 385 | */ |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 386 | bool |
| 387 | operator <= (const Name& other) const { return compare(other) <= 0; } |
| 388 | |
| 389 | /** |
| 390 | * Return true if this is less than the other Name in the NDN canonical ordering. |
| 391 | * @param other The other Name to compare with. |
| 392 | * |
| 393 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 394 | */ |
| 395 | bool |
| 396 | operator < (const Name& other) const { return compare(other) < 0; } |
| 397 | |
| 398 | /** |
| 399 | * Return true if this is less than or equal to the other Name in the NDN canonical ordering. |
| 400 | * @param other The other Name to compare with. |
| 401 | * |
| 402 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 403 | */ |
| 404 | bool |
| 405 | operator >= (const Name& other) const { return compare(other) >= 0; } |
| 406 | |
| 407 | /** |
| 408 | * Return true if this is greater than the other Name in the NDN canonical ordering. |
| 409 | * @param other The other Name to compare with. |
| 410 | * |
| 411 | * @see http://named-data.net/doc/0.2/technical/CanonicalOrder.html |
| 412 | */ |
| 413 | bool |
| 414 | operator > (const Name& other) const { return compare(other) > 0; } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 415 | |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 416 | // |
| 417 | // Iterator interface to name components. |
| 418 | // |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 419 | |
| 420 | /** |
| 421 | * Begin iterator (const). |
| 422 | */ |
| 423 | const_iterator |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 424 | begin() const |
| 425 | { |
| 426 | return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().begin()); |
| 427 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 428 | |
| 429 | /** |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 430 | * End iterator (const). |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 431 | * |
| 432 | * @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] | 433 | */ |
| 434 | const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 435 | end() const |
| 436 | { |
| 437 | return reinterpret_cast<const_iterator>(&*m_nameBlock.elements().end()); |
| 438 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 439 | |
| 440 | /** |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 441 | * Reverse begin iterator (const). |
| 442 | */ |
| 443 | const_reverse_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 444 | rbegin() const |
| 445 | { |
| 446 | return const_reverse_iterator(end()); |
| 447 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * Reverse end iterator (const). |
| 451 | */ |
| 452 | const_reverse_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame^] | 453 | rend() const |
| 454 | { |
| 455 | return const_reverse_iterator(begin()); |
| 456 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 457 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 458 | private: |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 459 | mutable Block m_nameBlock; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | std::ostream & |
| 463 | operator << (std::ostream &os, const Name &name); |
| 464 | |
| 465 | inline std::string |
| 466 | Name::toUri() const |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 467 | { |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 468 | std::ostringstream os; |
| 469 | os << *this; |
| 470 | return os.str(); |
Jeff Thompson | 49e321a | 2013-10-04 17:35:59 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 473 | inline const Block & |
| 474 | Name::wireEncode() const |
| 475 | { |
| 476 | if (m_nameBlock.hasWire()) |
| 477 | return m_nameBlock; |
| 478 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 479 | EncodingEstimator estimator; |
| 480 | size_t estimatedSize = wireEncode(estimator); |
| 481 | |
| 482 | EncodingBuffer buffer(estimatedSize, 0); |
| 483 | wireEncode(buffer); |
| 484 | |
| 485 | m_nameBlock = buffer.block(); |
| 486 | m_nameBlock.parse(); |
| 487 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 488 | return m_nameBlock; |
| 489 | } |
| 490 | |
| 491 | inline void |
| 492 | Name::wireDecode(const Block &wire) |
| 493 | { |
| 494 | m_nameBlock = wire; |
| 495 | m_nameBlock.parse(); |
| 496 | } |
| 497 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 498 | template<bool T> |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 499 | inline size_t |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 500 | Name::wireEncode(EncodingImpl<T>& blk) const |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 501 | { |
| 502 | size_t total_len = 0; |
| 503 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 504 | for (const_reverse_iterator i = rbegin (); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 505 | i != rend (); |
| 506 | ++i) |
| 507 | { |
| 508 | total_len += i->wireEncode (blk); |
| 509 | } |
| 510 | |
| 511 | total_len += blk.prependVarNumber (total_len); |
| 512 | total_len += blk.prependVarNumber (Tlv::Name); |
| 513 | return total_len; |
| 514 | } |
| 515 | |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 516 | template |
| 517 | size_t |
| 518 | Name::wireEncode<true>(EncodingBuffer& block) const; |
| 519 | |
| 520 | template |
| 521 | size_t |
| 522 | Name::wireEncode<false>(EncodingEstimator& block) const; |
| 523 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 524 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 525 | } // namespace ndn |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 526 | |
| 527 | #endif |
| 528 | |