blob: e7dc60f8981c27e1baed38d814a941e0428e1d3a [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07002/**
Junxiao Shia88c9c72016-09-04 04:30:23 +00003 * Copyright (c) 2013-2016 Regents of the University of California.
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -07006 *
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 Afanasyev258ec2b2014-05-14 16:15:37 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 */
23
24#ifndef NDN_ENCODING_BUFFER_STREAM_HPP
25#define NDN_ENCODING_BUFFER_STREAM_HPP
26
27#include "buffer.hpp"
28
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070029#include <boost/iostreams/categories.hpp>
30#include <boost/iostreams/stream.hpp>
31
32namespace ndn {
33
Junxiao Shia88c9c72016-09-04 04:30:23 +000034namespace detail {
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070035
Junxiao Shia88c9c72016-09-04 04:30:23 +000036/** @brief (implementation detail) a Boost.Iostreams.Sink which appends to an \p ndn::Buffer
37 */
38class BufferAppendDevice
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070039{
40public:
41 typedef char char_type;
42 typedef boost::iostreams::sink_tag category;
43
Junxiao Shia88c9c72016-09-04 04:30:23 +000044 explicit
45 BufferAppendDevice(Buffer& container);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070046
47 std::streamsize
Junxiao Shia88c9c72016-09-04 04:30:23 +000048 write(const char_type* s, std::streamsize n);
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070049
50protected:
51 Buffer& m_container;
52};
53
Junxiao Shia88c9c72016-09-04 04:30:23 +000054} // namespace detail
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070055
Junxiao Shia88c9c72016-09-04 04:30:23 +000056/** @brief implements an output stream that constructs \p ndn::Buffer
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070057 *
Junxiao Shia88c9c72016-09-04 04:30:23 +000058 * The benefit of using stream interface is that it provides automatic buffering of
59 * written data and eliminates (or reduces) overhead of resizing the underlying buffer
60 * when writing small pieces of data.
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070061 *
Junxiao Shia88c9c72016-09-04 04:30:23 +000062 * Usage example:
63 * @code
64 * OBufferStream obuf;
65 * obuf.put(0);
66 * obuf.write(anotherBuffer, anotherBufferSize);
67 * shared_ptr<Buffer> buf = obuf.buf();
68 * @endcode
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070069 */
Junxiao Shia88c9c72016-09-04 04:30:23 +000070class OBufferStream : public boost::iostreams::stream<detail::BufferAppendDevice>
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070071{
72public:
Junxiao Shia88c9c72016-09-04 04:30:23 +000073 OBufferStream();
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070074
Junxiao Shi02b51252016-09-04 04:30:31 +000075 ~OBufferStream();
76
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070077 /**
78 * Flush written data to the stream and return shared pointer to the underlying buffer
79 */
80 shared_ptr<Buffer>
Junxiao Shia88c9c72016-09-04 04:30:23 +000081 buf();
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070082
83private:
84 BufferPtr m_buffer;
Junxiao Shia88c9c72016-09-04 04:30:23 +000085 detail::BufferAppendDevice m_device;
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070086};
87
Junxiao Shia88c9c72016-09-04 04:30:23 +000088} // namespace ndn
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070089
90#endif // NDN_ENCODING_BUFFER_STREAM_HPP