From 134b343711d3be297ebccbeae896c24b4b7152e9 Mon Sep 17 00:00:00 2001 From: jez_higgins <> Date: Tue, 22 Nov 2005 12:57:45 +0000 Subject: [PATCH] Finally took my own advice on how to write custom streams :) See http://www.jezuk.co.uk/accu2005/stream-a-poloza/html/ from about slide 30 --- Utils/socket_stream.h | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Utils/socket_stream.h b/Utils/socket_stream.h index 27ec3bab..312747eb 100644 --- a/Utils/socket_stream.h +++ b/Utils/socket_stream.h @@ -335,7 +335,24 @@ int basic_socketbuf::closeSocket(int sock) const /////////////////////////////////////////////////////////// // basic_socketstream declaration template -class basic_socketstream : public std::basic_iostream +class socketstreambuf_init +{ +public: + typedef basic_socketbuf sockbuf; + + sockbuf* buf() const + { + return &buf_; + } // buf() + +private: + mutable sockbuf buf_; +}; // class socketstreambuf_init + +template +class basic_socketstream : + private virtual socketstreambuf_init, + public std::basic_iostream { public: #ifndef ARABICA_VS6_WORKAROUND @@ -352,25 +369,22 @@ class basic_socketstream : public std::basic_iostream bool is_open() const; void open(const char* hostname, unsigned short port); void close(); - - private: - basic_socketbuf sockbuf; }; // class basic_socketstream //////////////////////////////////////////////////////////////// // basic_socketstream definition template basic_socketstream::basic_socketstream() : - std::basic_iostream( 0 ) + socketstreambuf_init(), + std::basic_iostream(socketstreambuf_init::buf()) { - init( &sockbuf ); } // basic_socketstream template basic_socketstream::basic_socketstream(const char* hostname, int port) : - std::basic_iostream( 0 ) + socketstreambuf_init(), + std::basic_iostream(socketstreambuf_init::buf()) { - init( &sockbuf ); open(hostname, port); } // basic_socketstream @@ -382,19 +396,19 @@ basic_socketstream::~basic_socketstream() template basic_socketbuf* basic_socketstream::rdbuf() const { - return const_cast* >(&sockbuf); + return socketstreambuf_init::buf(); } // rdbuf template bool basic_socketstream::is_open() const { - return sockbuf.is_open(); + return socketstreambuf_init::buf()->is_open(); } // is_open template void basic_socketstream::open(const char* hostname, unsigned short port) { - if(sockbuf.open(hostname, port) == 0) + if(socketstreambuf_init::buf()->open(hostname, port) == 0) setstate(badbit); } // open @@ -404,7 +418,7 @@ void basic_socketstream::close() if(!is_open()) return; - if(sockbuf.close() == 0) + if(socketstreambuf_init::buf()->close() == 0) setstate(badbit); } // close