2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 contact: julien.jorge@gamned.org
26 * \file buffered_ostream.tpp
27 * \brief Implementation of the claw::buffered_ostream class.
28 * \author Julien Jorge
32/*----------------------------------------------------------------------------*/
35 * \param f The file associated to the stream.
36 * \param buffer_size The size of the buffer.
38template< typename Stream >
39claw::buffered_ostream<Stream>::buffered_ostream
40( stream_type& f, unsigned int buffer_size )
41 : m_stream(f), m_begin(new char[buffer_size]), m_end(m_begin+buffer_size),
45} // buffered_ostream::buffered_ostream()
47/*----------------------------------------------------------------------------*/
51template< typename Stream >
52claw::buffered_ostream<Stream>::~buffered_ostream()
56} // buffered_ostream::~buffered_ostream()
58/*----------------------------------------------------------------------------*/
60 * \brief Write somethnig in the buffer.
61 * \param v The value to write.
63template< typename Stream >
65void claw::buffered_ostream<Stream>::write( T v )
67 write( reinterpret_cast<const char*>(&v), sizeof(v) );
68} // buffered_ostream::read_more()
70/*----------------------------------------------------------------------------*/
72 * \brief Write a range of data in the buffer.
73 * \param p The begining of the range to write.
74 * \param n The length of the buffer pointed by p.
76template< typename Stream >
77void claw::buffered_ostream<Stream>::write(const char* p, unsigned int n )
81 unsigned int q = std::min( n, (unsigned int)(m_end - m_current) );
82 const char* end = p+q;
84 for ( ; p!=end ; ++p, ++m_current )
89 if (m_current == m_end)
92} // buffered_ostream::write()
94/*----------------------------------------------------------------------------*/
96 * \brief Write the data from the buffer in the stream.
98template< typename Stream >
99void claw::buffered_ostream<Stream>::flush()
101 if (m_current != m_begin)
103 m_stream.write( m_begin, m_current - m_begin );
106} // buffered_ostream::flush()