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