22 #ifndef NDN_TLV_ENCODER_HPP
23 #define NDN_TLV_ENCODER_HPP
28 #include <ndn-cpp/common.hpp>
29 #include "../util/dynamic-uint8-vector.hpp"
30 #include "../c/encoding/tlv/tlv-encoder.h"
44 : output_(initialLength)
46 ndn_TlvEncoder_initialize(
this, &output_);
60 writeTypeAndLength(
unsigned int type,
size_t length)
63 if ((error = ndn_TlvEncoder_writeTypeAndLength(
this, type, length)))
64 throw std::runtime_error(ndn_getErrorString(error));
68 writeNonNegativeInteger(uint64_t value)
71 if ((error = ndn_TlvEncoder_writeNonNegativeInteger(
this, value)))
72 throw std::runtime_error(ndn_getErrorString(error));
76 writeBlobTlv(
unsigned int type,
struct ndn_Blob *value)
79 if ((error = ndn_TlvEncoder_writeBlobTlv(
this, type, value)))
80 throw std::runtime_error(ndn_getErrorString(error));
92 ndn_Blob_initialize(&valueBlob, (
const uint8_t*)&value[0], value.size());
93 writeBlobTlv(type, &valueBlob);
97 writeNonNegativeIntegerTlv(
unsigned int type, uint64_t value)
100 if ((error = ndn_TlvEncoder_writeNonNegativeIntegerTlv(
this, type, value)))
101 throw std::runtime_error(ndn_getErrorString(error));
107 ndn_Error (*writeValue)(
const void *context,
struct ndn_TlvEncoder* encoder),
108 const void *context,
bool omitZeroLength =
false)
111 if ((error = ndn_TlvEncoder_writeNestedTlv
112 (
this, type, writeValue, context, omitZeroLength ? 1 : 0)))
113 throw std::runtime_error(ndn_getErrorString(error));
126 (
unsigned int type,
void (*writeValue)(
const void *context,
TlvEncoder& encoder),
127 const void *context,
bool omitZeroLength =
false)
129 WriteValueWrapper(writeValue, context).writeNestedTlv
130 (*
this, type, omitZeroLength);
137 class WriteValueWrapper {
147 (
void (*writeValue)(
const void *context,
TlvEncoder& encoder),
const void *context)
148 : writeValue_(writeValue), context_(context) {}
158 writeNestedTlv(
TlvEncoder& encoder,
unsigned int type,
bool omitZeroLength)
160 encoder.writeNestedTlv(type, writeValueWrapper,
this, omitZeroLength);
174 writeValueWrapper(
const void *context,
struct ndn_TlvEncoder *encoder)
176 const WriteValueWrapper& wrapper = *(
const WriteValueWrapper*)context;
177 wrapper.writeValue_(wrapper.context_, (
TlvEncoder&)*encoder);
180 return NDN_ERROR_success;
183 void (*writeValue_)(
const void *context,
TlvEncoder& encoder);
184 const void *context_;
187 DynamicUInt8Vector output_;
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
Blob finish()
Resize the output vector to the correct encoding length, transfer the bytes to a Blob and return the ...
Definition: tlv-encoder.hpp:57
size_t offset
The offset into output.array for the next encoding.
Definition: tlv-encoder.h:41
Blob finish(size_t size)
Resize the allocated vector to the given size, transfer the bytes to a Blob and return the Blob...
Definition: dynamic-uint8-vector.hpp:136
void writeRawStringTlv(unsigned int type, const std::string &value)
Call writeBlobTlv using the raw string bytes.
Definition: tlv-encoder.hpp:89
Copyright (C) 2014-2016 Regents of the University of California.
Definition: tlv-encoder.h:39
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
Copyright (C) 2015-2016 Regents of the University of California.
Definition: blob-types.h:33
TlvEncoder(size_t initialLength=16)
Initialize the base ndn_TlvEncoder struct with the initialLength.
Definition: tlv-encoder.hpp:43
A TlvEncoder extends a C ndn_TlvEncoder struct and wraps related functions.
Definition: tlv-encoder.hpp:37