Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -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 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 10 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_NDN_CHARBUF_H |
| 14 | #define NDN_NDN_CHARBUF_H |
| 15 | |
| 16 | #include "ndn.cxx/common.h" |
| 17 | #include <boost/shared_ptr.hpp> |
| 18 | #include <boost/iostreams/detail/ios.hpp> |
| 19 | #include <boost/iostreams/categories.hpp> |
| 20 | #include <boost/iostreams/stream.hpp> |
| 21 | |
| 22 | namespace ndn { |
| 23 | |
| 24 | class Charbuf; |
| 25 | typedef boost::shared_ptr<Charbuf> CharbufPtr; |
| 26 | |
| 27 | // This class is mostly used in Wrapper; users should not be directly using this class |
| 28 | // The main purpose of this class to is avoid manually create and destroy charbuf everytime |
| 29 | class Charbuf |
| 30 | { |
| 31 | public: |
| 32 | Charbuf(); |
| 33 | Charbuf(ccn_charbuf *buf); |
| 34 | Charbuf(const Charbuf &other); |
| 35 | Charbuf(const void *buf, size_t length); |
| 36 | ~Charbuf(); |
| 37 | |
| 38 | // expose internal data structure, use with caution!! |
| 39 | ccn_charbuf * |
| 40 | getBuf() { return m_buf; } |
| 41 | |
| 42 | const ccn_charbuf * |
| 43 | getBuf() const { return m_buf; } |
| 44 | |
| 45 | const unsigned char * |
| 46 | buf () const |
| 47 | { return m_buf->buf; } |
| 48 | |
| 49 | size_t |
| 50 | length () const |
| 51 | { return m_buf->length; } |
| 52 | |
| 53 | private: |
| 54 | void init(ccn_charbuf *buf); |
| 55 | |
| 56 | protected: |
| 57 | ccn_charbuf *m_buf; |
| 58 | }; |
| 59 | |
| 60 | namespace iostreams |
| 61 | { |
| 62 | |
| 63 | class charbuf_append_device { |
| 64 | public: |
| 65 | typedef char char_type; |
| 66 | typedef boost::iostreams::sink_tag category; |
| 67 | |
| 68 | charbuf_append_device (Charbuf& cnt); |
| 69 | |
| 70 | std::streamsize |
| 71 | write(const char_type* s, std::streamsize n); |
| 72 | protected: |
| 73 | Charbuf& container; |
| 74 | }; |
| 75 | |
| 76 | } // iostreams |
| 77 | |
| 78 | struct charbuf_stream : public boost::iostreams::stream<iostreams::charbuf_append_device> |
| 79 | { |
| 80 | charbuf_stream () |
| 81 | : m_device (m_buf) |
| 82 | { |
| 83 | open (m_device); |
| 84 | } |
| 85 | |
| 86 | Charbuf & |
| 87 | buf () |
| 88 | { |
| 89 | flush (); |
| 90 | return m_buf; |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | Charbuf m_buf; |
| 95 | iostreams::charbuf_append_device m_device; |
| 96 | }; |
| 97 | |
| 98 | } // ndn |
| 99 | |
| 100 | #endif // NDN_NDN_CHARBUF_H |