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

K. Frank kfrank29.c at gmail.com
Tue Jul 27 03:59:44 CEST 2010


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



More information about the Qt-interest-old mailing list