24 #include <boost/endian/conversion.hpp> 29 namespace endian = boost::endian;
32 : m_buffer(make_shared<
Buffer>(totalReserve))
34 m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0);
40 , m_end(m_buffer->
begin() + (block.
end() - m_buffer->
begin()))
47 if (m_end + size > m_buffer->end())
54 if (m_buffer->begin() + size > m_begin)
61 return Block(m_buffer, m_begin, m_end, verifyLength);
67 if (size < m_buffer->
size()) {
68 size = m_buffer->size();
72 size_t diffEnd = m_buffer->end() - m_end;
73 size_t diffBegin = m_buffer->end() - m_begin;
76 std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end());
80 m_end = m_buffer->end() - diffEnd;
81 m_begin = m_buffer->end() - diffBegin;
84 size_t diffEnd = m_end - m_buffer->begin();
85 size_t diffBegin = m_begin - m_buffer->begin();
88 std::copy(m_buffer->begin(), m_buffer->end(), buf->begin());
92 m_end = m_buffer->begin() + diffEnd;
93 m_begin = m_buffer->begin() + diffBegin;
112 if (varNumber < 253) {
116 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
117 uint16_t
value = endian::native_to_big(static_cast<uint16_t>(varNumber));
122 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
123 uint32_t
value = endian::native_to_big(static_cast<uint32_t>(varNumber));
129 uint64_t
value = endian::native_to_big(varNumber);
139 if (varNumber < 253) {
143 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
145 uint16_t
value = endian::native_to_big(static_cast<uint16_t>(varNumber));
149 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
151 uint32_t
value = endian::native_to_big(static_cast<uint32_t>(varNumber));
157 uint64_t
value = endian::native_to_big(varNumber);
166 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
169 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
170 uint16_t
value = endian::native_to_big(static_cast<uint16_t>(varNumber));
173 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
174 uint32_t
value = endian::native_to_big(static_cast<uint32_t>(varNumber));
178 uint64_t
value = endian::native_to_big(varNumber);
186 if (varNumber <= std::numeric_limits<uint8_t>::max()) {
187 return appendBytes({
static_cast<uint8_t
>(varNumber)});
189 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
190 uint16_t
value = endian::native_to_big(static_cast<uint16_t>(varNumber));
193 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
194 uint32_t
value = endian::native_to_big(static_cast<uint32_t>(varNumber));
198 uint64_t
value = endian::native_to_big(varNumber);
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
Copyright (c) 2011-2015 Regents of the University of California.
iterator begin() noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
size_t prependBytes(span< const uint8_t > bytes)
Prepend a sequence of bytes.
Represents a TLV element of the NDN packet format.
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
size_t size() const noexcept
Returns the size of the encoded buffer.
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
shared_ptr< Buffer > getBuffer() const noexcept
Get underlying buffer.
iterator end() noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
size_t prependNonNegativeInteger(uint64_t integer)
Prepend integer encoded as a NonNegativeInteger in NDN-TLV format.
size_t appendNonNegativeInteger(uint64_t integer)
Append integer encoded as a NonNegativeInteger in NDN-TLV format.
size_t appendVarNumber(uint64_t number)
Append number encoded as a VAR-NUMBER in NDN-TLV format.
General-purpose automatically managed/resized buffer.
Encoder(size_t totalReserve=MAX_NDN_PACKET_SIZE, size_t reserveFromBack=400)
Create instance of the encoder with the specified reserved sizes.
size_t appendBytes(span< const uint8_t > bytes)
Append a sequence of bytes.