Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / messagestringcollector.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013,2014, 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 /*! \libinternal \file
36  * \brief
37  * Declares ::gmx::MessageStringCollector.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H
44 #define GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H
45
46 #include <string>
47
48 #include "gromacs/utility/common.h"
49
50 namespace gmx
51 {
52
53 /*! \libinternal \brief
54  * Helper class for collecting message strings, optionally with context.
55  *
56  * After strings have been collected, they can be formatted into one long
57  * string for, e.g., printing out or for including in an exception.
58  *
59  * \inlibraryapi
60  * \ingroup module_utility
61  */
62 class MessageStringCollector
63 {
64     public:
65         MessageStringCollector();
66         ~MessageStringCollector();
67
68         /*! \brief
69          * Starts a context for messages.
70          *
71          * \param[in] name  Short description of the context.
72          *
73          * \see finishContext()
74          * \see MessageStringContext
75          */
76         void startContext(const char *name);
77         /*! \brief
78          * Convenience wrapper for startContext(const char *).
79          */
80         void startContext(const std::string &name)
81         { startContext(name.c_str()); }
82         /*! \brief
83          * Adds a new message.
84          */
85         void append(const char *message)
86         { append(std::string(message)); }
87         /*! \brief
88          * Adds a new message.
89          */
90         void append(const std::string &message);
91         /*! \brief
92          * Ends a context started with startContext().
93          *
94          * \see MessageStringContext
95          */
96         void finishContext();
97         /*! \brief
98          * Clears all collected messages.
99          */
100         void clear();
101
102         /*! \brief
103          * Returns true if any messages have been added.
104          *
105          * \returns true if append() has been called at least once.
106          *
107          * The return value is identical to `toString().empty()`.
108          * Calls to startContext() or finishContext() only do not cause this
109          * function to return true.
110          */
111         bool isEmpty() const;
112         /*! \brief
113          * Returns all collected messages as one string.
114          */
115         std::string toString() const;
116
117     private:
118         class Impl;
119
120         PrivateImplPointer<Impl> impl_;
121 };
122
123 /*! \libinternal \brief
124  * Convenience class for creating a message context.
125  *
126  * This class provides a RAII-style interface to the
127  * MessageStringCollector::startContext() and
128  * MessageStringCollector::finishContext() methods: finishContext() is called
129  * upon destruction of the object.  This avoids the need to call
130  * MessageStringCollector::finishContext() on every possible exit point.
131  *
132  * Example usage:
133  * \code
134    bool function(::gmx::MessageStringCollector *errors)
135    {
136        ::gmx::MessageStringContext errcontext(errors, "In function()");
137        bool bOk = function2(errors);
138        bOk = function3(errors) && bOk;
139        // <more processing>
140        return bOk;
141    }
142    \endcode
143  *
144  * \see MessageStringCollector
145  * \inlibraryapi
146  * \ingroup module_utility
147  */
148 class MessageStringContext
149 {
150     public:
151         /*! \brief
152          * Adds a context for the given object.
153          */
154         MessageStringContext(MessageStringCollector *collector, const char *name)
155             : collector_(*collector)
156         {
157             collector_.startContext(name);
158         }
159         /*! \brief
160          * Adds a context for the given object.
161          */
162         MessageStringContext(MessageStringCollector *collector,
163                              const std::string      &name)
164             : collector_(*collector)
165         {
166             collector_.startContext(name);
167         }
168         /*! \brief
169          * Calls MessageStringCollector::finishContext() on the wrapped object.
170          */
171         ~MessageStringContext()
172         {
173             collector_.finishContext();
174         }
175
176     private:
177         //! The wrapped object.
178         MessageStringCollector &collector_;
179
180         GMX_DISALLOW_COPY_AND_ASSIGN(MessageStringContext);
181 };
182
183 } // namespace gmx
184
185 #endif