Boost logo

Boost :

From: Jason House (jhouse_at_[hidden])
Date: 2003-02-06 11:30:32

Jason House wrote:

> Terje Slettebø wrote:
> > Another possibility might be to have a sentry object, doing automatic state
> > saving and restoring in the constructor and destructor. In fact, there are
> > already such classes in Boost: Daryle Walker's I/O state savers, which fits
> > this situation like a glove.
> >
> I think that I like your solution better :) putting it constructor/deconstructor
> does seem better. I can't even argue that it's more typing for multi-line
> expressions...

Well, I know at least have more fuel to think about...
First of all, does your constructor take the stream as an argument? It would have to
in order to do state saving in the constructor... If so, that at least makes prevents
the following case (that probably needs special handling)
  io_format<> var1(...);
  std::cout << var1 << stuff;
  std::cerr << var1 << more_stuff;

My understanding is that the constructor would not perform state saving, but that it
is the call to << that has to perform the locking. So what happens when multiple
streams are used?

what happens when you have 2 or more io_formats in the same function used on the same
  io_format<T> var1(...), var2(...);
  std:: cout << var1 << stuff1;
  std::cout << var2 << stuff2;
  std::cout << var1 << stuff3;
  std::cout << var2 << stuff4;
  std::cout << io_format<T>(...) << stuff5;
} /* deconstruct all 3 io_format<T>'s ... not necessarilly in the right order! */

Here, var1, var2, and the unnamed class from the last line in the function are all
being deconstructed at the same time. Some caution needs to occur here. Is that what
the state-saver class you referred to does already?

Boost list run by bdawes at, gregod at, cpdaniel at, john at