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

Atlant Schmidt aschmidt at dekaresearch.com
Tue Jul 27 13:51:58 CEST 2010


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, before everything had
  to be an ideal fully-encapsulated object, 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.

  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.

  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.

    http://en.wikipedia.org/wiki/Singleton_pattern


  And once you've written about twenty times as much code
  as you would have written just to use a global variable,
  the compiler will optimize all those inline functions
  away again, leaving you with code that's just about as
  efficient as if you'd used a global variable in the first
  place. But instead of saying, in embarrassed hushed tones,
  "Oh, he used a global -- how tragic for him", they'll all
  instead observe "What a clever C++ programmer he is!".

                       Atlant


-----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.

I often package pieces of data into plain-old-data classes, and I often
overload operator<< for these classes for convenience in debugging
and simple output.  (I tend to use a trailing '_' for names of data members,
and I print out simple, "obvious" data members without a name, but
print out more complicated data members as name_ = value.)

Sometimes these classes are nested (containment) in one another.
I'd like to set up some kind of cumulative indentation when I print
these classes out.

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:

[output for Point3d]  (no newlines)
(1.1, 2.2, 3.3)

[output for TwoStrings]  (operator<< for TwoStrings is responsible for
the newlines)
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
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
point3d_ = (5.5, 6.6, 7.7)
twoStrings_ = ...
string1_ = ppp
string2_ = qqq

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?

Now, I could just write operator<< for MoreStuff with the desired
indentation put in by hand (and I've done this many times when I've
wanted indentation or other special formatting enough), but then
MoreStuff would need to know the details of what's inside of SomeStuff
and wouldn't be reusing SomeStuff's operator<<.

Is this something that could/should be done with an iostream manipulator?

My approach to operator<< (without the indentation) is very simple-minded,
and I can do it on auto-pilot.  Ideally, an indentation scheme would also be
simple enough for a trained monkey, such as myself.

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