tlv-encoder.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_TLV_ENCODER_HPP
23 #define NDN_TLV_ENCODER_HPP
24 
25 #include <vector>
26 #include <string>
27 #include <stdexcept>
28 #include <ndn-cpp/common.hpp>
29 #include "../util/dynamic-uint8-vector.hpp"
30 #include "../c/encoding/tlv/tlv-encoder.h"
31 
32 namespace ndn {
33 
37 class TlvEncoder : public ndn_TlvEncoder {
38 public:
43  TlvEncoder(size_t initialLength = 16)
44  : output_(initialLength)
45  {
46  ndn_TlvEncoder_initialize(this, &output_);
47  }
48 
56  Blob
57  finish() { return output_.finish(offset); }
58 
59  void
60  writeTypeAndLength(unsigned int type, size_t length)
61  {
62  ndn_Error error;
63  if ((error = ndn_TlvEncoder_writeTypeAndLength(this, type, length)))
64  throw std::runtime_error(ndn_getErrorString(error));
65  }
66 
67  void
68  writeNonNegativeInteger(uint64_t value)
69  {
70  ndn_Error error;
71  if ((error = ndn_TlvEncoder_writeNonNegativeInteger(this, value)))
72  throw std::runtime_error(ndn_getErrorString(error));
73  }
74 
75  void
76  writeBlobTlv(unsigned int type, struct ndn_Blob *value)
77  {
78  ndn_Error error;
79  if ((error = ndn_TlvEncoder_writeBlobTlv(this, type, value)))
80  throw std::runtime_error(ndn_getErrorString(error));
81  }
82 
88  void
89  writeRawStringTlv(unsigned int type, const std::string& value)
90  {
91  struct ndn_Blob valueBlob;
92  ndn_Blob_initialize(&valueBlob, (const uint8_t*)&value[0], value.size());
93  writeBlobTlv(type, &valueBlob);
94  }
95 
96  void
97  writeNonNegativeIntegerTlv(unsigned int type, uint64_t value)
98  {
99  ndn_Error error;
100  if ((error = ndn_TlvEncoder_writeNonNegativeIntegerTlv(this, type, value)))
101  throw std::runtime_error(ndn_getErrorString(error));
102  }
103 
104  void
105  writeNestedTlv
106  (unsigned int type,
107  ndn_Error (*writeValue)(const void *context, struct ndn_TlvEncoder* encoder),
108  const void *context, bool omitZeroLength = false)
109  {
110  ndn_Error error;
111  if ((error = ndn_TlvEncoder_writeNestedTlv
112  (this, type, writeValue, context, omitZeroLength ? 1 : 0)))
113  throw std::runtime_error(ndn_getErrorString(error));
114  }
115 
124  void
125  writeNestedTlv
126  (unsigned int type, void (*writeValue)(const void *context, TlvEncoder& encoder),
127  const void *context, bool omitZeroLength = false)
128  {
129  WriteValueWrapper(writeValue, context).writeNestedTlv
130  (*this, type, omitZeroLength);
131  }
132 
133 private:
134  /* An WriteValueWrapper holds the caller context so that it can be
135  * passed to the caller's writeValue. See writeNestedTlv.
136  */
137  class WriteValueWrapper {
138  public:
146  WriteValueWrapper
147  (void (*writeValue)(const void *context, TlvEncoder& encoder), const void *context)
148  : writeValue_(writeValue), context_(context) {}
149 
157  void
158  writeNestedTlv(TlvEncoder& encoder, unsigned int type, bool omitZeroLength)
159  {
160  encoder.writeNestedTlv(type, writeValueWrapper, this, omitZeroLength);
161  }
162 
163  private:
173  static ndn_Error
174  writeValueWrapper(const void *context, struct ndn_TlvEncoder *encoder)
175  {
176  const WriteValueWrapper& wrapper = *(const WriteValueWrapper*)context;
177  wrapper.writeValue_(wrapper.context_, (TlvEncoder&)*encoder);
178 
179  // wrapper.writeValue_ has thrown an exception if needed.
180  return NDN_ERROR_success;
181  }
182 
183  void (*writeValue_)(const void *context, TlvEncoder& encoder);
184  const void *context_;
185  };
186 
187  DynamicUInt8Vector output_;
188 };
189 
190 }
191 
192 #endif
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