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 | |
| 15 | #ifndef NDN_BLOCK_HELPERS_HPP |
| 16 | #define NDN_BLOCK_HELPERS_HPP |
| 17 | |
| 18 | #include "block.hpp" |
| 19 | |
| 20 | namespace ndn { |
| 21 | |
| 22 | inline Block |
| 23 | nonNegativeIntegerBlock(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 | |
| 32 | inline uint64_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 33 | readNonNegativeInteger(const Block& block) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 34 | { |
| 35 | Buffer::const_iterator begin = block.value_begin(); |
| 36 | return Tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
| 37 | } |
| 38 | |
| 39 | inline Block |
| 40 | booleanBlock(uint32_t type) |
| 41 | { |
| 42 | OBufferStream os; |
| 43 | Tlv::writeVarNumber(os, type); |
| 44 | Tlv::writeVarNumber(os, 0); |
| 45 | return Block(os.buf()); |
| 46 | } |
| 47 | |
| 48 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 49 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 50 | { |
| 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 | |
| 59 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | dataBlock(uint32_t type, const unsigned char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 61 | { |
| 62 | return dataBlock(type, reinterpret_cast<const char*>(data), dataSize); |
| 63 | } |
| 64 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame^] | 65 | template<class InputIterator> |
| 66 | inline Block |
| 67 | dataBlock(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 Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 81 | } // namespace ndn |
| 82 | |
| 83 | #endif // NDN_BLOCK_HELPERS_HPP |