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. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #ifndef NDN_BLOB_STREAM_HPP |
| 9 | #define NDN_BLOB_STREAM_HPP |
| 10 | |
| 11 | // We can use ndnboost::iostreams because this is internal and will not conflict with the application if it uses boost::iostreams. |
| 12 | #include <ndnboost/iostreams/detail/ios.hpp> |
| 13 | #include <ndnboost/iostreams/categories.hpp> |
| 14 | #include <ndnboost/iostreams/stream.hpp> |
| 15 | #include <ndn-cpp/common.hpp> |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | class blob_append_device { |
| 20 | public: |
| 21 | typedef char char_type; |
| 22 | typedef ndnboost::iostreams::sink_tag category; |
| 23 | |
| 24 | blob_append_device(std::vector<uint8_t>& container) |
| 25 | : container_(container) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | std::streamsize |
| 30 | write(const char_type* s, std::streamsize n) |
| 31 | { |
| 32 | std::copy(s, s+n, std::back_inserter(container_)); |
| 33 | return n; |
| 34 | } |
| 35 | |
| 36 | protected: |
| 37 | std::vector<uint8_t>& container_; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * 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>. |
| 42 | * This is inteded for internal library use, not exported in the API. |
| 43 | */ |
| 44 | struct blob_stream : public ndnboost::iostreams::stream<blob_append_device> |
| 45 | { |
| 46 | blob_stream() |
| 47 | : buffer_(new std::vector<uint8_t>()) |
| 48 | , device_(*buffer_) |
| 49 | { |
| 50 | open(device_); |
| 51 | } |
| 52 | |
| 53 | ptr_lib::shared_ptr<std::vector<uint8_t> > |
| 54 | buf() |
| 55 | { |
| 56 | flush(); |
| 57 | return buffer_; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | ptr_lib::shared_ptr<std::vector<uint8_t> > buffer_; |
| 62 | blob_append_device device_; |
| 63 | }; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | #endif |