Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
optional_ref.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <cassert>
7#include <functional>
8#include <optional>
9
10namespace zrythm::utils
11{
12
25template <typename T> struct OptionalRef
26{
27 OptionalRef () = default;
28 OptionalRef (std::nullopt_t) { }
29 OptionalRef (T &ref) : ref_ (ref) { }
30
31 std::optional<std::reference_wrapper<T>> ref_;
32
39 {
40 assert (has_value ());
41 return ref_->get ();
42 }
43 const T &operator* () const
44 {
45 assert (has_value ());
46 return ref_->get ();
47 }
48
49 const T * operator->() const
50 {
51 assert (has_value ());
52 return std::addressof (ref_->get ());
53 }
54 T * operator->()
55 {
56 assert (has_value ());
57 return std::addressof (ref_->get ());
58 }
59
60 explicit operator bool () const { return has_value (); }
61
67 bool has_value () const { return ref_.has_value (); }
68
74 T &value ()
75 {
76 assert (has_value ());
77 return ref_->get ();
78 }
79};
80} // namespace zrythm::utils
String utilities.
T & operator*()
Dereference the underlying value.
T & value()
Get a reference to the underlying value.
bool has_value() const
Check if a value is present.