[Qt-interest] Semi-OT: indentation with operator<<

Atlant Schmidt aschmidt at dekaresearch.com
Tue Jul 27 18:06:04 CEST 2010


K. Frank:

  Yeah, that's basically the idea I had in mind.
  One difference between my implementation and
  yours:

  I conceived of the indent_level starting at -1
  so that every "<<" overload could simply increase
  the indent level on entry and decrease it on exit,
  then doing whatever it wanted to do in-between. That
  means that no special action need be taken if one
  "<<" overload decides to invoke another "<<" overload
  to display the deeper details; all the "<<" overload
  operators just "auto-nest" automagically and every
  "<<" overload has a very-similar "wrapper" handling
  the indent_level: increment on entry, do its thing,
  then decrement on exit.

  After I wrote my initial reply, I also realized you could
  play one more (optional) game with this scheme. Perhaps
  you might use a preset indent_level of -2 to indicate
  that no debugging output should be generated at all?
  Other special values could also be implemented.

                        Atlant


-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of K. Frank
Sent: Tuesday, July 27, 2010 11:41 AM
To: Qt-interest
Subject: Re: [Qt-interest] Semi-OT: indentation with operator<<

Hi Atlant -

Thanks for your suggestion.

On Tue, Jul 27, 2010 at 7:51 AM, Atlant Schmidt
<aschmidt at dekaresearch.com> wrote:
> K. Frank:
>
>  This is one of those cases where I just despair at "The C++
>  Way of Doing Things(R)".
>
>  Back in the old steam-powered days,...

Ah, steampunk programming.  I like it...  I LIKE it...

>  before everything had
>  to be an ideal fully-encapsulated object,

Hey, plain-old-data classes, with (shudder) public data members,
and without (double-shudder) getters and setters!  Steampunk
programming, yeah, BABY!

>  we would have just
>  declared a global variable, calling it something obvious like
>  "debug_printout_indent_depth". It would "idle" at -1, and
>  each successively-deeper debugging object being printed
>  would increment the variable and indent its printouts to
>  that depth. When the debugging object was finished, it
>  would decrement the depth and return. This would ensure
>  that you got exactly the behaviour you seek.

So, in more detail:

I have to add a global variable:

unsigned indent_depth = 0;

My operator<<'s have to know about, respect, and use indent_depth:

class MoreStuff {
  // ...
  friend std::ostream& operator<< (std::ostream& o, MoreStuff const& m);
};

std::ostream& operator<< (std::ostream& o, MoreStuff const& m) {
  for (unsigned i = 0; i < indent_depth; i++)  o << ' ';   <-- respect
current indentation
  o  <<  "double1_   =  "  << m.double1_  <<  "\n";
  for (unsigned i = 0; i < indent_depth; i++)  o << ' ';   <-- respect
current indentation
  o  <<  "point3d_   =  "  << m.point3d_  <<  "\n";
  for (unsigned i = 0; i < indent_depth; i++)  o << ' ';   <-- respect
current indentation
  o << "someStuff_ = ...\n";
  indent_depth += 2;                <-- MoreStuff decides to increase
indentation
  o << someStuff_;                  <-- SomeStuff's op<< takes over now
  indent_depth -= 2;                 <-- remember to restore previous
indentation
  return o;
}

And in my example, SomeStuff's operator<< further increases the indentation
before it prints out twoStrings_.

Is this the kind of thing you had in mind?  It doesn't seem too bad
to me.  My operator<<'s need to know about (and link to) the global
variable.  If I want the scheme to work, I have to write the extra code
to respect the indentation, but it's pretty mechanical, so it flows off
the fingers easily.  And classes that don't use this scheme (or mess
it up) don't really break anything, other than not being indented nicely.

This meets my test of being straightforward and mechanical.  Would
you have any thoughts on (easy and straightforward) ways to clean it
up or automate it a little?

>
>  Nowadays, of course, you can't do this. The use of global
>  variables is, well, if not the plague, at least the sign
>  of a very un-hip programmer.

Let me state for the record that I am so very un-hip that I am hip.
Consider me an exemplar of über-un-hip retro-hipness.

>
>  Luckily, you can achieve the same effect by declaring a
>  "Singleton Class(R)" that manages the debugging printout
>  indent depth and by allowing all of the classes that do
>  debugging printouts to also inherit from that class.
> ...
>
>                       Atlant

Thanks.  I appreciate your suggestion.


K. Frank

>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of K. Frank
> Sent: Monday, July 26, 2010 22:00 PM
> To: Qt-interest
> Subject: [Qt-interest] Semi-OT: indentation with operator<<
>
> Hello All -
>
> This is not a hard-core Qt question, although maybe some of the Qt
> facilities can help with it.
> ...
> To explain by way of example, lets say I some classes, Point3d,
> TwoStrings, SomeStuff, and MoreStuff, that I choose to format with my
> overloaded operator<< as:
> ...
> So far, so good.  But what I'd really like is to have nested indentation,
> thus:
>
> [output for Point3d]
> (1.1, 2.2, 3.3)
>
> [output for TwoStrings]
> string1_ = abc
> string2_ = xyz
>
> [output for SomeStuff]
> int1_ = 7
> point3d_ = (4.0, 5.0, 6.0)   <-- uses operator<< for Point3d
> twoStrings_ = ...               <-- uses operator<< for TwoStrings
>   string1_ = def                <-- but, members of a nested class are indented
>   string2_ = uvw
>
> [output for MoreStuff]
> double1_ = 3.45
> point3d_ = (2.2, 3.3, 4.4)  <-- uses operator<< for Point3d
> someStuff_ = ...               <-- uses operator<< SomeStuff
>   int1_ = 12                     <-- nested members are indented
>   point3d_ = (5.5, 6.6, 7.7)
>   twoStrings_ = ...
>      string1_ = ppp           <-- and doubly-nested members are doubly indented
>      string2_ = qqq
>
> What might be a straightforward way to do this?
> ...
> Thanks for any suggestions.
>
> K. Frank

_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest

This e-mail and the information, including any attachments, it contains are intended to be a confidential communication only to the person or entity to whom it is addressed and may contain information that is privileged. If the reader of this message is not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please immediately notify the sender and destroy the original message.

Thank you.

Please consider the environment before printing this email.




More information about the Qt-interest-old mailing list