2 * This file is part of the GROMACS molecular simulation package.
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.
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.
36 * \brief Defines helper function object for class enumerations
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.
42 * It is separated from enumerationhelpers.h because it is not as
43 * widely used and brings in several extra dependencies not needed by
46 * \author Mark Abraham <mark.j.abraham@gmail.com>
48 * \ingroup module_utility
50 #ifndef GMX_UTILITY_STRINGTOENUMVALUECONVERTER_H
51 #define GMX_UTILITY_STRINGTOENUMVALUECONVERTER_H
57 #include "enumerationhelpers.h"
58 #include "stringcompare.h"
59 #include "stringutil.h"
64 /*! \brief Enum class for whether StringToEnumValueConverter will strip strings
65 * of leading and trailing whitespace before comparison. */
66 enum class StripStrings : int
68 //! Do not strip strings
74 /*! \brief A class to convert a string to an enum value of type \c EnumType.
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
81 * - with different handling of how the string characters are compared.
85 * enum class Foo : int {
86 * Fizz, Buzz, Count, Default = Fizz
88 * StringToEnumValueConverter<Foo, enumValueToString> converter;
89 * Foo type = converter.valueFrom(theString);
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
101 template<typename EnumType,
102 const char*(enumValueToStringFunction)(EnumType),
103 StringCompareType stringCompareType = StringCompareType::Exact,
104 StripStrings stripStrings = StripStrings::No>
105 class StringToEnumValueConverter
108 StringToEnumValueConverter() : stringToEnumValue_(stringCompareType)
110 for (const auto type : EnumerationWrapper<EnumType>{})
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)
117 // Ensure leading and trailing spaces are removed
118 stringFromType = stripString(stringFromType);
120 stringToEnumValue_[stringFromType] = type;
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
127 typename decltype(stringToEnumValue_)::const_iterator typeIt;
128 if (stripStrings == StripStrings::Yes)
130 // Ensure leading and trailing spaces are removed
131 typeIt = stringToEnumValue_.find(stripString(s));
135 typeIt = stringToEnumValue_.find(s);
137 return (typeIt != stringToEnumValue_.end()) ? std::make_optional(typeIt->second) : std::nullopt;
141 /*! \brief Map of strings to enumeration values.
143 * By construction, those values never include
144 * EnumType::Count. */
145 std::map<std::string, EnumType, StringCompare> stringToEnumValue_;