Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
qt.h
1// SPDX-FileCopyrightText: © 2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include "utils/types.h"
7
8#include <QPointer>
9
10namespace zrythm::utils
11{
12
18template <class T>
19constexpr bool
20values_equal_for_qproperty_type (const T &a, const T &b)
21{
22 if constexpr (std::is_floating_point_v<T>)
23 return qFuzzyCompare (a, b);
24 else
25 return a == b;
26}
27
28template <typename T>
29concept QObjectDerived = std::is_base_of_v<QObject, T>;
30
37template <QObjectDerived T> class QObjectUniquePtr
38{
39public:
40 QObjectUniquePtr (T * ptr = nullptr) : ptr_ (ptr) { }
41
42 ~QObjectUniquePtr () { reset (); }
43
44 Z_DISABLE_COPY (QObjectUniquePtr)
45
46 // Allow moving
47 QObjectUniquePtr (QObjectUniquePtr &&other) noexcept : ptr_ (other.release ())
48 {
49 }
50 QObjectUniquePtr &operator= (QObjectUniquePtr &&other) noexcept
51 {
52 if (this != &other)
53 {
54 reset (other.release ());
55 }
56 return *this;
57 }
58
59 template <typename U>
60 QObjectUniquePtr &operator= (QObjectUniquePtr<U> &&other) noexcept
61 requires std::derived_from<U, T>
62 {
63 if (this != &other)
64 {
65 reset (other.release ());
66 }
67 return *this;
68 }
69
70 void reset (T * ptr = nullptr)
71 {
72 if (ptr_ != ptr)
73 {
74 if (ptr_ != nullptr)
75 {
76 delete ptr_.get ();
77 }
78 ptr_ = ptr;
79 }
80 }
81
82 T * release ()
83 {
84 auto ptr = ptr_;
85 ptr_ = nullptr;
86 return ptr.get ();
87 }
88
89 T * get () const { return ptr_; }
90 T * operator->() const { return ptr_; }
91 T &operator* () const { return *ptr_; }
92 explicit operator bool () 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:38
String utilities.
Definition algorithms.h:12
constexpr bool values_equal_for_qproperty_type(const T &a, const T &b)
Helper that checks if 2 values are equal.
Definition qt.h:20