Skip to content

Commit

Permalink
Fix build with C++20 and remove many boost usage and all fmt:: usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Feb 20, 2025
1 parent 4e6ba3e commit d01370f
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 253 deletions.
9 changes: 1 addition & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ find_package(Fcitx5ModuleLuaAddonLoader)
find_package(Pthread REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(Gettext REQUIRED)
find_package(fmt REQUIRED)

if (TARGET fmt::fmt-header-only)
set(FMT_TARGET fmt::fmt-header-only)
else()
set(FMT_TARGET fmt::fmt)
endif ()

include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake")

Expand Down Expand Up @@ -65,7 +58,7 @@ if (ENABLE_GUI)
endif()

# We do not need json if OpenCC is not needed, but just try to find it anyway to save some ifelse logic
find_package(Boost 1.61 REQUIRED COMPONENTS iostreams OPTIONAL_COMPONENTS json)
find_package(Boost 1.61 OPTIONAL_COMPONENTS json)
find_package(LibIMEPinyin 1.1.7 REQUIRED)
find_package(LibIMETable 1.1.4 REQUIRED)

Expand Down
2 changes: 1 addition & 1 deletion gui/customphraseeditor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CUSTOMPHRASEEDITOR_SOURCES
)

add_library(customphraseeditor MODULE ${CUSTOMPHRASEEDITOR_SOURCES})
target_link_libraries(customphraseeditor Fcitx5Qt${QT_MAJOR_VERSION}::WidgetsAddons ${BROWSER_TARGET} Qt${QT_MAJOR_VERSION}::Concurrent LibIME::Core Boost::iostreams ${FMT_TARGET})
target_link_libraries(customphraseeditor Fcitx5Qt${QT_MAJOR_VERSION}::WidgetsAddons ${BROWSER_TARGET} Qt${QT_MAJOR_VERSION}::Concurrent LibIME::Core)
set_target_properties(customphraseeditor PROPERTIES AUTOMOC TRUE AUTOUIC TRUE AUTOUIC_OPTIONS "-tr=fcitx::tr2fcitx;--include=fcitxqti18nhelper.h")

install(TARGETS customphraseeditor DESTINATION ${CMAKE_INSTALL_LIBDIR}/fcitx5/qt${QT_MAJOR_VERSION})
57 changes: 35 additions & 22 deletions gui/customphraseeditor/customphrasemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
*
*/
#include "customphrasemodel.h"
#include <QAbstractTableModel>
#include <QApplication>
#include <QByteArray>
#include <QFile>
#include <QFutureWatcher>
#include <QLatin1String>
#include <QList>
#include <QObject>
#include <QtConcurrentRun>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream_buffer.hpp>
#include <fcitx-utils/fdstreambuf.h>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/standardpath.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx-utils/utf8.h>
#include <fcntl.h>
#include <fstream>
#include <istream>
#include <ostream>
#include <qfuturewatcher.h>
#include <qnamespace.h>
#include <string>
#include <string_view>
#include <vector>

namespace fcitx {

Expand Down Expand Up @@ -85,13 +93,15 @@ QVariant CustomPhraseModel::headerData(int section, Qt::Orientation orientation,
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == Column_Key) {
return _("Key");
} else if (section == Column_Phrase) {
}
if (section == Column_Phrase) {
return _("Phrase");
} else if (section == Column_Order) {
}
if (section == Column_Order) {
return _("Order");
}
}
return QVariant();
return {};
}

int CustomPhraseModel::rowCount(const QModelIndex &parent) const {
Expand All @@ -113,9 +123,11 @@ QVariant CustomPhraseModel::data(const QModelIndex &index, int role) const {
index.row() < list_.count()) {
if (index.column() == Column_Key) {
return list_[index.row()].key;
} else if (index.column() == Column_Phrase) {
}
if (index.column() == Column_Phrase) {
return list_[index.row()].value;
} else if (index.column() == Column_Order) {
}
if (index.column() == Column_Order) {
return qAbs(list_[index.row()].order);
}
}
Expand All @@ -133,8 +145,9 @@ void CustomPhraseModel::addItem(const QString &key, const QString &word,
}

void CustomPhraseModel::deleteItem(int row) {
if (row >= list_.count() || row < 0)
if (row >= list_.count() || row < 0) {
return;
}
beginRemoveRows(QModelIndex(), row, row);
list_.removeAt(row);
endRemoveRows();
Expand All @@ -151,8 +164,9 @@ void CustomPhraseModel::deleteAllItem() {
}

Qt::ItemFlags CustomPhraseModel::flags(const QModelIndex &index) const {
if (!index.isValid())
if (!index.isValid()) {
return {};
}

if (index.column() == Column_Enable) {
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable |
Expand All @@ -171,22 +185,25 @@ bool CustomPhraseModel::setData(const QModelIndex &index, const QVariant &value,
return true;
}

if (role != Qt::EditRole)
if (role != Qt::EditRole) {
return false;
}

if (index.column() == Column_Key) {
list_[index.row()].key = value.toString();

Q_EMIT dataChanged(index, index);
setNeedSave(true);
return true;
} else if (index.column() == Column_Phrase) {
}
if (index.column() == Column_Phrase) {
list_[index.row()].value = value.toString();

Q_EMIT dataChanged(index, index);
setNeedSave(true);
return true;
} else if (index.column() == Column_Order) {
}
if (index.column() == Column_Order) {
list_[index.row()].order = value.toInt();

Q_EMIT dataChanged(index, index);
Expand Down Expand Up @@ -218,13 +235,12 @@ QList<CustomPhraseItem> CustomPhraseModel::parse(const QString &file) {
auto fp = fcitx::StandardPath::global().open(
fcitx::StandardPath::Type::PkgData, fileNameArray.constData(),
O_RDONLY);
if (fp.fd() < 0)
if (fp.fd() < 0) {
break;
}

IFDStreamBuf buffer(fp.fd());

boost::iostreams::stream_buffer<
boost::iostreams::file_descriptor_source>
buffer(fp.fd(),
boost::iostreams::file_descriptor_flags::never_close_handle);
std::istream in(&buffer);
CustomPhraseDict dict;
dict.load(in, /*loadDisabled=*/true);
Expand Down Expand Up @@ -266,10 +282,7 @@ bool CustomPhraseModel::saveData(const QString &file,
return StandardPath::global().safeSave(
StandardPath::Type::PkgData, filenameArray.constData(),
[&list](int fd) {
boost::iostreams::stream_buffer<
boost::iostreams::file_descriptor_sink>
buffer(fd, boost::iostreams::file_descriptor_flags::
never_close_handle);
OFDStreamBuf buffer(fd);
std::ostream out(&buffer);
auto printMultilineComment = [](std::ostream &out,
std::string_view text) {
Expand Down
2 changes: 1 addition & 1 deletion im/pinyin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(PINYIN_SOURCES
)

add_fcitx5_addon(pinyin ${PINYIN_SOURCES})
target_link_libraries(pinyin Fcitx5::Core Fcitx5::Config LibIME::Pinyin Boost::iostreams Fcitx5::Module::Punctuation Fcitx5::Module::QuickPhrase Fcitx5::Module::Notifications Fcitx5::Module::Spell Fcitx5::Module::PinyinHelper Pthread::Pthread ${FMT_TARGET})
target_link_libraries(pinyin Fcitx5::Core Fcitx5::Config LibIME::Pinyin Fcitx5::Module::Punctuation Fcitx5::Module::QuickPhrase Fcitx5::Module::Notifications Fcitx5::Module::Spell Fcitx5::Module::PinyinHelper Pthread::Pthread)

if (TARGET Fcitx5::Module::LuaAddonLoader)
target_compile_definitions(pinyin PRIVATE -DFCITX_HAS_LUA)
Expand Down
58 changes: 44 additions & 14 deletions im/pinyin/customphrase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
#include <fcitx-utils/charutils.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/stringutils.h>
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <format>
#include <functional>
#include <istream>
#include <iterator>
Expand All @@ -34,6 +32,37 @@

namespace fcitx {

// localtime_s is relatively evil and we simply want to avoid it.
namespace localtime_helper {

struct not_available_tag {};

not_available_tag localtime_r(...) { return {}; }

} // namespace localtime_helper

struct localtime_impl {
std::tm tm_{};
std::time_t time_{};

bool get() {
using namespace localtime_helper;
return localtime_fallback(localtime_r(&time_, &tm_));
}

bool localtime_fallback(std::tm *t) { return t != nullptr; }

bool localtime_fallback(localtime_helper::not_available_tag) {
// This is a least worse option since win has thread-local for it.
auto *t = std::localtime(&time_);
if (t) {
tm_ = *t;
return true;
}
return false;
}
};

void normalizeData(std::vector<CustomPhrase> &data) {
std::stable_sort(data.begin(), data.end(),
[](const CustomPhrase &lhs, const CustomPhrase &rhs) {
Expand Down Expand Up @@ -112,7 +141,7 @@ bool isComment(std::string_view line) {
}

inline std::tm currentTm() {
#ifdef FCITX_CUSTOM_PHRASE_TEST
#if defined(FCITX_CUSTOM_PHRASE_TEST)
std::tm timePoint;
timePoint.tm_year = 2023 - 1900;
timePoint.tm_mon = 6;
Expand All @@ -125,8 +154,9 @@ inline std::tm currentTm() {
#else
const std::chrono::system_clock::time_point now =
std::chrono::system_clock::now();
const std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
return fmt::localtime(currentTime);
localtime_impl impl{.time_ = std::chrono::system_clock::to_time_t(now)};
impl.get();
return impl.tm_;
#endif
}

Expand Down Expand Up @@ -303,25 +333,25 @@ std::string CustomPhrase::builtinEvaluator(std::string_view key) {
table = {
{"year", []() { return std::to_string(currentYear()); }},
{"year_yy",
[]() { return fmt::format("{:02d}", currentYear() % 100); }},
[]() { return std::format("{:02d}", currentYear() % 100); }},
{"month", []() { return std::to_string(currentMonth()); }},
{"month_mm",
[]() { return fmt::format("{:02d}", currentMonth()); }},
[]() { return std::format("{:02d}", currentMonth()); }},
{"day", []() { return std::to_string(currentDay()); }},
{"day_dd", []() { return fmt::format("{:02d}", currentDay()); }},
{"day_dd", []() { return std::format("{:02d}", currentDay()); }},
{"weekday", []() { return std::to_string(currentWeekday()); }},
{"fullhour", []() { return fmt::format("{:02d}", currentHour()); }},
{"fullhour", []() { return std::format("{:02d}", currentHour()); }},
{"halfhour",
[]() { return fmt::format("{:02d}", currentHalfHour()); }},
[]() { return std::format("{:02d}", currentHalfHour()); }},
{"ampm", []() { return currentHour() < 12 ? "AM" : "PM"; }},
{"minute", []() { return fmt::format("{:02d}", currentMinute()); }},
{"second", []() { return fmt::format("{:02d}", currentSecond()); }},
{"minute", []() { return std::format("{:02d}", currentMinute()); }},
{"second", []() { return std::format("{:02d}", currentSecond()); }},
{"year_cn",
[]() { return toChineseYear(std::to_string(currentYear())); }},
{"year_yy_cn",
[]() {
return toChineseYear(
fmt::format("{:02d}", currentYear() % 100));
std::format("{:02d}", currentYear() % 100));
}},
{"month_cn",
[]() {
Expand Down
Loading

0 comments on commit d01370f

Please sign in to comment.