KWaylandServer

utils.h
1 /*
2  SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #pragma once
8 
9 #include <KWaylandServer/kwaylandserver_export.h>
10 
11 #include <QRegion>
12 
13 #include <limits>
14 #include <type_traits>
15 
16 struct wl_resource;
17 
18 namespace KWaylandServer
19 {
20 template<typename T>
21 struct SafeGlobalDeleter {
22  static inline void cleanup(T *global)
23  {
24  if (global) {
25  global->remove();
26  }
27  }
28 };
29 
30 template<typename T>
31 using ScopedGlobalPointer = QScopedPointer<T, SafeGlobalDeleter<T>>;
32 
33 /**
34  * Returns an infinite region.
35  */
36 inline QRegion KWAYLANDSERVER_EXPORT infiniteRegion()
37 {
38  return QRegion(std::numeric_limits<int>::min() / 2, // "/ 2" is to avoid integer overflows
39  std::numeric_limits<int>::min() / 2,
40  std::numeric_limits<int>::max(),
41  std::numeric_limits<int>::max());
42 }
43 
44 template<typename T>
45 T resource_cast(::wl_resource *resource)
46 {
47  using ObjectType = std::remove_pointer_t<std::remove_cv_t<T>>;
48  if (auto resourceContainer = ObjectType::Resource::fromResource(resource)) {
49  return static_cast<T>(resourceContainer->object());
50  }
51  return T();
52 }
53 
54 } // namespace KWaylandServer
ObjectType
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:08:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.