refactor: enum utility allow overriding

This commit is contained in:
Austin Horstman
2023-09-09 11:18:12 -05:00
parent 8ce64ea784
commit 2b8c92e8fd
3 changed files with 21 additions and 16 deletions

View File

@ -1,26 +1,25 @@
#pragma once
#include <cctype>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
namespace waybar::util {
template <typename EnumType>
struct EnumParser {
EnumParser() {}
enum SORT_METHOD { ID, NAME, NUMBER, DEFAULT };
SORT_METHOD sortStringToEnum(const std::string& str) {
// Convert the input string to uppercase (make it lenient on config input)
EnumType sortStringToEnum(const std::string& str,
const std::map<std::string, EnumType>& enumMap) {
// Convert the input string to uppercase
std::string uppercaseStr;
for (char c : str) {
uppercaseStr += std::toupper(c);
uppercaseStr += std::toupper(c);
}
static const std::map<std::string, SORT_METHOD> enumMap = {
{"ID", ID}, {"NAME", NAME}, {"NUMBER", NUMBER}, {"DEFAULT", DEFAULT}};
auto it = enumMap.find(uppercaseStr);
if (it != enumMap.end()) {
return it->second;