Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 5 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * 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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 8 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 9 | * 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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 13 | */ |
| 14 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 15 | #ifndef NDN_ENCODING_BLOCK_HELPERS_HPP |
| 16 | #define NDN_ENCODING_BLOCK_HELPERS_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 17 | |
| 18 | #include "block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 19 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 20 | |
| 21 | namespace ndn { |
| 22 | |
| 23 | inline Block |
| 24 | nonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 25 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 26 | 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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | inline uint64_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | readNonNegativeInteger(const Block& block) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 37 | { |
| 38 | Buffer::const_iterator begin = block.value_begin(); |
| 39 | return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
| 40 | } |
| 41 | |
| 42 | inline Block |
| 43 | booleanBlock(uint32_t type) |
| 44 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 45 | 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 | |
| 54 | inline Block |
| 55 | dataBlock(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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 68 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 69 | return dataBlock(type, reinterpret_cast<const uint8_t*>(data), dataSize); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 72 | // 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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 80 | // OBufferStream os; |
| 81 | // Tlv::writeVarNumber(os, type); |
| 82 | // Tlv::writeVarNumber(os, dataSize); |
| 83 | // std::copy(first, last, std::ostream_iterator<uint8_t>(os)); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 84 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 85 | // return Block(os.buf()); |
| 86 | // } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 87 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 88 | } // namespace ndn |
| 89 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame^] | 90 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |