Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 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. |
| 8 | * |
| 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> |
| 13 | */ |
| 14 | |
| 15 | #ifndef NDN_ENCODING_BUFFER_STREAM_HPP |
| 16 | #define NDN_ENCODING_BUFFER_STREAM_HPP |
| 17 | |
| 18 | #include "buffer.hpp" |
| 19 | |
| 20 | #include <boost/iostreams/detail/ios.hpp> |
| 21 | #include <boost/iostreams/categories.hpp> |
| 22 | #include <boost/iostreams/stream.hpp> |
| 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | /// @cond include_hidden |
| 27 | namespace iostreams |
| 28 | { |
| 29 | |
| 30 | class buffer_append_device |
| 31 | { |
| 32 | public: |
| 33 | typedef char char_type; |
| 34 | typedef boost::iostreams::sink_tag category; |
| 35 | |
| 36 | buffer_append_device(Buffer& container) |
| 37 | : m_container(container) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | std::streamsize |
| 42 | write(const char_type* s, std::streamsize n) |
| 43 | { |
| 44 | std::copy(s, s+n, std::back_inserter(m_container)); |
| 45 | return n; |
| 46 | } |
| 47 | |
| 48 | protected: |
| 49 | Buffer& m_container; |
| 50 | }; |
| 51 | |
| 52 | } // iostreams |
| 53 | /// @endcond |
| 54 | |
| 55 | /** |
| 56 | * Class implementing interface similar to ostringstream, but to construct ndn::Buffer |
| 57 | * |
| 58 | * 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. |
| 61 | * |
| 62 | * Usage example: |
| 63 | * @code |
| 64 | * OBufferStream obuf; |
| 65 | * obuf.put(0); |
| 66 | * obuf.write(another_buffer, another_buffer_size); |
| 67 | * shared_ptr<Buffer> buf = obuf.get(); |
| 68 | * @endcode |
| 69 | */ |
| 70 | class OBufferStream : public boost::iostreams::stream<iostreams::buffer_append_device> |
| 71 | { |
| 72 | public: |
| 73 | /** |
| 74 | * Default constructor |
| 75 | */ |
| 76 | OBufferStream() |
| 77 | : m_buffer(make_shared<Buffer>()) |
| 78 | , m_device(*m_buffer) |
| 79 | { |
| 80 | open(m_device); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Flush written data to the stream and return shared pointer to the underlying buffer |
| 85 | */ |
| 86 | shared_ptr<Buffer> |
| 87 | buf() |
| 88 | { |
| 89 | flush(); |
| 90 | return m_buffer; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | BufferPtr m_buffer; |
| 95 | iostreams::buffer_append_device m_device; |
| 96 | }; |
| 97 | |
| 98 | } // ndn |
| 99 | |
| 100 | #endif // NDN_ENCODING_BUFFER_STREAM_HPP |