esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
value.hpp
Go to the documentation of this file.
1#ifndef NDNPH_TLV_VALUE_HPP
2#define NDNPH_TLV_VALUE_HPP
3
4#include "decoder.hpp"
5#include "encoder.hpp"
6
7namespace ndnph {
8namespace tlv {
9
11class Value {
12public:
13 static Value fromString(const char* str) {
14 return Value(reinterpret_cast<const uint8_t*>(str), std::strlen(str));
15 }
16
17 explicit Value() = default;
18
20 explicit Value(const uint8_t* value, size_t size)
21 : m_value(value)
22 , m_size(size) {}
23
25 explicit Value(const uint8_t* first, const uint8_t* last)
26 : m_value(first)
27 , m_size(last - first) {}
28
30 explicit Value(const Encoder& encoder)
31 : Value(encoder.begin(), encoder.size()) {}
32
34 explicit operator bool() const {
35 return size() > 0;
36 }
37
38 const uint8_t* begin() const {
39 return m_value;
40 }
41
42 const uint8_t* end() const {
43 return m_value + m_size;
44 }
45
46 size_t size() const {
47 return m_size;
48 }
49
50 void encodeTo(Encoder& encoder) const {
51 uint8_t* room = encoder.prependRoom(m_size);
52 if (room != nullptr) {
53 std::copy_n(m_value, m_size, room);
54 }
55 }
56
57 bool decodeFrom(const Decoder::Tlv& d) {
58 m_value = d.value;
59 m_size = d.length;
60 return true;
61 }
62
65 return Decoder(m_value, m_size);
66 }
67
73 Value clone(Region& region) const {
74 uint8_t* copyV = region.alloc(m_size);
75 if (copyV == nullptr) {
76 return Value();
77 }
78 std::copy(begin(), end(), copyV);
79 return Value(copyV, m_size);
80 }
81
82private:
83 const uint8_t* m_value = nullptr;
84 size_t m_size = 0;
85};
86
87inline bool
88operator==(const Value& lhs, const Value& rhs) {
89 return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
90}
91
92NDNPH_DECLARE_NE(Value, inline)
93
94} // namespace tlv
95} // namespace ndnph
96
97#endif // NDNPH_TLV_VALUE_HPP
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
uint8_t * prependRoom(size_t size)
Make room to prepend an object.
Definition encoder.hpp:90
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
A sequence of bytes, usually TLV-VALUE.
Definition value.hpp:11
const uint8_t * end() const
Definition value.hpp:42
Value(const Encoder &encoder)
Reference encoder output.
Definition value.hpp:30
Value(const uint8_t *first, const uint8_t *last)
Reference a byte range.
Definition value.hpp:25
Value clone(Region &region) const
Clone buffer into given region.
Definition value.hpp:73
Decoder makeDecoder() const
Create a Decoder over this value buffer.
Definition value.hpp:64
const uint8_t * begin() const
Definition value.hpp:38
bool decodeFrom(const Decoder::Tlv &d)
Definition value.hpp:57
static Value fromString(const char *str)
Definition value.hpp:13
Value(const uint8_t *value, size_t size)
Reference a byte range.
Definition value.hpp:20
void encodeTo(Encoder &encoder) const
Definition value.hpp:50
size_t size() const
Definition value.hpp:46
bool operator==(const Value &lhs, const Value &rhs)
Definition value.hpp:88
Definition fs.hpp:33
#define NDNPH_DECLARE_NE(T, specifier)
Declare operator!= in terms of operator==.
Definition operators.hpp:7