-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
438 changed files
with
1,601 additions
and
8,011 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
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> | ||
#include "core/utils.h" // @todo used for base::take | ||
|
||
// 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.