Skip to content

Commit

Permalink
Moving to C++20
Browse files Browse the repository at this point in the history
  • Loading branch information
winseros committed Jan 28, 2024
1 parent e3b6699 commit 162a13d
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_definitions(-DPBOM_VERSION="${PBOM_VERSION}")
add_definitions(-DPBOM_PROJECT_SITE="https://github.com/winseros/pboman3")
add_definitions(-DPBOM_API_SITE="https://api.github.com/repos/winseros/pboman3")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_subdirectory(pbom)
Expand Down
21 changes: 12 additions & 9 deletions pbom/commandline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ namespace pboman3 {
using namespace CLI;
using namespace std;

template <typename TChr>
concept CharOrWChar = is_same_v<TChr, char> || is_same_v<TChr, wchar_t>;

struct CommandLine {
struct Command {
Command() : command(nullptr) {
Expand All @@ -15,14 +18,14 @@ namespace pboman3 {

App* command;

bool hasBeenSet() const {
[[nodiscard]] bool hasBeenSet() const {
return command->parsed();
}

virtual void configure(App* cli) = 0;
};

template <typename TChr>
template <CharOrWChar TChr>
struct CommandOpen : Command {
basic_string<TChr> fileName;

Expand All @@ -34,7 +37,7 @@ namespace pboman3 {
}
};

template <typename TChr>
template <CharOrWChar TChr>
struct PackCommandBase : Command {
PackCommandBase()
: optOutputPath(nullptr)
Expand Down Expand Up @@ -66,7 +69,7 @@ namespace pboman3 {
#endif
};

template <typename TChr>
template <CharOrWChar TChr>
struct CommandPack : PackCommandBase<TChr> {
vector<basic_string<TChr>> folders;

Expand All @@ -92,7 +95,7 @@ namespace pboman3 {
}
};

template <typename TChr>
template <CharOrWChar TChr>
struct CommandUnpack : PackCommandBase<TChr> {
vector<basic_string<TChr>> files;

Expand All @@ -116,7 +119,7 @@ namespace pboman3 {
}
};

template <typename TChr>
template <CharOrWChar TChr>
struct Result {
#ifdef PBOM_GUI
CommandOpen<TChr> open;
Expand All @@ -131,7 +134,7 @@ namespace pboman3 {
: app_(app) {
}

template <typename TChr>
template <CharOrWChar TChr>
shared_ptr<Result<TChr>> build() const {
auto result = make_shared<Result<TChr>>();
#ifdef PBOM_GUI
Expand All @@ -143,12 +146,12 @@ namespace pboman3 {
return result;
}

template <typename TChr>
template <CharOrWChar TChr>
static QString toQt(const basic_string<TChr>& str) {
return QString::fromStdString(str);
}

template <typename TChr>
template <CharOrWChar TChr>
static QStringList toQt(const vector<basic_string<TChr>>& items) {
QStringList qtItems;
qtItems.reserve(static_cast<qsizetype>(items.size()));
Expand Down
4 changes: 2 additions & 2 deletions pbom/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace pboman3 {
return 0;
}

template <typename TChr>
template <CharOrWChar TChr>
int RunWithCliOptions(int argc, TChr* argv[]) {
using namespace CLI;
using namespace pboman3;
Expand Down Expand Up @@ -73,7 +73,7 @@ namespace pboman3 {
return exitCode;
}

template <typename TChr>
template <CharOrWChar TChr>
int RunMain(int argc, TChr* argv[]) {
const int exitCode = RunWithCliOptions(argc, argv);
return exitCode;
Expand Down
6 changes: 3 additions & 3 deletions pbom/domain/abstractnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace pboman3::domain {
template <typename T>
class AbstractNode : public QObject {
public:
AbstractNode(T* parentNode)
explicit AbstractNode(T* parentNode)
: parentNode_(parentNode) {
}

Expand All @@ -20,7 +20,7 @@ namespace pboman3::domain {
return childList_.at(index).get();
}

int depth() const {
[[nodiscard]] int depth() const {
int result = 0;
T* parentNode = parentNode_;
while (parentNode) {
Expand All @@ -30,7 +30,7 @@ namespace pboman3::domain {
return result;
}

qsizetype count() const {
[[nodiscard]] qsizetype count() const {
return childList_.size();
}

Expand Down
2 changes: 2 additions & 0 deletions pbom/io/pbodatastream.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace pboman3::io {
PboDataStream& operator<<(const QString& src);

template <typename T>
requires std::is_fundamental_v<T>
PboDataStream& operator>>(T& out) {
if (file_->read(reinterpret_cast<char*>(&out), sizeof out) != sizeof out) {
throw PboEofException();
Expand All @@ -24,6 +25,7 @@ namespace pboman3::io {
}

template <typename T>
requires std::is_fundamental_v<T>
PboDataStream& operator<<(const T& src) {
file_->write(reinterpret_cast<const char*>(&src), sizeof src);
return *this;
Expand Down
10 changes: 5 additions & 5 deletions pbom/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
using namespace std;

namespace pboman3 {
template <typename TChr>
template <CharOrWChar TChr>
class PboApplication : public QApplication {
public:
PboApplication(int& argc, TChr* argv[])
Expand Down Expand Up @@ -171,7 +171,7 @@ namespace pboman3 {
return 0;
}

template <typename TChr>
template <CharOrWChar TChr>
int RunWithCliOptions(int argc, TChr* argv[]) {
using namespace CLI;
using namespace pboman3;
Expand Down Expand Up @@ -231,7 +231,7 @@ namespace pboman3 {
return exitCode;
}

template <typename TChr>
template <CharOrWChar TChr>
QString ReadFileName(TChr* argv[]) {
return QString(argv[1]);
}
Expand All @@ -244,7 +244,7 @@ namespace pboman3 {
#endif


template <typename TChr>
template <CharOrWChar TChr>
int RunMain(int argc, TChr* argv[]) {
int exitCode;
if (argc == 2) {
Expand Down Expand Up @@ -277,7 +277,7 @@ void HandleEptr(const std::exception_ptr& ptr) try {
LOG(critical, "Uncaught exception has been thrown:", ex.what())
}

template <typename TChr>
template <pboman3::CharOrWChar TChr>
int MainImpl(int argc, TChr* argv[]) {
try {
const int res = pboman3::RunMain(argc, argv);
Expand Down
6 changes: 4 additions & 2 deletions pbom/ui/settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ namespace pboman3::ui {
void loadSettings() const;

template <typename T>
static void setRadioButtonValue(const QWidget* parent, T index) {
requires std::is_enum_v<T>
static void setRadioButtonValue(const QWidget *parent, T index) {
static_assert(std::is_enum_v<T>);
dynamic_cast<QRadioButton*>(parent->children().at(static_cast<int>(index) + 1))->setChecked(true);
}

template <typename T>
static T getRadioButtonValue(const QWidget* parent) {
requires std::is_enum_v<T>
static T getRadioButtonValue(const QWidget *parent) {
static_assert(std::is_enum_v<T>);
for (auto i = 0; i < parent->children().count(); i++) {
const auto radioButton = dynamic_cast<QRadioButton*>(parent->children().at(i));
Expand Down
2 changes: 1 addition & 1 deletion pbom/util/enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace pboman3::util {
}

template <typename TEnum, TEnum... Values>
requires std::is_enum_v<TEnum>
bool TryReadEnum(const QVariant& value, TEnum& result) {
static_assert(std::is_enum_v<TEnum>);
static_assert(sizeof...(Values) > 0);

if (value.typeId() != QMetaType::Int)
Expand Down
15 changes: 13 additions & 2 deletions pbom/util/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace pboman3::util {

class JsonStructureException : public AppException {
public:
JsonStructureException(QString message);
explicit JsonStructureException(QString message);

PBOMAN_EX_HEADER(JsonStructureException)
};
Expand All @@ -37,6 +37,10 @@ namespace pboman3::util {
}

template <typename T>
requires requires (T t) {
json::JsonValueTrait<T>::read(t);
json::JsonValueTrait<T>::typeName;
}
class JsonValue {
public:
T& value() {
Expand Down Expand Up @@ -86,7 +90,7 @@ namespace pboman3::util {

void settle(const QJsonValue& parent, const QString& parentPath, const QString& name, JsonMandatory mandatory = JsonMandatory::Yes);

QJsonObject makeJson() const;
[[nodiscard]] QJsonObject makeJson() const;

protected:
virtual void inflate(const QString& path, const QJsonObject& json) = 0;
Expand Down Expand Up @@ -120,6 +124,13 @@ namespace pboman3::util {
}

template <typename T>
requires requires {
typename json::JsonArrayTraits<T>::TypeInner;
typename json::JsonArrayTraits<T>::TypeOuter;
requires requires (typename json::JsonArrayTraits<T>::TypeOuter t) {
json::JsonArrayTraits<T>::makeJson(t);
};
}
class JsonArray {
public:
QList<typename json::JsonArrayTraits<T>::TypeOuter> data() {
Expand Down
8 changes: 8 additions & 0 deletions pbom/util/qpointerlistiterator.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#pragma once

#include <QSharedPointer>
#include <concepts>
#include <iterator>

namespace pboman3::util {
template <typename TElem, typename TIt = typename QList<QSharedPointer<TElem>>::iterator>
requires requires {
std::input_iterator<TIt>;
requires requires (TElem elem, TIt it) {
{it->get()} -> std::convertible_to<TElem*>;
};
}
class QPointerListIterator {
public:
explicit QPointerListIterator(TIt it)
Expand Down

0 comments on commit 162a13d

Please sign in to comment.