blob: 8163d0ce5c9df7c46290f7c98c226f6cd2c9592c [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080022 */
23
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070024#ifndef NDN_ENCODING_BLOCK_HELPERS_HPP
25#define NDN_ENCODING_BLOCK_HELPERS_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080026
27#include "block.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070028#include "encoding-buffer.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080029
30namespace ndn {
31
32inline Block
33nonNegativeIntegerBlock(uint32_t type, uint64_t value)
34{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070035 EncodingEstimator estimator;
36 size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
37
38 EncodingBuffer encoder(totalLength, 0);
39 prependNonNegativeIntegerBlock(encoder, type, value);
40
41 return encoder.block();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080042}
43
44inline uint64_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045readNonNegativeInteger(const Block& block)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080046{
47 Buffer::const_iterator begin = block.value_begin();
48 return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
49}
50
51inline Block
52booleanBlock(uint32_t type)
53{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070054 EncodingEstimator estimator;
55 size_t totalLength = prependBooleanBlock(estimator, type);
56
57 EncodingBuffer encoder(totalLength, 0);
58 prependBooleanBlock(encoder, type);
59
60 return encoder.block();
61}
62
63inline Block
64dataBlock(uint32_t type, const uint8_t* data, size_t dataSize)
65{
66 EncodingEstimator estimator;
67 size_t totalLength = prependByteArrayBlock(estimator, type, data, dataSize);
68
69 EncodingBuffer encoder(totalLength, 0);
70 prependByteArrayBlock(encoder, type, data, dataSize);
71
72 return encoder.block();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080073}
74
75inline Block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076dataBlock(uint32_t type, const char* data, size_t dataSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080077{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070078 return dataBlock(type, reinterpret_cast<const uint8_t*>(data), dataSize);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080079}
80
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070081// template<class InputIterator>
82// inline Block
83// dataBlock(uint32_t type, InputIterator first, InputIterator last)
84// {
85// size_t dataSize = 0;
86// for (InputIterator i = first; i != last; i++)
87// ++dataSize;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080088
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070089// OBufferStream os;
90// Tlv::writeVarNumber(os, type);
91// Tlv::writeVarNumber(os, dataSize);
92// std::copy(first, last, std::ostream_iterator<uint8_t>(os));
Alexander Afanasyev770827c2014-05-13 17:42:55 -070093
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070094// return Block(os.buf());
95// }
Alexander Afanasyev770827c2014-05-13 17:42:55 -070096
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080097} // namespace ndn
98
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070099#endif // NDN_ENCODING_BLOCK_HELPERS_HPP