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

K. Frank kfrank29.c at gmail.com
Tue Jul 27 17:41:21 CEST 2010


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




More information about the Qt-interest-old mailing list