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 std::string get_current_work_dir() const;
43
45 std::string join_paths(std::string const &path1, std::string const &path2) const;
46
48 virtual int backup_file(char const *filename);
49
51 inline int backup_file(std::string const &filename)
52 {
53 return backup_file(filename.c_str());
54 }
55
57 virtual int remove_file(char const *filename);
58
60 inline int remove_file(std::string const &filename)
61 {
62 return remove_file(filename.c_str());
63 }
64
66 virtual int rename_file(char const *filename, char const *newfilename);
67
69 inline int rename_file(std::string const &filename,
70 std::string const &newfilename)
71 {
72 return rename_file(filename.c_str(), newfilename.c_str());
73 }
74
76 inline std::string const & input_prefix() const
77 {
78 return input_prefix_str;
79 }
80
82 virtual int set_input_prefix(std::string const &prefix);
83
85 inline std::string const & output_prefix() const
86 {
87 return output_prefix_str;
88 }
89
91 virtual int set_output_prefix(std::string const &prefix);
92
94 inline std::string const & restart_output_prefix() const
95 {
97 }
98
100 virtual int set_restart_output_prefix(std::string const &prefix);
101
103 inline int default_restart_frequency() const
104 {
106 }
107
109 virtual int set_default_restart_frequency(int freq);
110
111 // The input stream functions below are not virtual, because they currently
112 // rely on the fact that either std::ifstream or std::istringstream is used
113
118 std::istream & input_stream(std::string const &input_name,
119 std::string const description = "file/channel",
120 bool error_on_fail = true);
121
126 std::istream & input_stream_from_string(std::string const &input_name,
127 std::string const &content,
128 std::string const description = "string");
129
131 bool input_stream_exists(std::string const &input_name);
132
134 int close_input_stream(std::string const &input_name);
135
138
140 int delete_input_stream(std::string const &input_name);
141
143 std::list<std::string> list_input_stream_names() const;
144
148 virtual std::ostream &output_stream(std::string const &output_name,
149 std::string const description);
150
152 virtual bool output_stream_exists(std::string const &output_name);
153
155 virtual int flush_output_stream(std::string const &output_name);
156
158 virtual int flush_output_streams();
159
161 virtual int close_output_stream(std::string const &output_name);
162
164 virtual int close_output_streams();
165
166protected:
167
169 std::string input_prefix_str;
170
172 std::string output_prefix_str;
173
176
179
181 std::map<std::string, std::istream *> input_streams_;
182
184 std::istream *input_stream_error_;
185
187 std::map<std::string, std::ostream *> output_streams_;
188
190 std::ostream *output_stream_error_;
191};
192
193
194#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:190
virtual int close_output_stream(std::string const &output_name)
Closes the given output file/channel.
Definition: colvarproxy_io.cpp:475
std::istream * input_stream_error_
Object whose reference is returned when read errors occur.
Definition: colvarproxy_io.h:184
std::string join_paths(std::string const &path1, std::string const &path2) const
Join two paths using the operating system's path separation.
Definition: colvarproxy_io.cpp:113
std::istream & input_stream_from_string(std::string const &input_name, std::string const &content, std::string const description="string")
Definition: colvarproxy_io.cpp:299
virtual int backup_file(char const *filename)
Rename the given file, before overwriting it.
Definition: colvarproxy_io.cpp:132
std::string output_prefix_str
Default prefix to be used for all output files (final configuration)
Definition: colvarproxy_io.h:172
std::istream & input_stream(std::string const &input_name, std::string const description="file/channel", bool error_on_fail=true)
Definition: colvarproxy_io.cpp:265
std::map< std::string, std::ostream * > output_streams_
Currently open output files/channels.
Definition: colvarproxy_io.h:187
virtual int get_frame(long int &)
Save the current frame number in the argument given.
Definition: colvarproxy_io.cpp:73
int rename_file(std::string const &filename, std::string const &newfilename)
Rename the given file.
Definition: colvarproxy_io.h:69
std::string get_current_work_dir() const
Get the current working directory of this process.
Definition: colvarproxy_io.cpp:85
std::string input_prefix_str
Prefix of the input state file to be read next.
Definition: colvarproxy_io.h:169
colvarproxy_io()
Constructor.
Definition: colvarproxy_io.cpp:48
virtual int set_restart_output_prefix(std::string const &prefix)
Set default restart state file prefix.
Definition: colvarproxy_io.cpp:244
int backup_file(std::string const &filename)
Rename the given file, before overwriting it.
Definition: colvarproxy_io.h:51
int default_restart_frequency() const
Default restart frequency (as set by the simulation engine)
Definition: colvarproxy_io.h:103
std::string const & restart_output_prefix() const
Prefix of the restart (checkpoint) file to be written next.
Definition: colvarproxy_io.h:94
int restart_frequency_engine
How often the simulation engine will write its own restart.
Definition: colvarproxy_io.h:178
virtual int set_frame(long int)
Set the current frame number (as well as colvarmodule::it)
Definition: colvarproxy_io.cpp:79
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:333
virtual std::ostream & output_stream(std::string const &output_name, std::string const description)
Definition: colvarproxy_io.cpp:408
int remove_file(std::string const &filename)
Remove the given file (on Windows only, rename to filename.old)
Definition: colvarproxy_io.h:60
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:437
virtual int flush_output_stream(std::string const &output_name)
Flushes the given output file/channel.
Definition: colvarproxy_io.cpp:443
virtual int flush_output_streams()
Flushes all output files/channels.
Definition: colvarproxy_io.cpp:459
std::string restart_output_prefix_str
Prefix of the restart (checkpoint) file to be written next.
Definition: colvarproxy_io.h:175
std::list< std::string > list_input_stream_names() const
List all input streams that were opened at some point.
Definition: colvarproxy_io.cpp:395
virtual int remove_file(char const *filename)
Remove the given file (on Windows only, rename to filename.old)
Definition: colvarproxy_io.cpp:164
virtual int set_default_restart_frequency(int freq)
Communicate/set the restart frequency of the simulation engine.
Definition: colvarproxy_io.cpp:256
virtual int rename_file(char const *filename, char const *newfilename)
Rename the given file.
Definition: colvarproxy_io.cpp:197
virtual int close_output_streams()
Close all open files/channels to prevent data loss.
Definition: colvarproxy_io.cpp:492
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:362
std::string const & output_prefix() const
Default prefix to be used for all output files (final configuration)
Definition: colvarproxy_io.h:85
virtual bool io_available()
Ensure that we're on the main thread (derived class will do actual check)
Definition: colvarproxy_io.cpp:67
int close_input_stream(std::string const &input_name)
Closes the given input stream.
Definition: colvarproxy_io.cpp:339
virtual int set_output_prefix(std::string const &prefix)
Set default output prefix.
Definition: colvarproxy_io.cpp:232
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:220
int close_input_streams()
Closes all input streams.
Definition: colvarproxy_io.cpp:382
std::string const & input_prefix() const
Prefix of the input state file to be read next.
Definition: colvarproxy_io.h:76
virtual ~colvarproxy_io()
Destructor.
Definition: colvarproxy_io.cpp:58
std::map< std::string, std::istream * > input_streams_
Container of input files/channels indexed by path name.
Definition: colvarproxy_io.h:181