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 | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 31 | #include "encoding/block-helpers.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 32 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 33 | #include "util/string-helper.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace name { |
| 37 | |
| 38 | /** |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 39 | * @brief Component holds a read-only name component value. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 40 | */ |
| 41 | class Component : public Block |
| 42 | { |
| 43 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 45 | * @brief Error that can be thrown from name::Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 47 | class Error : public Block::Error |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& what) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 52 | : Block::Error(what) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | { |
| 54 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 57 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 58 | * Create a new name::Component with an empty value |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 59 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 60 | Component(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 62 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 63 | * @brief Create name::Component from a wire block |
| 64 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 65 | * @param wire tlv::NameComponent Block from which to create name::Component |
| 66 | * @throws Error if wire.type() is not tlv::NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 67 | * |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 68 | * Any block can be implicitly converted to name::Component |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 69 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 70 | Component(const Block& wire); |
| 71 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 72 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 73 | * @brief Create a new name::Component from the buffer pointer (buffer pointer will be copied) |
| 74 | * |
| 75 | * @param buffer A pointer to an immutable buffer |
| 76 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 77 | * 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] | 78 | * Note that this method **will not** allocate new memory for and copy the payload until |
| 79 | * toWire() method is called. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 82 | Component(const ConstBufferPtr& buffer); |
| 83 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 84 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 85 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 86 | * @param buffer A reference to the buffer |
| 87 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 88 | * 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] | 89 | * 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] | 90 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | explicit |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 92 | Component(const Buffer& buffer); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 95 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 96 | * @param buffer A pointer to the first byte of the buffer |
| 97 | * @param bufferSize Size of the buffer |
| 98 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 99 | * 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] | 100 | * 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] | 101 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 102 | Component(const uint8_t* buffer, size_t bufferSize); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 106 | * @param begin Iterator pointing to the beginning of the buffer |
| 107 | * @param end Iterator pointing to the ending of the buffer |
| 108 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 109 | * 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] | 110 | * Note that this method **will** allocate new memory for and copy the payload. |
| 111 | */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 112 | template<class InputIterator> |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 113 | Component(InputIterator begin, InputIterator end); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 115 | /** |
| 116 | * @brief Create a new name::Component from the C string (data from string will be copied) |
| 117 | * |
| 118 | * @param str Zero-ended string. Note that this string will be interpreted as is (i.e., |
| 119 | * it will not be interpreted as URI) |
| 120 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 121 | * 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] | 122 | * Note that this method **will** allocate new memory for and copy the payload. |
| 123 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 124 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | Component(const char* str); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 127 | /** |
| 128 | * @brief Create a new name::Component from the STL string (data from string will be copied) |
| 129 | * |
| 130 | * @param str Const reference to STL string. Note that this string will be interpreted |
| 131 | * as is (i.e., it will not be interpreted as URI) |
| 132 | * |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 133 | * 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] | 134 | * Note that this method **will** allocate new memory for and copy the payload. |
| 135 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 136 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 137 | Component(const std::string& str); |
| 138 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 139 | /** |
| 140 | * @brief Fast encoding or block size estimation |
| 141 | */ |
| 142 | template<bool T> |
| 143 | size_t |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 144 | wireEncode(EncodingImpl<T>& block) const; |
| 145 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 146 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | * @brief Encode to a wire format |
| 148 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 149 | const Block& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | wireEncode() const; |
| 151 | |
| 152 | /** |
| 153 | * @brief Decode from the wire format |
| 154 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 155 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 156 | wireDecode(const Block& wire); |
| 157 | |
| 158 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 159 | * @brief Create name::Component by decoding the escapedString between beginOffset and |
| 160 | * endOffset according to the NDN URI Scheme. |
| 161 | * |
| 162 | * If the escaped string is "", "." or ".." then return an empty name::Component. Note |
| 163 | * that an empty name::Component should not be added to Name and if attempted, an |
| 164 | * exception will be thrown. |
| 165 | * |
| 166 | * @param escapedString String containing NDN URI-encoded name |
| 167 | * component. [escapedString+beginOffset, beginOffset+endOffset) |
| 168 | * must be a valid memory buffer. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 169 | * @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] | 170 | * @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] | 171 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 172 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 173 | fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 176 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 177 | * |
| 178 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 179 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 180 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 181 | fromEscapedString(const char* escapedString) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 182 | { |
| 183 | return fromEscapedString(escapedString, 0, ::strlen(escapedString)); |
| 184 | } |
| 185 | |
| 186 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 187 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 188 | * |
| 189 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 190 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 191 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 192 | fromEscapedString(const std::string& escapedString) |
| 193 | { |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 194 | return fromEscapedString(escapedString.c_str(), 0, escapedString.size()); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 198 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 199 | * |
| 200 | * @deprecated Use toUri(std::ostream&) instead |
| 201 | * |
| 202 | * This also adds "..." to a value with zero or more "." |
| 203 | * |
| 204 | * @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] | 205 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 206 | DEPRECATED( |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 207 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 208 | toEscapedString(std::ostream& os) const) |
| 209 | { |
| 210 | return toUri(os); |
| 211 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 212 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 213 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 214 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 215 | * |
| 216 | * @deprecated Use toUri() instead |
| 217 | * |
| 218 | * This also adds "..." to a value with zero or more "." |
| 219 | * |
| 220 | * @return The escaped string |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 221 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 222 | DEPRECATED( |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 223 | std::string |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 224 | toEscapedString() const) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 226 | return toUri(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 227 | } |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 229 | /** |
| 230 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 231 | * |
| 232 | * This also adds "..." to a value with zero or more "." |
| 233 | * |
| 234 | * @param os The output stream to where write the URI escaped version *this |
| 235 | */ |
| 236 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 237 | toUri(std::ostream& os) const; |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 238 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 239 | /** |
| 240 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 241 | * |
| 242 | * This also adds "..." to a value with zero or more "." |
| 243 | * |
| 244 | * @return The escaped string |
| 245 | */ |
| 246 | std::string |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 247 | toUri() const |
| 248 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 249 | std::ostringstream os; |
| 250 | toUri(os); |
| 251 | return os.str(); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 252 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 253 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 254 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 255 | * @brief Interpret this name component as nonNegativeInteger |
| 256 | * |
| 257 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 258 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 259 | * @return The integer number. |
| 260 | */ |
| 261 | uint64_t |
| 262 | toNumber() const; |
| 263 | |
| 264 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 265 | * @brief An alias for toNumber() |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 266 | */ |
| 267 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 268 | toVersion() const; |
| 269 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 270 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 271 | * @brief An alias for toNumber() |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 272 | */ |
| 273 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 274 | toSegment() const; |
| 275 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 276 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 277 | * @brief Create a component encoded as nonNegativeInteger |
| 278 | * |
| 279 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 280 | * |
| 281 | * @param number The non-negative number |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 282 | * @return The component value. |
| 283 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 284 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 285 | fromNumber(uint64_t number); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 286 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 287 | bool |
| 288 | empty() const |
| 289 | { |
| 290 | return !hasValue(); |
| 291 | } |
| 292 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 293 | Component |
| 294 | getSuccessor() const; |
| 295 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 296 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 297 | * @brief Check if this is the same component as other |
| 298 | * |
| 299 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 300 | * @return true if the components are equal, otherwise false. |
| 301 | */ |
| 302 | bool |
| 303 | equals(const Component& other) const |
| 304 | { |
| 305 | if (value_size() != other.value_size()) |
| 306 | return false; |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 307 | if (value_size() == 0 /* == other.value_size()*/) |
| 308 | return true; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 309 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 310 | // somehow, behavior is wrong on OSX 10.9 when component is empty |
| 311 | // (probably some bug in STL...) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 312 | return std::equal(value_begin(), value_end(), other.value_begin()); |
| 313 | } |
| 314 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 315 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 316 | * @brief Compare this to the other Component using NDN canonical ordering |
| 317 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 318 | * @param other The other Component to compare with. |
| 319 | * @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] | 320 | * 1 if *this comes after other in the canonical ordering. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 321 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 322 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 323 | */ |
| 324 | int |
| 325 | compare(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 326 | |
| 327 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 328 | * @brief Check if this is the same component as other |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 329 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 330 | * @param other The other Component to compare with. |
| 331 | * @return true if the components are equal, otherwise false. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 332 | */ |
| 333 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 334 | operator==(const Component& other) const |
| 335 | { |
| 336 | return equals(other); |
| 337 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 338 | |
| 339 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 340 | * @brief Check if this is not the same component as other |
| 341 | * @param other The other Component to compare with |
| 342 | * @return true if the components are not equal, otherwise false |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 343 | */ |
| 344 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 345 | operator!=(const Component& other) const |
| 346 | { |
| 347 | return !equals(other); |
| 348 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 349 | |
| 350 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 351 | * @brief Check if the *this is less than or equal to the other in NDN canonical ordering |
| 352 | * @param other The other Component to compare with |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 353 | * |
| 354 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 355 | */ |
| 356 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 357 | operator<=(const Component& other) const |
| 358 | { |
| 359 | return compare(other) <= 0; |
| 360 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 361 | |
| 362 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 363 | * @brief Check if the *this is less than the other in NDN canonical ordering |
| 364 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 365 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 366 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 367 | */ |
| 368 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 369 | operator<(const Component& other) const |
| 370 | { |
| 371 | return compare(other) < 0; |
| 372 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 373 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 374 | /** |
| 375 | * @brief Check if the *this is greater or equal than the other in NDN canonical ordering |
| 376 | * @param other The other Component to compare with |
| 377 | * |
| 378 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 379 | */ |
| 380 | bool |
| 381 | operator>=(const Component& other) const |
| 382 | { |
| 383 | return compare(other) >= 0; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * @brief Check if the *this is greater than the other in NDN canonical ordering |
| 388 | * @param other The other Component to compare with |
| 389 | * |
| 390 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 391 | */ |
| 392 | bool |
| 393 | operator>(const Component& other) const |
| 394 | { |
| 395 | return compare(other) > 0; |
| 396 | } |
| 397 | |
| 398 | // !!! NOTE TO IMPLEMENTOR !!! |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 399 | // |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 400 | // This class MUST NOT contain any data fields. |
| 401 | // Block can be reinterpret_cast'ed as Component type. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 402 | }; |
| 403 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 404 | inline std::ostream& |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 405 | operator<<(std::ostream& os, const Component& component) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 406 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 407 | component.toUri(os); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 408 | return os; |
| 409 | } |
| 410 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 411 | inline |
| 412 | Component::Component() |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 413 | : Block(tlv::NameComponent) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 414 | { |
| 415 | } |
| 416 | |
| 417 | inline |
| 418 | Component::Component(const Block& wire) |
| 419 | : Block(wire) |
| 420 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 421 | if (type() != tlv::NameComponent) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 422 | throw Error("Constructing name component from non name component TLV wire block"); |
| 423 | } |
| 424 | |
| 425 | inline |
| 426 | Component::Component(const ConstBufferPtr& buffer) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 427 | : Block(tlv::NameComponent, buffer) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 428 | { |
| 429 | } |
| 430 | |
| 431 | inline |
| 432 | Component::Component(const Buffer& value) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 433 | : Block(dataBlock(tlv::NameComponent, value.buf(), value.size())) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 434 | { |
| 435 | } |
| 436 | |
| 437 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 438 | Component::Component(const uint8_t* value, size_t valueLen) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 439 | : Block(dataBlock(tlv::NameComponent, value, valueLen)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 440 | { |
| 441 | } |
| 442 | |
| 443 | template<class InputIterator> |
| 444 | inline |
| 445 | Component::Component(InputIterator begin, InputIterator end) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 446 | : Block(dataBlock(tlv::NameComponent, begin, end)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 447 | { |
| 448 | } |
| 449 | |
| 450 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 451 | Component::Component(const char* str) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 452 | : Block(dataBlock(tlv::NameComponent, str, ::strlen(str))) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 453 | { |
| 454 | } |
| 455 | |
| 456 | inline |
| 457 | Component::Component(const std::string& str) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 458 | : Block(dataBlock(tlv::NameComponent, str.c_str(), str.size())) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 459 | { |
| 460 | } |
| 461 | |
| 462 | |
| 463 | inline Component |
| 464 | Component::fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset) |
| 465 | { |
| 466 | std::string trimmedString(escapedString + beginOffset, escapedString + endOffset); |
| 467 | trim(trimmedString); |
| 468 | std::string value = unescape(trimmedString); |
| 469 | |
| 470 | if (value.find_first_not_of(".") == std::string::npos) { |
| 471 | // Special case for component of only periods. |
| 472 | if (value.size() <= 2) |
| 473 | // Zero, one or two periods is illegal. Ignore this component. |
| 474 | return Component(); |
| 475 | else |
| 476 | // Remove 3 periods. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 477 | return Component(reinterpret_cast<const uint8_t*>(&value[3]), value.size() - 3); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 478 | } |
| 479 | else |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 480 | return Component(reinterpret_cast<const uint8_t*>(&value[0]), value.size()); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | inline void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 485 | Component::toUri(std::ostream& result) const |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 486 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 487 | const uint8_t* valuePtr = value(); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 488 | size_t valueSize = value_size(); |
| 489 | |
| 490 | bool gotNonDot = false; |
| 491 | for (unsigned i = 0; i < valueSize; ++i) { |
| 492 | if (valuePtr[i] != 0x2e) { |
| 493 | gotNonDot = true; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | if (!gotNonDot) { |
| 498 | // Special case for component of zero or more periods. Add 3 periods. |
| 499 | result << "..."; |
| 500 | for (size_t i = 0; i < valueSize; ++i) |
| 501 | result << '.'; |
| 502 | } |
| 503 | else { |
| 504 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 505 | std::ios::fmtflags saveFlags = result.flags(std::ios::hex | std::ios::uppercase); |
| 506 | |
| 507 | for (size_t i = 0; i < valueSize; ++i) { |
| 508 | uint8_t x = valuePtr[i]; |
| 509 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 510 | if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) || |
| 511 | (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d || |
| 512 | x == 0x2e || x == 0x5f) |
| 513 | result << x; |
| 514 | else { |
| 515 | result << '%'; |
| 516 | if (x < 16) |
| 517 | result << '0'; |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 518 | result << static_cast<unsigned int>(x); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | // Restore. |
| 523 | result.flags(saveFlags); |
| 524 | } |
| 525 | } |
| 526 | |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 527 | |
| 528 | inline Component |
| 529 | Component::fromNumber(uint64_t number) |
| 530 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 531 | /// \todo Change to tlv::NumberComponent |
| 532 | return nonNegativeIntegerBlock(tlv::NameComponent, number); |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
| 536 | inline uint64_t |
| 537 | Component::toNumber() const |
| 538 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 539 | /// \todo Check if Component is of tlv::NumberComponent type |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 540 | return readNonNegativeInteger(static_cast<const Block&>(*this)); |
| 541 | } |
| 542 | |
| 543 | |
| 544 | inline uint64_t |
| 545 | Component::toVersion() const |
| 546 | { |
| 547 | return toNumber(); |
| 548 | } |
| 549 | |
| 550 | inline uint64_t |
| 551 | Component::toSegment() const |
| 552 | { |
| 553 | return toNumber(); |
| 554 | } |
| 555 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 556 | inline int |
| 557 | Component::compare(const Component& other) const |
| 558 | { |
| 559 | // Imitate ndn_Exclude_compareComponents. |
| 560 | if (value_size() < other.value_size()) |
| 561 | return -1; |
| 562 | if (value_size() > other.value_size()) |
| 563 | return 1; |
| 564 | |
| 565 | if (value_size() == 0) |
| 566 | return 0; |
| 567 | |
| 568 | // The components are equal length. Just do a byte compare. |
| 569 | return std::memcmp(value(), other.value(), value_size()); |
| 570 | } |
| 571 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 572 | inline Component |
| 573 | Component::getSuccessor() const |
| 574 | { |
| 575 | size_t totalLength = 0; |
| 576 | EncodingBuffer encoder(size() + 1, 1); // + 1 in case there is an overflow |
| 577 | // in unlikely case TLV length changes more, |
| 578 | // EncodingBuffer will take care of that |
| 579 | |
| 580 | bool isOverflow = true; |
| 581 | size_t i = value_size(); |
| 582 | for (; isOverflow && i > 0; i--) { |
| 583 | uint8_t newValue = static_cast<uint8_t>((value()[i - 1] + 1) & 0xFF); |
| 584 | totalLength += encoder.prependByte(newValue); |
| 585 | isOverflow = (newValue == 0); |
| 586 | } |
| 587 | totalLength += encoder.prependByteArray(value(), i); |
| 588 | |
| 589 | if (isOverflow) { |
| 590 | // new name components has to be extended |
| 591 | totalLength += encoder.appendByte(0); |
| 592 | } |
| 593 | |
| 594 | totalLength += encoder.prependVarNumber(totalLength); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 595 | totalLength += encoder.prependVarNumber(tlv::NameComponent); |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 596 | |
| 597 | return encoder.block(); |
| 598 | } |
| 599 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 600 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 601 | template<bool T> |
| 602 | inline size_t |
| 603 | Component::wireEncode(EncodingImpl<T>& block) const |
| 604 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 605 | size_t totalLength = 0; |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 606 | if (value_size() > 0) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 607 | totalLength += block.prependByteArray(value(), value_size()); |
| 608 | totalLength += block.prependVarNumber(value_size()); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 609 | totalLength += block.prependVarNumber(tlv::NameComponent); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 610 | return totalLength; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 613 | inline const Block& |
| 614 | Component::wireEncode() const |
| 615 | { |
| 616 | if (this->hasWire()) |
| 617 | return *this; |
| 618 | |
| 619 | EncodingEstimator estimator; |
| 620 | size_t estimatedSize = wireEncode(estimator); |
| 621 | |
| 622 | EncodingBuffer buffer(estimatedSize, 0); |
| 623 | wireEncode(buffer); |
| 624 | |
| 625 | const_cast<Component&>(*this) = buffer.block(); |
| 626 | return *this; |
| 627 | } |
| 628 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 629 | inline void |
| 630 | Component::wireDecode(const Block& wire) |
| 631 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 632 | if (wire.type() != tlv::NameComponent) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 633 | throw Error("wireDecode name component from non name component TLV wire block"); |
| 634 | |
| 635 | *this = wire; |
| 636 | } |
| 637 | |
| 638 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 639 | } // namespace name |
| 640 | } // namespace ndn |
| 641 | |
| 642 | #endif // NDN_NAME_COMPONENT_HPP |