ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
name-component.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2025 Regents of the University of California.
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
22#ifndef NDN_CXX_NAME_COMPONENT_HPP
23#define NDN_CXX_NAME_COMPONENT_HPP
24
27#include "ndn-cxx/util/time.hpp"
28
29namespace ndn::name {
30
47
52enum class Convention {
53 MARKER = 1 << 0,
54 TYPED = 1 << 1,
56};
57
62enum : uint8_t {
68};
69
76getConventionEncoding() noexcept;
77
82void
84
91getConventionDecoding() noexcept;
92
98void
100
112class Component : public Block, private boost::less_than_comparable<Component>
113{
114public:
115 class Error : public Block::Error
116 {
117 public:
118 using Block::Error::Error;
119 };
120
121public: // constructors
126 explicit
127 Component(uint32_t type = tlv::GenericNameComponent);
128
133 explicit
134 Component(const Block& block);
135
144 Component(uint32_t type, ConstBufferPtr buffer);
145
154 explicit
156 : Component(tlv::GenericNameComponent, std::move(buffer))
157 {
158 }
159
163 Component(uint32_t type, span<const uint8_t> value);
164
168 explicit
169 Component(span<const uint8_t> value)
170 : Component(tlv::GenericNameComponent, value)
171 {
172 }
173
182 template<class Iterator>
183 Component(uint32_t type, Iterator first, Iterator last)
184 : Block(makeBinaryBlock(type, first, last))
185 {
186 }
187
191 template<class Iterator>
192 Component(Iterator first, Iterator last)
193 : Component(tlv::GenericNameComponent, first, last)
194 {
195 }
196
202 explicit
203 Component(std::string_view str);
204
205public: // encoding and URI
209 template<encoding::Tag TAG>
210 size_t
211 wireEncode(EncodingImpl<TAG>& encoder) const;
212
216 const Block&
217 wireEncode() const;
218
222 void
223 wireDecode(const Block& wire);
224
230 static Component
231 fromUri(std::string_view input);
232
237 void
238 toUri(std::ostream& os, UriFormat format = UriFormat::DEFAULT) const;
239
244 std::string
245 toUri(UriFormat format = UriFormat::DEFAULT) const;
246
247public: // naming conventions
252 bool
253 isNumber() const noexcept;
254
260 bool
261 isNumberWithMarker(uint8_t marker) const noexcept;
262
267 bool
268 isSegment() const noexcept;
269
274 bool
275 isByteOffset() const noexcept;
276
281 bool
282 isVersion() const noexcept;
283
288 bool
289 isTimestamp() const noexcept;
290
295 bool
296 isSequenceNumber() const noexcept;
297
303 uint64_t
304 toNumber() const;
305
317 uint64_t
318 toNumberWithMarker(uint8_t marker) const;
319
325 uint64_t
326 toSegment() const;
327
333 uint64_t
334 toByteOffset() const;
335
341 uint64_t
342 toVersion() const;
343
349 time::system_clock::time_point
350 toTimestamp() const;
351
357 uint64_t
358 toSequenceNumber() const;
359
368 static Component
369 fromNumber(uint64_t number, uint32_t type = tlv::GenericNameComponent);
370
388 static Component
389 fromNumberWithMarker(uint8_t marker, uint64_t number);
390
395 static Component
396 fromSegment(uint64_t segmentNo);
397
402 static Component
403 fromByteOffset(uint64_t offset);
404
409 static Component
410 fromVersion(uint64_t version);
411
416 static Component
417 fromTimestamp(const time::system_clock::time_point& timePoint);
418
423 static Component
424 fromSequenceNumber(uint64_t seqNo);
425
426public: // commonly used TLV-TYPEs
431 bool
432 isGeneric() const noexcept
433 {
434 return type() == tlv::GenericNameComponent;
435 }
436
442 bool
443 isImplicitSha256Digest() const noexcept;
444
450 bool
451 isParametersSha256Digest() const noexcept;
452
457 bool
458 isKeyword() const noexcept
459 {
460 return type() == tlv::KeywordNameComponent;
461 }
462
463public: // comparison
464 [[nodiscard]] bool
465 empty() const noexcept
466 {
467 return value_size() == 0;
468 }
469
480 int
481 compare(const Component& other) const;
482
509 getSuccessor() const;
510
511private:
515 void
516 ensureValid() const;
517
518private: // non-member operators
519 // NOTE: the following "hidden friend" operators are available via
520 // argument-dependent lookup only and must be defined inline.
521 // Block provides == and != operators.
522 // boost::less_than_comparable provides <=, >=, and > operators.
523
524 friend bool
525 operator<(const Component& lhs, const Component& rhs)
526 {
527 return lhs.compare(rhs) < 0;
528 }
529
530 friend std::ostream&
531 operator<<(std::ostream& os, const Component& component)
532 {
533 component.toUri(os);
534 return os;
535 }
536
537 // !!! NOTE TO IMPLEMENTOR !!!
538 //
539 // This class MUST NOT contain any non-static data members.
540 // Block can be reinterpret_cast'ed as Component type.
541};
542
544
545} // namespace ndn::name
546
547#endif // NDN_CXX_NAME_COMPONENT_HPP
Represents a TLV element of the NDN packet format.
Definition block.hpp:45
Represents a name component.
Component(ConstBufferPtr buffer)
Construct a GenericNameComponent, using TLV-VALUE from buffer.
Component(span< const uint8_t > value)
Construct a GenericNameComponent, copying the TLV-VALUE from value.
Component(Iterator first, Iterator last)
Construct a GenericNameComponent, copying the TLV-VALUE from a range.
void toUri(std::ostream &os, UriFormat format=UriFormat::DEFAULT) const
Write *this to the output stream, escaping characters according to the NDN URI format.
bool empty() const noexcept
Component(uint32_t type, Iterator first, Iterator last)
Construct a NameComponent of TLV-TYPE type, copying the TLV-VALUE from a range.
int compare(const Component &other) const
Compare this component to other using NDN canonical ordering.
#define NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
Convention
Identify a style of NDN Naming Conventions.
@ MARKER
Component markers (revision 1)
@ TYPED
Typed name components (revision 3)
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Convention getConventionEncoding() noexcept
Return which Naming Conventions style to use while encoding.
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
UriFormat
Format used for the URI representation of a name.
@ CANONICAL
Always use <type-number>=<percent-encoded-value> format.
@ DEFAULT
Use the library's default format; currently equivalent to UriFormat::ENV_OR_ALTERNATE.
@ ALTERNATE
Always prefer the alternate format when available.
@ ENV_OR_ALTERNATE
Same as UriFormat::ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to '0'.
@ ENV_OR_CANONICAL
Same as UriFormat::CANONICAL, unless NDN_NAME_ALT_URI environment variable is set to '1'.
Convention getConventionDecoding() noexcept
Return which Naming Conventions style(s) to accept while decoding.
@ GenericNameComponent
Definition tlv.hpp:72
@ KeywordNameComponent
Definition tlv.hpp:75
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition data.cpp:377
std::shared_ptr< const Buffer > ConstBufferPtr
Definition buffer.hpp:140
STL namespace.