Use fmt store instead of ToString fallback
Thanks to @jonathanpoelen for the idea
This commit is contained in:
committed by
Jérôme Leclercq
parent
a741672a51
commit
ab8bac2575
@@ -10,7 +10,7 @@
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <Nazara/Core/ToString.hpp>
|
||||
#include <Nazara/Core/Format.hpp>
|
||||
#include <string>
|
||||
|
||||
#if NAZARA_CORE_ENABLE_ASSERTS || defined(NAZARA_DEBUG)
|
||||
|
||||
21
include/Nazara/Core/Format.hpp
Normal file
21
include/Nazara/Core/Format.hpp
Normal file
@@ -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 <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename... Args> std::string Format(std::string_view str, Args&&... args);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Format.inl>
|
||||
|
||||
#endif // NAZARA_CORE_FORMAT_HPP
|
||||
41
include/Nazara/Core/Format.inl
Normal file
41
include/Nazara/Core/Format.inl
Normal file
@@ -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 <fmt/format.h>
|
||||
#include <fmt/std.h>
|
||||
#endif
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace Detail
|
||||
{
|
||||
NAZARA_CORE_API void ClearFormatStore();
|
||||
template<typename T> void PushFormatArgImpl(const T& value);
|
||||
template<typename T> void PushFormatArg(T&& value)
|
||||
{
|
||||
PushFormatArgImpl(static_cast<const std::decay_t<T>&>(value));
|
||||
}
|
||||
|
||||
NAZARA_CORE_API std::string FormatFromStore(std::string_view str);
|
||||
}
|
||||
|
||||
template<typename ...Args>
|
||||
std::string Format(std::string_view str, Args&&... args)
|
||||
{
|
||||
#ifdef NAZARA_BUILD
|
||||
return fmt::format(str, std::forward<Args>(args)...);
|
||||
#else
|
||||
Detail::ClearFormatStore();
|
||||
(Detail::PushFormatArg(args), ...);
|
||||
return Detail::FormatFromStore(str);
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef NAZARA_TO_STRING_STD_SPEC
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
@@ -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 <Nazara/Core/ToString.hpp>
|
||||
#include <Nazara/Core/Format.hpp>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
|
||||
@@ -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 <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <NazaraUtils/Algorithm.hpp>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<typename... Args> std::string Format(std::string_view str, Args&&... args);
|
||||
|
||||
template<typename T> decltype(auto) ToString(T&& value);
|
||||
|
||||
template<typename T>
|
||||
struct ToStringFormatter
|
||||
{
|
||||
static_assert(AlwaysFalse<T>(), "ToStringFormatter is not implemented for this type");
|
||||
};
|
||||
|
||||
template<>
|
||||
struct NAZARA_CORE_API ToStringFormatter<std::filesystem::path>
|
||||
{
|
||||
static std::string Format(const std::filesystem::path& path);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ToStringFormatter<std::string>
|
||||
{
|
||||
static const std::string& Format(const std::string& value);
|
||||
static std::string Format(std::string&& value);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ToStringFormatter<std::string_view>
|
||||
{
|
||||
static std::string Format(std::string_view value);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct ToStringFormatter<const char*>
|
||||
{
|
||||
static std::string_view Format(const char* value);
|
||||
template<std::size_t N> static std::string_view Format(const char(&str)[N]);
|
||||
};
|
||||
|
||||
// Specializations declared in .inl
|
||||
#define NAZARA_TO_STRING_INLINE_SPEC(Type) \
|
||||
template<> \
|
||||
struct ToStringFormatter<Type> \
|
||||
{ \
|
||||
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<Type> \
|
||||
{ \
|
||||
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 <Nazara/Core/ToString.inl>
|
||||
|
||||
#endif // NAZARA_CORE_TOSTRING_HPP
|
||||
@@ -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 <fmt/format.h>
|
||||
#include <fmt/std.h>
|
||||
#endif
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
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<typename ...Args>
|
||||
std::string Format(std::string_view str, Args&&... args)
|
||||
{
|
||||
#ifdef NAZARA_BUILD
|
||||
return fmt::format(str, std::forward<Args>(args)...);
|
||||
#else
|
||||
return Detail::FormatFallback(str, ToString(args)...);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
decltype(auto) ToString(T&& value)
|
||||
{
|
||||
return ToStringFormatter<std::decay_t<T>>::Format(std::forward<T>(value));
|
||||
}
|
||||
|
||||
|
||||
inline const std::string& ToStringFormatter<std::string>::Format(const std::string& value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
inline std::string ToStringFormatter<std::string>::Format(std::string&& value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
inline std::string ToStringFormatter<std::string_view>::Format(std::string_view value)
|
||||
{
|
||||
return std::string(value);
|
||||
}
|
||||
|
||||
|
||||
inline std::string_view ToStringFormatter<const char*>::Format(const char* value)
|
||||
{
|
||||
return std::string_view(value);
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
inline std::string_view ToStringFormatter<const char*>::Format(const char(&str)[N])
|
||||
{
|
||||
return std::string_view(str, N);
|
||||
}
|
||||
|
||||
#define NAZARA_TO_STRING_STD_SPEC(Type) \
|
||||
inline std::string ToStringFormatter<Type>::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 <Nazara/Core/DebugOff.hpp>
|
||||
Reference in New Issue
Block a user