This repository has been archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add guarded calls to crl::on_main[_sync]().
Some guard types are included: - std::shared_ptr, std::weak_ptr - QPointer, QSharedPointer, QWeakPointer - pointer to derived from QObject (using QPointer) - gsl::not_null<pointer to derived from QObject> Guard types can be added by library users through crl::guard_traits. For example Telegram Desktop adds: - base::weak_ptr - pointer to derived from base::has_weak_ptr (using base::weak_ptr) - gsl::not_null<pointer to derived from base::has_weak_ptr>
- Loading branch information
1 parent
9e11a5c
commit 705a5fd
Showing
4 changed files
with
269 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
This file is part of Telegram Desktop, | ||
the official desktop version of Telegram messaging app, see https://telegram.org | ||
Telegram Desktop 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/telegramdesktop/tdesktop/blob/master/LICENSE | ||
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org | ||
*/ | ||
#pragma once | ||
|
||
#include <crl/common/crl_common_config.h> | ||
|
||
#include <memory> | ||
|
||
namespace crl { | ||
|
||
template <typename T, typename Enable> | ||
struct guard_traits; | ||
|
||
|
||
template <typename T> | ||
struct guard_traits<std::weak_ptr<T>, void> { | ||
static std::weak_ptr<T> create(const std::weak_ptr<T> &value) { | ||
return value; | ||
} | ||
static std::weak_ptr<T> create(std::weak_ptr<T> &&value) { | ||
return std::move(value); | ||
} | ||
static bool check(const std::weak_ptr<T> &guard) { | ||
return guard.lock() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
template <typename T> | ||
struct guard_traits<std::shared_ptr<T>, void> { | ||
static std::weak_ptr<T> create(const std::shared_ptr<T> &value) { | ||
return value; | ||
} | ||
static std::weak_ptr<T> create(std::shared_ptr<T> &&value) { | ||
return value; | ||
} | ||
static bool check(const std::weak_ptr<T> &guard) { | ||
return guard.lock() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
} // namespace crl |
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,74 @@ | ||
/* | ||
This file is part of Telegram Desktop, | ||
the official desktop version of Telegram messaging app, see https://telegram.org | ||
Telegram Desktop 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/telegramdesktop/tdesktop/blob/master/LICENSE | ||
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org | ||
*/ | ||
#pragma once | ||
|
||
#include <crl/common/crl_common_config.h> | ||
|
||
#include <memory> | ||
#include <type_traits> | ||
|
||
namespace crl::details { | ||
|
||
template <typename T> | ||
constexpr std::size_t dependent_zero = 0; | ||
|
||
} // namespace crl::details | ||
|
||
namespace crl { | ||
|
||
template <typename T, typename Enable = void> | ||
struct guard_traits; | ||
|
||
template < | ||
typename Guard, | ||
typename Callable, | ||
typename GuardTraits = guard_traits<std::decay_t<Guard>>, | ||
typename = std::enable_if_t< | ||
sizeof(GuardTraits) != details::dependent_zero<GuardTraits>>> | ||
inline void on_main(Guard &&guard, Callable &&callable) { | ||
on_main([ | ||
passed_guard = GuardTraits::create(std::forward<Guard>(guard)), | ||
passed_callable = std::forward<Callable>(callable) | ||
]() mutable { | ||
if (GuardTraits::check(passed_guard)) { | ||
std::forward<Callable>(passed_callable)(); | ||
} | ||
}); | ||
} | ||
|
||
template < | ||
typename Guard, | ||
typename Callable, | ||
typename GuardTraits = guard_traits<std::decay_t<Guard>>, | ||
typename = std::enable_if_t< | ||
sizeof(GuardTraits) != details::dependent_zero<GuardTraits>>> | ||
inline void on_main_sync(Guard &&guard, Callable &&callable) { | ||
on_main_sync([ | ||
passed_guard = GuardTraits::create(std::forward<Guard>(guard)), | ||
passed_callable = std::forward<Callable>(callable) | ||
]() mutable { | ||
if (GuardTraits::check(passed_guard)) { | ||
std::forward<Callable>(passed_callable)(); | ||
} | ||
}); | ||
} | ||
|
||
} // namespace crl |
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,130 @@ | ||
/* | ||
This file is part of Telegram Desktop, | ||
the official desktop version of Telegram messaging app, see https://telegram.org | ||
Telegram Desktop 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/telegramdesktop/tdesktop/blob/master/LICENSE | ||
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org | ||
*/ | ||
#pragma once | ||
|
||
#include <crl/common/crl_common_config.h> | ||
|
||
#if __has_include(<QtCore/QPointer>) | ||
|
||
class QObject; | ||
|
||
template <typename T> | ||
class QPointer; | ||
|
||
template <typename T> | ||
class QWeakPointer; | ||
|
||
template <typename T> | ||
class QSharedPointer; | ||
|
||
#if __has_include(<gsl/gsl>) | ||
|
||
namespace gsl { | ||
|
||
template <typename T> | ||
class not_null; | ||
|
||
} // namespace gsl | ||
|
||
#endif // gsl | ||
|
||
namespace crl { | ||
|
||
template <typename T, typename Enable> | ||
struct guard_traits; | ||
|
||
template <typename T> | ||
struct guard_traits<QPointer<T>, void> { | ||
static QPointer<T> create(const QPointer<T> &value) { | ||
return value; | ||
} | ||
static QPointer<T> create(QPointer<T> &&value) { | ||
return std::move(value); | ||
} | ||
static bool check(const QPointer<T> &guard) { | ||
return guard.data() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
template <typename T> | ||
struct guard_traits< | ||
T*, | ||
std::enable_if_t< | ||
std::is_base_of_v<QObject, std::remove_cv_t<T>>>> { | ||
static QPointer<T> create(T *value) { | ||
return value; | ||
} | ||
static bool check(const QPointer<T> &guard) { | ||
return guard.data() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
#if __has_include(<gsl/gsl>) | ||
|
||
template <typename T> | ||
struct guard_traits< | ||
gsl::not_null<T*>, | ||
std::enable_if_t< | ||
std::is_base_of_v<QObject, std::remove_cv_t<T>>>> { | ||
static QPointer<T> create(gsl::not_null<T*> value) { | ||
return value.get(); | ||
} | ||
static bool check(const QPointer<T> &guard) { | ||
return guard.data() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
#endif // gsl | ||
|
||
template <typename T> | ||
struct guard_traits<QWeakPointer<T>, void> { | ||
static QWeakPointer<T> create(const QWeakPointer<T> &value) { | ||
return value; | ||
} | ||
static QWeakPointer<T> create(QWeakPointer<T> &&value) { | ||
return std::move(value); | ||
} | ||
static bool check(const QWeakPointer<T> &guard) { | ||
return guard.toStrongRef() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
template <typename T> | ||
struct guard_traits<QSharedPointer<T>, void> { | ||
static QWeakPointer<T> create(const QSharedPointer<T> &value) { | ||
return value; | ||
} | ||
static QWeakPointer<T> create(QSharedPointer<T> &&value) { | ||
return value; | ||
} | ||
static bool check(const QWeakPointer<T> &guard) { | ||
return guard.toStrongRef() != nullptr; | ||
} | ||
|
||
}; | ||
|
||
} // namespace crl | ||
|
||
#endif // Qt |