clang-tidy: google tests applicable
[alexxy/gromacs.git] / src / programs / mdrun / tests / trajectoryreader.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2018, 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35
36 /*! \internal \file
37  * \brief Declares interface of a class for tests that want to inspect
38  * trajectories produced by mdrun.
39  *
40  * Intended usage is to create a TrajectoryFrameReader. Successive
41  * calls to its readNextFrameStub and frame methods will return a handle
42  * to a t_trxframe object for each frame.
43  *
44  * \author Mark Abraham <mark.j.abraham@gmail.com>
45  * \ingroup module_mdrun_integration_tests
46  */
47 #ifndef GMX_PROGRAMS_MDRUN_TESTS_TRAJECTORYREADER_H
48 #define GMX_PROGRAMS_MDRUN_TESTS_TRAJECTORYREADER_H
49
50 #include <memory>
51 #include <string>
52
53 #include "gromacs/fileio/oenv.h"
54 #include "gromacs/fileio/trxio.h"
55 #include "gromacs/trajectory/trajectoryframe.h"
56 #include "gromacs/utility/classhelpers.h"
57 #include "gromacs/utility/unique_cptr.h"
58
59 namespace gmx
60 {
61
62 //! Forward declaration
63 class TrajectoryFrame;
64
65 namespace test
66 {
67
68 //! Convenience smart pointer typedef
69 typedef unique_cptr<gmx_output_env_t, output_env_done> oenv_ptr;
70 //! Convenience smart pointer typedef
71 typedef unique_cptr<t_trxstatus, close_trx> trxstatus_file_ptr;
72 //! Helper function to free all resources
73 void done_trxframe(t_trxframe *fr);
74 //! Convenience smart pointer typedef
75 typedef unique_cptr<t_trxframe, done_trxframe> trxframe_ptr;
76
77 /*! \internal
78  * \brief Manages returning a t_trxframe whose contents were read from
79  * successive frames of an trajectory file. */
80 class TrajectoryFrameReader
81 {
82     public:
83         /*! \brief Attempt to read the next frame from the trajectory file.
84          *
85          * \return Whether a frame was available to read.
86          *
87          * This call wraps the read_first_frame()/read_next_frame()
88          * API, which does the file opening as a side effect of
89          * reading the first frame.
90          *
91          * If true is returned, then TrajectoryFrame frame() should be called
92          * to get access to the data. If false is returned, then no
93          * further data exists and no further call to
94          * readNextFrameStub or TrajectoryFrame frame() should occur.
95          *
96          * \throws FileIOError upon reading the first frame, if the trajectory file cannot be opened
97          * \throws APIError    if an earlier probe has not been properly handled
98          *                     (by calling frame(), or stopping trying to read
99          *                     from the file). */
100         bool readNextFrame();
101         /*! \brief Return the next frame from the trajectory file.
102          *
103          * If the next frame has not been probed for, then probe for
104          * it. If no next frame exists, then throw APIError, because
105          * user code should have called readNextFrameStub itself if this
106          * is possible. (This permits user code to avoid making calls
107          * to readNextFrameStub in a case where it already knows that
108          * the frame exists.)
109          *
110          * \throws APIError  if no next frame exists, or if it lacks either time or step number. */
111         TrajectoryFrame frame();
112         /*! \brief Constructor
113          *
114          * \param[in] filename  Name of trajectory file to open and read. */
115         explicit TrajectoryFrameReader(const std::string &filename);
116     private:
117         //! Name of trajectory file to open and read
118         std::string        filename_;
119         //! Owning handle of output environment object
120         oenv_ptr           oenvGuard_;
121         //! Owning handle of an open trajectory file ready to read frames.
122         trxstatus_file_ptr trajectoryFileGuard_;
123         //! Owning handle of contents of trajectory file frame after reading.
124         const trxframe_ptr trxframeGuard_;
125         //! Whether the first frame has been read
126         bool               haveReadFirstFrame_;
127         //! Whether the API has been used properly (ie. probe before reading).
128         bool               haveProbedForNextFrame_;
129         //! Whether there has been a probe that found a next frame.
130         bool               nextFrameExists_;
131
132         // Multiple owners of these resources isn't very sensible, so prevent it
133         GMX_DISALLOW_COPY_AND_ASSIGN(TrajectoryFrameReader);
134 };
135
136 //! Convenience smart pointer typedef
137 typedef std::unique_ptr<TrajectoryFrameReader> TrajectoryFrameReaderPtr;
138
139 }  // namespace test
140 }  // namespace gmx
141
142 #endif