Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 8609bf2 | 2017-07-12 13:02:55 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 23 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifndef NDN_NAME_HPP |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 27 | #define NDN_NAME_HPP |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 29 | #include "ndn-cxx/name-component.hpp" |
| 30 | |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 31 | #include <iterator> |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 32 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 33 | namespace ndn { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 34 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 35 | class Name; |
| 36 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 37 | /** @brief Represents an arbitrary sequence of name components |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 38 | */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 39 | using PartialName = Name; |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 40 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 41 | /** @brief Represents an absolute name |
Jeff Thompson | c7d6550 | 2013-11-06 17:22:26 -0800 | [diff] [blame] | 42 | */ |
Junxiao Shi | 8609bf2 | 2017-07-12 13:02:55 +0000 | [diff] [blame] | 43 | class Name |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 44 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 45 | public: // nested types |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 46 | using Error = name::Component::Error; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 47 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 48 | using Component = name::Component; |
| 49 | using component_container = std::vector<Component>; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 50 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 51 | // Name appears as a container of name components |
| 52 | using value_type = Component; |
| 53 | using allocator_type = void; |
| 54 | using reference = Component&; |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 55 | using const_reference = const Component&; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 56 | using pointer = Component*; |
| 57 | using const_pointer = const Component*; |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 58 | using iterator = const Component*; // disallow modifying via iterator |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 59 | using const_iterator = const Component*; |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 60 | using reverse_iterator = std::reverse_iterator<iterator>; |
| 61 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 62 | using difference_type = component_container::difference_type; |
| 63 | using size_type = component_container::size_type; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 64 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 65 | public: // constructors, encoding, decoding |
| 66 | /** @brief Create an empty name |
| 67 | * @post empty() == true |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 68 | */ |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 69 | Name(); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 70 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 71 | /** @brief Decode Name from wire encoding |
| 72 | * @throw tlv::Error wire encoding is invalid |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 73 | * |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 74 | * This is a more efficient equivalent for |
| 75 | * @code |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 76 | * Name name; |
| 77 | * name.wireDecode(wire); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 78 | * @endcode |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 79 | */ |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 80 | explicit |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 81 | Name(const Block& wire); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 82 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 83 | /** @brief Parse name from NDN URI |
| 84 | * @param uri a null-terminated URI string |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 85 | * @sa https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 86 | */ |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 87 | Name(const char* uri); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 88 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 89 | /** @brief Create name from NDN URI |
| 90 | * @param uri a URI string |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 91 | * @sa https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme |
Jeff Thompson | 3549ef3 | 2013-09-25 14:05:17 -0700 | [diff] [blame] | 92 | */ |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 93 | Name(std::string uri); |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 94 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 95 | /** @brief Get URI representation of the name |
| 96 | * @return URI representation; "ndn:" scheme identifier is not included |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 97 | * @sa https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 98 | * @note To print URI representation into a stream, it is more efficient to use ``os << name``. |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 99 | */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 100 | std::string |
| 101 | toUri() const; |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 102 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 103 | /** @brief Check if this Name instance already has wire encoding |
| 104 | */ |
| 105 | bool |
| 106 | hasWire() const |
| 107 | { |
| 108 | return m_wire.hasWire(); |
| 109 | } |
| 110 | |
| 111 | /** @brief Fast encoding or block size estimation |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 112 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 113 | template<encoding::Tag TAG> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 114 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 115 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 117 | /** @brief Perform wire encoding, or return existing wire encoding |
| 118 | * @post hasWire() == true |
| 119 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 120 | const Block& |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 121 | wireEncode() const; |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 122 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 123 | /** @brief Decode name from wire encoding |
| 124 | * @throw tlv::Error wire encoding is invalid |
| 125 | * @post hasWire() == true |
| 126 | */ |
Alexander Afanasyev | 848c61a | 2014-01-03 13:52:04 -0800 | [diff] [blame] | 127 | void |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 128 | wireDecode(const Block& wire); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 129 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 130 | /** @brief Make a deep copy of the name, reallocating the underlying memory buffer |
| 131 | */ |
| 132 | Name |
| 133 | deepCopy() const; |
| 134 | |
| 135 | public: // access |
| 136 | /** @brief Check if name is empty |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 137 | */ |
| 138 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 139 | empty() const |
Jeff Thompson | 0f74345 | 2013-09-12 14:23:18 -0700 | [diff] [blame] | 140 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 141 | return m_wire.elements().empty(); |
Jeff Thompson | e5f839b | 2013-06-28 12:50:38 -0700 | [diff] [blame] | 142 | } |
Jeff Thompson | f72b1ac | 2013-08-16 16:44:41 -0700 | [diff] [blame] | 143 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 144 | /** @brief Get number of components |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 145 | */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 146 | size_t |
| 147 | size() const |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 148 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 149 | return m_wire.elements_size(); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 152 | /** @brief Get the component at the given index |
| 153 | * @param i zero-based index; if negative, it starts at the end of this name |
| 154 | * @warning Indexing out of bounds triggers undefined behavior. |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 155 | */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 156 | const Component& |
| 157 | get(ssize_t i) const |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 158 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 159 | if (i < 0) { |
| 160 | i += size(); |
| 161 | } |
| 162 | return reinterpret_cast<const Component&>(m_wire.elements()[i]); |
Jeff Thompson | 21eb721 | 2013-09-26 09:05:40 -0700 | [diff] [blame] | 163 | } |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 164 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 165 | /** @brief Equivalent to get(i) |
| 166 | */ |
| 167 | const Component& |
| 168 | operator[](ssize_t i) const |
| 169 | { |
| 170 | return get(i); |
| 171 | } |
| 172 | |
| 173 | /** @brief Get the component at the given index |
| 174 | * @param i zero-based index; if negative, size()+i is used instead |
| 175 | * @throws Name::Error index is out of bounds |
| 176 | */ |
| 177 | const Component& |
| 178 | at(ssize_t i) const; |
| 179 | |
| 180 | /** @brief Extract some components as a sub-name (PartialName) |
| 181 | * @param iStartComponent zero-based index of the first component; |
| 182 | * if negative, size()+iStartComponent is used instead |
| 183 | * @param nComponents Number of components starting at iStartComponent. |
| 184 | * Use @p npos to get the PartialName until the end of this Name. |
| 185 | * @return a new PartialName containing the extracted components |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 186 | * |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 187 | * If iStartComponent is positive and indexes out of bounds, returns an empty PartialName. |
| 188 | * If iStartComponent is negative and indexes out of bounds, returns components starting from the |
| 189 | * beginning of the Name. If nComponents is out of bounds, returns the components until the end |
| 190 | * of this Name. |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 191 | */ |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 192 | PartialName |
| 193 | getSubName(ssize_t iStartComponent, size_t nComponents = npos) const; |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 194 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 195 | /** @brief Extract a prefix of the name |
| 196 | * @param nComponents Number of components; if negative, size()+nComponents is used instead |
| 197 | * @return a new Name containing the prefix |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 198 | * the prefix up to name.size() - N. For example getPrefix(-1) |
| 199 | * returns the name without the final component. |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 200 | * @return A new partial name |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 201 | */ |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 202 | PartialName |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 203 | getPrefix(ssize_t nComponents) const |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 204 | { |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 205 | if (nComponents < 0) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 206 | return getSubName(0, size() + nComponents); |
Jeff Thompson | eb0358f | 2013-12-17 10:59:53 -0800 | [diff] [blame] | 207 | else |
| 208 | return getSubName(0, nComponents); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 209 | } |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 210 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 211 | public: // iterators |
| 212 | /** @brief Begin iterator |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 213 | */ |
| 214 | const_iterator |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 215 | begin() const |
| 216 | { |
Junxiao Shi | f0bf999 | 2017-07-15 15:51:52 +0000 | [diff] [blame] | 217 | return reinterpret_cast<const_iterator>(m_wire.elements().data()); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 218 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 219 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 220 | /** @brief End iterator |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 221 | */ |
| 222 | const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 223 | end() const |
| 224 | { |
Junxiao Shi | f0bf999 | 2017-07-15 15:51:52 +0000 | [diff] [blame] | 225 | return reinterpret_cast<const_iterator>(m_wire.elements().data() + m_wire.elements().size()); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 226 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 227 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 228 | /** @brief Reverse begin iterator |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 229 | */ |
| 230 | const_reverse_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 231 | rbegin() const |
| 232 | { |
| 233 | return const_reverse_iterator(end()); |
| 234 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 235 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 236 | /** @brief Reverse end iterator |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 237 | */ |
| 238 | const_reverse_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 239 | rend() const |
| 240 | { |
| 241 | return const_reverse_iterator(begin()); |
| 242 | } |
Jeff Thompson | ec39fbd | 2013-10-04 10:56:23 -0700 | [diff] [blame] | 243 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 244 | public: // modifiers |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 245 | /** @brief Append a component. |
| 246 | * @return a reference to this name, to allow chaining. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 247 | */ |
| 248 | Name& |
| 249 | append(const Component& component) |
| 250 | { |
| 251 | m_wire.push_back(component); |
| 252 | return *this; |
| 253 | } |
| 254 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 255 | /** @brief Append a NameComponent of TLV-TYPE @p type, copying @p count bytes at @p value as |
| 256 | * TLV-VALUE. |
| 257 | * @return a reference to this name, to allow chaining. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 258 | */ |
| 259 | Name& |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 260 | append(uint32_t type, const uint8_t* value, size_t count) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 261 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 262 | return append(Component(type, value, count)); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 265 | /** @brief Append a GenericNameComponent, copying @p count bytes at @p value as TLV-VALUE. |
| 266 | * @return a reference to this name, to allow chaining. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 267 | */ |
| 268 | Name& |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 269 | append(const uint8_t* value, size_t count) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 270 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 271 | return append(Component(value, count)); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 274 | /** @brief Append a NameComponent of TLV-TYPE @p type, copying TLV-VALUE from a range. |
| 275 | * @tparam Iterator an @c InputIterator dereferencing to a one-octet value type. More efficient |
| 276 | * implementation is available when it is a @c RandomAccessIterator. |
| 277 | * @param type the TLV-TYPE. |
| 278 | * @param first beginning of the range. |
| 279 | * @param last past-end of the range. |
| 280 | * @return a reference to this name, to allow chaining. |
| 281 | */ |
| 282 | template<class Iterator> |
| 283 | Name& |
| 284 | append(uint32_t type, Iterator first, Iterator last) |
| 285 | { |
| 286 | return append(Component(type, first, last)); |
| 287 | } |
| 288 | |
| 289 | /** @brief Append a GenericNameComponent, copying TLV-VALUE from a range. |
| 290 | * @tparam Iterator an @c InputIterator dereferencing to a one-octet value type. More efficient |
| 291 | * implementation is available when it is a @c RandomAccessIterator. |
| 292 | * @param first beginning of the range. |
| 293 | * @param last past-end of the range. |
| 294 | * @return a reference to this name, to allow chaining. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 295 | */ |
| 296 | template<class Iterator> |
| 297 | Name& |
| 298 | append(Iterator first, Iterator last) |
| 299 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 300 | return append(Component(first, last)); |
| 301 | } |
| 302 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 303 | /** @brief Append a GenericNameComponent, copying TLV-VALUE from a null-terminated string. |
| 304 | * @param str a null-terminated string. Bytes from the string are copied as is, and not |
| 305 | * interpreted as URI component. |
| 306 | * @return a reference to this name, to allow chaining. |
| 307 | */ |
| 308 | Name& |
| 309 | append(const char* str) |
| 310 | { |
| 311 | return append(Component(str)); |
| 312 | } |
| 313 | |
| 314 | /** @brief Append a GenericNameComponent from a TLV element. |
| 315 | * @param value a TLV element. If its type is @c tlv::GenericNameComponent, it is used as is. |
| 316 | * Otherwise, it is encapsulated into a GenericNameComponent. |
| 317 | * @return a reference to this name, to allow chaining. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 318 | */ |
| 319 | Name& |
| 320 | append(const Block& value) |
| 321 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 322 | if (value.type() == tlv::GenericNameComponent) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 323 | m_wire.push_back(value); |
| 324 | } |
| 325 | else { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 326 | m_wire.push_back(Block(tlv::GenericNameComponent, value)); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 327 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 328 | return *this; |
| 329 | } |
| 330 | |
| 331 | /** @brief Append a component with a nonNegativeInteger |
| 332 | * @sa number the number |
| 333 | * @return a reference to this name, to allow chaining |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 334 | * @sa https://named-data.net/doc/NDN-packet-spec/current/tlv.html#non-negative-integer-encoding |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 335 | */ |
| 336 | Name& |
| 337 | appendNumber(uint64_t number) |
| 338 | { |
| 339 | return append(Component::fromNumber(number)); |
| 340 | } |
| 341 | |
| 342 | /** @brief Append a component with a marked number |
| 343 | * @param marker 1-octet marker |
| 344 | * @param number the number |
| 345 | * |
| 346 | * The component is encoded as a 1-octet marker, followed by a nonNegativeInteger. |
| 347 | * |
| 348 | * @return a reference to this name, to allow chaining |
| 349 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 350 | */ |
| 351 | Name& |
| 352 | appendNumberWithMarker(uint8_t marker, uint64_t number) |
| 353 | { |
| 354 | return append(Component::fromNumberWithMarker(marker, number)); |
| 355 | } |
| 356 | |
| 357 | /** @brief Append a version component |
| 358 | * @return a reference to this name, to allow chaining |
| 359 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 360 | */ |
| 361 | Name& |
| 362 | appendVersion(uint64_t version) |
| 363 | { |
| 364 | return append(Component::fromVersion(version)); |
| 365 | } |
| 366 | |
| 367 | /** @brief Append a version component based on current time |
| 368 | * |
| 369 | * The version number is the current UNIX timestamp in milliseconds |
| 370 | * |
| 371 | * @return a reference to this name, to allow chaining |
| 372 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 373 | */ |
| 374 | Name& |
| 375 | appendVersion(); |
| 376 | |
| 377 | /** @brief Append a segment number (sequential) component |
| 378 | * @return a reference to this name, to allow chaining |
| 379 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 380 | */ |
| 381 | Name& |
| 382 | appendSegment(uint64_t segmentNo) |
| 383 | { |
| 384 | return append(Component::fromSegment(segmentNo)); |
| 385 | } |
| 386 | |
| 387 | /** @brief Append a segment byte offset component |
| 388 | * @return a reference to this name, to allow chaining |
| 389 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 390 | */ |
| 391 | Name& |
| 392 | appendSegmentOffset(uint64_t offset) |
| 393 | { |
| 394 | return append(Component::fromSegmentOffset(offset)); |
| 395 | } |
| 396 | |
| 397 | /** @brief Append a timestamp component |
| 398 | * @return a reference to this name, to allow chaining |
| 399 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 400 | */ |
| 401 | Name& |
| 402 | appendTimestamp(const time::system_clock::TimePoint& timePoint) |
| 403 | { |
| 404 | return append(Component::fromTimestamp(timePoint)); |
| 405 | } |
| 406 | |
| 407 | /** @brief Append a timestamp component based on current time |
| 408 | * @return a reference to this name, to allow chaining |
| 409 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 410 | */ |
| 411 | Name& |
| 412 | appendTimestamp(); |
| 413 | |
| 414 | /** @brief Append a sequence number component |
| 415 | * @return a reference to this name, to allow chaining |
| 416 | * @sa NDN Naming Conventions https://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 417 | */ |
| 418 | Name& |
| 419 | appendSequenceNumber(uint64_t seqNo) |
| 420 | { |
| 421 | return append(Component::fromSequenceNumber(seqNo)); |
| 422 | } |
| 423 | |
| 424 | /** @brief Append an ImplicitSha256Digest component |
| 425 | * @return a reference to this name, to allow chaining |
| 426 | */ |
| 427 | Name& |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 428 | appendImplicitSha256Digest(ConstBufferPtr digest) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 429 | { |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 430 | return append(Component::fromImplicitSha256Digest(std::move(digest))); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** @brief Append an ImplicitSha256Digest component |
| 434 | * @return a reference to this name, to allow chaining |
| 435 | */ |
| 436 | Name& |
| 437 | appendImplicitSha256Digest(const uint8_t* digest, size_t digestSize) |
| 438 | { |
| 439 | return append(Component::fromImplicitSha256Digest(digest, digestSize)); |
| 440 | } |
| 441 | |
| 442 | /** @brief Append a PartialName |
| 443 | * @param name the components to append |
| 444 | * @return a reference to this name, to allow chaining |
| 445 | */ |
| 446 | Name& |
| 447 | append(const PartialName& name); |
| 448 | |
| 449 | /** @brief Append a component |
| 450 | * @note This makes push_back an alias of append, giving Name a similar API as STL vector. |
| 451 | */ |
| 452 | template<class T> |
| 453 | void |
| 454 | push_back(const T& component) |
| 455 | { |
| 456 | append(component); |
| 457 | } |
| 458 | |
| 459 | /** @brief Remove all components |
| 460 | * @post empty() == true |
| 461 | */ |
| 462 | void |
| 463 | clear() |
| 464 | { |
| 465 | m_wire = Block(tlv::Name); |
| 466 | } |
| 467 | |
| 468 | public: // algorithms |
| 469 | /** @brief Get the successor of a name |
| 470 | * |
| 471 | * The successor of a name is defined as follows: |
| 472 | * |
| 473 | * N represents the set of NDN Names, and X,Y ∈ N. |
| 474 | * Operator < is defined by canonical order on N. |
| 475 | * Y is the successor of X, if (a) X < Y, and (b) ∄ Z ∈ N s.t. X < Z < Y. |
| 476 | * |
| 477 | * In plain words, successor of a name is the same name, but with its last component |
| 478 | * advanced to a next possible value. |
| 479 | * |
| 480 | * Examples: |
| 481 | * |
Junxiao Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 482 | * - successor of `/` is |
| 483 | * `/sha256digest=0000000000000000000000000000000000000000000000000000000000000000`. |
| 484 | * - successor of `/sha256digest=0000000000000000000000000000000000000000000000000000000000000000` |
| 485 | * is `/sha256digest=0000000000000000000000000000000000000000000000000000000000000001`. |
| 486 | * - successor of `/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff` |
| 487 | * is `/2=...`. |
| 488 | * - successor of `/P/A` is `/P/B`. |
| 489 | * - successor of `/Q/%FF` is `/Q/%00%00`. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 490 | * |
| 491 | * @return a new Name containing the successor |
| 492 | */ |
| 493 | Name |
| 494 | getSuccessor() const; |
| 495 | |
| 496 | /** @brief Check if this name is a prefix of another name |
| 497 | * |
| 498 | * This name is a prefix of @p other if the N components of this name are same as the first N |
| 499 | * components of @p other. |
| 500 | * |
| 501 | * @retval true this name is a prefix of @p other |
| 502 | * @retval false this name is not a prefix of @p other |
| 503 | */ |
| 504 | bool |
| 505 | isPrefixOf(const Name& other) const; |
| 506 | |
| 507 | /** @brief Check if this name equals another name |
| 508 | * |
| 509 | * Two names are equal if they have the same number of components, and components at each index |
| 510 | * are equal. |
| 511 | */ |
| 512 | bool |
| 513 | equals(const Name& other) const; |
| 514 | |
| 515 | /** @brief Compare this to the other Name using NDN canonical ordering. |
| 516 | * |
| 517 | * If the first components of each name are not equal, this returns a negative value if |
| 518 | * the first comes before the second using the NDN canonical ordering for name |
| 519 | * components, or a positive value if it comes after. If they are equal, this compares |
| 520 | * the second components of each name, etc. If both names are the same up to the size |
| 521 | * of the shorter name, this returns a negative value if the first name is shorter than |
| 522 | * the second or a positive value if it is longer. For example, if you std::sort gives: |
| 523 | * /a/b/d /a/b/cc /c /c/a /bb . |
| 524 | * This is intuitive because all names with the prefix /a are next to each other. |
| 525 | * But it may be also be counter-intuitive because /c comes before /bb according |
| 526 | * to NDN canonical ordering since it is shorter. |
| 527 | * |
| 528 | * @param other The other Name to compare with. |
| 529 | * |
| 530 | * @retval negative this comes before other in canonical ordering |
| 531 | * @retval zero this equals other |
| 532 | * @retval positive this comes after other in canonical ordering |
| 533 | * |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 534 | * @sa https://named-data.net/doc/NDN-packet-spec/current/name.html#canonical-order |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 535 | */ |
| 536 | int |
| 537 | compare(const Name& other) const |
| 538 | { |
| 539 | return this->compare(0, npos, other); |
| 540 | } |
| 541 | |
| 542 | /** @brief compares [pos1, pos1+count1) components in this Name |
| 543 | * to [pos2, pos2+count2) components in @p other |
| 544 | * |
| 545 | * This is equivalent to this->getSubName(pos1, count1).compare(other.getSubName(pos2, count2)); |
| 546 | */ |
| 547 | int |
| 548 | compare(size_t pos1, size_t count1, |
| 549 | const Name& other, size_t pos2 = 0, size_t count2 = npos) const; |
| 550 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 551 | public: |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 552 | /** @brief indicates "until the end" in getSubName and compare |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 553 | */ |
| 554 | static const size_t npos; |
| 555 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 556 | private: |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 557 | mutable Block m_wire; |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 558 | }; |
| 559 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 560 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(Name); |
| 561 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 562 | inline bool |
| 563 | operator==(const Name& lhs, const Name& rhs) |
| 564 | { |
| 565 | return lhs.equals(rhs); |
| 566 | } |
| 567 | |
| 568 | inline bool |
| 569 | operator!=(const Name& lhs, const Name& rhs) |
| 570 | { |
| 571 | return !lhs.equals(rhs); |
| 572 | } |
| 573 | |
| 574 | inline bool |
| 575 | operator<=(const Name& lhs, const Name& rhs) |
| 576 | { |
| 577 | return lhs.compare(rhs) <= 0; |
| 578 | } |
| 579 | |
| 580 | inline bool |
| 581 | operator<(const Name& lhs, const Name& rhs) |
| 582 | { |
| 583 | return lhs.compare(rhs) < 0; |
| 584 | } |
| 585 | |
| 586 | inline bool |
| 587 | operator>=(const Name& lhs, const Name& rhs) |
| 588 | { |
| 589 | return lhs.compare(rhs) >= 0; |
| 590 | } |
| 591 | |
| 592 | inline bool |
| 593 | operator>(const Name& lhs, const Name& rhs) |
| 594 | { |
| 595 | return lhs.compare(rhs) > 0; |
| 596 | } |
| 597 | |
| 598 | /** @brief Print URI representation of a name |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 599 | * @sa https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 600 | */ |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 601 | std::ostream& |
| 602 | operator<<(std::ostream& os, const Name& name); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 603 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 604 | /** @brief Parse URI from stream as Name |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 605 | * @sa https://named-data.net/doc/NDN-packet-spec/current/name.html#ndn-uri-scheme |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 606 | */ |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 607 | std::istream& |
| 608 | operator>>(std::istream& is, Name& name); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 609 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 610 | } // namespace ndn |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 611 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 612 | namespace std { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 613 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 614 | template<> |
| 615 | struct hash<ndn::Name> |
| 616 | { |
| 617 | size_t |
| 618 | operator()(const ndn::Name& name) const; |
| 619 | }; |
| 620 | |
| 621 | } // namespace std |
| 622 | |
Junxiao Shi | 8609bf2 | 2017-07-12 13:02:55 +0000 | [diff] [blame] | 623 | #endif // NDN_NAME_HPP |