Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
uuid_reference.h
1// SPDX-FileCopyrightText: © 2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3#pragma once
4
5#include "utils/iobject_registry.h"
6
7#include <QUuid>
8
9namespace zrythm::utils
10{
11
23class UuidReference
24{
25public:
26 UuidReference () = delete;
27
28 UuidReference (const QUuid &id, IObjectRegistry &registry)
29 : id_ (id), registry_ (&registry)
30 {
31 acquire_ref ();
32 }
33
34 UuidReference (IObjectRegistry &registry) : registry_ (&registry) { }
35
36 UuidReference (const UuidReference &other)
37 : id_ (other.id_), registry_ (other.registry_)
38 {
39 if (id_.has_value () && registry_ != nullptr)
40 {
41 acquire_ref ();
42 }
43 }
44
45 UuidReference &operator= (const UuidReference &other)
46 {
47 if (this != &other)
48 {
49 release_ref ();
50 id_ = other.id_;
51 registry_ = other.registry_;
52 if (id_.has_value () && registry_ != nullptr)
53 {
54 acquire_ref ();
55 }
56 }
57 return *this;
58 }
59
60 UuidReference (UuidReference &&other) noexcept
61 : id_ (std::exchange (other.id_, std::nullopt)),
62 registry_ (std::exchange (other.registry_, nullptr))
63 {
64 }
65
66 UuidReference &operator= (UuidReference &&other) noexcept
67 {
68 if (this != &other)
69 {
70 release_ref ();
71 id_ = std::exchange (other.id_, std::nullopt);
72 registry_ = std::exchange (other.registry_, nullptr);
73 }
74 return *this;
75 }
76
77 ~UuidReference () { release_ref (); }
78
79 QUuid id () const { return id_.value (); }
80
81 void set_id (const QUuid &id)
82 {
83 if (id_.has_value ())
84 {
85 throw std::runtime_error (
86 "Cannot set id of UuidReference that already has an id");
87 }
88 id_ = id;
89 acquire_ref ();
90 }
91
92 UuidIdentifiableBase * get () const
93 {
94 if (!id_.has_value () || registry_ == nullptr)
95 return nullptr;
96 return registry_->find_by_raw_uuid (id_.value ());
97 }
98
99 UuidIdentifiableBase * get_or_throw () const
100 {
101 if (!id_.has_value () || registry_ == nullptr)
102 {
103 throw std::runtime_error ("UuidReference: no id or registry");
104 }
105 auto * obj = registry_->find_by_raw_uuid (id_.value ());
106 if (obj == nullptr)
107 {
108 throw std::runtime_error (
109 "UuidReference: object not found for id "
110 + id_->toString ().toStdString ());
111 }
112 return obj;
113 }
114
115 bool has_value () const { return id_.has_value (); }
116
117 IObjectRegistry &get_registry () const
118 {
119 assert (registry_ != nullptr);
120 return *registry_;
121 }
122
123 friend void to_json (nlohmann::json &j, const UuidReference &ref);
124 friend void from_json (const nlohmann::json &j, UuidReference &ref);
125
126 friend bool operator== (const UuidReference &lhs, const UuidReference &rhs)
127 {
128 return lhs.id_ == rhs.id_;
129 }
130
131protected:
132 void acquire_ref ()
133 {
134 if (id_.has_value () && registry_ != nullptr)
135 {
136 registry_->acquire_reference (id_.value ());
137 }
138 }
139
140 void release_ref ()
141 {
142 if (id_.has_value () && registry_ != nullptr)
143 {
144 registry_->release_reference (id_.value ());
145 }
146 }
147
148 std::optional<QUuid> id_;
149 IObjectRegistry * registry_ = nullptr;
150};
151
152} // namespace zrythm::utils
Abstract interface for a UUID-keyed object registry.
QObject-based base for all UUID-identifiable objects.
String utilities.