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