TrinityCore
Loading...
Searching...
No Matches
ChatCommandHelpers.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITY_CHATCOMMANDHELPERS_H
19#define TRINITY_CHATCOMMANDHELPERS_H
20
21#include "Define.h"
22#include "Language.h"
23#include "Optional.h" // for std::nullopt
24#include <fmt/printf.h>
25#include <string>
26#include <string_view>
27#include <type_traits>
28#include <variant>
29
30class ChatHandler;
31
33{
34 /***************** HELPERS *************************\
35 |* These really aren't for outside use... *|
36 \***************************************************/
37
38 static constexpr char COMMAND_DELIMITER = ' ';
39
40 template <typename T, typename = void>
41 struct tag_base
42 {
43 using type = T;
44 };
45
46 template <typename T>
47 using tag_base_t = typename tag_base<T>::type;
48
50 {
51 explicit operator bool() const { return !token.empty(); }
52 std::string_view token;
53 std::string_view tail;
54 };
55
56 inline TokenizeResult tokenize(std::string_view args) noexcept
57 {
58 TokenizeResult result;
59 if (size_t delimPos = args.find(COMMAND_DELIMITER); delimPos != std::string_view::npos)
60 {
61 result.token = args.substr(0, delimPos);
62 if (size_t tailPos = args.find_first_not_of(COMMAND_DELIMITER, delimPos); tailPos != std::string_view::npos)
63 result.tail = args.substr(tailPos);
64 }
65 else
66 result.token = args;
67
68 return result;
69 }
70
71 // this essentially models std::optional<std::string_view>, except it can also hold an error message
72 // it has std::string_view's bool conversion and dereference operators
73 //
74 // monostate <-> unspecified error, typically end-of-string reached or parsing failed
75 // std::string <-> specified error, typically character-not-found or invalid item link
76 // std::string_view <-> success, string_view is remaining argument string
78 {
79 ChatCommandResult(std::nullopt_t) noexcept : _storage() {}
80 ChatCommandResult(std::string const&) = delete;
81 ChatCommandResult(std::string&& s) noexcept : _storage(std::in_place_type<std::string>, std::move(s)) {}
82 ChatCommandResult(char const* c) : _storage(std::in_place_type<std::string>, c) {}
83 ChatCommandResult(std::string_view s) noexcept : _storage(std::in_place_type<std::string_view>, s) {}
84
85 ChatCommandResult& operator=(std::nullopt_t) noexcept { _storage.emplace<std::monostate>(); return *this; }
86 ChatCommandResult& operator=(std::string&& s) noexcept { _storage.emplace<std::string>(std::move(s)); return *this; }
87 ChatCommandResult& operator=(char const* c) { _storage.emplace<std::string>(c); return *this; }
88 ChatCommandResult& operator=(std::string_view s) noexcept { _storage.emplace<std::string_view>(s); return *this; }
89
91 ChatCommandResult(ChatCommandResult&&) noexcept = default;
92 ChatCommandResult& operator=(ChatCommandResult const&) = delete;
93 ChatCommandResult& operator=(ChatCommandResult&&) noexcept = default;
94
95 ~ChatCommandResult() = default;
96
97 std::string_view operator*() const { return std::get<std::string_view>(_storage); }
98 bool IsSuccessful() const { return std::holds_alternative<std::string_view>(_storage); }
99 explicit operator bool() const { return IsSuccessful(); }
100 bool HasErrorMessage() const { return std::holds_alternative<std::string>(_storage); }
101 std::string const& GetErrorMessage() const & { return std::get<std::string>(_storage); }
102 std::string&& GetErrorMessage() && { return std::get<std::string>(std::move(_storage)); }
103
104 private:
105 std::variant<std::monostate, std::string_view, std::string> _storage;
106 };
107
108 TC_GAME_API void SendErrorMessageToHandler(ChatHandler* handler, std::string_view str);
109 TC_GAME_API char const* GetTrinityString(ChatHandler const* handler, TrinityStrings which);
110 TC_GAME_API std::string FormatTrinityString(std::string_view messageFormat, fmt::printf_args messageFormatArgs);
111 template <typename... Ts>
112 std::string FormatTrinityString(ChatHandler const* handler, TrinityStrings which, Ts&&... args)
113 {
114 return FormatTrinityString(GetTrinityString(handler, which), fmt::make_printf_args(args...));
115 }
116}
117
118#endif
#define TC_GAME_API
Definition Define.h:129
TrinityStrings
Definition Language.h:29
TokenizeResult tokenize(std::string_view args) noexcept
static constexpr char COMMAND_DELIMITER
typename tag_base< T >::type tag_base_t
TC_GAME_API char const * GetTrinityString(ChatHandler const *handler, TrinityStrings which)
TC_GAME_API std::string FormatTrinityString(std::string_view messageFormat, fmt::printf_args messageFormatArgs)
TC_GAME_API void SendErrorMessageToHandler(ChatHandler *handler, std::string_view str)
STL namespace.
ChatCommandResult(ChatCommandResult &&) noexcept=default
ChatCommandResult(ChatCommandResult const &)=delete
ChatCommandResult & operator=(std::nullopt_t) noexcept
std::variant< std::monostate, std::string_view, std::string > _storage
ChatCommandResult & operator=(char const *c)
ChatCommandResult & operator=(std::string &&s) noexcept
ChatCommandResult(std::string const &)=delete
ChatCommandResult & operator=(std::string_view s) noexcept