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"
46 ndn_TlvEncoder_initialize(
this, &output_);
53 const ptr_lib::shared_ptr<std::vector<uint8_t> >&
61 writeTypeAndLength(
unsigned int type,
size_t length)
64 if ((error = ndn_TlvEncoder_writeTypeAndLength(
this, type, length)))
65 throw std::runtime_error(ndn_getErrorString(error));
69 writeNonNegativeInteger(uint64_t value)
72 if ((error = ndn_TlvEncoder_writeNonNegativeInteger(
this, value)))
73 throw std::runtime_error(ndn_getErrorString(error));
77 writeBlobTlv(
unsigned int type,
struct ndn_Blob *value)
80 if ((error = ndn_TlvEncoder_writeBlobTlv(
this, type, value)))
81 throw std::runtime_error(ndn_getErrorString(error));
93 ndn_Blob_initialize(&valueBlob, (
const uint8_t*)&value[0], value.size());
94 writeBlobTlv(type, &valueBlob);
98 writeNonNegativeIntegerTlv(
unsigned int type, uint64_t value)
101 if ((error = ndn_TlvEncoder_writeNonNegativeIntegerTlv(
this, type, value)))
102 throw std::runtime_error(ndn_getErrorString(error));
108 ndn_Error (*writeValue)(
const void *context,
struct ndn_TlvEncoder* encoder),
109 const void *context,
bool omitZeroLength =
false)
112 if ((error = ndn_TlvEncoder_writeNestedTlv
113 (
this, type, writeValue, context, omitZeroLength ? 1 : 0)))
114 throw std::runtime_error(ndn_getErrorString(error));
127 (
unsigned int type,
void (*writeValue)(
const void *context,
TlvEncoder& encoder),
128 const void *context,
bool omitZeroLength =
false)
130 WriteValueWrapper(writeValue, context).writeNestedTlv
131 (*
this, type, omitZeroLength);
138 class WriteValueWrapper {
148 (
void (*writeValue)(
const void *context,
TlvEncoder& encoder),
const void *context)
149 : writeValue_(writeValue), context_(context) {}
159 writeNestedTlv(
TlvEncoder& encoder,
unsigned int type,
bool omitZeroLength)
161 encoder.writeNestedTlv(type, writeValueWrapper,
this, omitZeroLength);
175 writeValueWrapper(
const void *context,
struct ndn_TlvEncoder *encoder)
177 const WriteValueWrapper& wrapper = *(
const WriteValueWrapper*)context;
178 wrapper.writeValue_(wrapper.context_, (
TlvEncoder&)*encoder);
181 return NDN_ERROR_success;
184 void (*writeValue_)(
const void *context,
TlvEncoder& encoder);
185 const void *context_;
188 DynamicUInt8Vector output_;
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
size_t offset
The offset into output.array for the next encoding.
Definition: tlv-encoder.h:41
ptr_lib::shared_ptr< std::vector< uint8_t > > & get()
Get the shared_ptr to the allocated vector.
Definition: dynamic-uint8-vector.hpp:126
void writeRawStringTlv(unsigned int type, const std::string &value)
Call writeBlobTlv using the raw string bytes.
Definition: tlv-encoder.hpp:90
Copyright (C) 2014-2015 Regents of the University of California.
Definition: tlv-encoder.h:39
const ptr_lib::shared_ptr< std::vector< uint8_t > > & getOutput()
Resize the output vector to the correct encoding length and return.
Definition: tlv-encoder.hpp:54
Copyright (C) 2015 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