Refactor flag handling: move FlagParser implementation to flag.cc, remove ResourceManager, and update includes

This commit is contained in:
scawful
2025-01-26 13:36:27 -05:00
parent c86fc17b13
commit c8d4efda2e
5 changed files with 53 additions and 74 deletions

View File

@@ -17,6 +17,7 @@ set(
set(
YAZE_UTIL_SRC
util/bps.cc
util/flag.cc
util/hex.cc
)
@@ -133,8 +134,7 @@ if (YAZE_BUILD_LIB)
FILES
incl/yaze.h
incl/zelda.h
incl/snes_tile.h
incl/snes_color.h
incl/snes.h
incl/overworld.h
incl/dungeon.h
DESTINATION

View File

@@ -8,7 +8,6 @@
#include "app/editor/system/constant_manager.h"
#include "app/editor/system/extension_manager.h"
#include "app/editor/system/history_manager.h"
#include "app/editor/system/resource_manager.h"
namespace yaze {
@@ -23,7 +22,6 @@ struct EditorContext {
CommandManager command_manager;
ExtensionManager extension_manager;
HistoryManager history_manager;
ResourceManager resource_manager;
};
enum class EditorType {

View File

@@ -1,28 +0,0 @@
#ifndef YAZE_APP_EDITOR_SYSTEM_RESOURCE_MANAGER_H
#define YAZE_APP_EDITOR_SYSTEM_RESOURCE_MANAGER_H
#include <cstddef>
namespace yaze {
namespace editor {
// System resource manager.
class ResourceManager {
public:
ResourceManager() : count_(0) {}
~ResourceManager() = default;
void Load(const char* path);
void Unload(const char* path);
void UnloadAll();
size_t Count() const;
private:
size_t count_;
};
} // namespace editor
} // namespace yaze
#endif // YAZE_APP_EDITOR_SYSTEM_RESOURCE_MANAGER_H

50
src/util/flag.cc Normal file
View File

@@ -0,0 +1,50 @@
#include "flag.h"
namespace yaze {
namespace util {
void FlagParser::Parse(std::vector<std::string>* tokens) {
std::vector<std::string> leftover;
leftover.reserve(tokens->size());
for (size_t i = 0; i < tokens->size(); i++) {
const std::string& token = (*tokens)[i];
if (token.rfind("--", 0) == 0) {
// Found a token that starts with "--".
std::string flag_name;
std::string value_string;
if (!ExtractFlagAndValue(token, &flag_name, &value_string)) {
// If no value found after '=', see if next token is a value.
if ((i + 1) < tokens->size()) {
const std::string& next_token = (*tokens)[i + 1];
// If next token is NOT another flag, treat it as the value.
if (next_token.rfind("--", 0) != 0) {
value_string = next_token;
i++;
} else {
// If no explicit value, treat it as boolean 'true'.
value_string = "true";
}
} else {
value_string = "true";
}
flag_name = token;
}
// Attempt to parse the flag (strip leading dashes in the registry).
IFlag* flag_ptr = registry_->GetFlag(flag_name);
if (!flag_ptr) {
throw std::runtime_error("Unrecognized flag: " + flag_name);
}
// Set the parsed value on the matching flag.
flag_ptr->ParseValue(value_string);
} else {
leftover.push_back(token);
}
}
*tokens = leftover;
}
} // namespace util
} // namespace yaze

View File

@@ -129,48 +129,7 @@ class FlagParser {
// Parses flags out of the given token list. Recognizes forms:
// --flag=value or --flag value
// Any token not recognized as a flag is left in `leftover`.
void Parse(std::vector<std::string>* tokens) {
std::vector<std::string> leftover;
leftover.reserve(tokens->size());
for (size_t i = 0; i < tokens->size(); i++) {
const std::string& token = (*tokens)[i];
if (token.rfind("--", 0) == 0) {
// Found a token that starts with "--".
std::string flag_name;
std::string value_string;
if (!ExtractFlagAndValue(token, &flag_name, &value_string)) {
// If no value found after '=', see if next token is a value.
if ((i + 1) < tokens->size()) {
const std::string& next_token = (*tokens)[i + 1];
// If next token is NOT another flag, treat it as the value.
if (next_token.rfind("--", 0) != 0) {
value_string = next_token;
i++;
} else {
// If no explicit value, treat it as boolean 'true'.
value_string = "true";
}
} else {
value_string = "true";
}
flag_name = token;
}
// Attempt to parse the flag (strip leading dashes in the registry).
IFlag* flag_ptr = registry_->GetFlag(flag_name);
if (!flag_ptr) {
throw std::runtime_error("Unrecognized flag: " + flag_name);
}
// Set the parsed value on the matching flag.
flag_ptr->ParseValue(value_string);
} else {
leftover.push_back(token);
}
}
*tokens = leftover;
}
void Parse(std::vector<std::string>* tokens);
private:
FlagRegistry* registry_;