27 #include "ndn-cxx/impl/name-component-types.hpp" 36 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
40 static_assert(std::is_base_of<tlv::Error, Component::Error>::value,
41 "name::Component::Error must inherit from tlv::Error");
44 Component::ensureValid()
const 47 BOOST_THROW_EXCEPTION(Error(
"TLV-TYPE " +
to_string(
type()) +
" is not a valid NameComponent"));
49 detail::getComponentTypeTable().get(
type()).check(*
this);
89 std::ostringstream oss;
91 std::string
value = oss.str();
92 if (value.find_first_not_of(
'.') == std::string::npos) {
93 if (value.size() < 3) {
94 BOOST_THROW_EXCEPTION(
Component::Error(
"Illegal URI (name component cannot be . or ..)"));
96 return Component(type, reinterpret_cast<const uint8_t*>(value.data()), value.size() - 3);
98 return Component(type, reinterpret_cast<const uint8_t*>(value.data()), value.size());
104 size_t equalPos = input.
find(
'=');
105 if (equalPos == std::string::npos) {
109 long type = std::strtol(input.data(),
nullptr, 10);
112 size_t valuePos = equalPos + 1;
114 input.size() - valuePos);
117 auto typePrefix = input.substr(0, equalPos);
118 auto ct = detail::getComponentTypeTable().findByUriPrefix(typePrefix);
120 BOOST_THROW_EXCEPTION(
Error(
"Incorrect TLV-TYPE '" + typePrefix +
"' in NameComponent URI"));
122 return ct->parseAltUriValue(input.substr(equalPos + 1));
128 detail::getComponentTypeTable().get(
type()).writeUri(os, *
this);
134 std::ostringstream os;
192 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have nonNegativeInteger value"));
201 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have the requested marker " 202 "or the value is not a nonNegativeInteger"));
204 Buffer::const_iterator valueBegin =
value_begin() + 1;
253 valueLength += estimator.prependByteArray(&marker, 1);
254 size_t totalLength = valueLength;
255 totalLength += estimator.prependVarNumber(valueLength);
259 encoder.prependNonNegativeInteger(number);
260 encoder.prependByteArray(&marker, 1);
261 encoder.prependVarNumber(valueLength);
264 return encoder.block();
288 using namespace time;
289 uint64_t
value = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count();
310 return detail::getComponentType1().match(*
this);
316 return detail::getComponentType1().create(digest);
322 return detail::getComponentType1().create(digest, digestSize);
328 return detail::getComponentType2().match(*
this);
334 return detail::getComponentType2().create(digest);
340 return detail::getComponentType2().create(digest, digestSize);
365 int cmpType =
type() - other.
type();
382 bool isOverflow =
false;
384 std::tie(isOverflow, successor) =
385 detail::getComponentTypeTable().
get(
type()).getSuccessor(*
this);
391 const std::vector<uint8_t>&
value = detail::getComponentTypeTable().get(type).getMinValue();
392 return Component(type, value.data(), value.size());
395 template<encoding::Tag TAG>
399 size_t totalLength = 0;
402 totalLength += encoder.prependVarNumber(
value_size());
403 totalLength += encoder.prependVarNumber(
type());
421 const_cast<Component&
>(*this) = buffer.block();
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
bool isGeneric() const
Check if the component is GenericComponent.
uint64_t toSegmentOffset() const
Interpret as segment offset component using NDN naming conventions.
bool isTimestamp() const
Check if the component is timestamp per NDN naming conventions.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
const Block & wireEncode() const
Encode to a wire format.
std::string toUri() const
Convert *this by escaping characters according to the NDN URI Scheme.
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Component(uint32_t type=tlv::GenericNameComponent)
Construct a NameComponent of TLV-TYPE type, using empty TLV-VALUE.
time::system_clock::TimePoint toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
bool isNumberWithMarker(uint8_t marker) const
Check if the component is NameComponentWithMarker per NDN naming conventions.
bool isSequenceNumber() const
Check if the component is sequence number per NDN naming conventions.
static Component fromTimestamp(const time::system_clock::TimePoint &timePoint)
Create sequence number component using NDN naming conventions.
Represents a TLV element of NDN packet format.
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read nonNegativeInteger in NDN-TLV encoding.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
static const uint8_t SEGMENT_OFFSET_MARKER
Segment offset marker for NDN naming conventions.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
size_t size() const
Get size of encoded wire, including Type-Length-Value.
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
a concept check for TLV abstraction with .wireEncode method
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
bool isVersion() const
Check if the component is version per NDN naming conventions.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
std::string unescape(const std::string &str)
Decode a percent-encoded string.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
uint32_t type() const
Get TLV-TYPE.
Component getSuccessor() const
Get the successor of this name component.
static const uint8_t VERSION_MARKER
Version marker for NDN naming conventions.
static Component parseUriEscapedValue(uint32_t type, const char *input, size_t len)
Represents a name component.
void wireDecode(const Block &wire)
Decode from the wire format.
static const uint8_t SEGMENT_MARKER
Segment marker for NDN naming conventions.
bool isNumber() const
Check if the component is nonNegativeInteger.
const uint8_t * wire() const
Get pointer to encoded wire.
bool hasWire() const
Check if the Block has fully encoded wire.
const uint8_t * value() const
Get pointer to TLV-VALUE.
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
std::string to_string(const V &v)
bool isSegmentOffset() const
Check if the component is segment offset per NDN naming conventions.
static const uint8_t SEQUENCE_NUMBER_MARKER
Sequence number marker for NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
bool equals(const Component &other) const
Check if this is the same component as other.
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
a concept check for TLV abstraction with .wireEncode method
uint64_t toNumber() const
Interpret this name component as nonNegativeInteger.
static const uint8_t TIMESTAMP_MARKER
Timestamp marker for NDN naming conventions.
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
bool isParametersSha256Digest() const
Check if the component is ParametersSha256DigestComponent.
EncodingImpl< EncoderTag > EncodingBuffer
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
size_t prependNonNegativeInteger(uint64_t integer) const noexcept
Prepend non-negative integer integer of NDN TLV encoding.
EncodingImpl< EstimatorTag > EncodingEstimator
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
shared_ptr< const Buffer > ConstBufferPtr