472,975 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,975 software developers and data experts.

Interfaces and generic templates

(To the extent that this is a rehash of earlier questions, sorry.)

I feel like I'm starting to get the hang of how to make classes work
with << and >>, so now I have the (possibly dumb) idea to create
generic interfaces that use these, making it easy to subclass our
existing classes and make them behave a little better in the C++
world. I'm going to describe what I'm doing in some detail - feel
free to interject criticism at any point, since I'm not at all sure
what can be done better...

To start with: Create a class/interface Receivable that serves to
buffer data written to it (currently, a lot of code we have takes only
const char *'s for writing):

class Receivable // Buffered line input via <<
std::ostringstream outbuf;

virtual void ProcessInput( std::string& s )=0;

template <class T> void Buffer( const T& t );
template <class T> Receivable& operator<< ( const T& t );
void Flush() {ProcessInput(outbuf.str()+"\n");}

template <class T> void Receivable::Buffer( const T& t )
outbuf << t;
std::string& buf=outbuf.str();
if( buf[buf.length()-1]=='\n' ) {
ProcessInput( buf );
outbuf.str( "" );

template <class T> Receivable& Receivable::operator<< ( const T& t )
Buffer( t );
return( *this );

Okay, great, maybe. Now create an interface Stringizable that allows
classes that implement it to appear on the right side of << operators:

class Stringizable
virtual const char* ToString()=0; // returns const char * because
// that's what a lot of code
// already returns

Uh-huh, you say. But how to make Stringizables work with the <<
operator? Maybe

template <class T>
T& __fastcall operator<< ( const T& t, const Stringizable& s )
t << s.ToString();
return( t );

Fine and dandy, as long as T doesn't implement Stringizable, eh? How
can I make it that general? But onward - now I should be able to put
these interfaces to work in my previously-mentioned TLSFileStream

TLSFileStream : public TLSFile, public Receivable, public Stringizable
typedef TLSFile Inherited;

CPCHAR __fastcall ToString() {return(AsString());} // TLSFile
void __fastcall ProcessInput( std::string& s )
Inherited::Write( s.c_str() ); // implemented by TLSFile
~TLSFileStream() {Flush();}

So theoretically, I should be able to do

TLSFileStream fs1, fs2;
fs1 << fs2;

But, as I noted above, TLSFileStream implements both interfaces, and
thus the compiler can't figure it out.

Help/suggestions, please :)

Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
0 1028

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: gong | last post by:
hi i recently looked at alexandrescu's book on c++, and i found it pretty much unintelligible. i have a few points which i wonder about. 1. as a developer, it is important, from a bottom line...
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
by: redefined.horizons | last post by:
I'm coming from a Java background, so please don't stone me... I see that Python is missing "interfaces". The concept of an interface is a key to good programming design in Java, but I've read...
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
by: Tony | last post by:
class Interface { public: virtual void DoItNow()=0; }; class A: public Interface { public: void DoItNow(); // satisfies interface explicitly
by: Johs | last post by:
I have read that when you are using templates you are making generic programs. But I don't see whats so generic about templates. You can make generic programs without templates through the use of...
by: -- | last post by:
Imagine I have a class TypeX and a class TypeY that inherts TypeX. public class typeX { .... } public class typeY : typeX { ....
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.