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 | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 153 | wireEncode(EncodingImpl<TAG>& block) 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 | * |
| 209 | * @deprecated Use toUri(std::ostream&) instead |
| 210 | * |
| 211 | * This also adds "..." to a value with zero or more "." |
| 212 | * |
| 213 | * @param os The output stream to where write the URI escaped version *this |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 214 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 215 | DEPRECATED( |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 216 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 217 | toEscapedString(std::ostream& os) const) |
| 218 | { |
| 219 | return toUri(os); |
| 220 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 221 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 222 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 223 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 224 | * |
| 225 | * @deprecated Use toUri() instead |
| 226 | * |
| 227 | * This also adds "..." to a value with zero or more "." |
| 228 | * |
| 229 | * @return The escaped string |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 230 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 231 | DEPRECATED( |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 232 | std::string |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 233 | toEscapedString() const) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 234 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 235 | return toUri(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 236 | } |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 238 | /** |
| 239 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 240 | * |
| 241 | * This also adds "..." to a value with zero or more "." |
| 242 | * |
| 243 | * @param os The output stream to where write the URI escaped version *this |
| 244 | */ |
| 245 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 246 | toUri(std::ostream& os) const; |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 247 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 248 | /** |
| 249 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 250 | * |
| 251 | * This also adds "..." to a value with zero or more "." |
| 252 | * |
| 253 | * @return The escaped string |
| 254 | */ |
| 255 | std::string |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 256 | toUri() const; |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 257 | |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 258 | //////////////////////////////////////////////////////////////////////////////// |
| 259 | |
| 260 | /** |
| 261 | * @brief Check if the component is nonNegativeInteger |
| 262 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 263 | */ |
| 264 | bool |
| 265 | isNumber() const; |
| 266 | |
| 267 | /** |
| 268 | * @brief Check if the component is NameComponentWithMarker per NDN naming conventions |
| 269 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 270 | */ |
| 271 | bool |
| 272 | isNumberWithMarker(uint8_t marker) const; |
| 273 | |
| 274 | /** |
| 275 | * @brief Check if the component is version per NDN naming conventions |
| 276 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 277 | */ |
| 278 | bool |
| 279 | isVersion() const; |
| 280 | |
| 281 | /** |
| 282 | * @brief Check if the component is segment number per NDN naming conventions |
| 283 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 284 | */ |
| 285 | bool |
| 286 | isSegment() const; |
| 287 | |
| 288 | /** |
| 289 | * @brief Check if the component is segment offset per NDN naming conventions |
| 290 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 291 | */ |
| 292 | bool |
| 293 | isSegmentOffset() const; |
| 294 | |
| 295 | /** |
| 296 | * @brief Check if the component is timestamp per NDN naming conventions |
| 297 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 298 | */ |
| 299 | bool |
| 300 | isTimestamp() const; |
| 301 | |
| 302 | /** |
| 303 | * @brief Check if the component is sequence number per NDN naming conventions |
| 304 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 305 | */ |
| 306 | bool |
| 307 | isSequenceNumber() const; |
| 308 | |
| 309 | //////////////////////////////////////////////////////////////////////////////// |
| 310 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 311 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 312 | * @brief Interpret this name component as nonNegativeInteger |
| 313 | * |
| 314 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 315 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 316 | * @return The integer number. |
| 317 | */ |
| 318 | uint64_t |
| 319 | toNumber() const; |
| 320 | |
| 321 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 322 | * @brief Interpret this name component as NameComponentWithMarker |
| 323 | * |
| 324 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 325 | * |
| 326 | * @param marker 1-byte octet of the marker |
| 327 | * @return The integer number. |
| 328 | * @throws Error if name component does not have the specified marker. |
| 329 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 330 | */ |
| 331 | uint64_t |
| 332 | toNumberWithMarker(uint8_t marker) const; |
| 333 | |
| 334 | /** |
| 335 | * @brief Interpret as version component using NDN naming conventions |
| 336 | * |
| 337 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 338 | * |
| 339 | * @throws Error if name component does not have the specified marker. |
| 340 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 341 | */ |
| 342 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 343 | toVersion() const; |
| 344 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 345 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 346 | * @brief Interpret as segment number component using NDN naming conventions |
| 347 | * |
| 348 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 349 | * |
| 350 | * @throws Error if name component does not have the specified marker. |
| 351 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 352 | */ |
| 353 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 354 | toSegment() const; |
| 355 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 356 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 357 | * @brief Interpret as segment offset component using NDN naming conventions |
| 358 | * |
| 359 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 360 | * |
| 361 | * @throws Error if name component does not have the specified marker. |
| 362 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 363 | */ |
| 364 | uint64_t |
| 365 | toSegmentOffset() const; |
| 366 | |
| 367 | /** |
| 368 | * @brief Interpret as timestamp component using NDN naming conventions |
| 369 | * |
| 370 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 371 | * |
| 372 | * @throws Error if name component does not have the specified marker. |
| 373 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 374 | */ |
| 375 | time::system_clock::TimePoint |
| 376 | toTimestamp() const; |
| 377 | |
| 378 | /** |
| 379 | * @brief Interpret as sequence number component using NDN naming conventions |
| 380 | * |
| 381 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 382 | * |
| 383 | * @throws Error if name component does not have the specified marker. |
| 384 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 385 | */ |
| 386 | uint64_t |
| 387 | toSequenceNumber() const; |
| 388 | |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 389 | //////////////////////////////////////////////////////////////////////////////// |
| 390 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 391 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 392 | * @brief Create a component encoded as nonNegativeInteger |
| 393 | * |
| 394 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 395 | * |
| 396 | * @param number The non-negative number |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 397 | * @return The component value. |
| 398 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 399 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 400 | fromNumber(uint64_t number); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 401 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 402 | /** |
| 403 | * @brief Create a component encoded as NameComponentWithMarker |
| 404 | * |
| 405 | * NameComponentWithMarker is defined as: |
| 406 | * |
| 407 | * NameComponentWithMarker ::= NAME-COMPONENT-TYPE TLV-LEGTH |
| 408 | * Marker |
| 409 | * includedNonNegativeInteger |
| 410 | * Marker ::= BYTE |
| 411 | * includedNonNegativeInteger ::= BYTE{1,2,4,8} |
| 412 | * NDN-TLV := TLV-TYPE TLV-LENGTH TLV-VALUE? |
| 413 | * TLV-TYPE := VAR-NUMBER |
| 414 | * TLV-LENGTH := VAR-NUMBER |
| 415 | * TLV-VALUE := BYTE+ |
| 416 | * |
| 417 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 418 | * |
| 419 | * @param marker 1-byte marker octet |
| 420 | * @param number The non-negative number |
| 421 | * @return The component value. |
| 422 | */ |
| 423 | static Component |
| 424 | fromNumberWithMarker(uint8_t marker, uint64_t number); |
| 425 | |
| 426 | /** |
| 427 | * @brief Create version component using NDN naming conventions |
| 428 | * |
| 429 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 430 | */ |
| 431 | static Component |
| 432 | fromVersion(uint64_t version); |
| 433 | |
| 434 | /** |
| 435 | * @brief Create segment number component using NDN naming conventions |
| 436 | * |
| 437 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 438 | */ |
| 439 | static Component |
| 440 | fromSegment(uint64_t segmentNo); |
| 441 | |
| 442 | /** |
| 443 | * @brief Create segment offset component using NDN naming conventions |
| 444 | * |
| 445 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 446 | */ |
| 447 | static Component |
| 448 | fromSegmentOffset(uint64_t offset); |
| 449 | |
| 450 | /** |
| 451 | * @brief Create sequence number component using NDN naming conventions |
| 452 | * |
| 453 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 454 | */ |
| 455 | static Component |
| 456 | fromTimestamp(const time::system_clock::TimePoint& timePoint); |
| 457 | |
| 458 | /** |
| 459 | * @brief Create sequence number component using NDN naming conventions |
| 460 | * |
| 461 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 462 | */ |
| 463 | static Component |
| 464 | fromSequenceNumber(uint64_t seqNo); |
| 465 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 466 | //////////////////////////////////////////////////////////////////////////////// |
| 467 | |
| 468 | /** |
| 469 | * @brief Check if the component is GenericComponent |
| 470 | */ |
| 471 | bool |
| 472 | isGeneric() const; |
| 473 | |
| 474 | /** |
| 475 | * @brief Check if the component is ImplicitSha256DigestComponent |
| 476 | */ |
| 477 | bool |
| 478 | isImplicitSha256Digest() const; |
| 479 | |
| 480 | /** |
| 481 | * @brief Create ImplicitSha256DigestComponent component |
| 482 | */ |
| 483 | static Component |
| 484 | fromImplicitSha256Digest(const ConstBufferPtr& digest); |
| 485 | |
| 486 | /** |
| 487 | * @brief Create ImplicitSha256DigestComponent component |
| 488 | */ |
| 489 | static Component |
| 490 | fromImplicitSha256Digest(const uint8_t* digest, size_t digestSize); |
| 491 | |
| 492 | //////////////////////////////////////////////////////////////////////////////// |
| 493 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 494 | bool |
| 495 | empty() const |
| 496 | { |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 497 | return !hasValue() || value_size() == 0; |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 500 | Component |
| 501 | getSuccessor() const; |
| 502 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 503 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 504 | * @brief Check if this is the same component as other |
| 505 | * |
| 506 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 507 | * @return true if the components are equal, otherwise false. |
| 508 | */ |
| 509 | bool |
| 510 | equals(const Component& other) const |
| 511 | { |
| 512 | if (value_size() != other.value_size()) |
| 513 | return false; |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 514 | if (value_size() == 0 /* == other.value_size()*/) |
| 515 | return true; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 516 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 517 | // somehow, behavior is wrong on OSX 10.9 when component is empty |
| 518 | // (probably some bug in STL...) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 519 | return std::equal(value_begin(), value_end(), other.value_begin()); |
| 520 | } |
| 521 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 522 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 523 | * @brief Compare this to the other Component using NDN canonical ordering |
| 524 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 525 | * @param other The other Component to compare with. |
| 526 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 527 | * 1 if *this comes after other in the canonical ordering. |
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 | int |
| 532 | compare(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 533 | |
| 534 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 535 | * @brief Check if this is the same component as other |
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 | * @param other The other Component to compare with. |
| 538 | * @return true if the components are equal, otherwise false. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 539 | */ |
| 540 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 541 | operator==(const Component& other) const |
| 542 | { |
| 543 | return equals(other); |
| 544 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 545 | |
| 546 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 547 | * @brief Check if this is not the same component as other |
| 548 | * @param other The other Component to compare with |
| 549 | * @return true if the components are not equal, otherwise false |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 550 | */ |
| 551 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 552 | operator!=(const Component& other) const |
| 553 | { |
| 554 | return !equals(other); |
| 555 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 556 | |
| 557 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 558 | * @brief Check if the *this is less than or equal to the other in NDN canonical ordering |
| 559 | * @param other The other Component to compare with |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 560 | * |
| 561 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 562 | */ |
| 563 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 564 | operator<=(const Component& other) const |
| 565 | { |
| 566 | return compare(other) <= 0; |
| 567 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 568 | |
| 569 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 570 | * @brief Check if the *this is less than the other in NDN canonical ordering |
| 571 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 572 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 573 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 574 | */ |
| 575 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 576 | operator<(const Component& other) const |
| 577 | { |
| 578 | return compare(other) < 0; |
| 579 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 580 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 581 | /** |
| 582 | * @brief Check if the *this is greater or equal than the other in NDN canonical ordering |
| 583 | * @param other The other Component to compare with |
| 584 | * |
| 585 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 586 | */ |
| 587 | bool |
| 588 | operator>=(const Component& other) const |
| 589 | { |
| 590 | return compare(other) >= 0; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * @brief Check if the *this is greater than the other in NDN canonical ordering |
| 595 | * @param other The other Component to compare with |
| 596 | * |
| 597 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 598 | */ |
| 599 | bool |
| 600 | operator>(const Component& other) const |
| 601 | { |
| 602 | return compare(other) > 0; |
| 603 | } |
| 604 | |
| 605 | // !!! NOTE TO IMPLEMENTOR !!! |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 606 | // |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 607 | // This class MUST NOT contain any data fields. |
| 608 | // Block can be reinterpret_cast'ed as Component type. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 609 | }; |
| 610 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 611 | inline std::ostream& |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 612 | operator<<(std::ostream& os, const Component& component) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 613 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 614 | component.toUri(os); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 615 | return os; |
| 616 | } |
| 617 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 618 | template<class Iterator> |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 619 | inline |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 620 | Component::Component(Iterator first, Iterator last) |
| 621 | : Block(dataBlock(tlv::NameComponent, first, last)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 622 | { |
| 623 | } |
| 624 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 625 | } // namespace name |
| 626 | } // namespace ndn |
| 627 | |
| 628 | #endif // NDN_NAME_COMPONENT_HPP |