blob-stream.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
23 #ifndef NDN_BLOB_STREAM_HPP
24 #define NDN_BLOB_STREAM_HPP
25 
26 // We can use ndnboost::iostreams because this is internal and will not conflict with the application if it uses boost::iostreams.
27 #include <ndnboost/iostreams/detail/ios.hpp>
28 #include <ndnboost/iostreams/categories.hpp>
29 #include <ndnboost/iostreams/stream.hpp>
30 #include <ndn-cpp/common.hpp>
31 
32 namespace ndn {
33 
35 public:
36  typedef char char_type;
37  typedef ndnboost::iostreams::sink_tag category;
38 
39  blob_append_device(std::vector<uint8_t>& container)
40  : container_(container)
41  {
42  }
43 
44  std::streamsize
45  write(const char_type* s, std::streamsize n)
46  {
47  std::copy(s, s+n, std::back_inserter(container_));
48  return n;
49  }
50 
51 protected:
52  std::vector<uint8_t>& container_;
53 };
54 
59 struct blob_stream : public ndnboost::iostreams::stream<blob_append_device>
60 {
61  blob_stream()
62  : buffer_(new std::vector<uint8_t>())
63  , device_(*buffer_)
64  {
65  open(device_);
66  }
67 
68  ptr_lib::shared_ptr<std::vector<uint8_t> >
69  buf()
70  {
71  flush();
72  return buffer_;
73  }
74 
75 private:
76  ptr_lib::shared_ptr<std::vector<uint8_t> > buffer_;
77  blob_append_device device_;
78 };
79 
80 }
81 
82 #endif
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
Definition: blob-stream.hpp:34
This is called "blob_stream" but it doesn't use an ndn::Blob which is immutable.
Definition: blob-stream.hpp:59