added iOS source code
[wl-app.git] / iOS / Pods / Realm / include / core / realm / util / buffer_stream.hpp
1 /*************************************************************************
2  *
3  * REALM CONFIDENTIAL
4  * __________________
5  *
6  *  [2011] - [2016] Realm Inc
7  *  All Rights Reserved.
8  *
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.
18  *
19  **************************************************************************/
20
21 #ifndef REALM_UTIL_BUFFER_STREAM_HPP
22 #define REALM_UTIL_BUFFER_STREAM_HPP
23
24 #include <sstream>
25
26 namespace realm {
27 namespace util {
28
29
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> {
32 public:
33     using char_type = typename std::basic_stringbuf<C,T,A>::char_type;
34
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;
38
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;
44
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;
49 };
50
51
52 template<class C, class T = std::char_traits<C>, class A = std::allocator<C> >
53 class BasicResettableExpandableBufferOutputStream: public std::basic_ostream<C,T> {
54 public:
55     using char_type = typename std::basic_ostream<C,T>::char_type;
56
57     BasicResettableExpandableBufferOutputStream();
58
59     /// Calls BasicResettableExpandableOutputStreambuf::reset().
60     void reset() noexcept;
61
62     /// Calls BasicResettableExpandableOutputStreambuf::data().
63     char_type* data() noexcept;
64     const char_type* data() const noexcept;
65
66     /// Calls BasicResettableExpandableOutputStreambuf::size().
67     std::streamsize size() const noexcept;
68
69 private:
70     BasicResettableExpandableOutputStreambuf<C,T,A> m_streambuf;
71 };
72
73
74 using ResettableExpandableBufferOutputStream = BasicResettableExpandableBufferOutputStream<char>;
75
76
77
78
79 // Implementation
80
81 template<class C, class T, class A>
82 inline void BasicResettableExpandableOutputStreambuf<C,T,A>::reset() noexcept
83 {
84     char_type* pbeg = this->pbase();
85     char_type* pend = this->epptr();
86     this->setp(pbeg, pend);
87 }
88
89 template<class C, class T, class A>
90 inline typename BasicResettableExpandableOutputStreambuf<C,T,A>::char_type*
91 BasicResettableExpandableOutputStreambuf<C,T,A>::data() noexcept
92 {
93     return this->pbase();
94 }
95
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
99 {
100     return this->pbase();
101 }
102
103 template<class C, class T, class A>
104 inline std::streamsize BasicResettableExpandableOutputStreambuf<C,T,A>::size() const noexcept
105 {
106     std::streamsize s = std::streamsize(this->pptr() - this->pbase());
107     return s;
108 }
109
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
114 {
115 }
116
117 template<class C, class T, class A>
118 inline void BasicResettableExpandableBufferOutputStream<C,T,A>::reset() noexcept
119 {
120     m_streambuf.reset();
121 }
122
123 template<class C, class T, class A>
124 inline typename BasicResettableExpandableBufferOutputStream<C,T,A>::char_type*
125 BasicResettableExpandableBufferOutputStream<C,T,A>::data() noexcept
126 {
127     return m_streambuf.data();
128 }
129
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
133 {
134     return m_streambuf.data();
135 }
136
137 template<class C, class T, class A>
138 inline std::streamsize BasicResettableExpandableBufferOutputStream<C,T,A>::size() const noexcept
139 {
140     return m_streambuf.size();
141 }
142
143 } // namespace util
144 } // namespace realm
145
146 #endif // REALM_UTIL_BUFFER_STREAM_HPP