Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Commit

Permalink
Add guarded calls to crl::on_main[_sync]().
Browse files Browse the repository at this point in the history
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
john-preston committed Dec 30, 2017
1 parent 9e11a5c commit 705a5fd
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/crl/common/crl_common_guards.h
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
74 changes: 74 additions & 0 deletions src/crl/common/crl_common_on_main_guarded.h
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
4 changes: 4 additions & 0 deletions src/crl/crl_on_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#else // CRL_USE_QT
#error "Configuration is not supported."
#endif // !CRL_USE_WINAPI && !CRL_USE_DISPATCH && !CRL_USE_QT

#include <crl/common/crl_common_on_main_guarded.h>
#include <crl/common/crl_common_guards.h>
#include <crl/qt/crl_qt_guards.h>
130 changes: 130 additions & 0 deletions src/crl/qt/crl_qt_guards.h
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

0 comments on commit 705a5fd

Please sign in to comment.