Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | e6835fe | 2017-01-19 20:05:01 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 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 | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_NAME_COMPONENT_HPP |
| 23 | #define NDN_NAME_COMPONENT_HPP |
| 24 | |
| 25 | #include "common.hpp" |
| 26 | #include "encoding/block.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 27 | #include "encoding/block-helpers.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 28 | #include "util/time.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace name { |
| 32 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 33 | /// @brief Segment marker for NDN naming conventions |
| 34 | static const uint8_t SEGMENT_MARKER = 0x00; |
| 35 | /// @brief Segment offset marker for NDN naming conventions |
| 36 | static const uint8_t SEGMENT_OFFSET_MARKER = 0xFB; |
| 37 | /// @brief Version marker for NDN naming conventions |
| 38 | static const uint8_t VERSION_MARKER = 0xFD; |
| 39 | /// @brief Timestamp marker for NDN naming conventions |
| 40 | static const uint8_t TIMESTAMP_MARKER = 0xFC; |
| 41 | /// @brief Sequence number marker for NDN naming conventions |
| 42 | static const uint8_t SEQUENCE_NUMBER_MARKER = 0xFE; |
| 43 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 44 | /** |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 45 | * @brief Component holds a read-only name component value. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 46 | */ |
| 47 | class Component : public Block |
| 48 | { |
| 49 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 50 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 51 | * @brief Error that can be thrown from name::Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 53 | class Error : public Block::Error |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 54 | { |
| 55 | public: |
| 56 | explicit |
| 57 | Error(const std::string& what) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 58 | : Block::Error(what) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | { |
| 60 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 63 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 64 | * Create a new name::Component with an empty value |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 65 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 66 | Component(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 68 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 69 | * @brief Create name::Component from a wire block |
| 70 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 71 | * @param wire tlv::NameComponent Block from which to create name::Component |
| 72 | * @throws Error if wire.type() is not tlv::NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 73 | * |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 74 | * Any block can be implicitly converted to name::Component |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 75 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 76 | Component(const Block& wire); |
| 77 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 78 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 79 | * @brief Create a new name::Component from the buffer pointer (buffer pointer will be copied) |
| 80 | * |
| 81 | * @param buffer A pointer to an immutable buffer |
| 82 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 83 | * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 84 | * Note that this method **will not** allocate new memory for and copy the payload until |
| 85 | * toWire() method is called. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 86 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 87 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 88 | Component(const ConstBufferPtr& buffer); |
| 89 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 90 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 91 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 92 | * @param buffer A reference to the buffer |
| 93 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 94 | * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 95 | * Note that this method **will** allocate new memory for and copy the payload. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 96 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 97 | explicit |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 98 | Component(const Buffer& buffer); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 101 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 102 | * @param buffer A pointer to the first byte of the buffer |
| 103 | * @param bufferSize Size of the buffer |
| 104 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 105 | * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 106 | * Note that this method **will** allocate new memory for and copy the payload. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 107 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 108 | Component(const uint8_t* buffer, size_t bufferSize); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 110 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 111 | * @brief Create a new name::Component frome the range [@p first, @p last) of bytes |
| 112 | * @param first Iterator pointing to the beginning of the buffer |
| 113 | * @param last Iterator pointing to the ending of the buffer |
| 114 | * @tparam Iterator iterator type satisfying at least InputIterator concept. Implementation |
| 115 | * is more optimal when the iterator type satisfies RandomAccessIterator concept. |
| 116 | * It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 117 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 118 | * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 119 | * Note that this method **will** allocate new memory for and copy the payload. |
| 120 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 121 | template<class Iterator> |
| 122 | Component(Iterator first, Iterator last); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 124 | /** |
| 125 | * @brief Create a new name::Component from the C string (data from string will be copied) |
| 126 | * |
| 127 | * @param str Zero-ended string. Note that this string will be interpreted as is (i.e., |
| 128 | * it will not be interpreted as URI) |
| 129 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 130 | * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 131 | * Note that this method **will** allocate new memory for and copy the payload. |
| 132 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 133 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 134 | Component(const char* str); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 135 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 136 | /** |
| 137 | * @brief Create a new name::Component from the STL string (data from string will be copied) |
| 138 | * |
| 139 | * @param str Const reference to STL string. Note that this string will be interpreted |
| 140 | * as is (i.e., it will not be interpreted as URI) |
| 141 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 142 | * This constructor will create a new tlv::NameComponent Block with `buffer` as a payload. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 143 | * Note that this method **will** allocate new memory for and copy the payload. |
| 144 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 145 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 146 | Component(const std::string& str); |
| 147 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 148 | /** |
| 149 | * @brief Fast encoding or block size estimation |
| 150 | */ |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 151 | template<encoding::Tag TAG> |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 152 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 153 | wireEncode(EncodingImpl<TAG>& encoder) const; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 155 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 156 | * @brief Encode to a wire format |
| 157 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 158 | const Block& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 159 | wireEncode() const; |
| 160 | |
| 161 | /** |
| 162 | * @brief Decode from the wire format |
| 163 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 164 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 165 | wireDecode(const Block& wire); |
| 166 | |
| 167 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 168 | * @brief Create name::Component by decoding the escapedString between beginOffset and |
| 169 | * endOffset according to the NDN URI Scheme. |
| 170 | * |
| 171 | * If the escaped string is "", "." or ".." then return an empty name::Component. Note |
| 172 | * that an empty name::Component should not be added to Name and if attempted, an |
| 173 | * exception will be thrown. |
| 174 | * |
| 175 | * @param escapedString String containing NDN URI-encoded name |
| 176 | * component. [escapedString+beginOffset, beginOffset+endOffset) |
| 177 | * must be a valid memory buffer. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 178 | * @param beginOffset The offset in escapedString of the beginning of the portion to decode. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 179 | * @param endOffset The offset in escapedString of the end of the portion to decode. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 180 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 181 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 182 | fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 185 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 186 | * |
| 187 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 188 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 189 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 190 | fromEscapedString(const char* escapedString) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 191 | { |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 192 | return fromEscapedString(escapedString, 0, std::char_traits<char>::length(escapedString)); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 196 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 197 | * |
| 198 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 199 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 200 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 201 | fromEscapedString(const std::string& escapedString) |
| 202 | { |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 203 | return fromEscapedString(escapedString.c_str(), 0, escapedString.size()); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 207 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 208 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 209 | * This also adds "..." to a value with zero or more "." |
| 210 | * |
| 211 | * @param os The output stream to where write the URI escaped version *this |
| 212 | */ |
| 213 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 214 | toUri(std::ostream& os) const; |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 215 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 216 | /** |
| 217 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 218 | * |
| 219 | * This also adds "..." to a value with zero or more "." |
| 220 | * |
| 221 | * @return The escaped string |
| 222 | */ |
| 223 | std::string |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 224 | toUri() const; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 225 | |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 226 | //////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | /** |
| 229 | * @brief Check if the component is nonNegativeInteger |
| 230 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 231 | */ |
| 232 | bool |
| 233 | isNumber() const; |
| 234 | |
| 235 | /** |
| 236 | * @brief Check if the component is NameComponentWithMarker per NDN naming conventions |
| 237 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 238 | */ |
| 239 | bool |
| 240 | isNumberWithMarker(uint8_t marker) const; |
| 241 | |
| 242 | /** |
| 243 | * @brief Check if the component is version per NDN naming conventions |
| 244 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 245 | */ |
| 246 | bool |
| 247 | isVersion() const; |
| 248 | |
| 249 | /** |
| 250 | * @brief Check if the component is segment number per NDN naming conventions |
| 251 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 252 | */ |
| 253 | bool |
| 254 | isSegment() const; |
| 255 | |
| 256 | /** |
| 257 | * @brief Check if the component is segment offset per NDN naming conventions |
| 258 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 259 | */ |
| 260 | bool |
| 261 | isSegmentOffset() const; |
| 262 | |
| 263 | /** |
| 264 | * @brief Check if the component is timestamp per NDN naming conventions |
| 265 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 266 | */ |
| 267 | bool |
| 268 | isTimestamp() const; |
| 269 | |
| 270 | /** |
| 271 | * @brief Check if the component is sequence number per NDN naming conventions |
| 272 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 273 | */ |
| 274 | bool |
| 275 | isSequenceNumber() const; |
| 276 | |
| 277 | //////////////////////////////////////////////////////////////////////////////// |
| 278 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 279 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 280 | * @brief Interpret this name component as nonNegativeInteger |
| 281 | * |
| 282 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 283 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 284 | * @return The integer number. |
| 285 | */ |
| 286 | uint64_t |
| 287 | toNumber() const; |
| 288 | |
| 289 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 290 | * @brief Interpret this name component as NameComponentWithMarker |
| 291 | * |
| 292 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 293 | * |
| 294 | * @param marker 1-byte octet of the marker |
| 295 | * @return The integer number. |
| 296 | * @throws Error if name component does not have the specified marker. |
| 297 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 298 | */ |
| 299 | uint64_t |
| 300 | toNumberWithMarker(uint8_t marker) const; |
| 301 | |
| 302 | /** |
| 303 | * @brief Interpret as version component using NDN naming conventions |
| 304 | * |
| 305 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 306 | * |
| 307 | * @throws Error if name component does not have the specified marker. |
| 308 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 309 | */ |
| 310 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 311 | toVersion() const; |
| 312 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 313 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 314 | * @brief Interpret as segment number component using NDN naming conventions |
| 315 | * |
| 316 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 317 | * |
| 318 | * @throws Error if name component does not have the specified marker. |
| 319 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 320 | */ |
| 321 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 322 | toSegment() const; |
| 323 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 324 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 325 | * @brief Interpret as segment offset component using NDN naming conventions |
| 326 | * |
| 327 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 328 | * |
| 329 | * @throws Error if name component does not have the specified marker. |
| 330 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 331 | */ |
| 332 | uint64_t |
| 333 | toSegmentOffset() const; |
| 334 | |
| 335 | /** |
| 336 | * @brief Interpret as timestamp component using NDN naming conventions |
| 337 | * |
| 338 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 339 | * |
| 340 | * @throws Error if name component does not have the specified marker. |
| 341 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 342 | */ |
| 343 | time::system_clock::TimePoint |
| 344 | toTimestamp() const; |
| 345 | |
| 346 | /** |
| 347 | * @brief Interpret as sequence number component using NDN naming conventions |
| 348 | * |
| 349 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 350 | * |
| 351 | * @throws Error if name component does not have the specified marker. |
| 352 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 353 | */ |
| 354 | uint64_t |
| 355 | toSequenceNumber() const; |
| 356 | |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 357 | //////////////////////////////////////////////////////////////////////////////// |
| 358 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 359 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 360 | * @brief Create a component encoded as nonNegativeInteger |
| 361 | * |
| 362 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 363 | * |
| 364 | * @param number The non-negative number |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 365 | * @return The component value. |
| 366 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 367 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 368 | fromNumber(uint64_t number); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 369 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 370 | /** |
| 371 | * @brief Create a component encoded as NameComponentWithMarker |
| 372 | * |
| 373 | * NameComponentWithMarker is defined as: |
| 374 | * |
| 375 | * NameComponentWithMarker ::= NAME-COMPONENT-TYPE TLV-LEGTH |
| 376 | * Marker |
| 377 | * includedNonNegativeInteger |
| 378 | * Marker ::= BYTE |
| 379 | * includedNonNegativeInteger ::= BYTE{1,2,4,8} |
| 380 | * NDN-TLV := TLV-TYPE TLV-LENGTH TLV-VALUE? |
| 381 | * TLV-TYPE := VAR-NUMBER |
| 382 | * TLV-LENGTH := VAR-NUMBER |
| 383 | * TLV-VALUE := BYTE+ |
| 384 | * |
| 385 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 386 | * |
| 387 | * @param marker 1-byte marker octet |
| 388 | * @param number The non-negative number |
| 389 | * @return The component value. |
| 390 | */ |
| 391 | static Component |
| 392 | fromNumberWithMarker(uint8_t marker, uint64_t number); |
| 393 | |
| 394 | /** |
| 395 | * @brief Create version component using NDN naming conventions |
| 396 | * |
| 397 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 398 | */ |
| 399 | static Component |
| 400 | fromVersion(uint64_t version); |
| 401 | |
| 402 | /** |
| 403 | * @brief Create segment number component using NDN naming conventions |
| 404 | * |
| 405 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 406 | */ |
| 407 | static Component |
| 408 | fromSegment(uint64_t segmentNo); |
| 409 | |
| 410 | /** |
| 411 | * @brief Create segment offset component using NDN naming conventions |
| 412 | * |
| 413 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 414 | */ |
| 415 | static Component |
| 416 | fromSegmentOffset(uint64_t offset); |
| 417 | |
| 418 | /** |
| 419 | * @brief Create sequence number component using NDN naming conventions |
| 420 | * |
| 421 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 422 | */ |
| 423 | static Component |
| 424 | fromTimestamp(const time::system_clock::TimePoint& timePoint); |
| 425 | |
| 426 | /** |
| 427 | * @brief Create sequence number component using NDN naming conventions |
| 428 | * |
| 429 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 430 | */ |
| 431 | static Component |
| 432 | fromSequenceNumber(uint64_t seqNo); |
| 433 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 434 | //////////////////////////////////////////////////////////////////////////////// |
| 435 | |
| 436 | /** |
| 437 | * @brief Check if the component is GenericComponent |
| 438 | */ |
| 439 | bool |
| 440 | isGeneric() const; |
| 441 | |
| 442 | /** |
| 443 | * @brief Check if the component is ImplicitSha256DigestComponent |
| 444 | */ |
| 445 | bool |
| 446 | isImplicitSha256Digest() const; |
| 447 | |
| 448 | /** |
| 449 | * @brief Create ImplicitSha256DigestComponent component |
| 450 | */ |
| 451 | static Component |
| 452 | fromImplicitSha256Digest(const ConstBufferPtr& digest); |
| 453 | |
| 454 | /** |
| 455 | * @brief Create ImplicitSha256DigestComponent component |
| 456 | */ |
| 457 | static Component |
| 458 | fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize); |
| 459 | |
| 460 | //////////////////////////////////////////////////////////////////////////////// |
| 461 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 462 | bool |
| 463 | empty() const |
| 464 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 465 | return value_size() == 0; |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 468 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 469 | * @brief Check if this is the same component as other |
| 470 | * |
| 471 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 472 | * @return true if the components are equal, otherwise false. |
| 473 | */ |
| 474 | bool |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 475 | equals(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 476 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 477 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 478 | * @brief Compare this to the other Component using NDN canonical ordering |
| 479 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 480 | * @param other The other Component to compare with. |
Joao Pereira | aa8fd16 | 2015-06-05 16:35:15 -0400 | [diff] [blame] | 481 | * @retval negative this comes before other in canonical ordering |
| 482 | * @retval zero this equals other |
| 483 | * @retval positive this comes after other in canonical ordering |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 484 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 485 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 486 | */ |
| 487 | int |
| 488 | compare(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 489 | |
| 490 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 491 | * @brief Check if this is the same component as other |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 492 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 493 | * @param other The other Component to compare with. |
| 494 | * @return true if the components are equal, otherwise false. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 495 | */ |
| 496 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 497 | operator==(const Component& other) const |
| 498 | { |
| 499 | return equals(other); |
| 500 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 501 | |
| 502 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 503 | * @brief Check if this is not the same component as other |
| 504 | * @param other The other Component to compare with |
| 505 | * @return true if the components are not equal, otherwise false |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 506 | */ |
| 507 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 508 | operator!=(const Component& other) const |
| 509 | { |
| 510 | return !equals(other); |
| 511 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 512 | |
| 513 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 514 | * @brief Check if the *this is less than or equal to the other in NDN canonical ordering |
| 515 | * @param other The other Component to compare with |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 516 | * |
| 517 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 518 | */ |
| 519 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 520 | operator<=(const Component& other) const |
| 521 | { |
| 522 | return compare(other) <= 0; |
| 523 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 524 | |
| 525 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 526 | * @brief Check if the *this is less than the other in NDN canonical ordering |
| 527 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 528 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 529 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 530 | */ |
| 531 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 532 | operator<(const Component& other) const |
| 533 | { |
| 534 | return compare(other) < 0; |
| 535 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 536 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 537 | /** |
| 538 | * @brief Check if the *this is greater or equal than the other in NDN canonical ordering |
| 539 | * @param other The other Component to compare with |
| 540 | * |
| 541 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 542 | */ |
| 543 | bool |
| 544 | operator>=(const Component& other) const |
| 545 | { |
| 546 | return compare(other) >= 0; |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * @brief Check if the *this is greater than the other in NDN canonical ordering |
| 551 | * @param other The other Component to compare with |
| 552 | * |
| 553 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 554 | */ |
| 555 | bool |
| 556 | operator>(const Component& other) const |
| 557 | { |
| 558 | return compare(other) > 0; |
| 559 | } |
| 560 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 561 | Component |
| 562 | getSuccessor() const; |
| 563 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 564 | // !!! NOTE TO IMPLEMENTOR !!! |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 565 | // |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 566 | // This class MUST NOT contain any data fields. |
| 567 | // Block can be reinterpret_cast'ed as Component type. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 568 | }; |
| 569 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 570 | inline std::ostream& |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 571 | operator<<(std::ostream& os, const Component& component) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 572 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 573 | component.toUri(os); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 574 | return os; |
| 575 | } |
| 576 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 577 | template<class Iterator> |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 578 | inline |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 579 | Component::Component(Iterator first, Iterator last) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 580 | : Block(makeBinaryBlock(tlv::NameComponent, first, last)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 581 | { |
| 582 | } |
| 583 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 584 | } // namespace name |
| 585 | } // namespace ndn |
| 586 | |
| 587 | #endif // NDN_NAME_COMPONENT_HPP |