diff --git a/include/Nazara/Core/Error.hpp b/include/Nazara/Core/Error.hpp index b03619935..f9f82ca1d 100644 --- a/include/Nazara/Core/Error.hpp +++ b/include/Nazara/Core/Error.hpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #if NAZARA_CORE_ENABLE_ASSERTS || defined(NAZARA_DEBUG) diff --git a/include/Nazara/Core/Format.hpp b/include/Nazara/Core/Format.hpp new file mode 100644 index 000000000..65d460cc7 --- /dev/null +++ b/include/Nazara/Core/Format.hpp @@ -0,0 +1,21 @@ +// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_CORE_FORMAT_HPP +#define NAZARA_CORE_FORMAT_HPP + +#include +#include +#include + +namespace Nz +{ + template std::string Format(std::string_view str, Args&&... args); +} + +#include + +#endif // NAZARA_CORE_FORMAT_HPP diff --git a/include/Nazara/Core/Format.inl b/include/Nazara/Core/Format.inl new file mode 100644 index 000000000..ab8c3e179 --- /dev/null +++ b/include/Nazara/Core/Format.inl @@ -0,0 +1,41 @@ +// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#ifdef NAZARA_BUILD +#include +#include +#endif + +#include + +namespace Nz +{ + namespace Detail + { + NAZARA_CORE_API void ClearFormatStore(); + template void PushFormatArgImpl(const T& value); + template void PushFormatArg(T&& value) + { + PushFormatArgImpl(static_cast&>(value)); + } + + NAZARA_CORE_API std::string FormatFromStore(std::string_view str); + } + + template + std::string Format(std::string_view str, Args&&... args) + { +#ifdef NAZARA_BUILD + return fmt::format(str, std::forward(args)...); +#else + Detail::ClearFormatStore(); + (Detail::PushFormatArg(args), ...); + return Detail::FormatFromStore(str); +#endif + } + +#undef NAZARA_TO_STRING_STD_SPEC +} + +#include diff --git a/include/Nazara/Core/Log.inl b/include/Nazara/Core/Log.inl index c181b355f..f3a8e7fdc 100644 --- a/include/Nazara/Core/Log.inl +++ b/include/Nazara/Core/Log.inl @@ -2,7 +2,7 @@ // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp -#include +#include #include namespace Nz diff --git a/include/Nazara/Core/ToString.hpp b/include/Nazara/Core/ToString.hpp deleted file mode 100644 index 6711a2976..000000000 --- a/include/Nazara/Core/ToString.hpp +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#pragma once - -#ifndef NAZARA_CORE_TOSTRING_HPP -#define NAZARA_CORE_TOSTRING_HPP - -#include -#include -#include -#include -#include - -namespace Nz -{ - template std::string Format(std::string_view str, Args&&... args); - - template decltype(auto) ToString(T&& value); - - template - struct ToStringFormatter - { - static_assert(AlwaysFalse(), "ToStringFormatter is not implemented for this type"); - }; - - template<> - struct NAZARA_CORE_API ToStringFormatter - { - static std::string Format(const std::filesystem::path& path); - }; - - template<> - struct ToStringFormatter - { - static const std::string& Format(const std::string& value); - static std::string Format(std::string&& value); - }; - - template<> - struct ToStringFormatter - { - static std::string Format(std::string_view value); - }; - - template<> - struct ToStringFormatter - { - static std::string_view Format(const char* value); - template static std::string_view Format(const char(&str)[N]); - }; - - // Specializations declared in .inl -#define NAZARA_TO_STRING_INLINE_SPEC(Type) \ - template<> \ - struct ToStringFormatter \ - { \ - static std::string Format(Type value); \ - } - - NAZARA_TO_STRING_INLINE_SPEC(short); - NAZARA_TO_STRING_INLINE_SPEC(int); - NAZARA_TO_STRING_INLINE_SPEC(long); - NAZARA_TO_STRING_INLINE_SPEC(long long); - NAZARA_TO_STRING_INLINE_SPEC(unsigned short); - NAZARA_TO_STRING_INLINE_SPEC(unsigned int); - NAZARA_TO_STRING_INLINE_SPEC(unsigned long); - NAZARA_TO_STRING_INLINE_SPEC(unsigned long long); - -#undef NAZARA_TO_STRING_INLINE_SPEC - - // Specializations declared in .cpp -#define NAZARA_TO_STRING_CPP_SPEC(Type) \ - template<> \ - struct NAZARA_CORE_API ToStringFormatter \ - { \ - static std::string Format(Type value); \ - } - - NAZARA_TO_STRING_CPP_SPEC(float); - NAZARA_TO_STRING_CPP_SPEC(double); - NAZARA_TO_STRING_CPP_SPEC(long double); - -#undef NAZARA_TO_STRING_CPP_SPEC -} - -#include - -#endif // NAZARA_CORE_TOSTRING_HPP diff --git a/include/Nazara/Core/ToString.inl b/include/Nazara/Core/ToString.inl deleted file mode 100644 index b12858948..000000000 --- a/include/Nazara/Core/ToString.inl +++ /dev/null @@ -1,88 +0,0 @@ -// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#ifdef NAZARA_BUILD -#include -#include -#endif - -#include - -namespace Nz -{ - namespace Detail - { - NAZARA_CORE_API std::string FormatFallback(std::string_view str); - NAZARA_CORE_API std::string FormatFallback(std::string_view str, std::string_view param1); - NAZARA_CORE_API std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2); - NAZARA_CORE_API std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2, std::string_view param3); - NAZARA_CORE_API std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2, std::string_view param3, std::string_view param4); - NAZARA_CORE_API std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2, std::string_view param3, std::string_view param4, std::string_view param5); - } - - template - std::string Format(std::string_view str, Args&&... args) - { -#ifdef NAZARA_BUILD - return fmt::format(str, std::forward(args)...); -#else - return Detail::FormatFallback(str, ToString(args)...); -#endif - } - - - template - decltype(auto) ToString(T&& value) - { - return ToStringFormatter>::Format(std::forward(value)); - } - - - inline const std::string& ToStringFormatter::Format(const std::string& value) - { - return value; - } - - inline std::string ToStringFormatter::Format(std::string&& value) - { - return value; - } - - - inline std::string ToStringFormatter::Format(std::string_view value) - { - return std::string(value); - } - - - inline std::string_view ToStringFormatter::Format(const char* value) - { - return std::string_view(value); - } - - template - inline std::string_view ToStringFormatter::Format(const char(&str)[N]) - { - return std::string_view(str, N); - } - -#define NAZARA_TO_STRING_STD_SPEC(Type) \ - inline std::string ToStringFormatter::Format(Type value) \ - { \ - return std::to_string(value); \ - } - - NAZARA_TO_STRING_STD_SPEC(short); - NAZARA_TO_STRING_STD_SPEC(int); - NAZARA_TO_STRING_STD_SPEC(long); - NAZARA_TO_STRING_STD_SPEC(long long); - NAZARA_TO_STRING_STD_SPEC(unsigned short); - NAZARA_TO_STRING_STD_SPEC(unsigned int); - NAZARA_TO_STRING_STD_SPEC(unsigned long); - NAZARA_TO_STRING_STD_SPEC(unsigned long long); - -#undef NAZARA_TO_STRING_STD_SPEC -} - -#include diff --git a/src/Nazara/Core/Format.cpp b/src/Nazara/Core/Format.cpp new file mode 100644 index 000000000..472bfd529 --- /dev/null +++ b/src/Nazara/Core/Format.cpp @@ -0,0 +1,53 @@ +// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include +#include + +namespace Nz +{ + namespace Detail + { + thread_local fmt::dynamic_format_arg_store s_formatStore; + + void ClearFormatStore() + { + s_formatStore.clear(); + } + + std::string FormatFromStore(std::string_view str) + { + return fmt::vformat(str, s_formatStore); + } + +#define NAZARA_FORMAT_IMPLEM(Type) \ + template<> NAZARA_CORE_API void PushFormatArgImpl(Type const& value) \ + { \ + s_formatStore.push_back(value); \ + } + + NAZARA_FORMAT_IMPLEM(std::filesystem::path); + NAZARA_FORMAT_IMPLEM(std::string); + NAZARA_FORMAT_IMPLEM(std::string_view); + NAZARA_FORMAT_IMPLEM(const char*); + NAZARA_FORMAT_IMPLEM(short); + NAZARA_FORMAT_IMPLEM(int); + NAZARA_FORMAT_IMPLEM(long); + NAZARA_FORMAT_IMPLEM(long long); + NAZARA_FORMAT_IMPLEM(unsigned short); + NAZARA_FORMAT_IMPLEM(unsigned int); + NAZARA_FORMAT_IMPLEM(unsigned long); + NAZARA_FORMAT_IMPLEM(unsigned long long); + NAZARA_FORMAT_IMPLEM(float); + NAZARA_FORMAT_IMPLEM(double); + NAZARA_FORMAT_IMPLEM(long double); + +#undef NAZARA_FORMAT_IMPLEM + } + +#undef NAZARA_TO_STRING_CPP_SPEC +} diff --git a/src/Nazara/Core/ToString.cpp b/src/Nazara/Core/ToString.cpp deleted file mode 100644 index 5c2a36f55..000000000 --- a/src/Nazara/Core/ToString.cpp +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com) -// This file is part of the "Nazara Engine - Core module" -// For conditions of distribution and use, see copyright notice in Config.hpp - -#include -#include -#include -#include - -namespace Nz -{ - namespace Detail - { - std::string FormatFallback(std::string_view str) - { - return fmt::format(str); - } - - std::string FormatFallback(std::string_view str, std::string_view param1) - { - return fmt::format(str, param1); - } - - std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2) - { - return fmt::format(str, param1, param2); - } - - std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2, std::string_view param3) - { - return fmt::format(str, param1, param2, param3); - } - - std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2, std::string_view param3, std::string_view param4) - { - return fmt::format(str, param1, param2, param3, param4); - } - - std::string FormatFallback(std::string_view str, std::string_view param1, std::string_view param2, std::string_view param3, std::string_view param4, std::string_view param5) - { - return fmt::format(str, param1, param2, param3, param4, param5); - } - } - - std::string ToStringFormatter::Format(const std::filesystem::path& path) - { - return path.generic_u8string(); - } - -#define NAZARA_TO_STRING_FMT_SPEC(Type) \ - std::string ToStringFormatter::Format(Type value) \ - { \ - return fmt::format("{}", value); \ - } - - NAZARA_TO_STRING_FMT_SPEC(float); - NAZARA_TO_STRING_FMT_SPEC(double); - NAZARA_TO_STRING_FMT_SPEC(long double); - -#undef NAZARA_TO_STRING_CPP_SPEC -} diff --git a/src/Nazara/VulkanRenderer/VulkanSwapchain.cpp b/src/Nazara/VulkanRenderer/VulkanSwapchain.cpp index 39dfe474b..2b3dff6b3 100644 --- a/src/Nazara/VulkanRenderer/VulkanSwapchain.cpp +++ b/src/Nazara/VulkanRenderer/VulkanSwapchain.cpp @@ -495,7 +495,6 @@ namespace Nz bool VulkanSwapchain::SetupRenderPass() { - NazaraError("Test"); std::optional colorFormat = FromVulkan(m_surfaceFormat.format); if (!colorFormat) { diff --git a/tests/UnitTests/Engine/Core/ErrorTest.cpp b/tests/UnitTests/Engine/Core/ErrorTest.cpp index 7e58360a2..8a8da8c76 100644 --- a/tests/UnitTests/Engine/Core/ErrorTest.cpp +++ b/tests/UnitTests/Engine/Core/ErrorTest.cpp @@ -16,7 +16,7 @@ SCENARIO("Error", "[CORE][ERROR]") Nz::Error::Trigger(Nz::ErrorType::Internal, 2, "Error.cpp", "2nd place Internal", "ErrorType::{0}", "Internal"); REQUIRE("ErrorType::Internal" == Nz::Error::GetLastError()); Nz::Error::Trigger(Nz::ErrorType::Normal, "ErrorType::{1}{0}", "mal", "Nor"); - Nz::Error::Trigger(Nz::ErrorType::Normal, 2, "Error.cpp", "2nd place Normal", "ErrorType::{1}{0}", "rmal", "Nor"); + Nz::Error::Trigger(Nz::ErrorType::Normal, 2, "Error.cpp", "2nd place Normal", "ErrorType::{1}{0}", "mal", "Nor"); REQUIRE("ErrorType::Normal" == Nz::Error::GetLastError()); Nz::Error::Trigger(Nz::ErrorType::Warning, "ErrorType::Warning"); Nz::Error::Trigger(Nz::ErrorType::Warning, "ErrorType::Warning", 2, "Error.cpp", "2nd place Warning");