Skip to content

Commit

Permalink
refactor: change param to string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Oct 6, 2024
1 parent 387069b commit 0a89e9f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
37 changes: 23 additions & 14 deletions src/ll/api/command/OverloadData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ std::vector<CommandParameterData>& OverloadData::getParams() { return impl->para
CommandHandle& OverloadData::getHandle() { return impl->handle; }
std::weak_ptr<mod::Mod>& OverloadData::getMod() { return impl->mod; }

char const* OverloadData::storeStr(std::string_view str) { return impl->handle.storeStr(str); }
char const* OverloadData::storeStr(std::string_view str) {
if (str.empty()) {
return nullptr;
}
return impl->handle.storeStr(str);
}

std::lock_guard<std::recursive_mutex> OverloadData::lock() { return std::lock_guard{impl->mutex}; }

Expand All @@ -55,35 +60,39 @@ CommandParameterData& OverloadData::addParamImpl(
int flagOffset,
bool optional
) {
//TODO: remove in release
return addParamImpl(id, parser, name, type, enumNameOrPostfix, nullptr, offset, flagOffset, optional);
// TODO: remove in release
return addParamImpl(
id,
parser,
name,
type,
enumNameOrPostfix ? enumNameOrPostfix : "",
{},
offset,
flagOffset,
optional
);
}

CommandParameterData& OverloadData::addParamImpl(
Bedrock::typeid_t<CommandRegistry> id,
CommandRegistry::ParseFn parser,
std::string_view name,
CommandParameterDataType type,
char const* enumNameOrPostfix,
char const* subChain,
std::string_view enumNameOrPostfix,
std::string_view subChain,
int offset,
int flagOffset,
bool optional
) {
std::lock_guard lock{impl->mutex};
if (enumNameOrPostfix) {
enumNameOrPostfix = storeStr(enumNameOrPostfix);
}
if (subChain) {
subChain = storeStr(subChain);
}
auto& param = impl->params.emplace_back(
auto& param = impl->params.emplace_back(
id,
parser,
std::string{name},
type,
enumNameOrPostfix,
subChain,
storeStr(enumNameOrPostfix),
storeStr(subChain),
offset,
optional,
flagOffset
Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/command/OverloadData.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class OverloadData {
CommandRegistry::ParseFn parser,
std::string_view name,
CommandParameterDataType type,
char const* enumNameOrPostfix,
char const* subChain,
std::string_view enumNameOrPostfix,
std::string_view subChain,
int offset,
int flagOffset,
bool optional
Expand Down
12 changes: 6 additions & 6 deletions src/ll/api/command/ParamTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ template <class T>
struct ParamTraits {
static constexpr CommandParameterDataType dataType() { return CommandParameterDataType::Basic; }
static inline CommandRegistry::ParseFn parser() { return &CommandRegistry::parse<T>; }
static constexpr char const* enumNameOrPostfix() { return nullptr; }
static constexpr char const* subChain() { return nullptr; }
static constexpr std::string_view enumNameOrPostfix() { return {}; }
static constexpr std::string_view subChain() { return {}; }
static Bedrock::typeid_t<CommandRegistry> typeId() { return Bedrock::type_id<CommandRegistry, T>(); }
static constexpr void transformData(CommandParameterData&) {}
};
template <concepts::Require<std::is_enum> T>
struct ParamTraits<T> {
static constexpr CommandParameterDataType dataType() { return CommandParameterDataType::Enum; }
static inline CommandRegistry::ParseFn parser() { return &CommandRegistry::parse<T>; }
static constexpr char const* enumNameOrPostfix() { return enum_name_v<T>; }
static constexpr char const* subChain() { return nullptr; }
static constexpr std::string_view enumNameOrPostfix() { return enum_name_v<T>; }
static constexpr std::string_view subChain() { return {}; }
static Bedrock::typeid_t<CommandRegistry> typeId() { return Bedrock::type_id<CommandRegistry, T>(); }
static void transformData(CommandParameterData&) { CommandRegistrar::getInstance().template tryRegisterEnum<T>(); }
};
template <concepts::Specializes<SoftEnum> T>
struct ParamTraits<T> {
static constexpr CommandParameterDataType dataType() { return CommandParameterDataType::SoftEnum; }
static inline CommandRegistry::ParseFn parser() { return &CommandRegistry::parse<std::string>; }
static constexpr char const* enumNameOrPostfix() { return enum_name_v<T>; }
static constexpr char const* subChain() { return nullptr; }
static constexpr std::string_view enumNameOrPostfix() { return enum_name_v<T>; }
static constexpr std::string_view subChain() { return {}; }
static Bedrock::typeid_t<CommandRegistry> typeId() { return Bedrock::type_id<CommandRegistry, std::string>(); }
static void transformData(CommandParameterData&) {
CommandRegistrar::getInstance().template tryRegisterSoftEnum<T>();
Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/command/runtime/RuntimeOverload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void RuntimeOverload::addParam(std::string_view name, ParamKindType kind, Comman
&CommandRegistry::parse<ParamType>,
name,
type,
nullptr,
nullptr,
{},
{},
offset,
offset + OptionalOffsetGetter<ParamStorageType::value_type>::value,
true
Expand Down

0 comments on commit 0a89e9f

Please sign in to comment.