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