26 UuidReference () =
delete;
29 : id_ (
id), registry_ (®istry)
36 UuidReference (
const UuidReference &other)
37 : id_ (other.id_), registry_ (other.registry_)
39 if (id_.has_value () && registry_ !=
nullptr)
45 UuidReference &operator= (
const UuidReference &other)
51 registry_ = other.registry_;
52 if (id_.has_value () && registry_ !=
nullptr)
60 UuidReference (UuidReference &&other) noexcept
61 : id_ (std::exchange (other.id_, std::nullopt)),
62 registry_ (std::exchange (other.registry_,
nullptr))
66 UuidReference &operator= (UuidReference &&other)
noexcept
71 id_ = std::exchange (other.id_, std::nullopt);
72 registry_ = std::exchange (other.registry_,
nullptr);
77 ~UuidReference () { release_ref (); }
79 QUuid id ()
const {
return id_.value (); }
81 void set_id (
const QUuid &
id)
85 throw std::runtime_error (
86 "Cannot set id of UuidReference that already has an id");
94 if (!id_.has_value () || registry_ ==
nullptr)
96 return registry_->find_by_raw_uuid (id_.value ());
101 if (!id_.has_value () || registry_ ==
nullptr)
103 throw std::runtime_error (
"UuidReference: no id or registry");
105 auto * obj = registry_->find_by_raw_uuid (id_.value ());
108 throw std::runtime_error (
109 "UuidReference: object not found for id "
110 + id_->toString ().toStdString ());
115 bool has_value ()
const {
return id_.has_value (); }
119 assert (registry_ !=
nullptr);
123 friend void to_json (nlohmann::json &j,
const UuidReference &ref);
124 friend void from_json (
const nlohmann::json &j, UuidReference &ref);
126 friend bool operator== (
const UuidReference &lhs,
const UuidReference &rhs)
128 return lhs.id_ == rhs.id_;
134 if (id_.has_value () && registry_ !=
nullptr)
136 registry_->acquire_reference (id_.value ());
142 if (id_.has_value () && registry_ !=
nullptr)
144 registry_->release_reference (id_.value ());
148 std::optional<QUuid> id_;