blob: be767047bd923631d3487304b56e1df204d57d34 [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/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 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
29#include <boost/iostreams/detail/ios.hpp>
30#include <boost/iostreams/categories.hpp>
31#include <boost/iostreams/stream.hpp>
32
33namespace ndn {
34
35/// @cond include_hidden
36namespace iostreams
37{
38
39class buffer_append_device
40{
41public:
42 typedef char char_type;
43 typedef boost::iostreams::sink_tag category;
44
45 buffer_append_device(Buffer& container)
46 : m_container(container)
47 {
48 }
49
50 std::streamsize
51 write(const char_type* s, std::streamsize n)
52 {
53 std::copy(s, s+n, std::back_inserter(m_container));
54 return n;
55 }
56
57protected:
58 Buffer& m_container;
59};
60
61} // iostreams
62/// @endcond
63
64/**
65 * Class implementing interface similar to ostringstream, but to construct ndn::Buffer
66 *
67 * The benefit of using stream interface is that it provides automatic buffering of
68 * written data and eliminates (or reduces) overhead of resizing the underlying buffer
69 * when writing small pieces of data.
70 *
71 * Usage example:
72 * @code
73 * OBufferStream obuf;
74 * obuf.put(0);
75 * obuf.write(another_buffer, another_buffer_size);
76 * shared_ptr<Buffer> buf = obuf.get();
77 * @endcode
78 */
79class OBufferStream : public boost::iostreams::stream<iostreams::buffer_append_device>
80{
81public:
82 /**
83 * Default constructor
84 */
85 OBufferStream()
86 : m_buffer(make_shared<Buffer>())
87 , m_device(*m_buffer)
88 {
89 open(m_device);
90 }
91
92 /**
93 * Flush written data to the stream and return shared pointer to the underlying buffer
94 */
95 shared_ptr<Buffer>
96 buf()
97 {
98 flush();
99 return m_buffer;
100 }
101
102private:
103 BufferPtr m_buffer;
104 iostreams::buffer_append_device m_device;
105};
106
107} // ndn
108
109#endif // NDN_ENCODING_BUFFER_STREAM_HPP