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