BDE 4.14.0 Production release
|
Provide a guard for saving the state of a stream object.
The bslim::FormatGuard
type saves the configuration of a basic_ostream type, when the guard is created. When the guard is destroyed, the stream is restored to the state it was in when the guard was created.
The state that is saved is
fmtflags
stateNote that the width
field is not saved, because it does not normally persist among multiple items output, but automatically resets to 0 after a single item is ouput.
In the following example we illustrate the usage of FormatGuard
.
Suppose we want to do some output to a stream for which we must change the state of the stream.
First, we declare our stream:
Then, we use a FormatGuard
to save the state of oss
before we reconfigure it, so that when the FormatGuard
is destroyed it will resotre oss
to its original state.
Then, we reconfigure out stream and do some output:
Next, we leave the block and the destructor of guard
will restore oss
to its original configuration:
Now, we do another line of output:
Finally, we observe that our guarded output was in hex, as desired, and the output afterward was in decimal, as desired: