kwin/src/wayland/utils.h
Alexander Lohnau 525d12bee5 Run clang-format
If you want git blame to ignore formatting revisions run:
git config blame.ignoreRevsFile .git-blame-ignore-revs
2021-08-29 07:11:06 +02:00

54 lines
1.3 KiB
C++

/*
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#pragma once
#include <KWaylandServer/kwaylandserver_export.h>
#include <QRegion>
#include <limits>
#include <type_traits>
struct wl_resource;
namespace KWaylandServer
{
template<typename T>
struct SafeGlobalDeleter {
static inline void cleanup(T *global)
{
if (global) {
global->remove();
}
}
};
template<typename T>
using ScopedGlobalPointer = QScopedPointer<T, SafeGlobalDeleter<T>>;
/**
* Returns an infinite region.
*/
inline QRegion KWAYLANDSERVER_EXPORT infiniteRegion()
{
return QRegion(std::numeric_limits<int>::min() / 2, // "/ 2" is to avoid integer overflows
std::numeric_limits<int>::min() / 2,
std::numeric_limits<int>::max(),
std::numeric_limits<int>::max());
}
template<typename T>
T resource_cast(::wl_resource *resource)
{
using ObjectType = std::remove_pointer_t<std::remove_cv_t<T>>;
if (auto resourceContainer = ObjectType::Resource::fromResource(resource)) {
return static_cast<T>(resourceContainer->object());
}
return T();
}
} // namespace KWaylandServer