473,398 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Overloading / Getting rid of cout

Hi,

I'd like to get rid of all the cout stuff in my code.

Following situation:

Way over 2000 files, search and replace in all of them is not an option.
All of the files include a basic header.
In this basic header i must NOT include iostream.
Iostereams thus get included after my basic header, at any random place
in the code.

What I'd like to do is change the behaviour in a way that everything
sent to either cout or cerr comes to my own stream class (which is
accessible by including the base header) or can at least be supressed
(this is easy though)

with kind regards Philip

Jul 19 '05 #1
8 3473

"Philip Lawatsch" <ph****@waug.at> wrote in message
news:3e********@e-post.inode.at...
Hi,

I'd like to get rid of all the cout stuff in my code.

Following situation:

Way over 2000 files, search and replace in all of them is not an option.
All of the files include a basic header.
In this basic header i must NOT include iostream.
Iostereams thus get included after my basic header, at any random place
in the code.

What I'd like to do is change the behaviour in a way that everything
sent to either cout or cerr comes to my own stream class (which is
accessible by including the base header) or can at least be supressed
(this is easy though)

with kind regards Philip


Search and replace is the correct option, write a program to do it, or if
you are on Unix/Linux use a tool like sed or awk.

john
Jul 19 '05 #2
John Harrison wrote:

Search and replace is the correct option, write a program to do it, or if
you are on Unix/Linux use a tool like sed or awk.


Well, I'm aware that this would be a correct solution, but I'm afraid i
just can't do this.

Thus I was looking for something else, perhaps some special stuff the
iostreams provide for this
with kind regards Philip

Jul 19 '05 #3
John Harrison wrote:
You can redirect cout. Forget the exact syntax but its something like

int main()
{
streambuf* save_buffer = cout.rdbuf(my_stream.rdbuf());
...
cout.rdbuf(save_buffer);
}

Hmmm, nice thought, though this leads to a problem.
Using this approach it seems that my code (my stream code) won't get
called when data is written, and this would result in a huge buffer needed.

(One of the reasons I'd like to get rid of all this couts is that huge
amounts of output are written out).

Also, using this approach I could not easily filter the output I get and
only write some of the stuff out because I could only do it when I'm
inside the my stream class. Thus this would result in a huge lagg of
output since some parts of our code run for several minutes until
controll returns to some part where I could initiate parsing this buffer.
with kind regards Philip

Jul 19 '05 #4
On Sun, 29 Jun 2003 19:38:24 +0200, Philip Lawatsch <ph****@waug.at>
wrote:
John Harrison wrote:
Hmmm, nice thought, though this leads to a problem.
Using this approach it seems that my code (my stream code) won't get
called when data is written, and this would result in a huge buffer


needed.

Sounds like you've put your stream code in the wrong place. It should have
gone in the streambuf derived class you created (or should of created). The
stream derived class should just be a thin wrapper around the streambuf
derived class where the real work is done.


I see ...

For portability issues (we're compiling on more than 6 unices, linux and
windows).

Due to crappy compilers and ill behaved std libs we tried to code as
much of the basis framework ourselves as possible, and thus my stream
class is not derived from streambuf.

I'll have a look at this option !


What have you got currently? What does your stream class look like? If
you haven't derived from streambuf, I'm curious as to how your stream
class works. Is it completely independent of the iostreams heirarchy?

Tom
Jul 19 '05 #5
tom_usenet wrote:
On Sun, 29 Jun 2003 19:38:24 +0200, Philip Lawatsch <ph****@waug.at>
wrote:
What have you got currently? What does your stream class look like? If
you haven't derived from streambuf, I'm curious as to how your stream
class works. Is it completely independent of the iostreams heirarchy?


Yep, it is

I'm sorry, i just cant paste the source here, but in prinziple its a
class with stream ops for the basic data types defined (virtual).

And then some derived classes for streaming to files, the console, the
gui etc.

And my objects implement stream operators for my basic stream class.

This thing does not support everything the iostreams do, but enough for
my app.

Ohh, and, yes, my stream class cant be mixed with anything from iostream
since they dont know each other.
with kind regards philip

Jul 19 '05 #6
On Mon, 30 Jun 2003 17:09:48 +0200, Philip Lawatsch <ph****@waug.at>
wrote:
tom_usenet wrote:
On Sun, 29 Jun 2003 19:38:24 +0200, Philip Lawatsch <ph****@waug.at>
wrote:

What have you got currently? What does your stream class look like? If
you haven't derived from streambuf, I'm curious as to how your stream
class works. Is it completely independent of the iostreams heirarchy?


Yep, it is

I'm sorry, i just cant paste the source here, but in prinziple its a
class with stream ops for the basic data types defined (virtual).

And then some derived classes for streaming to files, the console, the
gui etc.

And my objects implement stream operators for my basic stream class.


How do you manage to have cout calls in the code then? If your classes
don't implement operator<< for ostream, then cout calls won't work. Or
perhaps the cout calls only use the built in types?

This thing does not support everything the iostreams do, but enough for
my app.

Ohh, and, yes, my stream class cant be mixed with anything from iostream
since they dont know each other.


Well, I won't chastise you for reinventing the wheel (and making it a
bit less round than current wheel designs), but instead tell you that
you can't redirect std::cout to anything but another streambuf. Where
exactly do you want to redirect it to? You obviously can't direct it
to your stream class, but you can get it to write to the same thing
that your stream class is writing to, but creating a streambuf class
that outputs to that (note this can be unbuffered if you choose -
streambuf is a slightly misleading name).

You can supress cout output with a call to:

std::streambuf* oldcout = std::cout.rdbuf(0);
//reset before exit:
std::cout.rdbuf(oldcout);

Still, from what you're saying, search and replace is by far the best
option.

Tom
Jul 19 '05 #7
tom_usenet wrote:
Yep, it is

I'm sorry, i just cant paste the source here, but in prinziple its a
class with stream ops for the basic data types defined (virtual).

And then some derived classes for streaming to files, the console, the
gui etc.

And my objects implement stream operators for my basic stream class.

How do you manage to have cout calls in the code then? If your classes
don't implement operator<< for ostream, then cout calls won't work. Or
perhaps the cout calls only use the built in types?


Well, this is exactly the point.
There should not be any calls to cout directly, but a lot of developers
just give a damn about design rules, and exchanging them is not an option.
So something has to be done to get rid of the problem.

If I just search and replace this won't keep any of them from adding new
couts and thus this would be a never ending stuff
This thing does not support everything the iostreams do, but enough for
my app.

Ohh, and, yes, my stream class cant be mixed with anything from iostream
since they dont know each other.

Well, I won't chastise you for reinventing the wheel (and making it a
bit less round than current wheel designs), but instead tell you that
you can't redirect std::cout to anything but another streambuf.


But I can (and am working on that currently) write a dummy stream class
derived from streambuf as a wrapper.
Not nice, and i just cross my fingers that this will be portable (I do
have bad experiences about such stuff, this is why we wrote as much
ourselves as possible in the first place)
Where
exactly do you want to redirect it to? You obviously can't direct it
to your stream class, but you can get it to write to the same thing
that your stream class is writing to, but creating a streambuf class
that outputs to that (note this can be unbuffered if you choose -
streambuf is a slightly misleading name).

You can supress cout output with a call to:

std::streambuf* oldcout = std::cout.rdbuf(0);
//reset before exit:
std::cout.rdbuf(oldcout);


Well, what i'll do is write a wrapper using a class derived from
streambuf and then distribute it to appropriate calls in our framework

Its not nice what I'm doing, and I am aware that such a situation should
never happen if just everyone would follow design guidelines, but if
you've ever worked in mid size projects you might know that you can't
just make everyone do what you want him to do.
with kind regards Philip

Jul 19 '05 #8
tom_usenet wrote:

Assuming you don't want buffering (which might interfere with other
usages of your stream class), you just need to override overflow:

class MyStreambuf: public std::streambuf
{
MyStream& m_stream;
public:
MyStreambuf(MyStream& s)
:m_stream(s)
{
}

protected:
int overflow(int c)
{
if (c != EOF)
{
char cc = traits_type::to_char_type(c);
m_stream.put(c); //or whatever puts a char
}
return traits_type::not_eof(c);
}

//optional
int sync()
{
m_stream.flush();
return 0;
}
};

And that's it. Inefficient (I virtual function call per character
output), but that probably doesn't matter to you.


This is in principle what I'm doing right now :)

Thanks for all the ideas

with kind regards Philip

Jul 19 '05 #9

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
3
by: dmegee | last post by:
Ok here's the deal I need to make a program that runs in this manner... I have the majority of it done but I have some errors when I try to compile it. Can anyone help me? Create a class...
2
by: KiranJyothi | last post by:
Can anyone please tell me how can I rectify my compile time error in this program. /********************************HEADER FILE***********************************/ #ifndef POLYNOMIAL_H //...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
1
by: Nikhil.S.Ketkar | last post by:
Hi, Does the following construct qualify as overloading on return type ? If not, in what way ? I overloaded the type conversion operator and used pointers to member functions. Its pretty...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.