Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 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/> |
| 24 | */ |
| 25 | |
| 26 | #include "name.hpp" |
| 27 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 28 | #include "encoding/block.hpp" |
| 29 | #include "encoding/encoding-buffer.hpp" |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 30 | #include "util/time.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 32 | #include <sstream> |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 33 | #include <boost/functional/hash.hpp> |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 34 | #include <boost/range/adaptor/reversed.hpp> |
| 35 | #include <boost/range/concepts.hpp> |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 37 | namespace ndn { |
| 38 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 39 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Name>)); |
| 40 | BOOST_CONCEPT_ASSERT((WireEncodable<Name>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 41 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<Name>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 42 | BOOST_CONCEPT_ASSERT((WireDecodable<Name>)); |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 43 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::iterator>)); |
| 44 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_iterator>)); |
| 45 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::reverse_iterator>)); |
| 46 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Name::const_reverse_iterator>)); |
| 47 | BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<Name>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 48 | static_assert(std::is_base_of<tlv::Error, Name::Error>::value, |
| 49 | "Name::Error must inherit from tlv::Error"); |
| 50 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 51 | const size_t Name::npos = std::numeric_limits<size_t>::max(); |
| 52 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 53 | // ---- constructors, encoding, decoding ---- |
| 54 | |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 55 | Name::Name() |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 56 | : m_wire(tlv::Name) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
| 60 | Name::Name(const Block& wire) |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 61 | : m_wire(wire) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 62 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 63 | m_wire.parse(); |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | Name::Name(const char* uri) |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 67 | : Name(std::string(uri)) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 68 | { |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 71 | Name::Name(std::string uri) |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 72 | { |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 73 | if (uri.empty()) |
| 74 | return; |
| 75 | |
| 76 | size_t iColon = uri.find(':'); |
| 77 | if (iColon != std::string::npos) { |
| 78 | // Make sure the colon came before a '/'. |
| 79 | size_t iFirstSlash = uri.find('/'); |
| 80 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
| 81 | // Omit the leading protocol such as ndn: |
| 82 | uri.erase(0, iColon + 1); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | // Trim the leading slash and possibly the authority. |
| 87 | if (uri[0] == '/') { |
| 88 | if (uri.size() >= 2 && uri[1] == '/') { |
| 89 | // Strip the authority following "//". |
| 90 | size_t iAfterAuthority = uri.find('/', 2); |
| 91 | if (iAfterAuthority == std::string::npos) |
| 92 | // Unusual case: there was only an authority. |
| 93 | return; |
| 94 | else { |
| 95 | uri.erase(0, iAfterAuthority + 1); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | else { |
| 99 | uri.erase(0, 1); |
Junxiao Shi | 3188c403 | 2016-07-18 20:53:56 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | size_t iComponentStart = 0; |
| 104 | |
| 105 | // Unescape the components. |
| 106 | while (iComponentStart < uri.size()) { |
| 107 | size_t iComponentEnd = uri.find("/", iComponentStart); |
| 108 | if (iComponentEnd == std::string::npos) |
| 109 | iComponentEnd = uri.size(); |
| 110 | |
| 111 | append(Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd)); |
| 112 | iComponentStart = iComponentEnd + 1; |
| 113 | } |
Alexander Afanasyev | c89efb4 | 2015-02-10 18:26:42 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 116 | std::string |
| 117 | Name::toUri() const |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 118 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 119 | std::ostringstream os; |
| 120 | os << *this; |
| 121 | return os.str(); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 124 | template<encoding::Tag TAG> |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 125 | size_t |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 126 | Name::wireEncode(EncodingImpl<TAG>& encoder) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 127 | { |
| 128 | size_t totalLength = 0; |
Junxiao Shi | adc334e | 2017-07-14 20:28:28 +0000 | [diff] [blame] | 129 | for (const Component& comp : *this | boost::adaptors::reversed) { |
| 130 | totalLength += comp.wireEncode(encoder); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 131 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 132 | |
| 133 | totalLength += encoder.prependVarNumber(totalLength); |
| 134 | totalLength += encoder.prependVarNumber(tlv::Name); |
| 135 | return totalLength; |
| 136 | } |
| 137 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 138 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Name); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 139 | |
| 140 | const Block& |
| 141 | Name::wireEncode() const |
| 142 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 143 | if (m_wire.hasWire()) |
| 144 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 145 | |
| 146 | EncodingEstimator estimator; |
| 147 | size_t estimatedSize = wireEncode(estimator); |
| 148 | |
| 149 | EncodingBuffer buffer(estimatedSize, 0); |
| 150 | wireEncode(buffer); |
| 151 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 152 | m_wire = buffer.block(); |
| 153 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 154 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 155 | return m_wire; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void |
| 159 | Name::wireDecode(const Block& wire) |
| 160 | { |
| 161 | if (wire.type() != tlv::Name) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 162 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 164 | m_wire = wire; |
| 165 | m_wire.parse(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 168 | Name |
| 169 | Name::deepCopy() const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 170 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 171 | Name copiedName(*this); |
| 172 | copiedName.m_wire.resetWire(); |
| 173 | copiedName.wireEncode(); // "compress" the underlying buffer |
| 174 | return copiedName; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 177 | // ---- accessors ---- |
| 178 | |
| 179 | const name::Component& |
| 180 | Name::at(ssize_t i) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 181 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 182 | if (i < 0) { |
| 183 | i = size() + i; |
| 184 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 185 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 186 | if (i < 0 || static_cast<size_t>(i) >= size()) { |
| 187 | BOOST_THROW_EXCEPTION(Error("Requested component does not exist (out of bounds)")); |
| 188 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 189 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 190 | return reinterpret_cast<const Component&>(m_wire.elements()[i]); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 191 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 192 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 193 | PartialName |
| 194 | Name::getSubName(ssize_t iStartComponent, size_t nComponents) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 195 | { |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 196 | PartialName result; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 197 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 198 | ssize_t iStart = iStartComponent < 0 ? this->size() + iStartComponent : iStartComponent; |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 199 | size_t iEnd = this->size(); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 200 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 201 | iStart = std::max(iStart, static_cast<ssize_t>(0)); |
| 202 | |
| 203 | if (nComponents != npos) |
| 204 | iEnd = std::min(this->size(), iStart + nComponents); |
| 205 | |
| 206 | for (size_t i = iStart; i < iEnd; ++i) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 207 | result.append(at(i)); |
| 208 | |
| 209 | return result; |
| 210 | } |
| 211 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 212 | // ---- modifiers ---- |
| 213 | |
| 214 | Name& |
| 215 | Name::appendVersion() |
| 216 | { |
| 217 | return appendVersion(time::toUnixTimestamp(time::system_clock::now()).count()); |
| 218 | } |
| 219 | |
| 220 | Name& |
| 221 | Name::appendTimestamp() |
| 222 | { |
| 223 | return appendTimestamp(time::system_clock::now()); |
| 224 | } |
| 225 | |
| 226 | Name& |
| 227 | Name::append(const PartialName& name) |
| 228 | { |
| 229 | if (&name == this) |
| 230 | // Copying from this name, so need to make a copy first. |
| 231 | return append(PartialName(name)); |
| 232 | |
| 233 | for (size_t i = 0; i < name.size(); ++i) |
| 234 | append(name.at(i)); |
| 235 | |
| 236 | return *this; |
| 237 | } |
| 238 | |
| 239 | // ---- algorithms ---- |
| 240 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 241 | Name |
| 242 | Name::getSuccessor() const |
| 243 | { |
| 244 | if (empty()) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 245 | static uint8_t firstValue[] {0}; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 246 | Name firstName; |
| 247 | firstName.append(firstValue, 1); |
| 248 | return firstName; |
| 249 | } |
| 250 | |
| 251 | return getPrefix(-1).append(get(-1).getSuccessor()); |
| 252 | } |
| 253 | |
| 254 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 255 | Name::isPrefixOf(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 256 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 257 | // This name is longer than the name we are checking against. |
| 258 | if (size() > other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 259 | return false; |
| 260 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 261 | // Check if at least one of given components doesn't match. |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 262 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 263 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | bool |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 271 | Name::equals(const Name& other) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 272 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 273 | if (size() != other.size()) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 274 | return false; |
| 275 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 276 | for (size_t i = 0; i < size(); ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 277 | if (get(i) != other.get(i)) |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 284 | int |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 285 | Name::compare(size_t pos1, size_t count1, const Name& other, size_t pos2, size_t count2) const |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 286 | { |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 287 | count1 = std::min(count1, this->size() - pos1); |
| 288 | count2 = std::min(count2, other.size() - pos2); |
| 289 | size_t count = std::min(count1, count2); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 290 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 291 | for (size_t i = 0; i < count; ++i) { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 292 | int comp = get(pos1 + i).compare(other.get(pos2 + i)); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 293 | if (comp != 0) { // i-th component differs |
| 294 | return comp; |
| 295 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 296 | } |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 297 | // [pos1, pos1+count) of this Name equals [pos2, pos2+count) of other Name |
Joao Pereira | aa8fd16 | 2015-06-05 16:35:15 -0400 | [diff] [blame] | 298 | return count1 - count2; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 301 | // ---- stream operators ---- |
| 302 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 303 | std::ostream& |
| 304 | operator<<(std::ostream& os, const Name& name) |
| 305 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 306 | if (name.empty()) { |
| 307 | os << "/"; |
| 308 | } |
| 309 | else { |
| 310 | for (const auto& component : name) { |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 311 | os << "/"; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 312 | component.toUri(os); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 313 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 314 | } |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 315 | return os; |
| 316 | } |
| 317 | |
| 318 | std::istream& |
| 319 | operator>>(std::istream& is, Name& name) |
| 320 | { |
| 321 | std::string inputString; |
| 322 | is >> inputString; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 323 | name = Name(inputString); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 324 | |
| 325 | return is; |
| 326 | } |
| 327 | |
| 328 | } // namespace ndn |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 329 | |
| 330 | namespace std { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 331 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 332 | size_t |
| 333 | hash<ndn::Name>::operator()(const ndn::Name& name) const |
| 334 | { |
| 335 | return boost::hash_range(name.wireEncode().wire(), |
| 336 | name.wireEncode().wire() + name.wireEncode().size()); |
| 337 | } |
| 338 | |
| 339 | } // namespace std |