Warn for type mismatch for gmx printf like functions 2/3
[gromacs.git] / src / gromacs / utility / textstream.h
bloba6e522bd8c3be41d027cfe62d446d4ecce9f612e
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS 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 GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
36 * \brief
37 * Declares interfaces for simple input/output streams.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \inlibraryapi
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_TEXTSTREAM_H
44 #define GMX_UTILITY_TEXTSTREAM_H
46 #include <memory>
48 namespace gmx
51 /*! \libinternal \brief
52 * Interface for reading text.
54 * Concrete implementations can read the text from, e.g., a file or an in-memory
55 * string. The main use is to allow unit tests to inject in-memory buffers
56 * instead of writing files to be read by the code under test, but there are
57 * also use cases outside the tests where it is useful to abstract out whether
58 * the input is from a real file or something else.
60 * To use more advanced formatting than reading raw lines, use TextReader.
62 * Both methods in the interface can throw std::bad_alloc or other exceptions
63 * that indicate failures to read from the stream.
65 * \inlibraryapi
66 * \ingroup module_utility
68 class TextInputStream
70 public:
71 virtual ~TextInputStream() {}
73 /*! \brief
74 * Reads a line (with newline included) from the stream.
76 * \param[out] line String to receive the line.
77 * \returns `false` if nothing was read because the stream ended.
79 * On error or when `false` is returned, \p line will be empty.
81 virtual bool readLine(std::string *line) = 0;
82 /*! \brief
83 * Closes the stream.
85 * It is not allowed to read from a stream after it has been closed.
86 * See TextOutputStream::close() for rationale for a close() method
87 * separate from the destructor. For input, failures during close
88 * should be rare, but it is clearer to keep the interface symmetric.
90 virtual void close() = 0;
93 /*! \libinternal \brief
94 * Interface for writing text.
96 * Concrete implementations can write the text to, e.g., a file or an in-memory
97 * string. The main use is to allow unit tests to inject in-memory buffers
98 * instead of reading in files produced by the code under test, but there are
99 * also use cases outside the tests where it is useful to abstract out whether
100 * the output is into a real file or something else.
102 * To use more advanced formatting than writing plain strings, use TextWriter.
104 * The current implementation assumes text-only output in several places, but
105 * this interface could possibly be generalized also for binary files.
106 * However, since all binary files currently written by \Gromacs are either
107 * XDR- or TNG-based, they may require a different approach. Also, it is worth
108 * keeping the distinction between text and binary files clear, since Windows
109 * does transparent `LF`-`CRLF` newline translation for text files, so mixing
110 * modes when reading and/or writing the same file can cause subtle issues.
112 * Both methods in the interface can throw std::bad_alloc or other exceptions
113 * that indicate failures to write to the stream.
115 * \inlibraryapi
116 * \ingroup module_utility
118 class TextOutputStream
120 public:
121 virtual ~TextOutputStream() {}
123 /*! \brief
124 * Writes a given string to the stream.
126 virtual void write(const char *text) = 0;
127 /*! \brief
128 * Closes the stream.
130 * It is not allowed to write to a stream after it has been closed.
131 * A method separate from the destructor is provided such that errors
132 * that occur while closing the stream (e.g., when closing the file)
133 * can be handled using exceptions.
134 * The destructor is not allowed to throw, so code that wants to
135 * observe such errors needs to call close() after it has finished
136 * writing to the stream.
138 virtual void close() = 0;
141 //! Shorthand for a smart pointer to a TextInputStream.
142 typedef std::shared_ptr<TextInputStream> TextInputStreamPointer;
143 //! Shorthand for a smart pointer to a TextOutputStream.
144 typedef std::shared_ptr<TextOutputStream> TextOutputStreamPointer;
146 } // namespace gmx
148 #endif