All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FileBase.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 The plumed team
3  (see the PEOPLE file at the root of the distribution for a list of names)
4 
5  See http://www.plumed-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
8 
9  plumed is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  plumed is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 #ifndef __PLUMED_tools_FileBase_h
23 #define __PLUMED_tools_FileBase_h
24 
25 #include <string>
26 
27 namespace PLMD{
28 
29 class Communicator;
30 class PlumedMain;
31 class Action;
32 
33 /**
34 Base class for dealing with files.
35 
36 This class just provides things which are common among OFile and IFile
37 */
38 
39 class FileBase{
40 /// Copy constructor is disabled (private and unimplemented)
41  FileBase(const FileBase&);
42 /// Assignment operator is disabled (private and unimplemented)
43  FileBase& operator=(const FileBase&);
44 protected:
45 /// Internal tool.
46 /// Base for IFile::Field and OFile::Field
47  class FieldBase{
48 // everything is public to simplify usage
49  public:
50  std::string name;
51  std::string value;
52  bool constant;
53  FieldBase(): constant(false){}
54  };
55 
56 /// file pointer
57  FILE* fp;
58 /// communicator. NULL if not set
60 /// pointer to main plumed object. NULL if not linked
62 /// pointer to corresponding action. NULL if not linked
64 /// Control closing on destructor.
65 /// If true, file will not be closed in destructor
66  bool cloned;
67 /// Private constructor.
68 /// In this manner one cannot instantiate a FileBase object
69  FileBase();
70 /// Set to true when end of file is encountered
71  bool eof;
72 /// Set to true when error is encountered
73  bool err;
74 /// path of the opened file
75  std::string path;
76 /// Set to true if you want flush to be heavy (close/reopen)
77  bool heavyFlush;
78 public:
79 /// Link to an already open filed
80  FileBase& link(FILE*);
81 /// Link to a PlumedMain object
82 /// Automatically links also the corresponding Communicator.
84 /// Link to a Communicator object
86 /// Link to an Action object.
87 /// Automatically links also the corresponding PlumedMain and Communicator.
88  FileBase& link(Action&);
89 /// Flushes the file to disk
90  FileBase& flush();
91 /// Closes the file
92 /// Should be used only for explicitely opened files.
93  void close();
94 /// Virtual destructor (allows inheritance)
95  virtual ~FileBase();
96 /// Runs a small testcase
97  static void test();
98 /// Check for error/eof.
99  operator bool () const;
100 /// Set heavyFlush flag
101  void setHeavyFlush(){ heavyFlush=true;}
102 /// Opens the file (without auto-backup)
103  FileBase& open(const std::string&name,const std::string& mode);
104 /// Check if the file exists
105  bool FileExist(const std::string& path);
106 /// Check if a file is open
107  bool isOpen();
108 };
109 
110 
111 }
112 
113 #endif
std::string path
path of the opened file
Definition: FileBase.h:75
Internal tool.
Definition: FileBase.h:47
Action * action
pointer to corresponding action. NULL if not linked
Definition: FileBase.h:63
FileBase & link(FILE *)
Link to an already open filed.
Definition: FileBase.cpp:64
virtual ~FileBase()
Virtual destructor (allows inheritance)
Definition: FileBase.cpp:160
Class containing wrappers to MPI.
Definition: Communicator.h:44
static void test()
Runs a small testcase.
Definition: FileBase.cpp:37
void setHeavyFlush()
Set heavyFlush flag.
Definition: FileBase.h:101
FileBase & flush()
Flushes the file to disk.
Definition: FileBase.cpp:70
Communicator * comm
communicator. NULL if not set
Definition: FileBase.h:59
bool heavyFlush
Set to true if you want flush to be heavy (close/reopen)
Definition: FileBase.h:77
FileBase()
Private constructor.
Definition: FileBase.cpp:148
bool eof
Set to true when end of file is encountered.
Definition: FileBase.h:71
void close()
Closes the file Should be used only for explicitely opened files.
Definition: FileBase.cpp:140
Base class for all the input Actions.
Definition: Action.h:60
bool isOpen()
Check if a file is open.
Definition: FileBase.cpp:134
FileBase & open(const std::string &name, const std::string &mode)
Opens the file (without auto-backup)
Definition: FileBase.cpp:99
bool cloned
Control closing on destructor.
Definition: FileBase.h:66
FileBase & operator=(const FileBase &)
Assignment operator is disabled (private and unimplemented)
FILE * fp
file pointer
Definition: FileBase.h:57
bool FileExist(const std::string &path)
Check if the file exists.
Definition: FileBase.cpp:118
bool err
Set to true when error is encountered.
Definition: FileBase.h:73
Main plumed object.
Definition: PlumedMain.h:71
Base class for dealing with files.
Definition: FileBase.h:39
PlumedMain * plumed
pointer to main plumed object. NULL if not linked
Definition: FileBase.h:61