C
Christopher Benson-Manica
I know I keep asking similar questions, but I really want to do this
at least sort of right. Not to mention I got no on-group replies to
my previous post I desperately want an interface that can allow
classes that implement it to act sort of like streams, but I just
can't seem to get everything I want...
class Writable
{
protected:
ostringstream outbuf; // or should I use private inheritance?
virtual void WriteData( std::string& s )=0;
public:
Writable& operator<< (const char *cp); // buffers it
template <class T>
Writable& operator<< (const T& t) {outbuf<<t; return *this;}
void Flush() {WriteData(outbuf.str()); outbuf.str("");}
};
So that's great, it all works like a charm. But I'm trying to find a
nice way to actually get Flush() called, and I just can't seem to find
the way to do it... I've tried adding
Writable& operator<< ((*f)(Writable& w)) {f(*this); return *this;}
to Writable and defining
Writable& flush( Writable& w ) {w.Flush(); return w;}
to let me do things like
a << 3 << flush << my_var << flush; // a is a class implementing Writable
but of course it doesn't work thanks to Writable's template function.
Is there any way I can make this work the way I want?
at least sort of right. Not to mention I got no on-group replies to
my previous post I desperately want an interface that can allow
classes that implement it to act sort of like streams, but I just
can't seem to get everything I want...
class Writable
{
protected:
ostringstream outbuf; // or should I use private inheritance?
virtual void WriteData( std::string& s )=0;
public:
Writable& operator<< (const char *cp); // buffers it
template <class T>
Writable& operator<< (const T& t) {outbuf<<t; return *this;}
void Flush() {WriteData(outbuf.str()); outbuf.str("");}
};
So that's great, it all works like a charm. But I'm trying to find a
nice way to actually get Flush() called, and I just can't seem to find
the way to do it... I've tried adding
Writable& operator<< ((*f)(Writable& w)) {f(*this); return *this;}
to Writable and defining
Writable& flush( Writable& w ) {w.Flush(); return w;}
to let me do things like
a << 3 << flush << my_var << flush; // a is a class implementing Writable
but of course it doesn't work thanks to Writable's template function.
Is there any way I can make this work the way I want?