Improve MessageStringCollector
[alexxy/gromacs.git] / src / gromacs / utility / stringtoenumvalueconverter.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \file
36  * \brief Defines helper function object for class enumerations
37  *
38  * This helper type facilitates efficient lookup of values from
39  * an enumeration identified by a string, given a pre-declared mapping of
40  * values to such strings.
41  *
42  * It is separated from enumerationhelpers.h because it is not as
43  * widely used and brings in several extra dependencies not needed by
44  * that header.
45  *
46  * \author Mark Abraham <mark.j.abraham@gmail.com>
47  * \inlibraryapi
48  * \ingroup module_utility
49  */
50 #ifndef GMX_UTILITY_STRINGTOENUMVALUECONVERTER_H
51 #define GMX_UTILITY_STRINGTOENUMVALUECONVERTER_H
52
53 #include <map>
54 #include <optional>
55 #include <string>
56
57 #include "enumerationhelpers.h"
58 #include "stringcompare.h"
59 #include "stringutil.h"
60
61 namespace gmx
62 {
63
64 /*! \brief Enum class for whether StringToEnumValueConverter will strip strings
65  * of leading and trailing whitespace before comparison. */
66 enum class StripStrings : int
67 {
68     //! Do not strip strings
69     No,
70     //! Strip strings
71     Yes
72 };
73
74 /*! \brief A class to convert a string to an enum value of type \c EnumType.
75  *
76  * It can be configured:
77  *  - to match different enumerations,
78  *  - to convert enumerations to strings in a custom way,
79  *  - either strip strings of leading and trailing whitespace before
80  *    comparison or not,
81  *  - with different handling of how the string characters are compared.
82  *
83  * Usage example:
84  *
85  *    enum class Foo : int {
86  *       Fizz, Buzz, Count, Default = Fizz
87  *    };
88  *    StringToEnumValueConverter<Foo, enumValueToString> converter;
89  *    Foo type = converter.valueFrom(theString);
90  *
91  * \tparam EnumType                    A class enum for which enumValueToString
92  *                                     is defined and maps all values
93  *                                     (except EnumType::Count) to a string.
94  * \tparam enumValueToStringFunction   Function to convert EnumValue to string, which
95  *                                     is typically enumValueToString, per convention
96  * \tparam stringCompareType           Indicates how the string should be compared
97  *                                     with respect to case, hyphens, underscores, etc.
98  * \tparam stripStrings                Indicates whether strings should have leading
99  *                                     and trailing whitespace removed before comparison
100  */
101 template<typename EnumType,
102          const char*(enumValueToStringFunction)(EnumType),
103          StringCompareType stringCompareType = StringCompareType::Exact,
104          StripStrings      stripStrings      = StripStrings::No>
105 class StringToEnumValueConverter
106 {
107 public:
108     StringToEnumValueConverter() : stringToEnumValue_(stringCompareType)
109     {
110         for (const auto type : EnumerationWrapper<EnumType>{})
111         {
112             GMX_RELEASE_ASSERT(type != EnumType::Count,
113                                "EnumerationWrapper<EnumType> should never return EnumType::Count");
114             std::string stringFromType = enumValueToStringFunction(type);
115             if (stripStrings == StripStrings::Yes)
116             {
117                 // Ensure leading and trailing spaces are removed
118                 stringFromType = stripString(stringFromType);
119             }
120             stringToEnumValue_[stringFromType] = type;
121         }
122     }
123
124     //! Return an optional enum value identified from the \c s (which is never EnumType::Count)
125     std::optional<EnumType> valueFrom(const std::string& s) const
126     {
127         typename decltype(stringToEnumValue_)::const_iterator typeIt;
128         if (stripStrings == StripStrings::Yes)
129         {
130             // Ensure leading and trailing spaces are removed
131             typeIt = stringToEnumValue_.find(stripString(s));
132         }
133         else
134         {
135             typeIt = stringToEnumValue_.find(s);
136         }
137         return (typeIt != stringToEnumValue_.end()) ? std::make_optional(typeIt->second) : std::nullopt;
138     }
139
140 private:
141     /*! \brief Map of strings to enumeration values.
142      *
143      * By construction, those values never include
144      * EnumType::Count. */
145     std::map<std::string, EnumType, StringCompare> stringToEnumValue_;
146 };
147
148 } // namespace gmx
149
150 #endif