lhapdf is hosted by Hepforge, IPPP Durham
LHAPDF  6.5.4
FileIO.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2023 The LHAPDF collaboration (see AUTHORS for details)
5 //
6 #pragma once
7 #ifndef LHAPDF_FileIO_H
8 #define LHAPDF_FileIO_H
9 
10 // STL includes
11 #include <fstream>
12 #include <sstream>
13 #include <string>
14 
16 namespace LHAPDF {
17 
18 
20  template <class FILETYPE>
21  class File {
22  public:
23 
24  // Constructor
25  File(const std::string& name)
26  : _name(name), _fileptr(nullptr), _streamptr(nullptr) {
27  open();
28  }
29 
31  ~File() { close(); }
32 
33 
35  bool open();
36 
38  bool close();
39 
41  FILETYPE* operator->() const { return _fileptr; }
42 
44  FILETYPE& operator*() const { return *_fileptr; }
45 
47  std::string getContent() const { return _streamptr != nullptr ? _streamptr->str() : ""; }
48 
49 
50  protected:
51 
52  std::string _name;
53 
54  FILETYPE* _fileptr;
55 
56  std::stringstream* _streamptr;
57 
58  };
59 
60 
61  // Convenience aliases (NB. have to use #defines due to how .cc template building works)
62  // typedef File<std::ifstream> IFile;
63  // typedef File<std::ofstream> OFile;
64  #define IFile File<std::ifstream>
65  #define OFile File<std::ofstream>
66 
67 
69  void flushFileCache();
70 
71 
72 }
73 #endif
std::string getContent() const
Get the file content.
Definition: FileIO.h:47
bool open()
Open file.
FILETYPE * operator->() const
Forward methods via pointer emulation.
Definition: FileIO.h:41
void flushFileCache()
Global function to flush the MPI-safe file cache.
bool close()
Close file.
~File()
Destructor.
Definition: FileIO.h:31
MPI-safe file I/O interface.
Definition: FileIO.h:21
FILETYPE & operator*() const
Forward methods via pointer-dereference emulation.
Definition: FileIO.h:44