ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
name.hpp
Go to the documentation of this file.
1
11#pragma once
12
13#include "ndn/common.hpp"
14
15namespace ndn {
16
27 const uint8_t* value;
28 size_t size;
29
34 std::string_view asString() const {
35 return std::string_view(reinterpret_cast<const char*>(value), size);
36 }
37};
38
64class Name {
65public:
71 Name() = default;
72
88 static Result<Name> fromUri(std::string_view uri);
89
98 static Result<Name> fromWire(const uint8_t* buf, size_t len, size_t* bytesRead = nullptr);
112 size_t toUri(char* buf, size_t bufSize) const;
113
122 Error encode(uint8_t* buf, size_t bufSize, size_t& encodedLen) const;
133 size_t componentCount() const { return numComponents_; }
134
142 NameComponent component(size_t index) const;
143
152 Error appendComponent(std::string_view comp);
153
161 Error appendComponent(const uint8_t* value, size_t len);
176 int compare(const Name& other) const;
177
184 bool equals(const Name& other) const;
185
200 bool isPrefixOf(const Name& other) const;
210 uint32_t hash() const;
211
220 bool empty() const { return numComponents_ == 0; }
221
226 const uint8_t* wireValue() const { return buffer_.data(); }
227
232 size_t wireLength() const { return length_; }
235private:
236 std::array<uint8_t, NAME_MAX_LENGTH> buffer_{};
237 size_t length_ = 0;
238
242 struct ComponentOffset {
243 uint16_t offset;
244 uint16_t length;
245 };
246 std::array<ComponentOffset, NAME_MAX_COMPONENTS> components_{};
247 uint8_t numComponents_ = 0;
248
255 Error appendComponentInternal(const uint8_t* value, size_t len);
256};
257
268inline bool operator==(const Name& lhs, const Name& rhs) {
269 return lhs.equals(rhs);
270}
271
278inline bool operator!=(const Name& lhs, const Name& rhs) {
279 return !lhs.equals(rhs);
280}
281
288inline bool operator<(const Name& lhs, const Name& rhs) {
289 return lhs.compare(rhs) < 0;
290}
293} // namespace ndn
NDN Name class.
Definition name.hpp:64
Error encode(uint8_t *buf, size_t bufSize, size_t &encodedLen) const
Encode the Name to TLV wire format.
Definition name.cpp:186
size_t wireLength() const
Get the length of the internal buffer.
Definition name.hpp:232
static Result< Name > fromWire(const uint8_t *buf, size_t len, size_t *bytesRead=nullptr)
Decode a Name from TLV wire format.
Definition name.cpp:84
bool empty() const
Check if the Name is empty.
Definition name.hpp:220
uint32_t hash() const
Compute hash value of the Name.
Definition name.cpp:334
Error appendComponent(std::string_view comp)
Append a string component.
Definition name.cpp:228
size_t toUri(char *buf, size_t bufSize) const
Convert the Name to a URI string.
Definition name.cpp:141
int compare(const Name &other) const
Compare with another Name.
Definition name.cpp:279
Name()=default
Default constructor.
size_t componentCount() const
Get the number of components.
Definition name.hpp:133
NameComponent component(size_t index) const
Get the component at a given index.
Definition name.cpp:219
bool equals(const Name &other) const
Check equality with another Name.
Definition name.cpp:309
bool isPrefixOf(const Name &other) const
Check if this Name is a prefix of another Name.
Definition name.cpp:313
static Result< Name > fromUri(std::string_view uri)
Create a Name from a URI string.
Definition name.cpp:25
const uint8_t * wireValue() const
Get pointer to the internal buffer.
Definition name.hpp:226
Common definitions for the NDN protocol stack.
Error
Error codes.
Definition common.hpp:24
bool operator<(const Name &lhs, const Name &rhs)
Less-than operator.
Definition name.hpp:288
bool operator==(const Name &lhs, const Name &rhs)
Equality operator.
Definition name.hpp:268
bool operator!=(const Name &lhs, const Name &rhs)
Inequality operator.
Definition name.hpp:278
Name component.
Definition name.hpp:26
std::string_view asString() const
Get the component as a string view.
Definition name.hpp:34
const uint8_t * value
Pointer to the component value.
Definition name.hpp:27
size_t size
Size of the component in bytes.
Definition name.hpp:28
Result type template.
Definition common.hpp:147