Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00030 #include <cassert>
00031
00032
00038 template< typename Stream >
00039 claw::buffered_ostream<Stream>::buffered_ostream
00040 ( stream_type& f, unsigned int buffer_size )
00041 : m_stream(f), m_begin(new char[buffer_size]), m_end(m_begin+buffer_size),
00042 m_current(m_begin)
00043 {
00044
00045 }
00046
00047
00051 template< typename Stream >
00052 claw::buffered_ostream<Stream>::~buffered_ostream()
00053 {
00054 flush();
00055 delete[] m_begin;
00056 }
00057
00058
00063 template< typename Stream >
00064 template<typename T>
00065 void claw::buffered_ostream<Stream>::write( T v )
00066 {
00067 write( reinterpret_cast<const char*>(&v), sizeof(v) );
00068 }
00069
00070
00076 template< typename Stream >
00077 void claw::buffered_ostream<Stream>::write(const char* p, unsigned int n )
00078 {
00079 while (n > 0)
00080 {
00081 unsigned int q = std::min( n, (unsigned int)(m_end - m_current) );
00082 const char* end = p+q;
00083
00084 for ( ; p!=end ; ++p, ++m_current )
00085 *m_current = *p;
00086
00087 n -= q;
00088
00089 if (m_current == m_end)
00090 flush();
00091 }
00092 }
00093
00094
00098 template< typename Stream >
00099 void claw::buffered_ostream<Stream>::flush()
00100 {
00101 if (m_current != m_begin)
00102 {
00103 m_stream.write( m_begin, m_current - m_begin );
00104 m_current = m_begin;
00105 }
00106 }