2 * This file is part of the GROMACS molecular simulation package.
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.
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
37 * Declares ::gmx::MessageStringCollector.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H
44 #define GMX_UTILITY_MESSAGESTRINGCOLLECTOR_H
49 #include "gromacs/utility/classhelpers.h"
54 /*! \libinternal \brief
55 * Helper class for collecting message strings, optionally with context.
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.
61 * \ingroup module_utility
63 class MessageStringCollector
66 MessageStringCollector();
67 ~MessageStringCollector();
70 * Starts a context for messages.
72 * \param[in] name Short description of the context.
74 * \see finishContext()
75 * \see MessageStringContext
77 void startContext(const char* name);
79 * Convenience wrapper for startContext(const char *).
81 void startContext(const std::string& name) { startContext(name.c_str()); }
85 void append(const char* message) { append(std::string(message)); }
89 void append(const std::string& message);
91 * Ends a context started with startContext().
93 * \see MessageStringContext
97 * Clears all collected messages.
102 * Returns false if any messages have been added.
104 * \returns false if append() has been called at least once.
106 * The return value is identical to `toString().empty()`.
107 * Calls to startContext() or finishContext() do not affect the
108 * return value of this function.
110 bool isEmpty() const;
112 * Returns all collected messages as one string.
114 std::string toString() const;
119 std::unique_ptr<Impl> impl_;
122 /*! \libinternal \brief
123 * Convenience class for creating a message context.
125 * This class provides a RAII-style interface to the
126 * MessageStringCollector::startContext() and
127 * MessageStringCollector::finishContext() methods: finishContext() is called
128 * upon destruction of the object. This avoids the need to call
129 * MessageStringCollector::finishContext() on every possible exit point.
133 bool function(::gmx::MessageStringCollector *errors)
135 ::gmx::MessageStringContext errcontext(errors, "In function()");
136 bool bOk = function2(errors);
137 bOk = function3(errors) && bOk;
143 * \see MessageStringCollector
145 * \ingroup module_utility
147 class MessageStringContext
151 * Adds a context for the given object.
153 MessageStringContext(MessageStringCollector* collector, const char* name) :
154 collector_(*collector)
156 collector_.startContext(name);
159 * Adds a context for the given object.
161 MessageStringContext(MessageStringCollector* collector, const std::string& name) :
162 collector_(*collector)
164 collector_.startContext(name);
167 * Calls MessageStringCollector::finishContext() on the wrapped object.
169 ~MessageStringContext() { collector_.finishContext(); }
172 //! The wrapped object.
173 MessageStringCollector& collector_;
175 GMX_DISALLOW_COPY_AND_ASSIGN(MessageStringContext);