AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
APropertyPrecomputed.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include "AObjectBase.h"
15#include "ASignal.h"
16
17namespace aui::property_precomputed {
18namespace detail {
19struct DependencyObserver : AObjectBase {
20public:
21 virtual void invalidate() = 0;
22};
23
24struct API_AUI_CORE DependencyObserverRegistrar {
25 explicit DependencyObserverRegistrar(DependencyObserver& observer);
26 ~DependencyObserverRegistrar();
27
28private:
29 DependencyObserver* mPrevObserver;
30};
31} // namespace detail
32
37API_AUI_CORE void addDependency(const AAbstractSignal& signal);
38
39} // namespace aui::property_precomputed
40
63template<typename T>
64struct APropertyPrecomputed final : aui::property_precomputed::detail::DependencyObserver {
65 using Underlying = T;
66 using Factory = std::function<T()>;
67
68 template<aui::factory<T> Factory>
69 APropertyPrecomputed(Factory&& expression): mCurrentValue([this, expression = std::forward<Factory>(expression)] { // NOLINT(*-explicit-constructor)
70 clearSignals();
71 aui::property_precomputed::detail::DependencyObserverRegistrar r(*this);
72 return expression();
73 }) {
74
75 }
76
77 APropertyPrecomputed(const APropertyPrecomputed&) = delete;
78 APropertyPrecomputed(APropertyPrecomputed&&) noexcept = delete;
79
80 template <ASignalInvokable SignalInvokable>
81 void operator^(SignalInvokable&& t) {
82 t.invokeSignal(nullptr);
83 }
84
85 void invalidate() override {
86 mCurrentValue.reset();
87 if (this->changed) {
88 emit this->changed(value());
89 }
90 }
91
92 AObjectBase* boundObject() {
93 return this;
94 }
95
96 [[nodiscard]]
97 const T& value() const {
98 return mCurrentValue;
99 }
100
101 [[nodiscard]]
102 operator const T&() const {
103 return value();
104 }
105
106 [[nodiscard]]
107 const T& operator*() const {
108 return value();
109 }
110
111signals:
112 emits<T> changed;
113
114private:
115 aui::lazy<T> mCurrentValue;
116};
117
118template<aui::invocable<> Factory>
120
121static_assert(APropertyReadable<APropertyPrecomputed<int>>, "APropertyPrecomputed must be a APropertyReadable");
Definition Factory.h:18
Definition concepts.h:194
ASignal< Args... > emits
A signal declaration.
Definition ASignal.h:348
#define emit
emits the specified signal in context of this object.
Definition AObject.h:310
Definition APropertyPrecomputed.h:64
A value that initializes when accessed for the first time.
Definition values.h:182