I would like to write a logging library of my own that provides abstraction for wherever the log entries are sent to.
The IO library of C++ already provides that kind of abstraction with std::stringstream
and std::fstream
. I would also like to be able to read/write from/to a socket.
I read that the proper way of extending the standard library is to inherit from std::basic_streambuf
. What I don't understand is, if inheriting from std::basic_streambuf
like std::basic_filebuf
does, where is the need for the std::ifsream
, std::ofstream
and std::fstream
classes ? Can't I just replace the buffer of some stream with a instance of a subclass of std::basic_streambuf
which outputs where I want it to ?
So far I have done the following, but I really am not sure about what I'm doing. Is the following design correct ?
template< typename char_type, typename traits_type = std::char_traits< char_type > >
class basic_sock_streambuf : public std::basic_streambuf< char_type, traits_type >
{
public:
basic_sock_streambuf()
{
}
~basic_sock_streambuf()
{
}
int overflow (int c = EOF)
{
fputc( c, stdout ); // Temporary.
return traits_type::to_int_type( c );
}
int underflow()
{
return fgetc( stdout ); // Temporary.
}
int sync()
{
return 0;
}
};
template< typename char_type, typename traits_type = std::char_traits< char_type > >
class basic_isockstream : public std::basic_istream< char_type, traits_type >
{
};
template< typename char_type, typename traits_type = std::char_traits< char_type > >
class basic_osockstream : public std::basic_ostream< char_type, traits_type >
{
};
template< typename char_type, typename traits_type = std::char_traits< char_type > >
class basic_socktream : public basic_isockstream< char_type, traits_type >, public basic_osockstream< char_type, traits_type >
{
private:
typedef basic_isockstream< char_type, traits_type > iparent;
typedef basic_osockstream< char_type, traits_type > oparent;
basic_sock_streambuf< char_type, traits_type > sock_sb;
std::basic_streambuf< char_type, traits_type > * old_isb;
std::basic_streambuf< char_type, traits_type > * old_osb;
public:
basic_socktream()
{
old_isb = iparent::rdbuf( & sock_sb );
old_osb = oparent::rdbuf( & sock_sb );
}
~basic_socktream() throw()
{
iparent::rdbuf( old_isb );
oparent::rdbuf( old_osb );
}
};
EDIT : Code updated based on answers :
template<
typename char_type,
typename traits_type = std::char_traits< char_type > >
class basic_sockbuf :
public std::basic_streambuf< char_type, traits_type >
{
public:
basic_sockbuf()
{
}
~basic_sockbuf()
{
}
int overflow( int c = EOF )
{
fputc( c, stdout ); // Temporary.
return traits_type::to_int_type( c );
}
int underflow()
{
return fgetc( stdout ); // Temporary.
}
int sync()
{
return 0;
}
};
template<
typename char_type,
typename traits_type = std::char_traits< char_type > >
class basic_isockstream :
public std::basic_istream< char_type, traits_type >
{
private:
typedef std::basic_istream< char_type, traits_type > parent;
basic_sockbuf< char_type, traits_type > buffer;
public:
basic_isockstream() :
std::basic_istream< char_type, traits_type >::basic_istream(),
buffer()
{
init( & buffer );
}
};
template<
typename char_type,
typename traits_type = std::char_traits< char_type > >
class basic_osockstream :
public std::basic_ostream< char_type, traits_type >
{
private:
basic_sockbuf< char_type, traits_type > buffer;
public:
basic_osockstream() :
std::basic_ostream< char_type, traits_type >::basic_istream(),
buffer()
{
init( & buffer );
}
};
template<
typename char_type,
typename traits_type = std::char_traits< char_type > >
class basic_socktream :
public std::basic_iostream< char_type, traits_type >,
public basic_sockbuf< char_type, traits_type >
{
private:
basic_sockbuf< char_type, traits_type > buffer;
public:
basic_socktream() :
std::basic_iostream< char_type, traits_type >::basic_iostream(),
buffer()
{
std::basic_iostream< char_type, traits_type >::init( & buffer );
}
};