Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 20 | * |
| 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 Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 29 | #include <boost/iostreams/categories.hpp> |
| 30 | #include <boost/iostreams/stream.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 34 | namespace detail { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 35 | |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 36 | /** @brief (implementation detail) a Boost.Iostreams.Sink which appends to an \p ndn::Buffer |
| 37 | */ |
| 38 | class BufferAppendDevice |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
| 41 | typedef char char_type; |
| 42 | typedef boost::iostreams::sink_tag category; |
| 43 | |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 44 | explicit |
| 45 | BufferAppendDevice(Buffer& container); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 46 | |
| 47 | std::streamsize |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 48 | write(const char_type* s, std::streamsize n); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 49 | |
| 50 | protected: |
| 51 | Buffer& m_container; |
| 52 | }; |
| 53 | |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 54 | } // namespace detail |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 55 | |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 56 | /** @brief implements an output stream that constructs \p ndn::Buffer |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 57 | * |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 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. |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 61 | * |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 62 | * 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 Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 69 | */ |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 70 | class OBufferStream : public boost::iostreams::stream<detail::BufferAppendDevice> |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 71 | { |
| 72 | public: |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 73 | OBufferStream(); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Flush written data to the stream and return shared pointer to the underlying buffer |
| 77 | */ |
| 78 | shared_ptr<Buffer> |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 79 | buf(); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | BufferPtr m_buffer; |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 83 | detail::BufferAppendDevice m_device; |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Junxiao Shi | a88c9c7 | 2016-09-04 04:30:23 +0000 | [diff] [blame^] | 86 | } // namespace ndn |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 87 | |
| 88 | #endif // NDN_ENCODING_BUFFER_STREAM_HPP |