Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 09:06:28

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