blob: 70ffec8dd3408126ff938609582d5cb3ebe3ab6f [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
3 * Copyright (c) 2013-2017 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_BUFFER_HPP
25#define NDN_ENCODING_BUFFER_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080026
Alexander Afanasyev19508852014-01-29 01:01:51 -080027#include "../common.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080028
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029#include <vector>
30
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080031namespace ndn {
32
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080033/**
Davide Pesavento5d0b0102017-10-07 13:43:16 -040034 * @brief General-purpose automatically managed/resized buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080035 *
Davide Pesavento5d0b0102017-10-07 13:43:16 -040036 * In most respect, the Buffer class is equivalent to a `std::vector<uint8_t>`, and it in fact
37 * uses the latter as a base class. In addition to that, it provides the get<T>() helper method
38 * that automatically casts the returned pointer to the requested type.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080039 */
40class Buffer : public std::vector<uint8_t>
41{
42public:
Davide Pesavento5d0b0102017-10-07 13:43:16 -040043 using std::vector<uint8_t>::vector;
44
45 /** @brief Creates an empty Buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080046 */
Junxiao Shic97d5d42014-11-27 09:33:37 -070047 Buffer();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080048
Davide Pesavento5d0b0102017-10-07 13:43:16 -040049 /** @brief Creates a Buffer by copying contents from a raw buffer
50 * @param buf const pointer to buffer to copy
Junxiao Shic97d5d42014-11-27 09:33:37 -070051 * @param length length of the buffer to copy
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080052 */
Junxiao Shic97d5d42014-11-27 09:33:37 -070053 Buffer(const void* buf, size_t length);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080054
Davide Pesavento5d0b0102017-10-07 13:43:16 -040055 /** @return pointer to the first byte of the buffer, cast to the requested type T
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080056 */
57 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -070058 T*
Davide Pesavento5d0b0102017-10-07 13:43:16 -040059 get() noexcept
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080060 {
Davide Pesavento5d0b0102017-10-07 13:43:16 -040061 return reinterpret_cast<T*>(data());
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080062 }
63
Davide Pesavento5d0b0102017-10-07 13:43:16 -040064 /** @return const pointer to the first byte of the buffer, cast to the requested type T
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080065 */
66 template<class T>
Alexander Afanasyeva465e972014-03-22 17:21:49 -070067 const T*
Davide Pesavento5d0b0102017-10-07 13:43:16 -040068 get() const noexcept
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080069 {
Davide Pesavento5d0b0102017-10-07 13:43:16 -040070 return reinterpret_cast<const T*>(data());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070071 }
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080072};
73
Davide Pesavento5d0b0102017-10-07 13:43:16 -040074using BufferPtr = shared_ptr<Buffer>;
75using ConstBufferPtr = shared_ptr<const Buffer>;
76
Junxiao Shic97d5d42014-11-27 09:33:37 -070077} // namespace ndn
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080078
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070079#endif // NDN_ENCODING_BUFFER_HPP