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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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/> |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifndef NDN_NAME_COMPONENT_HPP |
| 27 | #define NDN_NAME_COMPONENT_HPP |
| 28 | |
| 29 | #include "common.hpp" |
| 30 | #include "encoding/block.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 31 | #include "util/time.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace name { |
| 35 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 36 | /// @brief Segment marker for NDN naming conventions |
| 37 | static const uint8_t SEGMENT_MARKER = 0x00; |
| 38 | /// @brief Segment offset marker for NDN naming conventions |
| 39 | static const uint8_t SEGMENT_OFFSET_MARKER = 0xFB; |
| 40 | /// @brief Version marker for NDN naming conventions |
| 41 | static const uint8_t VERSION_MARKER = 0xFD; |
| 42 | /// @brief Timestamp marker for NDN naming conventions |
| 43 | static const uint8_t TIMESTAMP_MARKER = 0xFC; |
| 44 | /// @brief Sequence number marker for NDN naming conventions |
| 45 | static const uint8_t SEQUENCE_NUMBER_MARKER = 0xFE; |
| 46 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 47 | /** |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 48 | * @brief Component holds a read-only name component value. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 49 | */ |
| 50 | class Component : public Block |
| 51 | { |
| 52 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 54 | * @brief Error that can be thrown from name::Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 55 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 56 | class Error : public Block::Error |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 57 | { |
| 58 | public: |
| 59 | explicit |
| 60 | Error(const std::string& what) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 61 | : Block::Error(what) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 62 | { |
| 63 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 66 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 67 | * Create a new name::Component with an empty value |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 68 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 69 | Component(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 71 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 72 | * @brief Create name::Component from a wire block |
| 73 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 74 | * @param wire tlv::NameComponent Block from which to create name::Component |
| 75 | * @throws Error if wire.type() is not tlv::NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 76 | * |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 77 | * Any block can be implicitly converted to name::Component |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 78 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 79 | Component(const Block& wire); |
| 80 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 81 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 82 | * @brief Create a new name::Component from the buffer pointer (buffer pointer will be copied) |
| 83 | * |
| 84 | * @param buffer A pointer to an immutable buffer |
| 85 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 86 | * 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] | 87 | * Note that this method **will not** allocate new memory for and copy the payload until |
| 88 | * toWire() method is called. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 89 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 90 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 91 | Component(const ConstBufferPtr& buffer); |
| 92 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 93 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 94 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 95 | * @param buffer A reference to the buffer |
| 96 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 97 | * 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] | 98 | * 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] | 99 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 100 | explicit |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 101 | Component(const Buffer& buffer); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 102 | |
| 103 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 104 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 105 | * @param buffer A pointer to the first byte of the buffer |
| 106 | * @param bufferSize Size of the buffer |
| 107 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 108 | * 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] | 109 | * 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] | 110 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 111 | Component(const uint8_t* buffer, size_t bufferSize); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 112 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 113 | /** |
| 114 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 115 | * @param begin Iterator pointing to the beginning of the buffer |
| 116 | * @param end Iterator pointing to the ending of the buffer |
| 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 | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 121 | template<class InputIterator> |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 122 | Component(InputIterator begin, InputIterator end); |
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 | */ |
| 151 | template<bool T> |
| 152 | size_t |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 153 | wireEncode(EncodingImpl<T>& block) const; |
| 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 | { |
| 192 | return fromEscapedString(escapedString, 0, ::strlen(escapedString)); |
| 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 | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 258 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 259 | * @brief Interpret this name component as nonNegativeInteger |
| 260 | * |
| 261 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 262 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 263 | * @return The integer number. |
| 264 | */ |
| 265 | uint64_t |
| 266 | toNumber() const; |
| 267 | |
| 268 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 269 | * @brief Interpret this name component as NameComponentWithMarker |
| 270 | * |
| 271 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 272 | * |
| 273 | * @param marker 1-byte octet of the marker |
| 274 | * @return The integer number. |
| 275 | * @throws Error if name component does not have the specified marker. |
| 276 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 277 | */ |
| 278 | uint64_t |
| 279 | toNumberWithMarker(uint8_t marker) const; |
| 280 | |
| 281 | /** |
| 282 | * @brief Interpret as version component using NDN naming conventions |
| 283 | * |
| 284 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 285 | * |
| 286 | * @throws Error if name component does not have the specified marker. |
| 287 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 288 | */ |
| 289 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 290 | toVersion() const; |
| 291 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 292 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 293 | * @brief Interpret as segment number component using NDN naming conventions |
| 294 | * |
| 295 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 296 | * |
| 297 | * @throws Error if name component does not have the specified marker. |
| 298 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 299 | */ |
| 300 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 301 | toSegment() const; |
| 302 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 303 | /** |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 304 | * @brief Interpret as segment offset component using NDN naming conventions |
| 305 | * |
| 306 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 307 | * |
| 308 | * @throws Error if name component does not have the specified marker. |
| 309 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 310 | */ |
| 311 | uint64_t |
| 312 | toSegmentOffset() const; |
| 313 | |
| 314 | /** |
| 315 | * @brief Interpret as timestamp component using NDN naming conventions |
| 316 | * |
| 317 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 318 | * |
| 319 | * @throws Error if name component does not have the specified marker. |
| 320 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 321 | */ |
| 322 | time::system_clock::TimePoint |
| 323 | toTimestamp() const; |
| 324 | |
| 325 | /** |
| 326 | * @brief Interpret as sequence number component using NDN naming conventions |
| 327 | * |
| 328 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 329 | * |
| 330 | * @throws Error if name component does not have the specified marker. |
| 331 | * tlv::Error if format does not follow NameComponentWithMarker specification. |
| 332 | */ |
| 333 | uint64_t |
| 334 | toSequenceNumber() const; |
| 335 | |
| 336 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 337 | * @brief Create a component encoded as nonNegativeInteger |
| 338 | * |
| 339 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 340 | * |
| 341 | * @param number The non-negative number |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 342 | * @return The component value. |
| 343 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 344 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 345 | fromNumber(uint64_t number); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 346 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 347 | /** |
| 348 | * @brief Create a component encoded as NameComponentWithMarker |
| 349 | * |
| 350 | * NameComponentWithMarker is defined as: |
| 351 | * |
| 352 | * NameComponentWithMarker ::= NAME-COMPONENT-TYPE TLV-LEGTH |
| 353 | * Marker |
| 354 | * includedNonNegativeInteger |
| 355 | * Marker ::= BYTE |
| 356 | * includedNonNegativeInteger ::= BYTE{1,2,4,8} |
| 357 | * NDN-TLV := TLV-TYPE TLV-LENGTH TLV-VALUE? |
| 358 | * TLV-TYPE := VAR-NUMBER |
| 359 | * TLV-LENGTH := VAR-NUMBER |
| 360 | * TLV-VALUE := BYTE+ |
| 361 | * |
| 362 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 363 | * |
| 364 | * @param marker 1-byte marker octet |
| 365 | * @param number The non-negative number |
| 366 | * @return The component value. |
| 367 | */ |
| 368 | static Component |
| 369 | fromNumberWithMarker(uint8_t marker, uint64_t number); |
| 370 | |
| 371 | /** |
| 372 | * @brief Create version component using NDN naming conventions |
| 373 | * |
| 374 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 375 | */ |
| 376 | static Component |
| 377 | fromVersion(uint64_t version); |
| 378 | |
| 379 | /** |
| 380 | * @brief Create segment number component using NDN naming conventions |
| 381 | * |
| 382 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 383 | */ |
| 384 | static Component |
| 385 | fromSegment(uint64_t segmentNo); |
| 386 | |
| 387 | /** |
| 388 | * @brief Create segment offset component using NDN naming conventions |
| 389 | * |
| 390 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 391 | */ |
| 392 | static Component |
| 393 | fromSegmentOffset(uint64_t offset); |
| 394 | |
| 395 | /** |
| 396 | * @brief Create sequence number component using NDN naming conventions |
| 397 | * |
| 398 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 399 | */ |
| 400 | static Component |
| 401 | fromTimestamp(const time::system_clock::TimePoint& timePoint); |
| 402 | |
| 403 | /** |
| 404 | * @brief Create sequence number component using NDN naming conventions |
| 405 | * |
| 406 | * @see http://named-data.net/doc/tech-memos/naming-conventions.pdf |
| 407 | */ |
| 408 | static Component |
| 409 | fromSequenceNumber(uint64_t seqNo); |
| 410 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 411 | bool |
| 412 | empty() const |
| 413 | { |
| 414 | return !hasValue(); |
| 415 | } |
| 416 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 417 | Component |
| 418 | getSuccessor() const; |
| 419 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 420 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 421 | * @brief Check if this is the same component as other |
| 422 | * |
| 423 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 424 | * @return true if the components are equal, otherwise false. |
| 425 | */ |
| 426 | bool |
| 427 | equals(const Component& other) const |
| 428 | { |
| 429 | if (value_size() != other.value_size()) |
| 430 | return false; |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 431 | if (value_size() == 0 /* == other.value_size()*/) |
| 432 | return true; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 433 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 434 | // somehow, behavior is wrong on OSX 10.9 when component is empty |
| 435 | // (probably some bug in STL...) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 436 | return std::equal(value_begin(), value_end(), other.value_begin()); |
| 437 | } |
| 438 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 439 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 440 | * @brief Compare this to the other Component using NDN canonical ordering |
| 441 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 442 | * @param other The other Component to compare with. |
| 443 | * @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] | 444 | * 1 if *this comes after other in the canonical ordering. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 445 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 446 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 447 | */ |
| 448 | int |
| 449 | compare(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 450 | |
| 451 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 452 | * @brief Check if this is the same component as other |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 453 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 454 | * @param other The other Component to compare with. |
| 455 | * @return true if the components are equal, otherwise false. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 456 | */ |
| 457 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 458 | operator==(const Component& other) const |
| 459 | { |
| 460 | return equals(other); |
| 461 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 462 | |
| 463 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 464 | * @brief Check if this is not the same component as other |
| 465 | * @param other The other Component to compare with |
| 466 | * @return true if the components are not equal, otherwise false |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 467 | */ |
| 468 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 469 | operator!=(const Component& other) const |
| 470 | { |
| 471 | return !equals(other); |
| 472 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 473 | |
| 474 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 475 | * @brief Check if the *this is less than or equal to the other in NDN canonical ordering |
| 476 | * @param other The other Component to compare with |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 477 | * |
| 478 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 479 | */ |
| 480 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 481 | operator<=(const Component& other) const |
| 482 | { |
| 483 | return compare(other) <= 0; |
| 484 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 485 | |
| 486 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 487 | * @brief Check if the *this is less than the other in NDN canonical ordering |
| 488 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 489 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 490 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 491 | */ |
| 492 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 493 | operator<(const Component& other) const |
| 494 | { |
| 495 | return compare(other) < 0; |
| 496 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 497 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 498 | /** |
| 499 | * @brief Check if the *this is greater or equal than the other in NDN canonical ordering |
| 500 | * @param other The other Component to compare with |
| 501 | * |
| 502 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 503 | */ |
| 504 | bool |
| 505 | operator>=(const Component& other) const |
| 506 | { |
| 507 | return compare(other) >= 0; |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * @brief Check if the *this is greater than the other in NDN canonical ordering |
| 512 | * @param other The other Component to compare with |
| 513 | * |
| 514 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 515 | */ |
| 516 | bool |
| 517 | operator>(const Component& other) const |
| 518 | { |
| 519 | return compare(other) > 0; |
| 520 | } |
| 521 | |
| 522 | // !!! NOTE TO IMPLEMENTOR !!! |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 523 | // |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 524 | // This class MUST NOT contain any data fields. |
| 525 | // Block can be reinterpret_cast'ed as Component type. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 526 | }; |
| 527 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 528 | inline std::ostream& |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 529 | operator<<(std::ostream& os, const Component& component) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 530 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 531 | component.toUri(os); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 532 | return os; |
| 533 | } |
| 534 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 535 | template<class InputIterator> |
| 536 | inline |
| 537 | Component::Component(InputIterator begin, InputIterator end) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 538 | : Block(dataBlock(tlv::NameComponent, begin, end)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 539 | { |
| 540 | } |
| 541 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 542 | } // namespace name |
| 543 | } // namespace ndn |
| 544 | |
| 545 | #endif // NDN_NAME_COMPONENT_HPP |