ROSE
0.9.6a
|
Restores output stream flags to original values. More...
#include <FormatRestorer.h>
Public Member Functions | |
FormatRestorer (std::ostream &o) | |
Constructor saves output stream flags. More... | |
~FormatRestorer () | |
Destructor restores output stream flags. More... | |
void | save (std::ostream &o) |
Save current output stream flags. More... | |
void | restore () |
Restore saved flags. More... | |
Protected Attributes | |
std::ostream & | stream |
std::ios_base::fmtflags | fmt |
Restores output stream flags to original values.
To make temporary changes to stream flags within a function, first create one of these FormatRestorer objects in the same scope where the temporary changes are being made. When this object goes out of scope the format flags will be restored to their original values.
Example
This code does not work because it assumes that the format was decimal to start with:
This code isn't much better, because it doesn't restore the original format if bar() throws an exception:
This code is overly verbose, and gets worse as you add other return points and exception handling.
Using the FormatRestorer is cleaner:
Definition at line 52 of file FormatRestorer.h.
|
inline |
Constructor saves output stream flags.
Definition at line 59 of file FormatRestorer.h.
References save().
|
inline |
Destructor restores output stream flags.
Definition at line 64 of file FormatRestorer.h.
References restore().
|
inline |
Save current output stream flags.
Definition at line 69 of file FormatRestorer.h.
References fmt.
Referenced by FormatRestorer().
|
inline |
Restore saved flags.
Flags can be restored as many times as desired.
Definition at line 74 of file FormatRestorer.h.
Referenced by ~FormatRestorer().
|
protected |
Definition at line 54 of file FormatRestorer.h.
Referenced by restore().
|
protected |
Definition at line 55 of file FormatRestorer.h.