Gromacs
2025.0-dev-20241011-013a99c
|
#include <gromacs/utility/include/gromacs/utility/textstream.h>
Interface for writing text.
Concrete implementations can write the text to, e.g., a file or an in-memory string. The main use is to allow unit tests to inject in-memory buffers instead of reading in files produced by the code under test, but there are also use cases outside the tests where it is useful to abstract out whether the output is into a real file or something else.
To use more advanced formatting than writing plain strings, use TextWriter.
The current implementation assumes text-only output in several places, but this interface could possibly be generalized also for binary files. However, since all binary files currently written by GROMACS are either XDR- or TNG-based, they may require a different approach. Also, it is worth keeping the distinction between text and binary files clear, since Windows does transparent LF
-CRLF
newline translation for text files, so mixing modes when reading and/or writing the same file can cause subtle issues.
Both methods in the interface can throw std::bad_alloc or other exceptions that indicate failures to write to the stream.
Public Member Functions | |
virtual void | write (const char *text)=0 |
Writes a given string to the stream. | |
virtual void | close ()=0 |
Closes the stream. More... | |
|
pure virtual |
Closes the stream.
It is not allowed to write to a stream after it has been closed. A method separate from the destructor is provided such that errors that occur while closing the stream (e.g., when closing the file) can be handled using exceptions. The destructor is not allowed to throw, so code that wants to observe such errors needs to call close() after it has finished writing to the stream.
Implemented in gmx::TextOutputFile, and gmx::StringOutputStream.