Skip to content
Snippets Groups Projects
Commit f1bd05aa authored by Felix Weiglhofer's avatar Felix Weiglhofer
Browse files

algo: Add case-senstive flag to FromString for enum conversion.

parent 1cbd92dc
No related branches found
No related tags found
1 merge request!1175algo: Allow reading enums as strings from YAML.
Pipeline #22523 passed
......@@ -77,6 +77,7 @@ target_link_libraries(Algo
fmt::fmt
Boost::program_options
Boost::filesystem
Boost::headers
xpu
external::yaml-cpp
external::fles_logging
......@@ -88,6 +89,7 @@ xpu_attach(Algo ${DEVICE_SRCS})
install(TARGETS Algo DESTINATION lib)
install(DIRECTORY base/compat TYPE INCLUDE FILES_MATCHING PATTERN "*.h")
install(DIRECTORY base/config TYPE INCLUDE FILES_MATCHING PATTERN "*.h")
install(DIRECTORY base/util TYPE INCLUDE FILES_MATCHING PATTERN "*.h")
install(DIRECTORY base/gpu TYPE INCLUDE FILES_MATCHING PATTERN "*.h")
install(DIRECTORY data/sts TYPE INCLUDE FILES_MATCHING PATTERN "*.h")
......
......@@ -4,6 +4,8 @@
#ifndef CBM_ALGO_BASE_UTIL_SERIALIZABLEENUM_H
#define CBM_ALGO_BASE_UTIL_SERIALIZABLEENUM_H
#include <boost/algorithm/string/predicate.hpp>
#include <algorithm>
#include <iostream>
#include <optional>
......@@ -59,10 +61,14 @@ namespace cbm::algo
inline const EnumDict_t<T> EnumDict;
template<typename T, typename = std::enable_if_t<EnumIsSerializable<T>::value>>
std::optional<T> FromString(std::string_view str)
std::optional<T> FromString(std::string_view str, bool caseSensitive = false)
{
const auto& set = EnumDict<T>;
auto it = std::find_if(set.begin(), set.end(), [str](const auto& pair) { return pair.first == str; });
auto it = std::find_if(set.begin(), set.end(), [&](const auto& pair) {
if (caseSensitive) return pair.first == str;
else
return boost::iequals(pair.first, str);
});
if (it == set.end()) return std::nullopt;
return it->second;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment