-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add Qt bindings #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
48f200f
8cc27cd
ba7aead
cf3487c
f528902
546f1f9
8b5c79b
4d81db6
b67cde9
1f77c32
a3d6cdb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #pragma once | ||
|
|
||
| #include "types.hpp" | ||
|
|
||
| #include <QString> | ||
| #include <QJsonValue> | ||
| #include <QJsonObject> | ||
| #include <QJsonArray> | ||
| #include <QJsonDocument> | ||
|
|
||
| #include <future> | ||
| #include <memory> | ||
|
|
||
| namespace #root_namespace { | ||
|
|
||
| template<typename T> | ||
| struct [[nodiscard]] Result { | ||
| T result(); | ||
| QString error_message(); | ||
| int32_t error_code = 0; | ||
|
|
||
| /** | ||
| * Returns the result value. If the result is an error, logs a warning | ||
| * with the error details and the caller's source location, and returns | ||
| * a default-constructed T. | ||
| */ | ||
| T valueOrDefault(const char* file = __builtin_FILE(), int line = __builtin_LINE()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note for other reviewers: this |
||
| if (error_code) { | ||
| qWarning() << file << ":" << line | ||
| << "Error " << error_code << error_message; | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
|
|
||
| template<> | ||
| struct [[nodiscard]] Result<void> { | ||
| QString error_message(); | ||
| int32_t error_code = 0; | ||
|
|
||
| /** | ||
| * If the result is an error, logs a warning with the error details and the | ||
| * caller's source location | ||
| */ | ||
| void logError(const char* file = __builtin_FILE(), int line = __builtin_LINE()) { | ||
| if (error_code) { | ||
| qWarning() << file << ":" << line | ||
| << "Error " << error_code << error_message; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| static Result<QJsonValue> parseResult(const QJsonObject& val) { | ||
| if (val.contains("error")) { | ||
| QJsonObject err = val["error"].toObject(); | ||
| QJsonValue error_message = err["message"]; | ||
| int error_code = err["code"].toInt(); | ||
| if (!error_message.isString() || error_code == 0) | ||
| return {{}, "Invalid error in response: " + QJsonDocument(val).toJson(QJsonDocument::Compact), -32700}; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a comment: this is apparently still the standard way for C++ to initialize this kind of structures. "Designated initializers" ( Otherwise there is even
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes DeltaTouch would be using C++17. Because we still want to support Ubuntu Touch 20.04 we don't have a recent enough GCC version for newer C++ versions than C++17. |
||
| return {{}, error_message.toString(), error_code}; | ||
| } | ||
| if (!val.contains("result")) | ||
| return {{}, "Neither error nor result in response: " + QJsonDocument(val).toJson(QJsonDocument::Compact), -32700}; | ||
| return {val["result"], {}, 0}; | ||
| } | ||
|
|
||
| class Transport { | ||
| public: | ||
| virtual std::future<Result<QJsonValue>> send(const QString method, const QJsonValue request) = 0; | ||
| }; | ||
|
|
||
| class RawClient { | ||
| std::unique_ptr<Transport> transport_; | ||
|
|
||
| template <typename T> | ||
| std::future<Result<T>> request(const QString method, | ||
| const QJsonArray params) { | ||
| std::future<Result<QJsonValue>> inner = transport_->send(method, params); | ||
| return std::async( | ||
| std::launch::deferred, | ||
| [method, inner = std::move(inner)]() mutable -> Result<T> { | ||
| auto val = inner.get(); | ||
| if constexpr (std::is_void_v<T>) { | ||
| if (val.error_code) | ||
| return {method + ": " + val.error_message, val.error_code}; | ||
| return {{}, 0}; | ||
| } else { | ||
| if (val.error_code) | ||
| return {{}, method + ": " + val.error_message, val.error_code}; | ||
| T out; | ||
| if (!tryFromJson(val.result, out)) { | ||
| return {{}, | ||
| method + ": Could not parse result " + | ||
| QJsonDocument(QJsonArray{val.result}) | ||
| .toJson(QJsonDocument::Compact), | ||
| -32700}; | ||
| } | ||
| return {out, {}, 0}; | ||
| } | ||
| }); | ||
| } | ||
| public: | ||
| RawClient(std::unique_ptr<Transport> t) : transport_{std::move(t)} {} | ||
|
|
||
| #methods | ||
| }; | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.