Files
tdesktop/Telegram/lib_crl/crl/common/crl_common_utils.h
allhaileris afb81b8278
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
init
2026-02-16 15:50:16 +03:00

74 lines
1.8 KiB
C++

// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#pragma once
#include <crl/common/crl_common_config.h>
#include <utility>
namespace crl {
using main_queue_processor = void(*)(void (*callable)(void*), void *argument);
using main_queue_wrapper = void(*)(void (*callable)(void*), void *argument);
} // namespace crl
namespace crl::details {
using true_t = char;
struct false_t {
char data[2];
};
template <typename Return, typename ...Args>
struct check_plain_function {
static false_t check(...);
static true_t check(Return(*)(Args...));
};
template <typename Callable, typename Return, typename ...Args>
constexpr bool is_plain_function_v = sizeof(
check_plain_function<Return, Args...>::check(
std::declval<Callable>())) == sizeof(true_t);
template <typename Callable>
class finalizer {
public:
explicit finalizer(Callable &&callable)
: _callable(std::move(callable)) {
}
finalizer(const finalizer &other) = delete;
finalizer &operator=(const finalizer &other) = delete;
finalizer(finalizer &&other)
: _callable(std::move(other._callable))
, _disabled(std::exchange(other._disabled, true)) {
}
finalizer &operator=(finalizer &&other) {
_callable = std::move(other._callable);
_disabled = std::exchange(other._disabled, true);
return *this;
}
~finalizer() {
if (!_disabled) {
_callable();
}
}
private:
Callable _callable;
bool _disabled = false;
};
template <
typename Callable,
typename = std::enable_if_t<!std::is_reference_v<Callable>>>
finalizer<Callable> finally(Callable &&callable) {
return finalizer<Callable>{ std::move(callable) };
}
} // namespace crl::details