Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:19:19

0001 // -*- C++ -*-
0002 //
0003 // This file is part of LHAPDF
0004 // Copyright (C) 2012-2024 The LHAPDF collaboration (see AUTHORS for details)
0005 //
0006 #pragma once
0007 #ifndef LHAPDF_FileIO_H
0008 #define LHAPDF_FileIO_H
0009 
0010 // STL includes
0011 #include <fstream>
0012 #include <sstream>
0013 #include <string>
0014 
0015 /// Namespace for all LHAPDF functions and classes
0016 namespace LHAPDF {
0017 
0018 
0019   /// @brief MPI-safe file I/O interface
0020   template <class FILETYPE>
0021   class File {
0022   public:
0023 
0024     // Constructor
0025     File(const std::string& name)
0026       : _name(name), _fileptr(nullptr), _streamptr(nullptr) {
0027       open();
0028     }
0029 
0030     /// Destructor
0031     ~File() { close(); }
0032 
0033 
0034     /// Open file
0035     bool open();
0036 
0037     /// Close file
0038     bool close();
0039 
0040     /// Forward methods via pointer emulation
0041     FILETYPE* operator->() const { return _fileptr; }
0042 
0043     /// Forward methods via pointer-dereference emulation
0044     FILETYPE& operator*() const { return *_fileptr; }
0045 
0046     /// Get the file content
0047     std::string getContent() const { return _streamptr != nullptr ? _streamptr->str() : ""; }
0048 
0049 
0050   protected:
0051 
0052     std::string _name;
0053 
0054     FILETYPE* _fileptr;
0055 
0056     std::stringstream* _streamptr;
0057 
0058   };
0059 
0060 
0061   // Convenience aliases (NB. have to use #defines due to how .cc template building works)
0062   // typedef File<std::ifstream> IFile;
0063   // typedef File<std::ofstream> OFile;
0064   #define IFile File<std::ifstream>
0065   #define OFile File<std::ofstream>
0066 
0067 
0068   /// Global function to flush the MPI-safe file cache
0069   void flushFileCache();
0070 
0071 
0072 }
0073 #endif