Skip to content

Commit

Permalink
Switch from fmtlib to std::format. (#143)
Browse files Browse the repository at this point in the history
* Switch from fmtlib to std::format.
  • Loading branch information
Holt59 authored May 25, 2024
1 parent e5fa88e commit 4ed2daa
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ jobs:
- name: Build UI Base
uses: ModOrganizer2/build-with-mob-action@master
with:
mo2-third-parties: fmt gtest spdlog boost
mo2-third-parties: gtest spdlog boost
mo2-dependencies: cmake_common
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mo2_configure_uibase(uibase
WARNINGS ON
EXTERNAL_WARNINGS ON
TRANSLATIONS OFF
PUBLIC_DEPENDS fmt Qt::Widgets Qt::Network Qt::QuickWidgets
PUBLIC_DEPENDS Qt::Widgets Qt::Network Qt::QuickWidgets
PRIVATE_DEPENDS boost boost::thread Qt::Qml Qt::Quick spdlog)
target_compile_definitions(uibase PRIVATE -DUIBASE_EXPORT)
mo2_install_target(uibase)
Expand Down
177 changes: 177 additions & 0 deletions src/formatters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#pragma once

#include <QColor>
#include <QFlag>
#include <QFlags>
#include <QRect>
#include <QSize>
#include <QString>
#include <QVariant>

namespace MOBase::details
{
template <class CharT>
inline std::basic_string<CharT> toStdBasicString(QString const& qstring);

template <>
inline std::basic_string<char> toStdBasicString(QString const& qstring)
{
return qstring.toStdString();
}
template <>
inline std::basic_string<wchar_t> toStdBasicString(QString const& qstring)
{
return qstring.toStdWString();
}
template <>
inline std::basic_string<char16_t> toStdBasicString(QString const& qstring)
{
return qstring.toStdU16String();
}
template <>
inline std::basic_string<char32_t> toStdBasicString(QString const& qstring)
{
return qstring.toStdU32String();
}

inline QString fromStdBasicString(std::string const& value)
{
return QString::fromStdString(value);
}
inline QString fromStdBasicString(std::wstring const& value)
{
return QString::fromStdWString(value);
}
inline QString fromStdBasicString(std::u16string const& value)
{
return QString::fromStdU16String(value);
}
inline QString fromStdBasicString(std::u32string const& value)
{
return QString::fromStdU32String(value);
}
} // namespace MOBase::details

template <class CharT>
struct std::formatter<QString, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QString s, FmtContext& ctx) const
{
return std::formatter<std::basic_string<CharT>, CharT>::format(
MOBase::details::toStdBasicString<CharT>(s), ctx);
}
};

template <class CharT1, class CharT2>
requires(!std::is_same_v<CharT1, CharT2>)
struct std::formatter<std::basic_string<CharT1>, CharT2>
: std::formatter<std::basic_string<CharT2>, CharT2>
{
template <class FmtContext>
FmtContext::iterator format(std::basic_string<CharT1> s, FmtContext& ctx) const
{
return std::formatter<std::basic_string<CharT2>, CharT2>::format(
MOBase::details::toStdBasicString<CharT2>(
MOBase::details::fromStdBasicString(s)),
ctx);
}
};

template <class CharT>
struct std::formatter<QStringView, CharT> : std::formatter<QString, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QStringView s, FmtContext& ctx) const
{
return std::formatter<QString, CharT>::format(s.toString(), ctx);
}
};

template <class CharT>
struct std::formatter<QSize, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QSize s, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QSize({}, {})", s.width(), s.height());
}
};

template <class CharT>
struct std::formatter<QRect, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QRect r, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QRect({},{}-{},{})", r.left(), r.top(), r.right(),
r.bottom());
}
};

template <class CharT>
struct std::formatter<QColor, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QColor c, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QColor({}, {}, {}, {})", c.red(), c.green(),
c.blue(), c.alpha());
}
};

template <class CharT>
struct std::formatter<QByteArray, CharT>
: std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QByteArray v, FmtContext& ctx) const
{
return std::format_to(ctx.out(), "QByteArray({} bytes)", v.size());
}
};

template <class CharT>
struct std::formatter<QVariant, CharT> : std::formatter<std::basic_string<CharT>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QVariant v, FmtContext& ctx) const
{
return std::format_to(
ctx.out(), "QVariant(type={}, value={})", v.typeName(),
(v.typeId() == QMetaType::Type::QByteArray ? "(binary)" : v.toString()));
}
};

template <class CharT>
struct std::formatter<QFlag, CharT> : std::formatter<int, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QFlag v, FmtContext& ctx) const
{
return std::formatter<int, CharT>::format(static_cast<int>(v), ctx);
}
};

template <class T, class CharT>
struct std::formatter<QFlags<T>, CharT> : std::formatter<int, CharT>
{
template <class FmtContext>
FmtContext::iterator format(QFlags<T> v, FmtContext& ctx) const
{
// TODO: display flags has aa | bb | cc?
return std::formatter<int, CharT>::format(v.toInt(), ctx);
}
};

template <class Enum, class CharT>
requires std::is_enum_v<Enum>
struct std::formatter<Enum, CharT> : std::formatter<std::underlying_type_t<Enum>, CharT>
{
template <class FmtContext>
FmtContext::iterator format(Enum v, FmtContext& ctx) const
{
return std::formatter<std::underlying_type_t<Enum>, CharT>::format(
static_cast<std::underlying_type_t<Enum>>(v), ctx);
}
};
45 changes: 1 addition & 44 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void Logger::setFile(const File& f)
addSink(m_file);
}
} catch (spdlog::spdlog_ex& e) {
error(e.what());
error("{}", e.what());
}
}
}
Expand Down Expand Up @@ -369,49 +369,6 @@ Logger& getDefault()
namespace MOBase::log::details
{

std::string converter<std::wstring>::convert(const std::wstring& s)
{
return QString::fromStdWString(s).toStdString();
}

std::string converter<QString>::convert(const QString& s)
{
return s.toStdString();
}

std::string converter<QStringView>::convert(const QStringView& s)
{
return converter<QString>::convert(s.toString());
}

std::string converter<QSize>::convert(const QSize& s)
{
return fmt::format("QSize({}, {})", s.width(), s.height());
}

std::string converter<QRect>::convert(const QRect& r)
{
return fmt::format("QRect({},{}-{},{})", r.left(), r.top(), r.right(), r.bottom());
}

std::string converter<QColor>::convert(const QColor& c)
{
return fmt::format("QColor({}, {}, {}, {})", c.red(), c.green(), c.blue(), c.alpha());
}

std::string converter<QByteArray>::convert(const QByteArray& v)
{
return fmt::format("QByteArray({} bytes)", v.size());
}

std::string converter<QVariant>::convert(const QVariant& v)
{
return fmt::format("QVariant(type={}, value='{}')", v.typeName(),
(v.typeId() == QMetaType::Type::QByteArray
? "(binary)"
: v.toString().toStdString()));
}

void doLogImpl(spdlog::logger& lg, Levels lv, const std::string& s) noexcept
{
try {
Expand Down
Loading

0 comments on commit 4ed2daa

Please sign in to comment.