Skip to content

Commit 1614308

Browse files
committed
Core/Misc: Allow formatting optionals with Trinity::StringFormat
(cherry picked from commit 20b29c5ff56c605d52fb6e391c28daea26ab719a)
1 parent fa4d455 commit 1614308

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

cmake/compiler/clang/settings.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,12 @@ endif()
129129

130130
# -Wno-narrowing needed to suppress a warning in g3d
131131
# -Wno-deprecated-register is needed to suppress 185 gsoap warnings on Unix systems.
132-
# -Wno-deprecated-copy needed to suppress a warning in g3d
132+
# -Wno-undefined-inline needed for a compile time optimization hack with fmt
133133
target_compile_options(trinity-compile-option-interface
134134
INTERFACE
135135
-Wno-narrowing
136-
-Wno-deprecated-register)
136+
-Wno-deprecated-register
137+
-Wno-undefined-inline)
137138

138139
if(BUILD_SHARED_LIBS)
139140
# -fPIC is needed to allow static linking in shared libs.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
#include "StringFormat.h"
19+
#include "Define.h"
20+
#include <fmt/format.h>
21+
22+
// explicit template instantiations
23+
template struct TC_COMMON_API fmt::formatter<int>;
24+
template struct TC_COMMON_API fmt::formatter<unsigned>;
25+
template struct TC_COMMON_API fmt::formatter<long long>;
26+
template struct TC_COMMON_API fmt::formatter<unsigned long long>;
27+
template struct TC_COMMON_API fmt::formatter<bool>;
28+
template struct TC_COMMON_API fmt::formatter<char>;
29+
template struct TC_COMMON_API fmt::formatter<float>;
30+
template struct TC_COMMON_API fmt::formatter<double>;
31+
template struct TC_COMMON_API fmt::formatter<long double>;
32+
template struct TC_COMMON_API fmt::formatter<char const*>;
33+
template struct TC_COMMON_API fmt::formatter<void const*>;
34+
template struct TC_COMMON_API fmt::formatter<fmt::basic_string_view<char>>;
35+
36+
template TC_COMMON_API fmt::appender fmt::formatter<int>::format<fmt::format_context>(int const&, format_context&) const;
37+
template TC_COMMON_API fmt::appender fmt::formatter<unsigned>::format<fmt::format_context>(unsigned const&, format_context&) const;
38+
template TC_COMMON_API fmt::appender fmt::formatter<long long>::format<fmt::format_context>(long long const&, format_context&) const;
39+
template TC_COMMON_API fmt::appender fmt::formatter<unsigned long long>::format<fmt::format_context>(unsigned long long const&, format_context&) const;
40+
template TC_COMMON_API fmt::appender fmt::formatter<bool>::format<fmt::format_context>(bool const&, format_context&) const;
41+
template TC_COMMON_API fmt::appender fmt::formatter<char>::format<fmt::format_context>(char const&, format_context&) const;
42+
template TC_COMMON_API fmt::appender fmt::formatter<float>::format<fmt::format_context>(float const&, format_context&) const;
43+
template TC_COMMON_API fmt::appender fmt::formatter<double>::format<fmt::format_context>(double const&, format_context&) const;
44+
template TC_COMMON_API fmt::appender fmt::formatter<long double>::format<fmt::format_context>(long double const&, format_context&) const;
45+
template TC_COMMON_API fmt::appender fmt::formatter<char const*>::format<fmt::format_context>(char const* const&, format_context&) const;
46+
template TC_COMMON_API fmt::appender fmt::formatter<void const*>::format<fmt::format_context>(void const* const&, format_context &) const;
47+
template TC_COMMON_API fmt::appender fmt::formatter<fmt::string_view>::format<fmt::format_context>(string_view const&, format_context &) const;

src/common/Utilities/StringFormat.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#ifndef TRINITYCORE_STRING_FORMAT_H
1919
#define TRINITYCORE_STRING_FORMAT_H
2020

21-
#include "fmt/core.h"
21+
#include "Optional.h"
22+
#include <fmt/core.h>
2223

2324
namespace Trinity
2425
{
@@ -108,4 +109,17 @@ namespace Trinity
108109
}
109110
}
110111

112+
template<typename T, typename Char>
113+
struct fmt::formatter<Optional<T>, Char> : formatter<T, Char>
114+
{
115+
template<typename FormatContext>
116+
auto format(Optional<T> const& value, FormatContext& ctx) const -> decltype(ctx.out())
117+
{
118+
if (value.has_value())
119+
return formatter<T, Char>::format(*value, ctx);
120+
121+
return formatter<std::string_view, Char>().format("(nullopt)", ctx);
122+
}
123+
};
124+
111125
#endif

0 commit comments

Comments
 (0)