ROSE 0.11.145.147
|
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 53 of file FormatRestorer.h.
#include <Rose/FormatRestorer.h>
Public Member Functions | |
FormatRestorer (std::ostream &o) | |
Constructor saves output stream flags. | |
~FormatRestorer () | |
Destructor restores output stream flags. | |
void | save (std::ostream &o) |
Save current output stream flags. | |
void | restore () |
Restore saved flags. | |
Protected Attributes | |
std::ostream & | stream |
std::ios_base::fmtflags | fmt |
|
inline |
Constructor saves output stream flags.
Definition at line 60 of file FormatRestorer.h.
References save().
|
inline |
Destructor restores output stream flags.
Definition at line 65 of file FormatRestorer.h.
References restore().
|
inline |
Save current output stream flags.
Definition at line 70 of file FormatRestorer.h.
Referenced by FormatRestorer().
|
inline |
Restore saved flags.
Flags can be restored as many times as desired.
Definition at line 75 of file FormatRestorer.h.
Referenced by ~FormatRestorer().
|
protected |
Definition at line 55 of file FormatRestorer.h.
|
protected |
Definition at line 56 of file FormatRestorer.h.