blob: 36c94746c86a46bca0758c56b024c96032eea4f1 [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
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070015#ifndef NDN_ENCODING_BLOCK_HELPERS_HPP
16#define NDN_ENCODING_BLOCK_HELPERS_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080017
18#include "block.hpp"
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070019#include "encoding-buffer.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080020
21namespace ndn {
22
23inline Block
24nonNegativeIntegerBlock(uint32_t type, uint64_t value)
25{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070026 EncodingEstimator estimator;
27 size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value);
28
29 EncodingBuffer encoder(totalLength, 0);
30 prependNonNegativeIntegerBlock(encoder, type, value);
31
32 return encoder.block();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080033}
34
35inline uint64_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036readNonNegativeInteger(const Block& block)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080037{
38 Buffer::const_iterator begin = block.value_begin();
39 return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end());
40}
41
42inline Block
43booleanBlock(uint32_t type)
44{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070045 EncodingEstimator estimator;
46 size_t totalLength = prependBooleanBlock(estimator, type);
47
48 EncodingBuffer encoder(totalLength, 0);
49 prependBooleanBlock(encoder, type);
50
51 return encoder.block();
52}
53
54inline Block
55dataBlock(uint32_t type, const uint8_t* data, size_t dataSize)
56{
57 EncodingEstimator estimator;
58 size_t totalLength = prependByteArrayBlock(estimator, type, data, dataSize);
59
60 EncodingBuffer encoder(totalLength, 0);
61 prependByteArrayBlock(encoder, type, data, dataSize);
62
63 return encoder.block();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080064}
65
66inline Block
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067dataBlock(uint32_t type, const char* data, size_t dataSize)
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080068{
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070069 return dataBlock(type, reinterpret_cast<const uint8_t*>(data), dataSize);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080070}
71
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070072// template<class InputIterator>
73// inline Block
74// dataBlock(uint32_t type, InputIterator first, InputIterator last)
75// {
76// size_t dataSize = 0;
77// for (InputIterator i = first; i != last; i++)
78// ++dataSize;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080079
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070080// OBufferStream os;
81// Tlv::writeVarNumber(os, type);
82// Tlv::writeVarNumber(os, dataSize);
83// std::copy(first, last, std::ostream_iterator<uint8_t>(os));
Alexander Afanasyev770827c2014-05-13 17:42:55 -070084
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070085// return Block(os.buf());
86// }
Alexander Afanasyev770827c2014-05-13 17:42:55 -070087
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080088} // namespace ndn
89
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070090#endif // NDN_ENCODING_BLOCK_HELPERS_HPP