refactor: enumparser create implementation file
parent
b8630968b2
commit
79cf33b9f1
|
@ -1,38 +1,19 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cctype>
|
|
||||||
#include <iostream>
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "util/string.hpp"
|
|
||||||
|
|
||||||
namespace waybar::util {
|
namespace waybar::util {
|
||||||
|
|
||||||
template <typename EnumType>
|
template <typename EnumType>
|
||||||
struct EnumParser {
|
struct EnumParser {
|
||||||
EnumParser() {}
|
public:
|
||||||
|
EnumParser();
|
||||||
|
~EnumParser();
|
||||||
|
|
||||||
EnumType parseStringToEnum(const std::string& str,
|
EnumType parseStringToEnum(const std::string& str,
|
||||||
const std::map<std::string, EnumType>& enumMap) {
|
const std::map<std::string, EnumType>& enumMap);
|
||||||
// Convert the input string to uppercase
|
|
||||||
std::string uppercaseStr = capitalize(str);
|
|
||||||
|
|
||||||
// Capitalize the map keys before searching
|
|
||||||
std::map<std::string, EnumType> capitalizedEnumMap;
|
|
||||||
std::transform(
|
|
||||||
enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
|
||||||
[this](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); });
|
|
||||||
|
|
||||||
// Return enum match of string
|
|
||||||
auto it = enumMap.find(uppercaseStr);
|
|
||||||
if (it != enumMap.end()) return it->second;
|
|
||||||
|
|
||||||
// Throw error if it doesnt return
|
|
||||||
throw std::invalid_argument("Invalid string representation for enum");
|
|
||||||
}
|
|
||||||
|
|
||||||
~EnumParser() = default;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::util
|
} // namespace waybar::util
|
||||||
|
|
|
@ -171,6 +171,7 @@ src_files = files(
|
||||||
'src/client.cpp',
|
'src/client.cpp',
|
||||||
'src/config.cpp',
|
'src/config.cpp',
|
||||||
'src/group.cpp',
|
'src/group.cpp',
|
||||||
|
'src/util/enum.cpp',
|
||||||
'src/util/prepare_for_sleep.cpp',
|
'src/util/prepare_for_sleep.cpp',
|
||||||
'src/util/ustring_clen.cpp',
|
'src/util/ustring_clen.cpp',
|
||||||
'src/util/sanitize_str.cpp',
|
'src/util/sanitize_str.cpp',
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
#include "util/enum.hpp"
|
||||||
|
|
||||||
|
#include <algorithm> // for std::transform
|
||||||
|
#include <cctype> // for std::toupper
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "modules/hyprland/workspaces.hpp"
|
||||||
|
#include "util/string.hpp"
|
||||||
|
|
||||||
|
namespace waybar::util {
|
||||||
|
|
||||||
|
template <typename EnumType>
|
||||||
|
EnumParser<EnumType>::EnumParser() = default;
|
||||||
|
|
||||||
|
template <typename EnumType>
|
||||||
|
EnumParser<EnumType>::~EnumParser() = default;
|
||||||
|
|
||||||
|
template <typename EnumType>
|
||||||
|
EnumType EnumParser<EnumType>::parseStringToEnum(const std::string& str,
|
||||||
|
const std::map<std::string, EnumType>& enumMap) {
|
||||||
|
// Convert the input string to uppercase
|
||||||
|
std::string uppercaseStr = capitalize(str);
|
||||||
|
|
||||||
|
// Capitalize the map keys before searching
|
||||||
|
std::map<std::string, EnumType> capitalizedEnumMap;
|
||||||
|
std::transform(
|
||||||
|
enumMap.begin(), enumMap.end(), std::inserter(capitalizedEnumMap, capitalizedEnumMap.end()),
|
||||||
|
[this](const auto& pair) { return std::make_pair(capitalize(pair.first), pair.second); });
|
||||||
|
|
||||||
|
// Return enum match of string
|
||||||
|
auto it = capitalizedEnumMap.find(uppercaseStr);
|
||||||
|
if (it != capitalizedEnumMap.end()) return it->second;
|
||||||
|
|
||||||
|
// Throw error if it doesn't return
|
||||||
|
throw std::invalid_argument("Invalid string representation for enum");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicit instantiations for specific EnumType types you intend to use
|
||||||
|
// Add explicit instantiations for all relevant EnumType types
|
||||||
|
template struct EnumParser<modules::hyprland::Workspaces::SORT_METHOD>;
|
||||||
|
|
||||||
|
} // namespace waybar::util
|
Loading…
Reference in New Issue