Collective Variables Module - Developer Documentation
Loading...
Searching...
No Matches
colvarproxy_io.h
1// -*- c++ -*-
2
3// This file is part of the Collective Variables module (Colvars).
4// The original version of Colvars and its updates are located at:
5// https://github.com/Colvars/colvars
6// Please update all Colvars source files before making any changes.
7// If you wish to distribute your changes, please submit them to the
8// Colvars repository at GitHub.
9
10#ifndef COLVARPROXY_IO_H
11#define COLVARPROXY_IO_H
12
13#include <iosfwd>
14#include <list>
15#include <map>
16#include <string>
17
18
21
22public:
23
26
28 virtual ~colvarproxy_io();
29
31 virtual bool io_available();
32
34 // Returns error code
35 virtual int get_frame(long int &);
36
38 // Returns error code
39 virtual int set_frame(long int);
40
42 virtual int backup_file(char const *filename);
43
45 inline int backup_file(std::string const &filename)
46 {
47 return backup_file(filename.c_str());
48 }
49
51 virtual int remove_file(char const *filename);
52
54 inline int remove_file(std::string const &filename)
55 {
56 return remove_file(filename.c_str());
57 }
58
60 virtual int rename_file(char const *filename, char const *newfilename);
61
63 inline int rename_file(std::string const &filename,
64 std::string const &newfilename)
65 {
66 return rename_file(filename.c_str(), newfilename.c_str());
67 }
68
70 inline std::string const & input_prefix() const
71 {
72 return input_prefix_str;
73 }
74
76 virtual int set_input_prefix(std::string const &prefix);
77
79 inline std::string const & output_prefix() const
80 {
81 return output_prefix_str;
82 }
83
85 virtual int set_output_prefix(std::string const &prefix);
86
88 inline std::string const & restart_output_prefix() const
89 {
91 }
92
94 virtual int set_restart_output_prefix(std::string const &prefix);
95
97 inline int default_restart_frequency() const
98 {
100 }
101
103 virtual int set_default_restart_frequency(int freq);
104
105 // The input stream functions below are not virtual, because they currently
106 // rely on the fact that either std::ifstream or std::istringstream is used
107
112 std::istream & input_stream(std::string const &input_name,
113 std::string const description = "file/channel",
114 bool error_on_fail = true);
115
120 std::istream & input_stream_from_string(std::string const &input_name,
121 std::string const &content,
122 std::string const description = "string");
123
125 bool input_stream_exists(std::string const &input_name);
126
128 int close_input_stream(std::string const &input_name);
129
132
134 int delete_input_stream(std::string const &input_name);
135
137 std::list<std::string> list_input_stream_names() const;
138
142 virtual std::ostream &output_stream(std::string const &output_name,
143 std::string const description);
144
146 virtual bool output_stream_exists(std::string const &output_name);
147
149 virtual int flush_output_stream(std::string const &output_name);
150
152 virtual int flush_output_streams();
153
155 virtual int close_output_stream(std::string const &output_name);
156
158 virtual int close_output_streams();
159
160protected:
161
163 std::string input_prefix_str;
164
166 std::string output_prefix_str;
167
170
173
175 std::map<std::string, std::istream *> input_streams_;
176
178 std::istream *input_stream_error_;
179
181 std::map<std::string, std::ostream *> output_streams_;
182
184 std::ostream *output_stream_error_;
185};
186
187
188#endif
Methods for data input/output.
Definition: colvarproxy_io.h:20
std::ostream * output_stream_error_
Object whose reference is returned when write errors occur.
Definition: colvarproxy_io.h:184
virtual int close_output_stream(std::string const &output_name)
Closes the given output file/channel.
Definition: colvarproxy_io.cpp:410
std::istream * input_stream_error_
Object whose reference is returned when read errors occur.
Definition: colvarproxy_io.h:178
std::istream & input_stream_from_string(std::string const &input_name, std::string const &content, std::string const description="string")
Definition: colvarproxy_io.cpp:234
virtual int backup_file(char const *filename)
Rename the given file, before overwriting it.
Definition: colvarproxy_io.cpp:67
std::string output_prefix_str
Default prefix to be used for all output files (final configuration)
Definition: colvarproxy_io.h:166
std::istream & input_stream(std::string const &input_name, std::string const description="file/channel", bool error_on_fail=true)
Definition: colvarproxy_io.cpp:200
std::map< std::string, std::ostream * > output_streams_
Currently open output files/channels.
Definition: colvarproxy_io.h:181
virtual int get_frame(long int &)
Save the current frame number in the argument given.
Definition: colvarproxy_io.cpp:55
int rename_file(std::string const &filename, std::string const &newfilename)
Rename the given file.
Definition: colvarproxy_io.h:63
std::string input_prefix_str
Prefix of the input state file to be read next.
Definition: colvarproxy_io.h:163
colvarproxy_io()
Constructor.
Definition: colvarproxy_io.cpp:30
virtual int set_restart_output_prefix(std::string const &prefix)
Set default restart state file prefix.
Definition: colvarproxy_io.cpp:179
int backup_file(std::string const &filename)
Rename the given file, before overwriting it.
Definition: colvarproxy_io.h:45
int default_restart_frequency() const
Default restart frequency (as set by the simulation engine)
Definition: colvarproxy_io.h:97
std::string const & restart_output_prefix() const
Prefix of the restart (checkpoint) file to be written next.
Definition: colvarproxy_io.h:88
int restart_frequency_engine
How often the simulation engine will write its own restart.
Definition: colvarproxy_io.h:172
virtual int set_frame(long int)
Set the current frame number (as well as colvarmodule::it)
Definition: colvarproxy_io.cpp:61
bool input_stream_exists(std::string const &input_name)
Check if the file/channel is open (without opening it if not)
Definition: colvarproxy_io.cpp:268
virtual std::ostream & output_stream(std::string const &output_name, std::string const description)
Definition: colvarproxy_io.cpp:343
int remove_file(std::string const &filename)
Remove the given file (on Windows only, rename to filename.old)
Definition: colvarproxy_io.h:54
virtual bool output_stream_exists(std::string const &output_name)
Check if the file/channel is open (without opening it if not)
Definition: colvarproxy_io.cpp:372
virtual int flush_output_stream(std::string const &output_name)
Flushes the given output file/channel.
Definition: colvarproxy_io.cpp:378
virtual int flush_output_streams()
Flushes all output files/channels.
Definition: colvarproxy_io.cpp:394
std::string restart_output_prefix_str
Prefix of the restart (checkpoint) file to be written next.
Definition: colvarproxy_io.h:169
std::list< std::string > list_input_stream_names() const
List all input streams that were opened at some point.
Definition: colvarproxy_io.cpp:330
virtual int remove_file(char const *filename)
Remove the given file (on Windows only, rename to filename.old)
Definition: colvarproxy_io.cpp:99
virtual int set_default_restart_frequency(int freq)
Communicate/set the restart frequency of the simulation engine.
Definition: colvarproxy_io.cpp:191
virtual int rename_file(char const *filename, char const *newfilename)
Rename the given file.
Definition: colvarproxy_io.cpp:132
virtual int close_output_streams()
Close all open files/channels to prevent data loss.
Definition: colvarproxy_io.cpp:427
int delete_input_stream(std::string const &input_name)
Same as close_input_stream(), but also removes the corresponding entry from memory.
Definition: colvarproxy_io.cpp:297
std::string const & output_prefix() const
Default prefix to be used for all output files (final configuration)
Definition: colvarproxy_io.h:79
virtual bool io_available()
Ensure that we're on the main thread (derived class will do actual check)
Definition: colvarproxy_io.cpp:49
int close_input_stream(std::string const &input_name)
Closes the given input stream.
Definition: colvarproxy_io.cpp:274
virtual int set_output_prefix(std::string const &prefix)
Set default output prefix.
Definition: colvarproxy_io.cpp:167
virtual int set_input_prefix(std::string const &prefix)
Initialize input_prefix (NOTE: it will be erased after state file is read)
Definition: colvarproxy_io.cpp:155
int close_input_streams()
Closes all input streams.
Definition: colvarproxy_io.cpp:317
std::string const & input_prefix() const
Prefix of the input state file to be read next.
Definition: colvarproxy_io.h:70
virtual ~colvarproxy_io()
Destructor.
Definition: colvarproxy_io.cpp:40
std::map< std::string, std::istream * > input_streams_
Container of input files/channels indexed by path name.
Definition: colvarproxy_io.h:175