public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* std::iostream help
@ 2009-03-17 20:00 Brian McGrew
  2009-03-18 17:46 ` John (Eljay) Love-Jensen
  2009-03-18 19:02 ` John Fine
  0 siblings, 2 replies; 3+ messages in thread
From: Brian McGrew @ 2009-03-17 20:00 UTC (permalink / raw)
  To: gcc-help

Greetings...

I need a wrapper to use around std::iostream...

Right now, I have this:

std::iostream debug_out;

And I use it the same way you¹d use std::cout...

Debug_out << ³Debug Message² << std::endl;

And it works fine.

But, debug_out gets used in millions of places in my code, so, somehow I
need to come up with a wrapper around the std::iostream to check the debug
level...  I realize the below isn¹t valid code (I tried) but, will give and
idea of what I want to do:

U_short TH_ALGO_DEBUG = 0;

If (TH_ALGO_DEBUG > 0) {
    std::iostream debug_out;
}

So I want debug_out to only with if debug is greater than zero; without
having to change the debug_out call in millions of places in my code?!?!?!

Please help!

-b

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: std::iostream help
  2009-03-17 20:00 std::iostream help Brian McGrew
@ 2009-03-18 17:46 ` John (Eljay) Love-Jensen
  2009-03-18 19:02 ` John Fine
  1 sibling, 0 replies; 3+ messages in thread
From: John (Eljay) Love-Jensen @ 2009-03-18 17:46 UTC (permalink / raw)
  To: Brian McGrew, GCC-help

Hi Brian,

> I need a wrapper to use around std::iostream...
> 
> Right now, I have this:
> 
> std::iostream debug_out;
> 
> And I use it the same way you¹d use std::cout...
> 
> Debug_out << ³Debug Message² << std::endl;
> 
> And it works fine.
> 
> But, debug_out gets used in millions of places in my code, so, somehow I
> need to come up with a wrapper around the std::iostream to check the debug
> level...  I realize the below isn¹t valid code (I tried) but, will give and
> idea of what I want to do:
> 
> U_short TH_ALGO_DEBUG = 0;
> 
> If (TH_ALGO_DEBUG > 0) {
>     std::iostream debug_out;
> }

Could you do this?

#if DEBUG_LEVEL > 0
std::iostream debug_out;
#else
boost::iostreams::basic_null_sink<char> debug_out;
#endif

That would affect the build, rather than being able to alter TH_ALGO_DEBUG
on the fly.

And I assume you have access to Boost:

http://www.boost.org/doc/libs/1_38_0/libs/iostreams/doc/classes/null.html

If you need to be able to alter TH_ALGO_DEBUG on the fly, you could make
your own ostream derived class which redirects slewing characters to either
std::iostream or just ignore the characters much in the same way that
boost:iostreams::basic_null_sink<char> does.

If you want something trickier, such as...

debug_out << ExpensiveFunctionForDebugOnly();

And you don't want ExpensiveFunctionForDebugOnly() to be called in the
TH_ALGO_DEBUG < 1 situation... alas, you need macro magic.

#define DEBUG_OUT(x) do{if(TH_ALGO_DEBUG > 0) debug_out << x;}while(false)

Which would be used like:
DEBUG_OUT(ExpensiveFunctionForDebugOnly());

> So I want debug_out to only with if debug is greater than zero; without
> having to change the debug_out call in millions of places in my code?!?!?!

Hmmm, the combination of debug_out as being compiled std::iostream or
boost::iostreams::basic_null_sink<char> ... or perhaps having your own
ostream derived class that ignores the << stream based on TH_ALGO_DEBUG, and
the magic macro DEBUG_OUT (which would hopefully only require thousands of
select changes instead of millions of changes) may accomplish your goal.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: std::iostream help
  2009-03-17 20:00 std::iostream help Brian McGrew
  2009-03-18 17:46 ` John (Eljay) Love-Jensen
@ 2009-03-18 19:02 ` John Fine
  1 sibling, 0 replies; 3+ messages in thread
From: John Fine @ 2009-03-18 19:02 UTC (permalink / raw)
  To: Brian McGrew; +Cc: gcc-help

How many different data types are sent to Debug_out in those millions of 
places and how many of those types are themselves wrappers?

It is quite easy to create a class that contains a std::iostream as a 
member and wraps the behavior of std::iostream.

The direct way to do that is to provide a templated definition of 
operator<<() in that wrapper class that checks the debug level and (by 
run time conditional) redirects all the work to the contained iostream.

That method works until it conflicts with the templated operator<<() 
definitions inside data wrapper types that are passed to it.

The data wrapper operator<<() typically redirects all << operations to 
the wrapped data regardless of the stream type, just as your 
operator<<() redirects all << operations (after the debug level test) to 
the wrapped stream.

But the compiler can't decide which operator<<() template to chose.  
Choosing either one would generate the same correct behavior, but so far 
as I understand there is no way to tell the compiler to choose a 
specific one or to choose on its own, or to work at all in this case.  
It just rejects the code because of the ambiguity.

If you are using no such data wrappers, the stream wrapper you want is easy.

If you are using few (by type) such data wrappers (even if many by 
instance) you can individually code non templated overrides for 
operator<<() for the problem combinations.

Brian McGrew wrote:
> I need a wrapper to use around std::iostream...
>
> Right now, I have this:
>
> std::iostream debug_out;
>
> And I use it the same way you¹d use std::cout...
>
> Debug_out << ³Debug Message² << std::endl;
>
> And it works fine.
>
> But, debug_out gets used in millions of places in my code, so, somehow I
> need to come up with a wrapper around the std::iostream to check the debug
> level...  

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-03-18 19:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-17 20:00 std::iostream help Brian McGrew
2009-03-18 17:46 ` John (Eljay) Love-Jensen
2009-03-18 19:02 ` John Fine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).