Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
typed_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/traits.h"
6#include "utils/uuid_identifiable_object.h"
7#include "utils/uuid_reference.h"
8
9#include <type_safe/strong_typedef.hpp>
10
11namespace zrythm::utils
12{
13
29template <UuidIdentifiable T> class TypedUuidReference
30{
31public:
32 using UuidType = typename T::Uuid;
33
34 TypedUuidReference () = delete;
35
36 TypedUuidReference (const UuidType &id, IObjectRegistry &registry)
37 : ref_ (type_safe::get (id), registry)
38 {
39 }
40
41 TypedUuidReference (IObjectRegistry &registry) : ref_ (registry) { }
42
43 TypedUuidReference (const TypedUuidReference &other) = default;
44 TypedUuidReference &operator= (const TypedUuidReference &other) = default;
45 TypedUuidReference (TypedUuidReference &&other) noexcept = default;
46 TypedUuidReference &operator= (TypedUuidReference &&other) noexcept = default;
47
48 template <UuidIdentifiable U>
49 requires std::derived_from<U, T>
50 TypedUuidReference (const TypedUuidReference<U> &other)
51 : ref_ (other.as_untyped ())
52 {
53 }
54
55 UuidType id () const { return UuidType (ref_.id ()); }
56
57 void set_id (const UuidType &id) { ref_.set_id (type_safe::get (id)); }
58
59 T * get () const
60 {
61 auto * base = ref_.get ();
62 return qobject_cast<T *> (base);
63 }
64
65 template <typename DerivedT>
66 requires std::derived_from<DerivedT, T>
67 DerivedT * get_object_as () const
68 {
69 return qobject_cast<DerivedT *> (get ());
70 }
71
72 UuidIdentifiableBase * get_object_base () const { return ref_.get (); }
73
74 T * get_or_throw () const
75 {
76 auto * ptr = get ();
77 if (ptr == nullptr)
78 {
79 throw std::runtime_error (
80 fmt::format ("TypedUuidReference: object not found or wrong type"));
81 }
82 return ptr;
83 }
84
85 bool has_value () const { return ref_.has_value (); }
86
87 IObjectRegistry &get_registry () const { return ref_.get_registry (); }
88
89 const UuidReference &as_untyped () const { return ref_; }
90
91 friend void to_json (nlohmann::json &j, const TypedUuidReference &ref)
92 {
93 to_json (j, ref.ref_);
94 }
95 friend void from_json (const nlohmann::json &j, TypedUuidReference &ref)
96 {
97 from_json (j, ref.ref_);
98 }
99
100 friend auto format_as (const TypedUuidReference &ref)
101 {
102 return type_safe::get (ref.id ());
103 }
104
105private:
106 UuidReference ref_;
107};
108
109} // namespace zrythm::utils
Abstract interface for a UUID-keyed object registry.
QObject-based base for all UUID-identifiable objects.
Untyped, reference-counted UUID reference into an IObjectRegistry.
String utilities.