forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a class that can be used to save and restore IO stream format in a RAII way. Below is an example. auto& os = amrex::OutStream(); auto old_fill = os.fill(); auto old_flags = os.flags(); auto old_precision = os.precision(); auto old_width = os.width(); { IOFormatSaver iofmtsaver(os); os << std::setfill('x'); os << std::left; os << std::setprecision(4); os << std::setw(111); } AMREX_ALWAYS_ASSERT(old_fill == os.fill()); AMREX_ALWAYS_ASSERT(old_flags == os.flags()); AMREX_ALWAYS_ASSERT(old_precision == os.precision()); AMREX_ALWAYS_ASSERT(old_width == os.width());
- Loading branch information
1 parent
b05f69f
commit 4e7dee5
Showing
5 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef AMREX_IO_FORMAT_H_ | ||
#define AMREX_IO_FORMAT_H_ | ||
|
||
#include <ios> | ||
|
||
namespace amrex { | ||
|
||
/* | ||
* \brief I/O stream format saver | ||
* | ||
* This class can be used to save and restore I/O stream format in a RAII | ||
* way. It handles fill, fmtflag, precision, and width. | ||
*/ | ||
template <class CharT, class Traits> | ||
class IOFormatSaver | ||
{ | ||
public: | ||
using BasicIos = std::basic_ios<CharT, Traits>; | ||
|
||
explicit IOFormatSaver (BasicIos& ios) | ||
: m_ios(&ios), | ||
m_fill(ios.fill()), | ||
m_flags(ios.flags()), | ||
m_precision(ios.precision()), | ||
m_width(ios.width()) | ||
{} | ||
|
||
~IOFormatSaver () | ||
{ | ||
m_ios->fill(m_fill); | ||
m_ios->flags(m_flags); | ||
m_ios->precision(m_precision); | ||
m_ios->width(m_width); | ||
} | ||
|
||
private: | ||
BasicIos* m_ios; | ||
CharT m_fill; | ||
typename BasicIos::fmtflags m_flags; | ||
std::streamsize m_precision; | ||
std::streamsize m_width; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters