1 /*************************************************************************
6 * [2011] - [2016] Realm Inc
9 * NOTICE: All information contained herein is, and remains
10 * the property of Realm Incorporated and its suppliers,
11 * if any. The intellectual and technical concepts contained
12 * herein are proprietary to Realm Incorporated
13 * and its suppliers and may be covered by U.S. and Foreign Patents,
14 * patents in process, and are protected by trade secret or copyright law.
15 * Dissemination of this information or reproduction of this material
16 * is strictly forbidden unless prior written permission is obtained
17 * from Realm Incorporated.
19 **************************************************************************/
21 #ifndef REALM_UTIL_BUFFER_STREAM_HPP
22 #define REALM_UTIL_BUFFER_STREAM_HPP
30 template<class C, class T = std::char_traits<C>, class A = std::allocator<C> >
31 class BasicResettableExpandableOutputStreambuf: public std::basic_stringbuf<C,T,A> {
33 using char_type = typename std::basic_stringbuf<C,T,A>::char_type;
35 /// Reset current writing position (std::basic_streambuf::pptr()) to the
36 /// beginning of the output buffer without reallocating buffer memory.
37 void reset() noexcept;
39 /// Get a pointer to the beginning of the output buffer
40 /// (std::basic_streambuf::pbase()). Note that this will change as the
41 /// buffer is reallocated.
42 char_type* data() noexcept;
43 const char_type* data() const noexcept;
45 /// Get the number of bytes written to the output buffer since the creation
46 /// of the stream buffer, or since the last invocation of reset()
47 /// (std::basic_streambuf::pptr() - std::basic_streambuf::pbase()).
48 std::streamsize size() const noexcept;
52 template<class C, class T = std::char_traits<C>, class A = std::allocator<C> >
53 class BasicResettableExpandableBufferOutputStream: public std::basic_ostream<C,T> {
55 using char_type = typename std::basic_ostream<C,T>::char_type;
57 BasicResettableExpandableBufferOutputStream();
59 /// Calls BasicResettableExpandableOutputStreambuf::reset().
60 void reset() noexcept;
62 /// Calls BasicResettableExpandableOutputStreambuf::data().
63 char_type* data() noexcept;
64 const char_type* data() const noexcept;
66 /// Calls BasicResettableExpandableOutputStreambuf::size().
67 std::streamsize size() const noexcept;
70 BasicResettableExpandableOutputStreambuf<C,T,A> m_streambuf;
74 using ResettableExpandableBufferOutputStream = BasicResettableExpandableBufferOutputStream<char>;
81 template<class C, class T, class A>
82 inline void BasicResettableExpandableOutputStreambuf<C,T,A>::reset() noexcept
84 char_type* pbeg = this->pbase();
85 char_type* pend = this->epptr();
86 this->setp(pbeg, pend);
89 template<class C, class T, class A>
90 inline typename BasicResettableExpandableOutputStreambuf<C,T,A>::char_type*
91 BasicResettableExpandableOutputStreambuf<C,T,A>::data() noexcept
96 template<class C, class T, class A>
97 inline const typename BasicResettableExpandableOutputStreambuf<C,T,A>::char_type*
98 BasicResettableExpandableOutputStreambuf<C,T,A>::data() const noexcept
100 return this->pbase();
103 template<class C, class T, class A>
104 inline std::streamsize BasicResettableExpandableOutputStreambuf<C,T,A>::size() const noexcept
106 std::streamsize s = std::streamsize(this->pptr() - this->pbase());
110 template<class C, class T, class A>
111 inline BasicResettableExpandableBufferOutputStream<C,T,A>::
112 BasicResettableExpandableBufferOutputStream():
113 std::basic_ostream<C,T>(&m_streambuf) // Throws
117 template<class C, class T, class A>
118 inline void BasicResettableExpandableBufferOutputStream<C,T,A>::reset() noexcept
123 template<class C, class T, class A>
124 inline typename BasicResettableExpandableBufferOutputStream<C,T,A>::char_type*
125 BasicResettableExpandableBufferOutputStream<C,T,A>::data() noexcept
127 return m_streambuf.data();
130 template<class C, class T, class A>
131 inline const typename BasicResettableExpandableBufferOutputStream<C,T,A>::char_type*
132 BasicResettableExpandableBufferOutputStream<C,T,A>::data() const noexcept
134 return m_streambuf.data();
137 template<class C, class T, class A>
138 inline std::streamsize BasicResettableExpandableBufferOutputStream<C,T,A>::size() const noexcept
140 return m_streambuf.size();
146 #endif // REALM_UTIL_BUFFER_STREAM_HPP