NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
block-helpers.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_ENCODING_BLOCK_HELPERS_HPP
23 #define NDN_ENCODING_BLOCK_HELPERS_HPP
24 
25 #include "block.hpp"
26 #include "encoding-buffer.hpp"
27 #include "../util/concepts.hpp"
28 
29 #include <iterator>
30 
31 namespace ndn {
32 namespace encoding {
33 
38 template<Tag TAG>
39 size_t
40 prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value);
41 
46 Block
47 makeNonNegativeIntegerBlock(uint32_t type, uint64_t value);
48 
54 uint64_t
55 readNonNegativeInteger(const Block& block);
56 
58 
63 template<Tag TAG>
64 size_t
65 prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type);
66 
71 Block
72 makeEmptyBlock(uint32_t type);
73 
75 
80 template<Tag TAG>
81 size_t
82 prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value);
83 
88 Block
89 makeStringBlock(uint32_t type, const std::string& value);
90 
95 std::string
96 readString(const Block& block);
97 
99 
103 Block
104 makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length);
105 
109 Block
110 makeBinaryBlock(uint32_t type, const char* value, size_t length);
111 
115 template<class Iterator>
117 {
118 public:
119  BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>));
120 
121  static Block
122  makeBlock(uint32_t type, Iterator first, Iterator last)
123  {
124  EncodingEstimator estimator;
125  size_t valueLength = last - first;
126  size_t totalLength = valueLength;
127  totalLength += estimator.prependVarNumber(valueLength);
128  totalLength += estimator.prependVarNumber(type);
129 
130  EncodingBuffer encoder(totalLength, 0);
131  encoder.prependRange(first, last);
132  encoder.prependVarNumber(valueLength);
133  encoder.prependVarNumber(type);
134 
135  return encoder.block();
136  }
137 };
138 
142 template<class Iterator>
144 {
145 public:
146  BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>));
147 
148  static Block
149  makeBlock(uint32_t type, Iterator first, Iterator last)
150  {
151  // reserve 4 bytes in front (common for 1(type)-3(length) encoding
152  // Actual size will be adjusted as necessary by the encoder
153  EncodingBuffer encoder(4, 4);
154  size_t valueLength = encoder.appendRange(first, last);
155  encoder.prependVarNumber(valueLength);
156  encoder.prependVarNumber(type);
157 
158  return encoder.block();
159  }
160 };
161 
168 template<class Iterator>
169 inline Block
170 makeBinaryBlock(uint32_t type, Iterator first, Iterator last)
171 {
172  static_assert(sizeof(typename std::iterator_traits<Iterator>::value_type) == 1,
173  "Iterator should point only to char or unsigned char");
174 
175  typedef typename boost::mpl::if_<
176  std::is_base_of<std::random_access_iterator_tag,
177  typename std::iterator_traits<Iterator>::iterator_category>,
179  DataBlockSlow<Iterator>>::type DataBlock;
180 
181  return DataBlock::makeBlock(type, first, last);
182 }
183 
185 
191 template<Tag TAG, class U>
192 inline size_t
193 prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value)
194 {
195  BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>));
196 
197  size_t valueLength = value.wireEncode(encoder);
198  size_t totalLength = valueLength;
199  totalLength += encoder.prependVarNumber(valueLength);
200  totalLength += encoder.prependVarNumber(type);
201 
202  return totalLength;
203 }
204 
210 template<class U>
211 inline Block
212 makeNestedBlock(uint32_t type, const U& value)
213 {
214  EncodingEstimator estimator;
215  size_t totalLength = prependNestedBlock(estimator, type, value);
216 
217  EncodingBuffer encoder(totalLength, 0);
218  prependNestedBlock(encoder, type, value);
219 
220  return encoder.block();
221 }
222 
223 #define NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS
224 #ifdef NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS
225 
229 template<Tag TAG>
230 inline size_t
231 prependBlock(EncodingImpl<TAG>& encoder, const Block& block)
232 {
233  return encoder.prependBlock(block);
234 }
235 
239 template<Tag TAG>
240 inline size_t
242  uint32_t type, const uint8_t* array, size_t arraySize)
243 {
244  return encoder.prependByteArrayBlock(type, array, arraySize);
245 }
246 
250 inline Block
251 nonNegativeIntegerBlock(uint32_t type, uint64_t value)
252 {
253  return makeNonNegativeIntegerBlock(type, value);
254 }
255 
259 template<Tag TAG>
260 size_t
261 prependBooleanBlock(EncodingImpl<TAG>& encoder, uint32_t type)
262 {
263  return prependEmptyBlock(encoder, type);
264 }
265 
269 inline Block
270 booleanBlock(uint32_t type)
271 {
272  return makeEmptyBlock(type);
273 }
274 
278 inline Block
279 dataBlock(uint32_t type, const uint8_t* data, size_t dataSize)
280 {
281  return makeBinaryBlock(type, data, dataSize);
282 }
283 
287 inline Block
288 dataBlock(uint32_t type, const char* data, size_t dataSize)
289 {
290  return makeBinaryBlock(type, data, dataSize);
291 }
292 
296 template<class Iterator>
297 inline Block
298 dataBlock(uint32_t type, Iterator first, Iterator last)
299 {
300  return makeBinaryBlock(type, first, last);
301 }
302 
306 template<class U>
307 inline Block
308 nestedBlock(uint32_t type, const U& value)
309 {
310  return makeNestedBlock(type, value);
311 }
312 
313 #endif // NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS
314 
315 } // namespace encoding
316 
324 
325 #ifdef NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS
326 
329 using encoding::dataBlock;
330 
331 #endif // NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS
332 
333 } // namespace ndn
334 
335 #endif // NDN_ENCODING_BLOCK_HELPERS_HPP
Copyright (c) 2011-2015 Regents of the University of California.
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:329
EncodingImpl specialization for real TLV encoding.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
size_t prependByteArrayBlock(EncodingImpl< TAG > &encoder, uint32_t type, const uint8_t *array, size_t arraySize)
Block makeEmptyBlock(uint32_t type)
Create a TLV block type type containing no value (i.e., a boolean block)
Helper class template to create a data block when RandomAccessIterator is used.
Block makeNestedBlock(uint32_t type, const U &value)
Create a TLV block of type type with WireEncodable value as a value.
size_t prependNestedBlock(EncodingImpl< TAG > &encoder, uint32_t type, const U &value)
Prepend a TLV block of type type with WireEncodable value as a value.
static Block makeBlock(uint32_t type, Iterator first, Iterator last)
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Helper to prepend TLV block type type with value from a string value.
std::string readString(const Block &block)
Helper to read a string value from a block.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block type type containing non-negative integer value.
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
EncodingImpl specialization TLV size estimation.
Block nonNegativeIntegerBlock(uint32_t type, uint64_t value)
size_t prependVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
Definition: encoder.cpp:146
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:50
Block booleanBlock(uint32_t type)
size_t prependEmptyBlock(EncodingImpl< TAG > &encoder, uint32_t type)
Helper to prepend TLV block type type containing no value (i.e., a boolean block) ...
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
size_t prependVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
Definition: estimator.cpp:57
static Block makeBlock(uint32_t type, Iterator first, Iterator last)
Helper class template to create a data block when generic InputIterator is used.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block type type with value from a string .
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:316
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:60
Block dataBlock(uint32_t type, const uint8_t *data, size_t dataSize)
size_t prependBooleanBlock(EncodingImpl< TAG > &encoder, uint32_t type)
size_t prependBlock(EncodingImpl< TAG > &encoder, const Block &block)
Block nestedBlock(uint32_t type, const U &value)