comparison DEPENDENCIES/generic/include/boost/log/detail/attachable_sstream_buf.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 /*
2 * Copyright Andrey Semashev 2007 - 2013.
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)
6 */
7 /*!
8 * \file attachable_sstream_buf.hpp
9 * \author Andrey Semashev
10 * \date 29.07.2007
11 *
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.
14 */
15
16 #ifndef BOOST_LOG_ATTACHABLE_SSTREAM_BUF_HPP_INCLUDED_
17 #define BOOST_LOG_ATTACHABLE_SSTREAM_BUF_HPP_INCLUDED_
18
19 #include <memory>
20 #include <string>
21 #include <streambuf>
22 #include <boost/assert.hpp>
23 #include <boost/utility/addressof.hpp>
24 #include <boost/log/detail/config.hpp>
25 #include <boost/log/detail/header.hpp>
26
27 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #pragma once
29 #endif
30
31 namespace boost {
32
33 BOOST_LOG_OPEN_NAMESPACE
34
35 namespace aux {
36
37 //! A streambuf that puts the formatted data to an external string
38 template<
39 typename CharT,
40 typename TraitsT = std::char_traits< CharT >,
41 typename AllocatorT = std::allocator< CharT >
42 >
43 class basic_ostringstreambuf :
44 public std::basic_streambuf< CharT, TraitsT >
45 {
46 //! Self type
47 typedef basic_ostringstreambuf< CharT, TraitsT, AllocatorT > this_type;
48 //! Base type
49 typedef std::basic_streambuf< CharT, TraitsT > base_type;
50
51 //! Buffer size
52 enum { buffer_size = 16 };
53
54 public:
55 //! Character type
56 typedef typename base_type::char_type char_type;
57 //! Traits type
58 typedef typename base_type::traits_type traits_type;
59 //! String type
60 typedef std::basic_string< char_type, traits_type, AllocatorT > string_type;
61 //! Int type
62 typedef typename base_type::int_type int_type;
63
64 private:
65 //! A reference to the string that will be filled
66 string_type* m_Storage;
67 //! A buffer used to temporarily store output
68 char_type m_Buffer[buffer_size];
69
70 public:
71 //! Constructor
72 explicit basic_ostringstreambuf() : m_Storage(0)
73 {
74 base_type::setp(m_Buffer, m_Buffer + (sizeof(m_Buffer) / sizeof(*m_Buffer)));
75 }
76 //! Constructor
77 explicit basic_ostringstreambuf(string_type& storage) : m_Storage(boost::addressof(storage))
78 {
79 base_type::setp(m_Buffer, m_Buffer + (sizeof(m_Buffer) / sizeof(*m_Buffer)));
80 }
81
82 //! Clears the buffer to the initial state
83 void clear()
84 {
85 register char_type* pBase = this->pbase();
86 register char_type* pPtr = this->pptr();
87 if (pBase != pPtr)
88 this->pbump(static_cast< int >(pBase - pPtr));
89 }
90
91 //! Detaches the buffer from the string
92 void detach()
93 {
94 if (m_Storage)
95 {
96 this_type::sync();
97 m_Storage = 0;
98 }
99 }
100
101 //! Attaches the buffer to another string
102 void attach(string_type& storage)
103 {
104 detach();
105 m_Storage = boost::addressof(storage);
106 }
107
108 //! Returns a pointer to the attached string
109 string_type* storage() const { return m_Storage; }
110
111 protected:
112 //! Puts all buffered data to the string
113 int sync()
114 {
115 BOOST_ASSERT(m_Storage != 0);
116 register char_type* pBase = this->pbase();
117 register char_type* pPtr = this->pptr();
118 if (pBase != pPtr)
119 {
120 m_Storage->append(pBase, pPtr);
121 this->pbump(static_cast< int >(pBase - pPtr));
122 }
123 return 0;
124 }
125 //! Puts an unbuffered character to the string
126 int_type overflow(int_type c)
127 {
128 BOOST_ASSERT(m_Storage != 0);
129 basic_ostringstreambuf::sync();
130 if (!traits_type::eq_int_type(c, traits_type::eof()))
131 {
132 m_Storage->push_back(traits_type::to_char_type(c));
133 return c;
134 }
135 else
136 return traits_type::not_eof(c);
137 }
138 //! Puts a character sequence to the string
139 std::streamsize xsputn(const char_type* s, std::streamsize n)
140 {
141 BOOST_ASSERT(m_Storage != 0);
142 basic_ostringstreambuf::sync();
143 typedef typename string_type::size_type string_size_type;
144 register const string_size_type max_storage_left =
145 m_Storage->max_size() - m_Storage->size();
146 if (static_cast< string_size_type >(n) < max_storage_left)
147 {
148 m_Storage->append(s, static_cast< string_size_type >(n));
149 return n;
150 }
151 else
152 {
153 m_Storage->append(s, max_storage_left);
154 return static_cast< std::streamsize >(max_storage_left);
155 }
156 }
157
158 //! Copy constructor (closed)
159 BOOST_DELETED_FUNCTION(basic_ostringstreambuf(basic_ostringstreambuf const& that))
160 //! Assignment (closed)
161 BOOST_DELETED_FUNCTION(basic_ostringstreambuf& operator= (basic_ostringstreambuf const& that))
162 };
163
164 } // namespace aux
165
166 BOOST_LOG_CLOSE_NAMESPACE // namespace log
167
168 } // namespace boost
169
170 #include <boost/log/detail/footer.hpp>
171
172 #endif // BOOST_LOG_ATTACHABLE_SSTREAM_BUF_HPP_INCLUDED_