Skip to content

Commit 11a62dd

Browse files
authored
Merge pull request #48 from winseros/cpp20
Target the codebase to C++ 20
2 parents e3b6699 + 12a975d commit 11a62dd

10 files changed

+54
-27
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_definitions(-DPBOM_VERSION="${PBOM_VERSION}")
1111
add_definitions(-DPBOM_PROJECT_SITE="https://github.com/winseros/pboman3")
1212
add_definitions(-DPBOM_API_SITE="https://api.github.com/repos/winseros/pboman3")
1313

14-
set(CMAKE_CXX_STANDARD 17)
14+
set(CMAKE_CXX_STANDARD 20)
1515
set(CMAKE_CXX_STANDARD_REQUIRED ON)
1616

1717
add_subdirectory(pbom)

pbom/commandline.h

+12-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ namespace pboman3 {
66
using namespace CLI;
77
using namespace std;
88

9+
template <typename TChr>
10+
concept CharOrWChar = is_same_v<TChr, char> || is_same_v<TChr, wchar_t>;
11+
912
struct CommandLine {
1013
struct Command {
1114
Command() : command(nullptr) {
@@ -15,14 +18,14 @@ namespace pboman3 {
1518

1619
App* command;
1720

18-
bool hasBeenSet() const {
21+
[[nodiscard]] bool hasBeenSet() const {
1922
return command->parsed();
2023
}
2124

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

25-
template <typename TChr>
28+
template <CharOrWChar TChr>
2629
struct CommandOpen : Command {
2730
basic_string<TChr> fileName;
2831

@@ -34,7 +37,7 @@ namespace pboman3 {
3437
}
3538
};
3639

37-
template <typename TChr>
40+
template <CharOrWChar TChr>
3841
struct PackCommandBase : Command {
3942
PackCommandBase()
4043
: optOutputPath(nullptr)
@@ -66,7 +69,7 @@ namespace pboman3 {
6669
#endif
6770
};
6871

69-
template <typename TChr>
72+
template <CharOrWChar TChr>
7073
struct CommandPack : PackCommandBase<TChr> {
7174
vector<basic_string<TChr>> folders;
7275

@@ -92,7 +95,7 @@ namespace pboman3 {
9295
}
9396
};
9497

95-
template <typename TChr>
98+
template <CharOrWChar TChr>
9699
struct CommandUnpack : PackCommandBase<TChr> {
97100
vector<basic_string<TChr>> files;
98101

@@ -116,7 +119,7 @@ namespace pboman3 {
116119
}
117120
};
118121

119-
template <typename TChr>
122+
template <CharOrWChar TChr>
120123
struct Result {
121124
#ifdef PBOM_GUI
122125
CommandOpen<TChr> open;
@@ -131,7 +134,7 @@ namespace pboman3 {
131134
: app_(app) {
132135
}
133136

134-
template <typename TChr>
137+
template <CharOrWChar TChr>
135138
shared_ptr<Result<TChr>> build() const {
136139
auto result = make_shared<Result<TChr>>();
137140
#ifdef PBOM_GUI
@@ -143,12 +146,12 @@ namespace pboman3 {
143146
return result;
144147
}
145148

146-
template <typename TChr>
149+
template <CharOrWChar TChr>
147150
static QString toQt(const basic_string<TChr>& str) {
148151
return QString::fromStdString(str);
149152
}
150153

151-
template <typename TChr>
154+
template <CharOrWChar TChr>
152155
static QStringList toQt(const vector<basic_string<TChr>>& items) {
153156
QStringList qtItems;
154157
qtItems.reserve(static_cast<qsizetype>(items.size()));

pbom/console.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace pboman3 {
3636
return 0;
3737
}
3838

39-
template <typename TChr>
39+
template <CharOrWChar TChr>
4040
int RunWithCliOptions(int argc, TChr* argv[]) {
4141
using namespace CLI;
4242
using namespace pboman3;
@@ -73,7 +73,7 @@ namespace pboman3 {
7373
return exitCode;
7474
}
7575

76-
template <typename TChr>
76+
template <CharOrWChar TChr>
7777
int RunMain(int argc, TChr* argv[]) {
7878
const int exitCode = RunWithCliOptions(argc, argv);
7979
return exitCode;

pbom/domain/abstractnode.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace pboman3::domain {
88
template <typename T>
99
class AbstractNode : public QObject {
1010
public:
11-
AbstractNode(T* parentNode)
11+
explicit AbstractNode(T* parentNode)
1212
: parentNode_(parentNode) {
1313
}
1414

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

23-
int depth() const {
23+
[[nodiscard]] int depth() const {
2424
int result = 0;
2525
T* parentNode = parentNode_;
2626
while (parentNode) {
@@ -30,7 +30,7 @@ namespace pboman3::domain {
3030
return result;
3131
}
3232

33-
qsizetype count() const {
33+
[[nodiscard]] qsizetype count() const {
3434
return childList_.size();
3535
}
3636

pbom/io/pbodatastream.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace pboman3::io {
77
class PboEofException: public QException {
88
};
99

10+
template <typename T>
11+
concept PboHeaderType = std::is_fundamental_v<T> || std::is_enum_v<T>;
12+
1013
class PboDataStream : public QDataStream {
1114
public:
1215
explicit PboDataStream(PboFile* file);
@@ -15,15 +18,15 @@ namespace pboman3::io {
1518

1619
PboDataStream& operator<<(const QString& src);
1720

18-
template <typename T>
21+
template <PboHeaderType T>
1922
PboDataStream& operator>>(T& out) {
2023
if (file_->read(reinterpret_cast<char*>(&out), sizeof out) != sizeof out) {
2124
throw PboEofException();
2225
}
2326
return *this;
2427
}
2528

26-
template <typename T>
29+
template <PboHeaderType T>
2730
PboDataStream& operator<<(const T& src) {
2831
file_->write(reinterpret_cast<const char*>(&src), sizeof src);
2932
return *this;

pbom/main.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
using namespace std;
2626

2727
namespace pboman3 {
28-
template <typename TChr>
28+
template <CharOrWChar TChr>
2929
class PboApplication : public QApplication {
3030
public:
3131
PboApplication(int& argc, TChr* argv[])
@@ -171,7 +171,7 @@ namespace pboman3 {
171171
return 0;
172172
}
173173

174-
template <typename TChr>
174+
template <CharOrWChar TChr>
175175
int RunWithCliOptions(int argc, TChr* argv[]) {
176176
using namespace CLI;
177177
using namespace pboman3;
@@ -231,7 +231,7 @@ namespace pboman3 {
231231
return exitCode;
232232
}
233233

234-
template <typename TChr>
234+
template <CharOrWChar TChr>
235235
QString ReadFileName(TChr* argv[]) {
236236
return QString(argv[1]);
237237
}
@@ -244,7 +244,7 @@ namespace pboman3 {
244244
#endif
245245

246246

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

280-
template <typename TChr>
280+
template <pboman3::CharOrWChar TChr>
281281
int MainImpl(int argc, TChr* argv[]) {
282282
try {
283283
const int res = pboman3::RunMain(argc, argv);

pbom/ui/settingsdialog.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ namespace pboman3::ui {
2525
void loadSettings() const;
2626

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

3334
template <typename T>
34-
static T getRadioButtonValue(const QWidget* parent) {
35+
requires std::is_enum_v<T>
36+
static T getRadioButtonValue(const QWidget *parent) {
3537
static_assert(std::is_enum_v<T>);
3638
for (auto i = 0; i < parent->children().count(); i++) {
3739
const auto radioButton = dynamic_cast<QRadioButton*>(parent->children().at(i));

pbom/util/enum.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace pboman3::util {
2121
}
2222

2323
template <typename TEnum, TEnum... Values>
24+
requires std::is_enum_v<TEnum>
2425
bool TryReadEnum(const QVariant& value, TEnum& result) {
25-
static_assert(std::is_enum_v<TEnum>);
2626
static_assert(sizeof...(Values) > 0);
2727

2828
if (value.typeId() != QMetaType::Int)

pbom/util/json.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace pboman3::util {
1212

1313
class JsonStructureException : public AppException {
1414
public:
15-
JsonStructureException(QString message);
15+
explicit JsonStructureException(QString message);
1616

1717
PBOMAN_EX_HEADER(JsonStructureException)
1818
};
@@ -37,6 +37,10 @@ namespace pboman3::util {
3737
}
3838

3939
template <typename T>
40+
requires requires (T t) {
41+
json::JsonValueTrait<T>::read(t);
42+
json::JsonValueTrait<T>::typeName;
43+
}
4044
class JsonValue {
4145
public:
4246
T& value() {
@@ -86,7 +90,7 @@ namespace pboman3::util {
8690

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

89-
QJsonObject makeJson() const;
93+
[[nodiscard]] QJsonObject makeJson() const;
9094

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

122126
template <typename T>
127+
requires requires {
128+
typename json::JsonArrayTraits<T>::TypeInner;
129+
typename json::JsonArrayTraits<T>::TypeOuter;
130+
requires requires (typename json::JsonArrayTraits<T>::TypeOuter t) {
131+
json::JsonArrayTraits<T>::makeJson(t);
132+
};
133+
}
123134
class JsonArray {
124135
public:
125136
QList<typename json::JsonArrayTraits<T>::TypeOuter> data() {

pbom/util/qpointerlistiterator.h

+8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#pragma once
22

33
#include <QSharedPointer>
4+
#include <concepts>
5+
#include <iterator>
46

57
namespace pboman3::util {
68
template <typename TElem, typename TIt = typename QList<QSharedPointer<TElem>>::iterator>
9+
requires requires {
10+
std::input_iterator<TIt>;
11+
requires requires (TElem elem, TIt it) {
12+
{it->get()} -> std::convertible_to<TElem*>;
13+
};
14+
}
715
class QPointerListIterator {
816
public:
917
explicit QPointerListIterator(TIt it)

0 commit comments

Comments
 (0)