f5d2fd67b2b1d1ffe9c33a759a85e29091763e2b
[alexxy/gromacs.git] / src / gromacs / coordinateio / outputadaptercontainer.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019, 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 /*! \file
36  * \brief
37  * Declares gmx::OutputAdapterContainer, a storage object for
38  * multiple outputadapters derived from the IOutputadaper interface.
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \inlibraryapi
42  * \ingroup module_coordinateio
43  */
44 #ifndef GMX_COORDINATEIO_OUTPUTADAPTERCONTAINER_H
45 #define GMX_COORDINATEIO_OUTPUTADAPTERCONTAINER_H
46
47 #include <memory>
48 #include <vector>
49
50 #include "gromacs/coordinateio/ioutputadapter.h"
51 #include "gromacs/utility/arrayref.h"
52 #include "gromacs/utility/classhelpers.h"
53 #include "gromacs/utility/enumerationhelpers.h"
54
55 namespace gmx
56 {
57
58 /*! \libinternal \brief
59  * Storage for output adapters that modify the state of a t_trxframe object.
60  *
61  * The OutputAdapterContainer is responsible for storing the number of
62  * OutputAdapters, as well as the bitmask representing the current requirements
63  * for constructing an CoordinateFile object with the modules registered. It is responsible
64  * for ensuring that no module can be registered multiple times, and that the
65  * correct order for some modifications is observed (e.g. we can not reduce the
66  * number of coordinates written to a file before we have set all the other flags).
67  * Any other behaviour indicates a programming error and triggers an assertion.
68  *
69  * The abilities that need to be cross checked for the container are usually constrained
70  * by the file format the coordinate data will be written to. When declaring new abilities,
71  * these must match the file type for the output.
72  *
73  * \todo Keeping track of those abilities has to be the responsibility of an object
74  *       implementing and interface that declares it capabilities and will execute the
75  *       the function of writing to a file.
76  * \todo This could be changed to instead construct the container with a pointer to an
77  *       ICoordinateOutputWriter that can be passed to the IOutputAdapter modules to check
78  *       their cross-dependencies.
79  */
80 class OutputAdapterContainer
81 {
82 public:
83     //! Only allow constructing the container with defined output abilities.
84     explicit OutputAdapterContainer(unsigned long abilities) : abilities_(abilities) {}
85     //! Allow abilities to be also defined using the enum class.
86     explicit OutputAdapterContainer(CoordinateFileFlags abilities) :
87         abilities_(convertFlag(abilities))
88     {
89     }
90
91     /*! \brief
92      * Add an adapter of a type not previously added.
93      *
94      * Only one adapter of each type can be registered, and the order of adapters
95      * is predefined in the underlying storage object.
96      * Calls internal checks to make sure that the new adapter does not violate
97      * any of the preconditions set to make an CoordinateFile object containing
98      * the registered modules.
99      *
100      * \param[in] adapter unique_ptr to adapter, with container taking ownership here.
101      * \param[in] type What kind of adapter is being added.
102      * \throws InternalError When registering an adapter of a type already registered .
103      * \throws InconsistentInputError When incompatible modules are added.
104      */
105     void addAdapter(OutputAdapterPointer adapter, CoordinateFileFlags type);
106
107     //! Get vector of all registered adapters.
108     ArrayRef<const OutputAdapterPointer> getAdapters() { return outputAdapters_; }
109     //! Get info if we have any registered adapters.
110     bool isEmpty() const;
111
112 private:
113     //! Array of registered modules.
114     EnumerationArray<CoordinateFileFlags, OutputAdapterPointer> outputAdapters_;
115     //! Construction time bitmask declaring what the OutputManager can do.
116     unsigned long abilities_ = convertFlag(CoordinateFileFlags::Base);
117 };
118
119 } // namespace gmx
120
121 #endif