Psi4
PsiFileImpl.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * @END LICENSE
26  */
27 #ifndef PSIFILEIMPL_H_
28 #define PSIFILEIMPL_H_
29 
31 #include <fstream>
32 
34 typedef std::fstream::openmode stdFMode;
35 namespace psi{
36 
39  T=3,BINARY=4,BIN=4};
40 
42 template<typename T>
44  private:
46  std::map<FileMode,stdFMode> FOptions_;
47 
49  void LoadFOptions();
50 
52  PsiFileImpl<T>(const PsiFileImpl<T>& /*other*/){}
53 
55  const PsiFileImpl<T>& operator=(const PsiFileImpl<T>& /*other*/){
56  return *this;
57  }
58 
59  protected:
60  template<typename T2>
61  void Open(const std::string& filename, const FileMode& Mode,
62  std::shared_ptr<T2>& FileStream,const bool ImSpecial){
63  if (ImSpecial&&filename!="NULL") {
64  this->Close(FileStream);
65  FileStream=std::shared_ptr<T>(
66  (Mode==NOFILEMODE? new T(filename.c_str()):
67  new T(filename.c_str(), FOptions_[Mode])));
68  if (!FileStream) {
69  std::string error="Could not open file: "+filename;
70  throw PSIEXCEPTION(error.c_str());
71  }
72  }
73  }
75  template<typename T2>
76  void Close(std::shared_ptr<T2>& FileStream){
77  if(FileStream)FileStream.reset();
78  }
79  public:
81  LoadFOptions();
82  }
83 
84 };
85 
86 template<typename T>
88  FOptions_[NOFILEMODE]=std::fstream::out;
89  FOptions_[END]=std::fstream::ate;
90  FOptions_[APPEND]=std::fstream::app;
91  FOptions_[TRUNCATE]=std::fstream::trunc;
92  FOptions_[BINARY]=std::fstream::binary;
93 }
94 }
95 
96 
97 
98 #endif /* PSIFILEIMPL_H_ */
Definition: PsiFileImpl.h:38
const PsiFileImpl< T > & operator=(const PsiFileImpl< T > &)
No assignment of file streams.
Definition: PsiFileImpl.h:55
void Open(const std::string &filename, const FileMode &Mode, std::shared_ptr< T2 > &FileStream, const bool ImSpecial)
Definition: PsiFileImpl.h:61
The machinery common to input and output files.
Definition: PsiFileImpl.h:43
Definition: PsiFileImpl.h:38
void Close(std::shared_ptr< T2 > &FileStream)
We cheat to avoid the upcast.
Definition: PsiFileImpl.h:76
Definition: PsiFileImpl.h:38
Definition: PsiFileImpl.h:38
Definition: PsiFileImpl.h:39
FileMode
For each value the first name is preferred, but let&#39;s face it typing stinks.
Definition: PsiFileImpl.h:38
Definition: PsiFileImpl.h:39
std::fstream::openmode stdFMode
Typedef of std::file modes.
Definition: PsiFileImpl.h:34
void LoadFOptions()
Function that loads FOptions_ up.
Definition: PsiFileImpl.h:87
Definition: PsiFileImpl.h:38
Definition: PsiFileImpl.h:38
std::map< FileMode, stdFMode > FOptions_
Maps enumerated FileModes to those in c++ std library.
Definition: PsiFileImpl.h:46
Definition: PsiFileImpl.h:38
Definition: PsiFileImpl.h:38
#define PSIEXCEPTION(message)
Definition: exception.h:47
Definition: PsiFileImpl.h:39