16#include "AUI/Common/ASignal.h"
17#include "AUI/Traits/concepts.h"
18#include "AUI/Util/ADataBinding.h"
20template<
typename T, aui::invocable<const T&> AdapterCallable>
31 using Observer = std::function<void()>;
32 using ObserverHandle = Observer*;
35 mValue(std::move(initial))
39 void setValue(T t, ObserverHandle exceptObserver =
nullptr) {
41 mValue = std::move(t);
42 notifyObservers(exceptObserver);
46 void notifyObservers(ObserverHandle exceptObserver =
nullptr) {
47 for (
const auto& v : mObservers) {
48 if (&v != exceptObserver) {
81 operator const T&()
const noexcept
87 const T& value()
const noexcept {
91 T* operator->()
noexcept {
95 T
const * operator->()
const noexcept {
103 template<
typename Observer_t>
105 constexpr bool expectsValue = !std::is_invocable_v<Observer_t>;
106 if constexpr (expectsValue) {
108 mObservers.push_back([
this, observer = std::forward<Observer_t>(observer)] {
113 mObservers.push_back(std::forward<Observer_t>(observer));
116 return &mObservers.back();
123 template<
typename Observer_t>
125 return addObserver(std::forward<Observer_t>(observer));
139 mObservers.erase(std::remove_if(mObservers.begin(), mObservers.end(), [&](
const Observer& o) {
141 }), mObservers.end());
144 template<aui::invocable<const T&> AdapterCallable>
149 std::list<Observer> mObservers;
152template<
typename T, aui::invocable<const T&> AdapterCallable>
155 AdapterCallable callable;
157 using return_t =
decltype(callable(std::declval<T>()));
161template<aui::invocable<const T&> AdapterCallable>
163 return { .field = *
this, .callable = std::forward<AdapterCallable>(callable) };
167template<
typename View,
typename Data>
169 using ObserverHandle =
typename std::decay_t<
decltype(observable)>::ObserverHandle;
170 auto observerHandle = _new<ObserverHandle>(
nullptr);
172 *observerHandle = observable << [weak =
object.weak(), observerHandle, observable = &observable](
const Data& newValue) {
173 auto object = weak.lock();
174 if (
object ==
nullptr) {
181 AObject::connect(
object.get()->*getter,
object, [&observable, observerHandle = *observerHandle](Data newValue) {
182 observable.setValue(std::move(newValue), observerHandle);
189template<
typename View,
typename ModelData,
typename AdapterCallback>
191 using Data =
typename AFieldObservableAdapter<ModelData, AdapterCallback>::return_t;
192 auto& observable = observableAdapter.field;
193 using ObserverHandle =
typename std::decay_t<
decltype(observable)>::ObserverHandle;
194 auto observerHandle = _new<ObserverHandle>(
nullptr);
196 *observerHandle = observable << [weak =
object.weak(), observable = &observable, observerHandle, transformer = std::move(observableAdapter.callable)](
const ModelData& newValue) {
197 auto object = weak.lock();
198 if (
object ==
nullptr) {
199 observable->removeObserver(*observerHandle);
Stores a value and observes it's changes, notifying observers.
Definition: AFieldObservable.h:29
ObserverHandle operator<<(Observer_t observer)
Adds an observer, immediately feeding the observer with the current value.
Definition: AFieldObservable.h:124
void operator>>(ObserverHandle h)
Removes an observer.
Definition: AFieldObservable.h:131
ObserverHandle addObserver(Observer_t &&observer)
Adds an observer, immediately feeding the observer with the current value.
Definition: AFieldObservable.h:104
void removeObserver(ObserverHandle h)
Removes an observer.
Definition: AFieldObservable.h:138
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Definition: ADataBinding.h:24
Definition: AFieldObservable.h:153