mirror of https://github.com/procxx/kepka.git
Move object_ptr to separate file base/object_ptr.h
This commit is contained in:
parent
c5c174dd5e
commit
de8e4605d3
|
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
This file is part of Kepka - The Unofficial Telegram Desktop client,
|
||||
see https://github.com/procxx/kepka
|
||||
|
||||
Kepka is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
It is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
In addition, as a special exception, the copyright holders give permission
|
||||
to link the code of portions of this program with the OpenSSL library.
|
||||
|
||||
Full license: https://github.com/procxx/kepka/blob/master/LICENSE
|
||||
Copyright (c) 2018 pro.cxx Community
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
// Smart pointer for QObject*, has move semantics, destroys object if it doesn't have a parent.
|
||||
template <typename Object>
|
||||
class object_ptr {
|
||||
public:
|
||||
object_ptr(std::nullptr_t) {
|
||||
}
|
||||
|
||||
// No default constructor, but constructors with at least
|
||||
// one argument are simply make functions.
|
||||
template <typename Parent, typename... Args>
|
||||
explicit object_ptr(Parent &&parent, Args&&... args) : _object(new Object(std::forward<Parent>(parent), std::forward<Args>(args)...)) {
|
||||
}
|
||||
|
||||
object_ptr(const object_ptr &other) = delete;
|
||||
object_ptr &operator=(const object_ptr &other) = delete;
|
||||
object_ptr(object_ptr &&other) : _object(base::take(other._object)) {
|
||||
}
|
||||
object_ptr &operator=(object_ptr &&other) {
|
||||
auto temp = std::move(other);
|
||||
destroy();
|
||||
std::swap(_object, temp._object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename OtherObject, typename = std::enable_if_t<std::is_base_of<Object, OtherObject>::value>>
|
||||
object_ptr(object_ptr<OtherObject> &&other) : _object(base::take(other._object)) {
|
||||
}
|
||||
|
||||
template <typename OtherObject, typename = std::enable_if_t<std::is_base_of<Object, OtherObject>::value>>
|
||||
object_ptr &operator=(object_ptr<OtherObject> &&other) {
|
||||
_object = base::take(other._object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
object_ptr &operator=(std::nullptr_t) {
|
||||
_object = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// So we can pass this pointer to methods like connect().
|
||||
Object *data() const {
|
||||
return static_cast<Object*>(_object.data());
|
||||
}
|
||||
operator Object*() const {
|
||||
return data();
|
||||
}
|
||||
|
||||
explicit operator bool() const {
|
||||
return _object != nullptr;
|
||||
}
|
||||
|
||||
Object *operator->() const {
|
||||
return data();
|
||||
}
|
||||
Object &operator*() const {
|
||||
return *data();
|
||||
}
|
||||
|
||||
// Use that instead "= new Object(parent, ...)"
|
||||
template <typename Parent, typename... Args>
|
||||
void create(Parent &&parent, Args&&... args) {
|
||||
destroy();
|
||||
_object = new Object(std::forward<Parent>(parent), std::forward<Args>(args)...);
|
||||
}
|
||||
void destroy() {
|
||||
delete base::take(_object);
|
||||
}
|
||||
void destroyDelayed() {
|
||||
if (_object) {
|
||||
if (auto widget = base::up_cast<QWidget*>(data())) {
|
||||
widget->hide();
|
||||
}
|
||||
base::take(_object)->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
~object_ptr() {
|
||||
if (auto pointer = _object) {
|
||||
if (!pointer->parent()) {
|
||||
destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ResultType, typename SourceType>
|
||||
friend object_ptr<ResultType> static_object_cast(object_ptr<SourceType> source);
|
||||
|
||||
private:
|
||||
template <typename OtherObject>
|
||||
friend class object_ptr;
|
||||
|
||||
QPointer<QObject> _object;
|
||||
|
||||
};
|
||||
|
||||
template <typename ResultType, typename SourceType>
|
||||
inline object_ptr<ResultType> static_object_cast(object_ptr<SourceType> source) {
|
||||
auto result = object_ptr<ResultType>(nullptr);
|
||||
result._object = static_cast<ResultType*>(base::take(source._object).data());
|
||||
return std::move(result);
|
||||
}
|
||||
|
|
@ -28,6 +28,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
|||
#include <QPointer>
|
||||
#include "base/lambda.h"
|
||||
#include "base/flags.h"
|
||||
#include "base/object_ptr.h"
|
||||
#include "ui/style/style_core.h"
|
||||
#include "styles/palette.h"
|
||||
#include "styles/style_basic.h"
|
||||
|
|
@ -426,108 +427,6 @@ private:
|
|||
|
||||
};
|
||||
|
||||
// Smart pointer for QObject*, has move semantics, destroys object if it doesn't have a parent.
|
||||
template <typename Object>
|
||||
class object_ptr {
|
||||
public:
|
||||
object_ptr(std::nullptr_t) {
|
||||
}
|
||||
|
||||
// No default constructor, but constructors with at least
|
||||
// one argument are simply make functions.
|
||||
template <typename Parent, typename... Args>
|
||||
explicit object_ptr(Parent &&parent, Args&&... args) : _object(new Object(std::forward<Parent>(parent), std::forward<Args>(args)...)) {
|
||||
}
|
||||
|
||||
object_ptr(const object_ptr &other) = delete;
|
||||
object_ptr &operator=(const object_ptr &other) = delete;
|
||||
object_ptr(object_ptr &&other) : _object(base::take(other._object)) {
|
||||
}
|
||||
object_ptr &operator=(object_ptr &&other) {
|
||||
auto temp = std::move(other);
|
||||
destroy();
|
||||
std::swap(_object, temp._object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename OtherObject, typename = std::enable_if_t<std::is_base_of<Object, OtherObject>::value>>
|
||||
object_ptr(object_ptr<OtherObject> &&other) : _object(base::take(other._object)) {
|
||||
}
|
||||
|
||||
template <typename OtherObject, typename = std::enable_if_t<std::is_base_of<Object, OtherObject>::value>>
|
||||
object_ptr &operator=(object_ptr<OtherObject> &&other) {
|
||||
_object = base::take(other._object);
|
||||
return *this;
|
||||
}
|
||||
|
||||
object_ptr &operator=(std::nullptr_t) {
|
||||
_object = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// So we can pass this pointer to methods like connect().
|
||||
Object *data() const {
|
||||
return static_cast<Object*>(_object.data());
|
||||
}
|
||||
operator Object*() const {
|
||||
return data();
|
||||
}
|
||||
|
||||
explicit operator bool() const {
|
||||
return _object != nullptr;
|
||||
}
|
||||
|
||||
Object *operator->() const {
|
||||
return data();
|
||||
}
|
||||
Object &operator*() const {
|
||||
return *data();
|
||||
}
|
||||
|
||||
// Use that instead "= new Object(parent, ...)"
|
||||
template <typename Parent, typename... Args>
|
||||
void create(Parent &&parent, Args&&... args) {
|
||||
destroy();
|
||||
_object = new Object(std::forward<Parent>(parent), std::forward<Args>(args)...);
|
||||
}
|
||||
void destroy() {
|
||||
delete base::take(_object);
|
||||
}
|
||||
void destroyDelayed() {
|
||||
if (_object) {
|
||||
if (auto widget = base::up_cast<QWidget*>(data())) {
|
||||
widget->hide();
|
||||
}
|
||||
base::take(_object)->deleteLater();
|
||||
}
|
||||
}
|
||||
|
||||
~object_ptr() {
|
||||
if (auto pointer = _object) {
|
||||
if (!pointer->parent()) {
|
||||
destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ResultType, typename SourceType>
|
||||
friend object_ptr<ResultType> static_object_cast(object_ptr<SourceType> source);
|
||||
|
||||
private:
|
||||
template <typename OtherObject>
|
||||
friend class object_ptr;
|
||||
|
||||
QPointer<QObject> _object;
|
||||
|
||||
};
|
||||
|
||||
template <typename ResultType, typename SourceType>
|
||||
inline object_ptr<ResultType> static_object_cast(object_ptr<SourceType> source) {
|
||||
auto result = object_ptr<ResultType>(nullptr);
|
||||
result._object = static_cast<ResultType*>(base::take(source._object).data());
|
||||
return std::move(result);
|
||||
}
|
||||
|
||||
void sendSynteticMouseEvent(QWidget *widget, QEvent::Type type, Qt::MouseButton button, const QPoint &globalPoint);
|
||||
|
||||
inline void sendSynteticMouseEvent(QWidget *widget, QEvent::Type type, Qt::MouseButton button) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue