as2js: as2js::raii_stream_flags Class Reference

as2js::raii_stream_flags Class Reference

A class used to safely handle stream flags, width, and precision. More...

#include <os_raii.h>

Public Member Functions  raii_stream_flags (std::ios_base &s)  Save the current format flags, width, and precision of a stream. More...
   raii_stream_flags (raii_stream_flags const &rhs)=delete    ~raii_stream_flags ()  Restore the flags, width, and precision of a stream. More...
  raii_stream_flagsoperator= (raii_stream_flags const &rhs)=delete   void restore ()  The restore function copies the flags, width, and precision back in the stream. More...
  Private Attributes std::ios_base::fmtflags f_flags = std::ios_base::fmtflags()   std::streamsize f_precision = 0   std::ios_base * f_stream = nullptr   std::streamsize f_width = 0   Detailed Description

Create an object of this type on your stack, and the flags, width, and precision of your standard streams will be safe-guarded.

See the constructor for an example.

Definition at line 42 of file os_raii.h.

Constructor & Destructor Documentation
as2js::raii_stream_flags::raii_stream_flags ( std::ios_base &  s)

This function saves the flags, precision, and width of a stream inside this object so as to restore them later.

The destructor will automatically restore the flags. The restore() function can also be called early, although that will eventually break the RAII feature since restore only restores the flags once. Further calls to the restore() function do nothing.

To use:

{
as2js::raii_stream_flags stream_flags(std::cout);
...
// this changes the flags to write numbers in hexadecimal
std::cout << std::hex << 123 << ...;
...
} // here all flags, width, precision get restored automatically
Bug:
This class does not know about the fill character.
Parameters
[in]sThe stream of which flags are to be saved.

Definition at line 79 of file os_raii.cpp.

as2js::raii_stream_flags::raii_stream_flags ( raii_stream_flags const &  rhs) delete
as2js::raii_stream_flags::~raii_stream_flags ( )

The destructor automatically restores the stream flags, width, and precision when called. Putting such an object on the stack is the safest way to make sure that your function does not leak the stream flags, width, and precision.

This function calls the restore() function. Note that restore() has no effect when called more than once.

Definition at line 98 of file os_raii.cpp.

References restore().

Member Function Documentation
raii_stream_flags& as2js::raii_stream_flags::operator= ( raii_stream_flags const &  rhs) delete
void as2js::raii_stream_flags::restore ( )

This function restores the flags, width, and precision of the stream as they were when the object was passed to the constructor of this object.

The function can be called any number of time, however, it only restores the flags, width, and precision the first time it is called.

In most cases, you want to let your raii_stream_flags object destructor call this restore() function automatically, although you may need to restore the format early once in a while.

Definition at line 118 of file os_raii.cpp.

References f_flags, f_precision, f_stream, and f_width.

Referenced by ~raii_stream_flags().

Member Data Documentation
std::ios_base::fmtflags as2js::raii_stream_flags::f_flags = std::ios_base::fmtflags() private

Definition at line 55 of file os_raii.h.

Referenced by restore().

std::streamsize as2js::raii_stream_flags::f_precision = 0 private

Definition at line 56 of file os_raii.h.

Referenced by restore().

std::ios_base* as2js::raii_stream_flags::f_stream = nullptr private

Definition at line 54 of file os_raii.h.

Referenced by restore().

std::streamsize as2js::raii_stream_flags::f_width = 0 private

Definition at line 57 of file os_raii.h.

Referenced by restore().


The documentation for this class was generated from the following files:
  • /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/os_raii.h
  • /home/snapwebsites/snapcpp/contrib/as2js/lib/os_raii.cpp

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly