mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-06 17:41:04 +00:00
Move EnumString to separate file and add enum_to_string (#15714)
This commit is contained in:
parent
bee541f378
commit
e9826f7819
22 changed files with 90 additions and 75 deletions
|
@ -18,4 +18,5 @@ set(util_SRCS
|
|||
${CMAKE_CURRENT_SOURCE_DIR}/srp.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/timetaker.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/png.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/enum_string.cpp
|
||||
PARENT_SCOPE)
|
||||
|
|
31
src/util/enum_string.cpp
Normal file
31
src/util/enum_string.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2025 cx384
|
||||
|
||||
#include "util/enum_string.h"
|
||||
#include <cassert>
|
||||
|
||||
bool string_to_enum(const EnumString *spec, int &result, std::string_view str)
|
||||
{
|
||||
const EnumString *esp = spec;
|
||||
while (esp->str) {
|
||||
if (str == esp->str) {
|
||||
assert(esp->num >= 0);
|
||||
result = esp->num;
|
||||
return true;
|
||||
}
|
||||
esp++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *enum_to_string(const EnumString *spec, int num)
|
||||
{
|
||||
if (num < 0)
|
||||
return nullptr;
|
||||
// assume array order matches enum order
|
||||
auto *p = &spec[num];
|
||||
assert(p->num == num);
|
||||
assert(p->str);
|
||||
return p->str;
|
||||
}
|
17
src/util/enum_string.h
Normal file
17
src/util/enum_string.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
// Luanti
|
||||
// SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
// Copyright (C) 2025 cx384
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
struct EnumString
|
||||
{
|
||||
int num;
|
||||
const char *str;
|
||||
};
|
||||
|
||||
bool string_to_enum(const EnumString *spec, int &result, std::string_view str);
|
||||
|
||||
const char *enum_to_string(const EnumString *spec, int num);
|
Loading…
Add table
Add a link
Reference in a new issue