Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:05

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/io/ScopedStreamFormat.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <ios>
0010 
0011 #include "corecel/Macros.hh"
0012 
0013 namespace celeritas
0014 {
0015 //---------------------------------------------------------------------------//
0016 /*!
0017  * Save a stream's state and restore on destruction.
0018  *
0019  * Example:
0020  * \code
0021      {
0022          ScopedStreamFormat save_fmt(&std::cout);
0023          std::cout << setprecision(16) << 1.0;
0024      }
0025  * \endcode
0026  */
0027 class ScopedStreamFormat
0028 {
0029   public:
0030     // Construct with stream to safe
0031     explicit inline ScopedStreamFormat(std::ios* s);
0032 
0033     // Restore formats on destruction
0034     inline ~ScopedStreamFormat();
0035 
0036     //!@{
0037     //! Prevent copying and moving for RAII class
0038     CELER_DELETE_COPY_MOVE(ScopedStreamFormat);
0039     //!@}
0040 
0041   private:
0042     std::ios* stream_;
0043     std::ios orig_;
0044 };
0045 
0046 //---------------------------------------------------------------------------//
0047 // INLINE DEFINITIONS
0048 //---------------------------------------------------------------------------//
0049 /*!
0050  * Construct with defaults.
0051  */
0052 ScopedStreamFormat::ScopedStreamFormat(std::ios* s)
0053     : stream_{s}, orig_{nullptr}
0054 {
0055     CELER_EXPECT(s);
0056     orig_.copyfmt(*s);
0057 }
0058 
0059 //---------------------------------------------------------------------------//
0060 /*!
0061  * Restore formats on destruction.
0062  */
0063 ScopedStreamFormat::~ScopedStreamFormat()
0064 {
0065     stream_->copyfmt(orig_);
0066 }
0067 
0068 //---------------------------------------------------------------------------//
0069 }  // namespace celeritas