Zrythm v2.0.0-alpha.1
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
qt.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <QPointer>
7
8namespace zrythm::utils
9{
10
16template <class T>
17constexpr bool
18values_equal_for_qproperty_type (const T &a, const T &b)
19{
20 if constexpr (std::is_floating_point_v<T>)
21 return qFuzzyCompare (a, b);
22 else
23 return a == b;
24}
25
26template <typename T>
27concept QObjectDerived = std::is_base_of_v<QObject, T>;
28
35template <QObjectDerived T> class QObjectUniquePtr
36{
37public:
38 QObjectUniquePtr (T * ptr = nullptr) : ptr_ (ptr) { }
39
40 ~QObjectUniquePtr () { reset (); }
41
42 Q_DISABLE_COPY (QObjectUniquePtr)
43
44 // Allow moving
45 QObjectUniquePtr (QObjectUniquePtr &&other) noexcept : ptr_ (other.release ())
46 {
47 }
48
49 // Allow converting move from a derived type
50 template <typename U>
51 QObjectUniquePtr (QObjectUniquePtr<U> &&other) noexcept
52 requires std::derived_from<U, T>
53 : ptr_ (other.release ())
54 {
55 }
56 QObjectUniquePtr &operator= (QObjectUniquePtr &&other) noexcept
57 {
58 if (this != &other)
59 {
60 reset (other.release ());
61 }
62 return *this;
63 }
64
65 template <typename U>
66 QObjectUniquePtr &operator= (QObjectUniquePtr<U> &&other) noexcept
67 requires std::derived_from<U, T>
68 {
69 if (this != &other)
70 {
71 reset (other.release ());
72 }
73 return *this;
74 }
75
76 void reset (T * ptr = nullptr)
77 {
78 if (ptr_ != ptr)
79 {
80 if (ptr_ != nullptr)
81 {
82 delete ptr_.get ();
83 }
84 ptr_ = ptr;
85 }
86 }
87
88 T * release ()
89 {
90 auto ptr = ptr_;
91 ptr_ = nullptr;
92 return ptr.get ();
93 }
94
95 T * get () const { return ptr_; }
96 T * operator->() const { return ptr_; }
97 T &operator* () const { return *ptr_; }
98 explicit operator bool () const { return !ptr_.isNull (); }
99
100 bool operator== (std::nullptr_t) const { return ptr_.isNull (); }
101
102 // Conversion to QPointer
103 explicit operator QPointer<T> () const { return ptr_; }
104
105private:
106 QPointer<T> ptr_;
107};
108
109template <typename T, typename... Args>
111make_qobject_unique (Args &&... args)
112{
113 return QObjectUniquePtr<T> (new T (std::forward<Args> (args)...));
114}
115} // namespace zrythm::utils
A unique pointer for QObject objects that also works with QObject-based ownership.
Definition qt.h:36
String utilities.
constexpr bool values_equal_for_qproperty_type(const T &a, const T &b)
Helper that checks if 2 values are equal.
Definition qt.h:18