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