-
Notifications
You must be signed in to change notification settings - Fork 9
/
pybind11_all.h
145 lines (117 loc) · 4.59 KB
/
pybind11_all.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#ifndef PYTHON_PYBIND11_ALL_H
#define PYTHON_PYBIND11_ALL_H
#include <filesystem>
#include <QDir>
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl/filesystem.h>
#include "pybind11_qt/pybind11_qt.h"
#include "pybind11_utils/functional.h"
#include "pybind11_utils/shared_cpp_owner.h"
#include "pybind11_utils/smart_variant_wrapper.h"
#include <game_feature.h>
#include <isavegame.h>
#include <pluginrequirements.h>
namespace mo2::python {
namespace detail {
template <>
struct smart_variant_converter<QString> {
static QString from(std::filesystem::path const& path)
{
return QString::fromStdWString(path.native());
}
static QString from(QFileInfo const& fileInfo)
{
return fileInfo.filePath();
}
static QString from(QDir const& dir) { return dir.path(); }
};
template <>
struct smart_variant_converter<std::filesystem::path> {
static std::filesystem::path from(QString const& value)
{
return {value.toStdWString()};
}
static std::filesystem::path from(QFileInfo const& fileInfo)
{
return fileInfo.filesystemFilePath();
}
static std::filesystem::path from(QDir const& dir)
{
return dir.filesystemPath();
}
};
// we do not need specialization for QFileInfo and QDir because both of them can
// be constructed from std::filesystem::path and QString already
} // namespace detail
using FileWrapper = smart_variant<QString, std::filesystem::path, QFileInfo>;
using DirectoryWrapper = smart_variant<QString, std::filesystem::path, QDir>;
// wrap the given function to accept FileWrapper (str | PathLike | QFileInfo) at the
// given argument positions (or any valid positions if Is... is empty)
//
template <std::size_t... Is, class Fn>
auto wrap_for_filepath(Fn&& fn)
{
return mo2::python::wrap_arguments<FileWrapper, Is...>(std::forward<Fn>(fn));
}
// wrap the given function to accept DirectoryWrapper (str | PathLike | QDir)
// at the given argument positions (or any valid positions if Is... is empty)
//
template <std::size_t... Is, class Fn>
auto wrap_for_directory(Fn&& fn)
{
return mo2::python::wrap_arguments<DirectoryWrapper, Is...>(
std::forward<Fn>(fn));
}
// wrap a function-like object to return a FileWrapper instead of its return type,
// useful to generate proper typing in Python
//
// note that QFileInfo has a __fspath__ in Python, so it is quite easy to convert
// from "FileWrapper", a.k.a., str | os.PathLike | QFileInfo to Path by simply
// calling Path() on the return type if necessary
//
// this should be combined with custom return-value in PYBIND11_OVERRIDE(_PURE), see
// ISaveGame binding for an example
//
template <class Fn>
auto wrap_return_for_filepath(Fn&& fn)
{
return mo2::python::wrap_return<FileWrapper>(std::forward<Fn>(fn));
}
// similar to wrap_return_for_filepath, except it returns a DirectoryWrapper instead
// of its return type
//
// this is much less practical than wrap_return_for_filepath since QDir does not
// expose __fspath__, so more complex things need to be done in Python, which is why
// this should be used carefully (e.g., should not be used if the return type is
// already QDir)
//
template <class Fn>
auto wrap_return_for_directory(Fn&& fn)
{
return mo2::python::wrap_return<DirectoryWrapper>(std::forward<Fn>(fn));
}
// convert a QList to QStringList - QString must be constructible from QString
//
template <class T>
QStringList toQStringList(QList<T> const& list)
{
static_assert(std::is_constructible_v<QString, T>,
"QString must be constructible from T.");
return {list.begin(), list.end()};
}
// convert a QStringList to a QList - T must be constructible from QString
//
template <class T>
QList<T> toQList(QStringList const& list)
{
static_assert(std::is_constructible_v<T, QString>,
"T must be constructible from QString.");
return {list.begin(), list.end()};
}
} // namespace mo2::python
MO2_PYBIND11_SHARED_CPP_HOLDER(MOBase::IPluginRequirement)
MO2_PYBIND11_SHARED_CPP_HOLDER(MOBase::ISaveGame)
MO2_PYBIND11_SHARED_CPP_HOLDER(MOBase::GameFeature)
#endif