2 * Copyright Andrey Semashev 2007 - 2016.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file attachable_sstream_buf.hpp
9 * \author Andrey Semashev
12 * \brief This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
16 #ifndef BOOST_LOG_ATTACHABLE_SSTREAM_BUF_HPP_INCLUDED_
17 #define BOOST_LOG_ATTACHABLE_SSTREAM_BUF_HPP_INCLUDED_
24 #include <boost/assert.hpp>
25 #include <boost/mpl/bool.hpp>
26 #include <boost/locale/utf.hpp>
27 #include <boost/log/detail/config.hpp>
28 #include <boost/log/detail/header.hpp>
30 #ifdef BOOST_HAS_PRAGMA_ONCE
36 BOOST_LOG_OPEN_NAMESPACE
40 //! A streambuf that puts the formatted data to an external string
43 typename TraitsT = std::char_traits< CharT >,
44 typename AllocatorT = std::allocator< CharT >
46 class basic_ostringstreambuf :
47 public std::basic_streambuf< CharT, TraitsT >
50 typedef basic_ostringstreambuf< CharT, TraitsT, AllocatorT > this_type;
52 typedef std::basic_streambuf< CharT, TraitsT > base_type;
55 enum { buffer_size = 16 };
59 typedef typename base_type::char_type char_type;
61 typedef typename base_type::traits_type traits_type;
63 typedef std::basic_string< char_type, traits_type, AllocatorT > string_type;
65 typedef typename string_type::size_type size_type;
67 typedef typename base_type::int_type int_type;
71 //! A reference to the string that will be filled
73 //! Max size of the storage, in characters
75 //! Indicates that storage overflow happened
78 BOOST_CONSTEXPR storage_state() BOOST_NOEXCEPT : storage(NULL), max_size(0u), overflow(false)
84 //! Buffer storage state
85 storage_state m_storage_state;
86 //! A buffer used to temporarily store output
87 char_type m_buffer[buffer_size];
91 basic_ostringstreambuf() BOOST_NOEXCEPT
93 base_type::setp(m_buffer, m_buffer + (sizeof(m_buffer) / sizeof(*m_buffer)));
96 explicit basic_ostringstreambuf(string_type& storage) BOOST_NOEXCEPT
98 base_type::setp(m_buffer, m_buffer + (sizeof(m_buffer) / sizeof(*m_buffer)));
102 storage_state const& get_storage_state() const BOOST_NOEXCEPT { return m_storage_state; }
103 void set_storage_state(storage_state const& st) BOOST_NOEXCEPT { m_storage_state = st; }
105 //! Detaches the buffer from the string
108 if (m_storage_state.storage)
111 m_storage_state.storage = NULL;
112 m_storage_state.max_size = 0u;
113 m_storage_state.overflow = false;
117 //! Attaches the buffer to another string
118 void attach(string_type& storage)
120 attach(storage, storage.max_size());
123 //! Attaches the buffer to another string
124 void attach(string_type& storage, size_type max_size)
127 m_storage_state.storage = &storage;
128 this->max_size(max_size);
131 //! Returns a pointer to the attached string
132 string_type* storage() const BOOST_NOEXCEPT { return m_storage_state.storage; }
134 //! Returns the maximum size of the storage
135 size_type max_size() const BOOST_NOEXCEPT { return m_storage_state.max_size; }
136 //! Sets the maximum size of the storage
137 void max_size(size_type size)
139 if (m_storage_state.storage)
141 const size_type storage_max_size = m_storage_state.storage->max_size();
142 size = size > storage_max_size ? storage_max_size : size;
145 m_storage_state.max_size = size;
148 //! Makes sure the storage does not exceed the max size limit. Should be called after the storage is modified externally.
149 void ensure_max_size()
151 if (m_storage_state.storage && m_storage_state.storage->size() > m_storage_state.max_size)
153 const size_type len = length_until_boundary(m_storage_state.storage->c_str(), m_storage_state.storage->size(), m_storage_state.max_size);
154 m_storage_state.storage->resize(len);
155 m_storage_state.overflow = true;
159 //! Returns true if the max size limit has been exceeded
160 bool storage_overflow() const BOOST_NOEXCEPT { return m_storage_state.overflow; }
161 //! Sets the overflow flag
162 void storage_overflow(bool f) BOOST_NOEXCEPT { m_storage_state.overflow = f; }
164 //! Returns the size left in the storage
165 size_type size_left() const BOOST_NOEXCEPT
167 BOOST_ASSERT(m_storage_state.storage != NULL);
169 const size_type size = m_storage_state.storage->size();
170 return size < m_storage_state.max_size ? m_storage_state.max_size - size : static_cast< size_type >(0u);
173 //! Appends a string to the storage and returns the number of written characters
174 size_type append(const char_type* s, size_type n)
176 if (!m_storage_state.overflow)
178 BOOST_ASSERT(m_storage_state.storage != NULL);
180 size_type left = size_left();
181 BOOST_LOG_ASSUME(left <= m_storage_state.storage->max_size());
182 if (BOOST_LIKELY(n <= left))
184 m_storage_state.storage->append(s, n);
189 // We have to find out where the last character that fits before the limit ends
190 left = length_until_boundary(s, n, left);
191 m_storage_state.storage->append(s, left);
192 m_storage_state.overflow = true;
199 //! Appends the specified number of characters to the storage and returns the number of written characters
200 size_type append(size_type n, char_type c)
202 if (!m_storage_state.overflow)
204 BOOST_ASSERT(m_storage_state.storage != NULL);
206 const size_type left = size_left();
207 BOOST_LOG_ASSUME(left <= m_storage_state.storage->max_size());
208 if (BOOST_LIKELY(n <= left))
210 m_storage_state.storage->append(n, c);
215 m_storage_state.storage->append(left, c);
216 m_storage_state.overflow = true;
223 //! Appends a character to the storage and returns the number of written characters
224 size_type push_back(char_type c)
226 if (!m_storage_state.overflow)
228 BOOST_ASSERT(m_storage_state.storage != NULL);
230 BOOST_LOG_ASSUME(m_storage_state.max_size <= m_storage_state.storage->max_size());
231 if (BOOST_LIKELY(m_storage_state.storage->size() < m_storage_state.max_size))
233 m_storage_state.storage->push_back(c);
238 m_storage_state.overflow = true;
246 //! Puts all buffered data to the string
249 char_type* pBase = this->pbase();
250 char_type* pPtr = this->pptr();
253 this->append(pBase, static_cast< size_type >(pPtr - pBase));
254 this->pbump(static_cast< int >(pBase - pPtr));
258 //! Puts an unbuffered character to the string
259 int_type overflow(int_type c)
262 if (!traits_type::eq_int_type(c, traits_type::eof()))
264 this->push_back(traits_type::to_char_type(c));
268 return traits_type::not_eof(c);
270 //! Puts a character sequence to the string
271 std::streamsize xsputn(const char_type* s, std::streamsize n)
274 return static_cast< std::streamsize >(this->append(s, static_cast< size_type >(n)));
277 //! Finds the string length so that it includes only complete characters, and does not exceed \a max_size
278 size_type length_until_boundary(const char_type* s, size_type n, size_type max_size) const
280 return length_until_boundary(s, n, max_size, mpl::bool_< sizeof(char_type) == 1u >());;
283 //! Finds the string length so that it includes only complete characters, and does not exceed \a max_size
284 size_type length_until_boundary(const char_type* s, size_type n, size_type max_size, mpl::true_) const
286 std::locale loc = this->getloc();
287 std::codecvt< wchar_t, char, std::mbstate_t > const& fac = std::use_facet< std::codecvt< wchar_t, char, std::mbstate_t > >(loc);
288 std::mbstate_t mbs = std::mbstate_t();
289 return static_cast< size_type >(fac.length(mbs, s, s + max_size, ~static_cast< std::size_t >(0u)));
292 //! Finds the string length so that it includes only complete characters, and does not exceed \a max_size
293 static size_type length_until_boundary(const char_type* s, size_type n, size_type max_size, mpl::false_)
295 // Note: Although it's not required to be true for wchar_t, here we assume that the string has Unicode encoding.
296 // Compilers use some version of Unicode for wchar_t on all tested platforms, and std::locale doesn't offer a way
297 // to find the character boundary for character types other than char anyway.
298 typedef boost::locale::utf::utf_traits< CharT > utf_traits;
300 size_type pos = max_size;
304 if (utf_traits::is_lead(s[pos]))
306 const char_type* p = s + pos;
307 boost::locale::utf::code_point cp = utf_traits::decode(p, s + n);
308 if (boost::locale::utf::is_valid_codepoint(cp) && p <= (s + max_size))
309 return static_cast< size_type >(p - s);
316 //! Copy constructor (closed)
317 BOOST_DELETED_FUNCTION(basic_ostringstreambuf(basic_ostringstreambuf const& that))
318 //! Assignment (closed)
319 BOOST_DELETED_FUNCTION(basic_ostringstreambuf& operator= (basic_ostringstreambuf const& that))
324 BOOST_LOG_CLOSE_NAMESPACE // namespace log
328 #include <boost/log/detail/footer.hpp>
330 #endif // BOOST_LOG_ATTACHABLE_SSTREAM_BUF_HPP_INCLUDED_