Add Ui::WrapAsQObject.

This commit is contained in:
John Preston 2020-10-12 12:40:29 +03:00
parent 663e2cc133
commit 8a70d4b6c5

View file

@ -24,23 +24,11 @@ class object_ptr;
namespace Ui { namespace Ui {
namespace details { namespace details {
struct ForwardTag {
};
struct InPlaceTag {
};
template <typename Value> template <typename Value>
class AttachmentOwner : public QObject { class AttachmentOwner : public QObject {
public: public:
template <typename OtherValue>
AttachmentOwner(QObject *parent, const ForwardTag&, OtherValue &&value)
: QObject(parent)
, _value(std::forward<OtherValue>(value)) {
}
template <typename ...Args> template <typename ...Args>
AttachmentOwner(QObject *parent, const InPlaceTag&, Args &&...args) AttachmentOwner(QObject *parent, Args &&...args)
: QObject(parent) : QObject(parent)
, _value(std::forward<Args>(args)...) { , _value(std::forward<Args>(args)...) {
} }
@ -56,6 +44,15 @@ private:
} // namespace details } // namespace details
template <typename Value>
inline not_null<details::AttachmentOwner<std::decay_t<Value>>*> WrapAsQObject(
not_null<QObject*> parent,
Value &&value) {
return CreateChild<details::AttachmentOwner<std::decay_t<Value>>>(
parent.get(),
std::forward<Value>(value));
}
template <typename Widget, typename ...Args> template <typename Widget, typename ...Args>
inline base::unique_qptr<Widget> CreateObject(Args &&...args) { inline base::unique_qptr<Widget> CreateObject(Args &&...args) {
return base::make_unique_q<Widget>( return base::make_unique_q<Widget>(
@ -74,7 +71,6 @@ inline Value *CreateChild(
} else { } else {
return CreateChild<details::AttachmentOwner<Value>>( return CreateChild<details::AttachmentOwner<Value>>(
parent, parent,
details::InPlaceTag{},
std::forward<Args>(args)...)->value(); std::forward<Args>(args)...)->value();
} }
} }
@ -92,10 +88,7 @@ template <typename Value>
inline not_null<std::decay_t<Value>*> AttachAsChild( inline not_null<std::decay_t<Value>*> AttachAsChild(
not_null<QObject*> parent, not_null<QObject*> parent,
Value &&value) { Value &&value) {
return CreateChild<details::AttachmentOwner<std::decay_t<Value>>>( return WrapAsQObject(parent, std::forward<Value>(value))->value();
parent.get(),
details::ForwardTag{},
std::forward<Value>(value))->value();
} }
[[nodiscard]] bool AppInFocus(); [[nodiscard]] bool AppInFocus();