blob: 317cd13d184012a9c29c9148bbc90d2d8532a2bf [file] [log] [blame]
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08005 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08008 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07009 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080013 */
14
15#ifndef NDN_BLOCK_HELPERS_HPP
16#define NDN_BLOCK_HELPERS_HPP
17
18#include "block.hpp"
19
20namespace ndn {
21
22inline Block
23nonNegativeIntegerBlock(uint32_t type, uint64_t value)
24{
25 OBufferStream os;
26 Tlv::writeVarNumber(os, type);
27 Tlv::writeVarNumber(os, Tlv::sizeOfNonNegativeInteger(value));
28 Tlv::writeNonNegativeInteger(os, value);
29 return Block(os.buf());
30}
31
32inline uint64_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070033readNonNegativeInteger(const Block& block)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080034{
35 Buffer::const_iterator begin = block.value_begin();
36 return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
37}
38
39inline Block
40booleanBlock(uint32_t type)
41{
42 OBufferStream os;
43 Tlv::writeVarNumber(os, type);
44 Tlv::writeVarNumber(os, 0);
45 return Block(os.buf());
46}
47
48inline Block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070049dataBlock(uint32_t type, const char* data, size_t dataSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080050{
51 OBufferStream os;
52 Tlv::writeVarNumber(os, type);
53 Tlv::writeVarNumber(os, dataSize);
54 os.write(data, dataSize);
55
56 return Block(os.buf());
57}
58
59inline Block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060dataBlock(uint32_t type, const unsigned char* data, size_t dataSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080061{
62 return dataBlock(type, reinterpret_cast<const char*>(data), dataSize);
63}
64
Alexander Afanasyev770827c2014-05-13 17:42:55 -070065template<class InputIterator>
66inline Block
67dataBlock(uint32_t type, InputIterator first, InputIterator last)
68{
69 size_t dataSize = 0;
70 for (InputIterator i = first; i != last; i++)
71 ++dataSize;
72
73 OBufferStream os;
74 Tlv::writeVarNumber(os, type);
75 Tlv::writeVarNumber(os, dataSize);
76 std::copy(first, last, std::ostream_iterator<uint8_t>(os));
77
78 return Block(os.buf());
79}
80
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080081} // namespace ndn
82
83#endif // NDN_BLOCK_HELPERS_HPP