All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
block-helpers.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2013, Regents of the University of California
4  *
5  * BSD license, See the LICENSE file for more information
6  *
7  * Author: Alexander Afanasyev <[email protected]>
8  */
9 
10 #ifndef NDN_BLOCK_HELPERS_HPP
11 #define NDN_BLOCK_HELPERS_HPP
12 
13 #include "block.hpp"
14 
15 namespace ndn {
16 
17 inline Block
18 nonNegativeIntegerBlock(uint32_t type, uint64_t value)
19 {
20  OBufferStream os;
21  Tlv::writeVarNumber(os, type);
24  return Block(os.buf());
25 }
26 
27 inline uint64_t
29 {
30  Buffer::const_iterator begin = block.value_begin();
31  return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
32 }
33 
34 inline Block
35 booleanBlock(uint32_t type)
36 {
37  OBufferStream os;
38  Tlv::writeVarNumber(os, type);
39  Tlv::writeVarNumber(os, 0);
40  return Block(os.buf());
41 }
42 
43 inline Block
44 dataBlock(uint32_t type, const char* data, size_t dataSize)
45 {
46  OBufferStream os;
47  Tlv::writeVarNumber(os, type);
48  Tlv::writeVarNumber(os, dataSize);
49  os.write(data, dataSize);
50 
51  return Block(os.buf());
52 }
53 
54 inline Block
55 dataBlock(uint32_t type, const unsigned char* data, size_t dataSize)
56 {
57  return dataBlock(type, reinterpret_cast<const char*>(data), dataSize);
58 }
59 
60 } // namespace ndn
61 
62 #endif // NDN_BLOCK_HELPERS_HPP
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
Definition: buffer.hpp:176
uint64_t readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
Definition: tlv.hpp:372
size_t writeVarNumber(std::ostream &os, uint64_t varNumber)
Write VAR-NUMBER to the specified stream.
Definition: tlv.hpp:344
Block nonNegativeIntegerBlock(uint32_t type, uint64_t value)
size_t sizeOfNonNegativeInteger(uint64_t varNumber)
Get number of bytes necessary to hold value of nonNegativeInteger.
Definition: tlv.hpp:481
Class representing wire element of the NDN packet.
Definition: block.hpp:26
Block booleanBlock(uint32_t type)
uint64_t readNonNegativeInteger(const Block &block)
Buffer::const_iterator value_begin() const
Definition: block.hpp:425
Buffer::const_iterator value_end() const
Definition: block.hpp:431
size_t writeNonNegativeInteger(std::ostream &os, uint64_t varNumber)
Write nonNegativeInteger to the specified stream.
Definition: tlv.hpp:499
ptr_lib::shared_ptr< Buffer > buf()
Flush written data to the stream and return shared pointer to the underlying buffer.
Definition: buffer.hpp:192
size_t value_size() const
Definition: block.hpp:455
Block dataBlock(uint32_t type, const char *data, size_t dataSize)