aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2024-02-28 20:42:20 +0100
committerShauren <shauren.trinity@gmail.com>2024-02-28 20:42:20 +0100
commit20b29c5ff56c605d52fb6e391c28daea26ab719a (patch)
treeb97c7bc41678ac254981fc4624ae78074112a725
parent288966e796bfa4309fdcb08b9392b61befc3faab (diff)
Core/Misc: Allow formatting optionals with Trinity::StringFormat
-rw-r--r--cmake/compiler/clang/settings.cmake5
-rw-r--r--src/common/Utilities/StringFormat.cpp47
-rw-r--r--src/common/Utilities/StringFormat.h16
3 files changed, 65 insertions, 3 deletions
diff --git a/cmake/compiler/clang/settings.cmake b/cmake/compiler/clang/settings.cmake
index e8944bf3294..0fa27e6493c 100644
--- a/cmake/compiler/clang/settings.cmake
+++ b/cmake/compiler/clang/settings.cmake
@@ -140,11 +140,12 @@ endif()
# -Wno-narrowing needed to suppress a warning in g3d
# -Wno-deprecated-register is needed to suppress 185 gsoap warnings on Unix systems.
-# -Wno-deprecated-copy needed to suppress a warning in g3d
+# -Wno-undefined-inline needed for a compile time optimization hack with fmt
target_compile_options(trinity-compile-option-interface
INTERFACE
-Wno-narrowing
- -Wno-deprecated-register)
+ -Wno-deprecated-register
+ -Wno-undefined-inline)
if(BUILD_SHARED_LIBS)
# -fPIC is needed to allow static linking in shared libs.
diff --git a/src/common/Utilities/StringFormat.cpp b/src/common/Utilities/StringFormat.cpp
new file mode 100644
index 00000000000..9550294fd64
--- /dev/null
+++ b/src/common/Utilities/StringFormat.cpp
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "StringFormat.h"
+#include "Define.h"
+#include <fmt/format.h>
+
+// explicit template instantiations
+template struct TC_COMMON_API fmt::formatter<int>;
+template struct TC_COMMON_API fmt::formatter<unsigned>;
+template struct TC_COMMON_API fmt::formatter<long long>;
+template struct TC_COMMON_API fmt::formatter<unsigned long long>;
+template struct TC_COMMON_API fmt::formatter<bool>;
+template struct TC_COMMON_API fmt::formatter<char>;
+template struct TC_COMMON_API fmt::formatter<float>;
+template struct TC_COMMON_API fmt::formatter<double>;
+template struct TC_COMMON_API fmt::formatter<long double>;
+template struct TC_COMMON_API fmt::formatter<char const*>;
+template struct TC_COMMON_API fmt::formatter<void const*>;
+template struct TC_COMMON_API fmt::formatter<fmt::basic_string_view<char>>;
+
+template TC_COMMON_API fmt::appender fmt::formatter<int>::format<fmt::format_context>(int const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<unsigned>::format<fmt::format_context>(unsigned const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<long long>::format<fmt::format_context>(long long const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<unsigned long long>::format<fmt::format_context>(unsigned long long const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<bool>::format<fmt::format_context>(bool const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<char>::format<fmt::format_context>(char const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<float>::format<fmt::format_context>(float const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<double>::format<fmt::format_context>(double const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<long double>::format<fmt::format_context>(long double const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<char const*>::format<fmt::format_context>(char const* const&, format_context&) const;
+template TC_COMMON_API fmt::appender fmt::formatter<void const*>::format<fmt::format_context>(void const* const&, format_context &) const;
+template TC_COMMON_API fmt::appender fmt::formatter<fmt::string_view>::format<fmt::format_context>(string_view const&, format_context &) const;
diff --git a/src/common/Utilities/StringFormat.h b/src/common/Utilities/StringFormat.h
index d34528a04df..fd31ddc28d5 100644
--- a/src/common/Utilities/StringFormat.h
+++ b/src/common/Utilities/StringFormat.h
@@ -18,7 +18,8 @@
#ifndef TRINITYCORE_STRING_FORMAT_H
#define TRINITYCORE_STRING_FORMAT_H
-#include "fmt/core.h"
+#include "Optional.h"
+#include <fmt/core.h>
namespace Trinity
{
@@ -108,4 +109,17 @@ namespace Trinity
}
}
+template<typename T, typename Char>
+struct fmt::formatter<Optional<T>, Char> : formatter<T, Char>
+{
+ template<typename FormatContext>
+ auto format(Optional<T> const& value, FormatContext& ctx) const -> decltype(ctx.out())
+ {
+ if (value.has_value())
+ return formatter<T, Char>::format(*value, ctx);
+
+ return formatter<std::string_view, Char>().format("(nullopt)", ctx);
+ }
+};
+
#endif