Branch data Line data Source code
1 : : // ***************************************************************************** 2 : : /*! 3 : : \file src/Base/Writer.hpp 4 : : \copyright 2012-2015 J. Bakosi, 5 : : 2016-2018 Los Alamos National Security, LLC., 6 : : 2019-2021 Triad National Security, LLC., 7 : : 2022-2024 J. Bakosi 8 : : All rights reserved. See the LICENSE file for details. 9 : : \brief Writer base class declaration 10 : : \details Writer base class declaration. Writer base serves as a base class 11 : : for various file writers. It does generic low-level I/O, e.g., opening and 12 : : closing a file, and associated error handling. 13 : : */ 14 : : // ***************************************************************************** 15 : : #pragma once 16 : : 17 : : #include <fstream> 18 : : 19 : : namespace tk { 20 : : 21 : : //! Writer base serves as a base class for various file writers. It does generic 22 : : //! low-level I/O, e.g., opening and closing a file, and associated error 23 : : //! handling. 24 : : class Writer { 25 : : 26 : : public: 27 : : //! Constructor: Acquire file handle. Protected: designed to be base only. 28 : : explicit Writer( const std::string& filename, 29 : : std::ios_base::openmode mode = std::ios_base::out ); 30 : : 31 : : //! Destructor: Release file handle 32 : : virtual ~Writer() noexcept; 33 : : 34 : : //! Unformatted write 35 : : //! \param[in] data Buffer to write 36 : : //! \param[in] count Number of characters to write 37 : : void write( const char* data, std::streamsize count ) 38 : : { m_outFile.write( data, count ); } 39 : : 40 : : //! Write access to underlying output file stream 41 : 275 : std::ofstream& stream() { return m_outFile; } 42 : : 43 : : protected: 44 : : const std::string m_filename; //!< File name 45 : : mutable std::ofstream m_outFile; //!< File output stream 46 : : }; 47 : : 48 : : } // tk::