Jeff Thompson | 68192a3 | 2013-10-17 17:34:17 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
Jeff Thompson | e37aa21 | 2013-10-17 17:53:23 -0700 | [diff] [blame] | 4 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Jeff Thompson | 68192a3 | 2013-10-17 17:34:17 -0700 | [diff] [blame] | 5 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 6 | * See COPYING for copyright and distribution information. |
| 7 | */ |
| 8 | |
| 9 | #ifndef NDN_BLOB_STREAM_HPP |
| 10 | #define NDN_BLOB_STREAM_HPP |
| 11 | |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame^] | 12 | #include <ndn-cpp-dev/common.hpp> |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 13 | |
| 14 | #if NDN_CPP_USE_SYSTEM_BOOST |
| 15 | #include <boost/iostreams/detail/ios.hpp> |
| 16 | #include <boost/iostreams/categories.hpp> |
| 17 | #include <boost/iostreams/stream.hpp> |
| 18 | namespace ndnboost = boost; |
| 19 | #else |
Jeff Thompson | 68192a3 | 2013-10-17 17:34:17 -0700 | [diff] [blame] | 20 | // We can use ndnboost::iostreams because this is internal and will not conflict with the application if it uses boost::iostreams. |
| 21 | #include <ndnboost/iostreams/detail/ios.hpp> |
| 22 | #include <ndnboost/iostreams/categories.hpp> |
| 23 | #include <ndnboost/iostreams/stream.hpp> |
Alexander Afanasyev | 8e96e58 | 2013-11-19 12:07:04 -0800 | [diff] [blame] | 24 | #endif |
Jeff Thompson | 68192a3 | 2013-10-17 17:34:17 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | |
| 28 | class blob_append_device { |
| 29 | public: |
| 30 | typedef char char_type; |
| 31 | typedef ndnboost::iostreams::sink_tag category; |
| 32 | |
| 33 | blob_append_device(std::vector<uint8_t>& container) |
| 34 | : container_(container) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | std::streamsize |
| 39 | write(const char_type* s, std::streamsize n) |
| 40 | { |
| 41 | std::copy(s, s+n, std::back_inserter(container_)); |
| 42 | return n; |
| 43 | } |
| 44 | |
| 45 | protected: |
| 46 | std::vector<uint8_t>& container_; |
| 47 | }; |
| 48 | |
| 49 | /** |
| 50 | * This is called "blob_stream" but it doesn't use an ndn::Blob which is immutable. It uses a pointer to a vector<uint8_t>. |
| 51 | * This is inteded for internal library use, not exported in the API. |
| 52 | */ |
| 53 | struct blob_stream : public ndnboost::iostreams::stream<blob_append_device> |
| 54 | { |
| 55 | blob_stream() |
| 56 | : buffer_(new std::vector<uint8_t>()) |
| 57 | , device_(*buffer_) |
| 58 | { |
| 59 | open(device_); |
| 60 | } |
| 61 | |
| 62 | ptr_lib::shared_ptr<std::vector<uint8_t> > |
| 63 | buf() |
| 64 | { |
| 65 | flush(); |
| 66 | return buffer_; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | ptr_lib::shared_ptr<std::vector<uint8_t> > buffer_; |
| 71 | blob_append_device device_; |
| 72 | }; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | #endif |