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_(16)
45  {
46  ndn_TlvEncoder_initialize(this, &output_);
47  }
48 
53  const ptr_lib::shared_ptr<std::vector<uint8_t> >&
55  {
56  output_.get()->resize(offset);
57  return output_.get();
58  }
59 
60  void
61  writeTypeAndLength(unsigned int type, size_t length)
62  {
63  ndn_Error error;
64  if ((error = ndn_TlvEncoder_writeTypeAndLength(this, type, length)))
65  throw std::runtime_error(ndn_getErrorString(error));
66  }
67 
68  void
69  writeNonNegativeInteger(uint64_t value)
70  {
71  ndn_Error error;
72  if ((error = ndn_TlvEncoder_writeNonNegativeInteger(this, value)))
73  throw std::runtime_error(ndn_getErrorString(error));
74  }
75 
76  void
77  writeBlobTlv(unsigned int type, struct ndn_Blob *value)
78  {
79  ndn_Error error;
80  if ((error = ndn_TlvEncoder_writeBlobTlv(this, type, value)))
81  throw std::runtime_error(ndn_getErrorString(error));
82  }
83 
89  void
90  writeRawStringTlv(unsigned int type, const std::string& value)
91  {
92  struct ndn_Blob valueBlob;
93  ndn_Blob_initialize(&valueBlob, (const uint8_t*)&value[0], value.size());
94  writeBlobTlv(type, &valueBlob);
95  }
96 
97  void
98  writeNonNegativeIntegerTlv(unsigned int type, uint64_t value)
99  {
100  ndn_Error error;
101  if ((error = ndn_TlvEncoder_writeNonNegativeIntegerTlv(this, type, value)))
102  throw std::runtime_error(ndn_getErrorString(error));
103  }
104 
105  void
106  writeNestedTlv
107  (unsigned int type,
108  ndn_Error (*writeValue)(const void *context, struct ndn_TlvEncoder* encoder),
109  const void *context, bool omitZeroLength = false)
110  {
111  ndn_Error error;
112  if ((error = ndn_TlvEncoder_writeNestedTlv
113  (this, type, writeValue, context, omitZeroLength ? 1 : 0)))
114  throw std::runtime_error(ndn_getErrorString(error));
115  }
116 
125  void
126  writeNestedTlv
127  (unsigned int type, void (*writeValue)(const void *context, TlvEncoder& encoder),
128  const void *context, bool omitZeroLength = false)
129  {
130  WriteValueWrapper(writeValue, context).writeNestedTlv
131  (*this, type, omitZeroLength);
132  }
133 
134 private:
135  /* An WriteValueWrapper holds the caller context so that it can be
136  * passed to the caller's writeValue. See writeNestedTlv.
137  */
138  class WriteValueWrapper {
139  public:
147  WriteValueWrapper
148  (void (*writeValue)(const void *context, TlvEncoder& encoder), const void *context)
149  : writeValue_(writeValue), context_(context) {}
150 
158  void
159  writeNestedTlv(TlvEncoder& encoder, unsigned int type, bool omitZeroLength)
160  {
161  encoder.writeNestedTlv(type, writeValueWrapper, this, omitZeroLength);
162  }
163 
164  private:
174  static ndn_Error
175  writeValueWrapper(const void *context, struct ndn_TlvEncoder *encoder)
176  {
177  const WriteValueWrapper& wrapper = *(const WriteValueWrapper*)context;
178  wrapper.writeValue_(wrapper.context_, (TlvEncoder&)*encoder);
179 
180  // wrapper.writeValue_ has thrown an exception if needed.
181  return NDN_ERROR_success;
182  }
183 
184  void (*writeValue_)(const void *context, TlvEncoder& encoder);
185  const void *context_;
186  };
187 
188  DynamicUInt8Vector output_;
189 };
190 
191 }
192 
193 #endif
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