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