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