AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > > Struct Template Reference

#include "ChatCommandArgs.h"

Public Types

using SearchMap = std::map< std::string_view, Optional< T >, StringCompareLessI_T >
 

Static Public Member Functions

static SearchMap MakeSearchMap ()
 
static T const * Match (std::string_view s)
 
static ChatCommandResult TryConsume (T &val, ChatHandler const *handler, std::string_view args)
 

Static Public Attributes

static SearchMap const _map = MakeSearchMap()
 

Detailed Description

template<typename T>
struct Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > >

Member Typedef Documentation

◆ SearchMap

template<typename T >
using Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > >::SearchMap = std::map<std::string_view, Optional<T>, StringCompareLessI_T>

Member Function Documentation

◆ MakeSearchMap()

template<typename T >
static SearchMap Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > >::MakeSearchMap ( )
inlinestatic
131 {
132 SearchMap map;
133 for (T val : EnumUtils::Iterate<T>())
134 {
135 EnumText text = EnumUtils::ToString(val);
136
137 std::string_view title(text.Title);
138 std::string_view constant(text.Constant);
139
140 auto [constantIt, constantNew] = map.try_emplace(title, val);
141 if (!constantNew)
142 constantIt->second = std::nullopt;
143
144 if (title != constant)
145 {
146 auto [titleIt, titleNew] = map.try_emplace(title, val);
147 if (!titleNew)
148 titleIt->second = std::nullopt;
149 }
150 }
151 return map;
152 }
Definition: SmartEnum.h:24
char const *const Constant
Definition: SmartEnum.h:27
char const *const Title
Definition: SmartEnum.h:29
static EnumText ToString(Enum value)
Definition: SmartEnum.h:52
std::map< std::string_view, Optional< T >, StringCompareLessI_T > SearchMap
Definition: ChatCommandArgs.h:129

References EnumText::Constant, EnumText::Title, and EnumUtils::ToString().

◆ Match()

template<typename T >
static T const * Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > >::Match ( std::string_view  s)
inlinestatic
157 {
158 auto it = _map.lower_bound(s);
159 if (it == _map.end() || !StringStartsWithI(it->first, s)) // not a match
160 return nullptr;
161
162 if (!StringEqualI(it->first, s)) // we don't have an exact match - check if it is unique
163 {
164 auto it2 = it;
165 ++it2;
166 if ((it2 != _map.end()) && StringStartsWithI(it2->first, s)) // not unique
167 return nullptr;
168 }
169
170 if (it->second)
171 return &*it->second;
172 else
173 return nullptr;
174 }
bool StringStartsWithI(std::string_view haystack, std::string_view needle)
Definition: Util.h:402
bool StringEqualI(std::string_view a, std::string_view b)
Definition: Util.cpp:592
static SearchMap const _map
Definition: ChatCommandArgs.h:154

References StringEqualI(), and StringStartsWithI().

◆ TryConsume()

template<typename T >
static ChatCommandResult Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > >::TryConsume ( T &  val,
ChatHandler const *  handler,
std::string_view  args 
)
inlinestatic
177 {
178 std::string_view strVal;
179 ChatCommandResult next1 = ArgInfo<std::string_view>::TryConsume(strVal, handler, args);
180 if (next1)
181 {
182 if (T const* match = Match(strVal))
183 {
184 val = *match;
185 return next1;
186 }
187 }
188
189 // Value not found. Try to parse arg as underlying type and cast it to enum type
190 using U = std::underlying_type_t<T>;
191 U uVal = 0;
192 if (ChatCommandResult next2 = ArgInfo<U>::TryConsume(uVal, handler, args))
193 {
194 if (EnumUtils::IsValid<T>(uVal))
195 {
196 val = static_cast<T>(uVal);
197 return next2;
198 }
199 }
200
201 if (next1)
202 return FormatAcoreString(handler, LANG_CMDPARSER_STRING_VALUE_INVALID, strVal, GetTypeName<T>());
203 else
204 return next1;
205 }
@ LANG_CMDPARSER_STRING_VALUE_INVALID
Definition: Language.h:993
std::string FormatAcoreString(ChatHandler const *handler, AcoreStrings which, Ts &&... args)
Definition: ChatCommandHelpers.h:125
static T const * Match(std::string_view s)
Definition: ChatCommandArgs.h:156
Definition: ChatCommandHelpers.h:100

References Acore::Impl::ChatCommands::FormatAcoreString(), and LANG_CMDPARSER_STRING_VALUE_INVALID.

Member Data Documentation

◆ _map

template<typename T >
SearchMap const Acore::Impl::ChatCommands::ArgInfo< T, std::enable_if_t< std::is_enum_v< T > > >::_map = MakeSearchMap()
inlinestatic