1 #ifndef NDNPH_PACKET_NAME_HPP
2 #define NDNPH_PACKET_NAME_HPP
4 #include "../core/input-iterator-pointer-proxy.hpp"
24 static_assert(std::is_base_of<std::forward_iterator_tag,
25 typename std::iterator_traits<It>::iterator_category>::
value,
27 static_assert(std::is_same<uint8_t,
typename std::iterator_traits<It>::value_type>::
value,
"");
28 size_t length = std::distance(first, last);
30 if (
value !=
nullptr) {
31 std::copy(first, last,
value);
50 size_t uriLen = std::strlen(uri);
51 size_t bufLen = 2 * uriLen;
52 uint8_t* buf = region.
alloc(bufLen);
58 ssize_t
length = parseUri(buf, bufLen, uri, uriLen, nComps);
60 region.
free(buf, bufLen);
73 explicit operator bool()
const {
112 return pointer(this->
operator*());
129 if (isOutOfRange(i)) {
149 if (isOutOfRange(first) || isOutOfRange(last,
true) || first >= last) {
154 std::advance(it, first);
155 auto firstComp = *it;
156 std::advance(it, last - first - 1);
158 return Name(firstComp.tlv(), lastComp.tlv() + lastComp.size() - firstComp.tlv(), last - first);
182 template<
typename... C>
185 size_t nComps = prependComps(encoder, comps...);
187 if (room !=
nullptr) {
188 std::copy_n(m_value, m_length, room);
195 return Name(encoder.
begin(), encoder.
size(), m_nComps + nComps);
218 size_t commonLength = std::min(m_length, other.m_length);
219 if (commonLength > 0) {
220 int commonCmp = std::memcmp(m_value, other.m_value, commonLength);
229 if (m_length > commonLength) {
232 if (other.m_length > commonLength) {
252 #ifdef NDNPH_PRINT_ARDUINO
257 count += p.print(
'/');
259 for (
const auto& comp : *
this) {
260 count += p.print(
'/');
262 count += comp.printTo(p);
273 , m_nComps(nComps) {}
275 bool decodeValue(
const uint8_t*
value,
size_t length) {
277 if (decodeComps(
length)) {
281 m_length = m_nComps = 0;
285 bool decodeComps(
size_t length) {
286 Decoder decoder(m_value,
length);
287 auto it = decoder.begin(),
end = decoder.end();
288 for (; it !=
end; ++it) {
290 if (!it->decode(comp)) {
293 m_length += it->size;
296 return !it.hasError();
299 bool isOutOfRange(
int i,
bool acceptPastEnd =
false)
const {
301 (acceptPastEnd ? i >
static_cast<int>(m_nComps) : i >=
static_cast<int>(m_nComps));
304 static ssize_t parseUri(uint8_t* buf,
size_t bufLen,
const char* uri,
size_t uriLen,
310 const char* uriEnd = uri + uriLen;
312 while (uri < uriEnd) {
314 const char* compEnd = std::strchr(uri,
'/');
315 if (compEnd ==
nullptr) {
330 template<
typename... C>
331 static size_t prependComps(Encoder& encoder,
const Component& comp,
const C&... rest) {
332 size_t nComps = prependComps(encoder, rest...);
333 encoder.prepend(comp);
337 template<
typename Convention,
typename Arg,
typename... C>
338 static typename std::enable_if<!std::is_base_of<Component, Convention>::value,
size_t>::type
339 prependComps(Encoder& encoder,
const Convention&,
const Arg& arg,
const C&... rest) {
340 size_t nComps = prependComps(encoder, rest...);
342 size_t headroom = encoder.availableHeadroom();
343 Region overlay(
const_cast<uint8_t*
>(encoder.begin() - headroom), headroom);
344 auto comp = Convention::create(overlay, arg);
346 uint8_t* room = encoder.prependRoom(comp.size());
347 if (!comp || room ==
nullptr) {
351 std::memmove(room, comp.tlv(), comp.size());
355 static size_t prependComps(Encoder&) {
360 const uint8_t* m_value =
nullptr;
379 #ifdef NDNPH_PRINT_OSTREAM
382 operator<<(std::ostream& os,
const Name& name) {
383 if (name.size() == 0) {
386 for (
const auto& comp : name) {
Name component.
Definition: component.hpp:16
static Component parse(Region ®ion, const char *uri)
Parse from URI.
Definition: component.hpp:97
bool decodeFrom(const Decoder::Tlv &d)
Definition: component.hpp:169
Iterator over TLV elements.
Definition: decoder.hpp:69
Decoded TLV.
Definition: decoder.hpp:13
size_t length
Definition: decoder.hpp:39
const uint8_t * value
Definition: decoder.hpp:40
TLV decoder.
Definition: decoder.hpp:10
TLV encoder that accepts items in reverse order.
Definition: encoder.hpp:10
const uint8_t * begin() const
Get output begin.
Definition: encoder.hpp:34
void trim() const
Release unused space to the Region.
Definition: encoder.hpp:58
void discard()
Release all space to the Region.
Definition: encoder.hpp:72
uint8_t * prependRoom(size_t size)
Make room to prepend an object.
Definition: encoder.hpp:90
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition: encoder.hpp:143
size_t size() const
Get output size.
Definition: encoder.hpp:44
Iterator over name components.
Definition: name.hpp:91
reference operator*()
Definition: name.hpp:105
const Component value_type
Definition: name.hpp:95
Iterator(const super &inner)
Definition: name.hpp:102
std::ptrdiff_t difference_type
Definition: name.hpp:96
std::input_iterator_tag iterator_category
Definition: name.hpp:94
pointer operator->()
Definition: name.hpp:111
detail::InputIteratorPointerProxy< value_type > pointer
Definition: name.hpp:97
Name.
Definition: name.hpp:14
static Name parse(Region ®ion, const char *uri)
Parse from URI.
Definition: name.hpp:49
Name(Region ®ion, std::initializer_list< uint8_t > value)
Construct from TLV-VALUE.
Definition: name.hpp:37
size_t length() const
Definition: name.hpp:77
Iterator end() const
Definition: name.hpp:120
Component operator[](int i) const
Access i-th component.
Definition: name.hpp:125
size_t size() const
Get number of components.
Definition: name.hpp:86
CompareResult
Name compare result.
Definition: name.hpp:208
@ CMP_EQUAL
lhs and rhs are equal
Definition: name.hpp:211
@ CMP_RPREFIX
rhs is a prefix of lhs
Definition: name.hpp:212
@ CMP_LT
lhs is less than, but not a prefix of rhs
Definition: name.hpp:209
@ CMP_GT
rhs is less than, but not a prefix of lhs
Definition: name.hpp:213
@ CMP_LPREFIX
lhs is a prefix of rhs
Definition: name.hpp:210
bool decodeFrom(const Decoder::Tlv &d)
Definition: name.hpp:248
Name append(Region ®ion, const C &... comps) const
Append a sequence of components.
Definition: name.hpp:183
Name clone(Region ®ion) const
Clone TLV-VALUE into given region.
Definition: name.hpp:203
Name getPrefix(int n=0) const
Get prefix of n components.
Definition: name.hpp:165
Name slice(int first=0, int last=0) const
Get sub name [first, last).
Definition: name.hpp:142
Name(const uint8_t *value=nullptr, size_t length=0)
Construct referencing TLV-VALUE.
Definition: name.hpp:17
Name(Region ®ion, It first, It last)
Construct from TLV-VALUE.
Definition: name.hpp:23
size_t printTo(::Print &p) const final
Print name as URI.
Definition: name.hpp:254
Iterator begin() const
Definition: name.hpp:116
const uint8_t * value() const
Definition: name.hpp:81
void encodeTo(Encoder &encoder) const
Definition: name.hpp:244
bool isPrefixOf(const Name &other) const
Determine if this name is a prefix of other.
Definition: name.hpp:239
CompareResult compare(const Name &other) const
Compare with other name.
Definition: name.hpp:217
Definition: printing.hpp:22
Region-based memory allocator thats owns memory of NDNph objects.
Definition: region.hpp:9
uint8_t * alloc(size_t size)
Allocate a buffer with no alignment requirement.
Definition: region.hpp:27
bool free(const uint8_t *first, const uint8_t *last)
Deallocate (part of) last allocated buffer.
Definition: region.hpp:51
A sequence of bytes, usually TLV-VALUE.
Definition: value.hpp:11
@ Name
Definition: an.hpp:19
bool operator<(const Name &lhs, const Name &rhs)
Definition: name.hpp:371
bool operator==(const ValidityPeriod &lhs, const ValidityPeriod &rhs)
Definition: validity-period.hpp:156
#define NDNPH_DECLARE_GT_LE_GE(T, specifier)
Declare operator>, operator<=, operator>= in terms of operator<.
Definition: operators.hpp:11
#define NDNPH_DECLARE_NE(T, specifier)
Declare operator!= in terms of operator==.
Definition: operators.hpp:7