// -*- C++ -*- // // This file is part of LHAPDF // Copyright (C) 2012-2021 The LHAPDF collaboration (see AUTHORS for details) // #pragma once #ifndef LHAPDF_FileIO_H #define LHAPDF_FileIO_H // STL includes #include #include #include /// Namespace for all LHAPDF functions and classes namespace LHAPDF { /// @brief MPI-safe file I/O interface template class File { public: // Constructor File(const std::string& name) : _name(name), _fileptr(nullptr), _streamptr(nullptr) { open(); } /// Destructor ~File() { close(); } /// Open file bool open(); /// Close file bool close(); /// Forward methods via pointer emulation FILETYPE* operator->() const { return _fileptr; } /// Forward methods via pointer-dereference emulation FILETYPE& operator*() const { return *_fileptr; } /// Get the file content std::string getContent() const { return _streamptr != nullptr ? _streamptr->str() : ""; } protected: std::string _name; FILETYPE* _fileptr; std::stringstream* _streamptr; }; // Convenience aliases (NB. have to use #defines due to how .cc template building works) // typedef File IFile; // typedef File OFile; #define IFile File #define OFile File /// Global function to flush the MPI-safe file cache void flushFileCache(); } #endif