2025-01-26 19:17:14 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2025 cx384
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string_view>
|
2025-02-19 18:45:45 +01:00
|
|
|
#include <type_traits>
|
2025-01-26 19:17:14 +01:00
|
|
|
|
|
|
|
struct EnumString
|
|
|
|
{
|
|
|
|
int num;
|
|
|
|
const char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool string_to_enum(const EnumString *spec, int &result, std::string_view str);
|
|
|
|
|
2025-02-19 18:45:45 +01:00
|
|
|
template <typename T, std::enable_if_t<std::is_enum_v<T>, bool> = true>
|
|
|
|
bool string_to_enum(const EnumString *spec, T &result, std::string_view str)
|
|
|
|
{
|
|
|
|
int result_int = result;
|
|
|
|
bool ret = string_to_enum(spec, result_int, str);
|
|
|
|
result = static_cast<T>(result_int);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]] const char *enum_to_string(const EnumString *spec, int num);
|