|
Boost : |
From: quendezus (quendez_at_[hidden])
Date: 2002-02-11 04:31:21
--- In boost_at_y..., Matthias Troyer <troyer_at_i...> wrote:
> Maybe I miss the point but I don't see how this will work. In
>
> //-----------------------------
>
> // Class A uses Jens Maurer binary_stream
> // to serialize
>
> struct A
> {
> int I;
> };
>
> DECLARE_SERIAL( A );
>
> std::streambuf& operator<<( std::streambuf& dest, const A& a )
> {
> binary_stream s( &dest );
> s << I;
> return dest;
> }
>
> std::streambuf& operator>>( std::streambuf& src, const A& a )
> {
> binary_stream s( &src );
> s >> I;
> return src;
> }
>
> you explicitly write the stream type (binary_stream) in the
> (de)serialization functions for the class A. Thus as far as I
> can see, you only use one type of stream ever.
>
I think "how to write in the stream buffer" should be a user
decision. In the 3 examples I have posted, the framework (the
persistence library) only calls :
std::streambuf& operator<<( std::streambuf& dest, const A& a );
std::streambuf& operator>>( std::streambuf& src, const A& a );
Then the user decides what do do with the streambuf, usually wrapping
it into a stream class.
>
>In my applications
> I use usually two, for debugging three types of streams all the
time.
> These are:
>
> i) the binary_stream (or no stream at all) for message passing
between
> nodes
>
> ii) the XDR_Stream for writing portable checkpoint files
>
> iii) sometimes ascii-based streams using standard iostreams
> for debugging
>
> As I don't want to duplicate code I don't see a way around either
> runtime or compile time polymorphism on the stream. Can you explain
> how this would work in your approach?
>
See the code below. Does it meet your need?
Sylvain
//-----------------------------
struct A
{
int I;
};
DECLARE_SERIAL( A );
//-----------------------------
// Simple switch debug/release
std::streambuf& operator<<( std::streambuf& dest, const A& a )
{
#ifdef NDEBUG
binary_stream s( &dest );
#else
debug_stream s( &dest );
#endif
s << a.I;
return dest;
}
//-----------------------------
// Stream my_stream has several
// behaviors according to macros or
// state or whatever.
std::streambuf& operator<<( std::streambuf& dest, const A& a )
{
my_stream s( &dest );
s << a.I;
return dest;
}
class my_stream
{
void write(const void* data, unsigned len)
{
#ifdef NDEBUG
...
#else
...
#endif
}
...
}
// or
class my_stream
{
void write(const void* data, unsigned len)
{
// Looking at the static variable 'state'
switch ( state )
{
case E_Debug: ...
case E_Ascii: ...
case E_Binary: ...
}
}
...
};
//-----------------------------
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk