Zrythm v2.0.0-DEV
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 QObjectUniquePtr &operator= (QObjectUniquePtr &&other) noexcept
49 {
50 if (this != &other)
51 {
52 reset (other.release ());
53 }
54 return *this;
55 }
56
57 template <typename U>
58 QObjectUniquePtr &operator= (QObjectUniquePtr<U> &&other) noexcept
59 requires std::derived_from<U, T>
60 {
61 if (this != &other)
62 {
63 reset (other.release ());
64 }
65 return *this;
66 }
67
68 void reset (T * ptr = nullptr)
69 {
70 if (ptr_ != ptr)
71 {
72 if (ptr_ != nullptr)
73 {
74 delete ptr_.get ();
75 }
76 ptr_ = ptr;
77 }
78 }
79
80 T * release ()
81 {
82 auto ptr = ptr_;
83 ptr_ = nullptr;
84 return ptr.get ();
85 }
86
87 T * get () const { return ptr_; }
88 T * operator->() const { return ptr_; }
89 T &operator* () const { return *ptr_; }
90 explicit operator bool () const { return !ptr_.isNull (); }
91
92 bool operator== (std::nullptr_t) const { return ptr_.isNull (); }
93
94 // Conversion to QPointer
95 explicit operator QPointer<T> () const { return ptr_; }
96
97private:
98 QPointer<T> ptr_;
99};
100
101template <typename T, typename... Args>
103make_qobject_unique (Args &&... args)
104{
105 return QObjectUniquePtr<T> (new T (std::forward<Args> (args)...));
106}
107} // 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