From 6b13cd102b77d518b24521dccdd50676eb22ff39 Mon Sep 17 00:00:00 2001 From: Antoine Chavasse Date: Sun, 6 May 2007 16:09:34 +0200 Subject: [PATCH] IO: added BufferedSeekableOutputStream --- abf/write-context.h | 2 +- abf/write-digest/digest-writer.h | 4 ++-- abf/writer-interface.h | 8 +++++--- io/BufferedOutputStream.h | 43 ++++++++++++++++++++++++++++++++++------ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/abf/write-context.h b/abf/write-context.h index fb9af11..b4ccf7b 100644 --- a/abf/write-context.h +++ b/abf/write-context.h @@ -7,7 +7,7 @@ namespace awful { namespace abfImpl { template< class EndianessPolicy > struct WriteContext { - typedef io::BufferedOutputStream< EndianessPolicy > stream_type; + typedef io::BufferedSeekableOutputStream< EndianessPolicy > stream_type; WriteContext( stream_type& s ) : stream( s ) {} stream_type& stream; diff --git a/abf/write-digest/digest-writer.h b/abf/write-digest/digest-writer.h index c3a3097..bf9cabc 100644 --- a/abf/write-digest/digest-writer.h +++ b/abf/write-digest/digest-writer.h @@ -10,7 +10,7 @@ namespace awful { namespace abfImpl template< class EndianessPolicy > class DigestWriter { public: - DigestWriter( io::BufferedOutputStream< EndianessPolicy >& Stream_ ) : + DigestWriter( io::BufferedSeekableOutputStream< EndianessPolicy >& Stream_ ) : m_Stream( Stream_ ) { } @@ -27,7 +27,7 @@ namespace awful { namespace abfImpl } private: - io::BufferedOutputStream< EndianessPolicy >& m_Stream; + io::BufferedSeekableOutputStream< EndianessPolicy >& m_Stream; std::set< unsigned long long > m_Digests; std::set< const void* > m_Objects; }; diff --git a/abf/writer-interface.h b/abf/writer-interface.h index f3659f3..7b05607 100644 --- a/abf/writer-interface.h +++ b/abf/writer-interface.h @@ -2,6 +2,7 @@ #define AWFUL_ABF_WRITER_INTERFACE_H #include "core/core.h" +#include "io/SeekableOutputStream.h" #include "io/BufferedOutputStream.h" #include "write-context.h" @@ -12,7 +13,7 @@ namespace awful { namespace abf public: static const unsigned short version = 1; - GenericWriter( io::OutputStream* pStream_ ) : + GenericWriter( io::SeekableOutputStream* pStream_ ) : m_Stream( pStream_ ) { m_Stream.writeEndianessHeader(); @@ -38,7 +39,7 @@ namespace awful { namespace abf void writeString( const char* pStr_ ); private: - io::BufferedOutputStream< EndianessPolicy > m_Stream; + io::BufferedSeekableOutputStream< EndianessPolicy > m_Stream; }; // Native endianess writer. I don't use endianess::Native here because it would cause @@ -54,7 +55,8 @@ namespace awful { namespace abf typedef GenericWriter< endianess::Big > WriterBigEndian; typedef GenericWriter< endianess::Little > WriterLittleEndian; - template< class EP, class C > void Save( abfImpl::WriteContext< EP >& w, const C* pObj ) FORCE_SYMBOL_USE; + template< class EP, class C > void Save( abfImpl::WriteContext< EP >& w, const C* pObj ) + FORCE_SYMBOL_USE; }} #endif diff --git a/io/BufferedOutputStream.h b/io/BufferedOutputStream.h index 4a871ee..853da00 100644 --- a/io/BufferedOutputStream.h +++ b/io/BufferedOutputStream.h @@ -13,11 +13,12 @@ namespace awful { namespace io // Premature optimisation? // possibly. The generated serialization code from abf will have to be checked to see if // it actually turns out like that. - template< class EndianessPolicy = endianess::Native, int BufferSize = 4096 > + template< class EndianessPolicy = endianess::Native, int BufferSize = 4096, + class StreamType = OutputStream > class BufferedOutputStream : virtual public RefCounted { public: - BufferedOutputStream( OutputStream* pOutStream_ ) : + BufferedOutputStream( StreamType* pOutStream_ ) : m_pOutStream( pOutStream_ ) { m_WriteIndex = 0; @@ -28,8 +29,8 @@ namespace awful { namespace io flush(); } - const Pointer< OutputStream >& getOutStream() const { return m_pOutStream; } - void setOutStream( const Pointer< OutputStream >& s ) { m_pOutStream = s; } + const Pointer< StreamType >& getOutStream() const { return m_pOutStream; } + void setOutStream( const Pointer< StreamType >& s ) { m_pOutStream = s; } unsigned int used() const { @@ -134,12 +135,42 @@ namespace awful { namespace io writeU8( out ); } - private: - Pointer< OutputStream > m_pOutStream; + protected: + Pointer< StreamType > m_pOutStream; unsigned char m_WriteIndex; unsigned char m_Buffer[ BufferSize ]; }; + + template< class EndianessPolicy = endianess::Native, int BufferSize = 4096 > + class BufferedSeekableOutputStream : + public BufferedOutputStream< EndianessPolicy, BufferSize, SeekableOutputStream > + { + typedef BufferedOutputStream< EndianessPolicy, BufferSize, SeekableOutputStream > + base; + public: + BufferedSeekableOutputStream( SeekableOutputStream* pOutStream_ ) : + base( pOutStream_ ) + { + } + + void seek( unsigned long Pos_ ) + { + base::flush(); + base::m_pOutStream->seek( Pos_ ); + } + + void seek( unsigned long long Pos_ ) + { + base::flush(); + base::m_pOutStream->seek( Pos_ ); + } + + unsigned long long tell() const + { + return base::m_pOutStream->tell(); + } + }; }} #endif -- 2.11.4.GIT