AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AFieldSignalEmitter.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#include "AUI/Common/ASignal.h"
14
19template <typename T>
21{
22public:
23 using signal_t = ASignal<T>&;
24private:
25 signal_t mSignal;
26 T mValue;
27
28public:
29 AFieldSignalEmitter(signal_t signal, T initial = T()):
30 mSignal(signal), mValue(std::move(initial))
31 {
32
33 }
34
35 AFieldSignalEmitter& operator=(T t)
36 {
37 if (mValue != t) {
38 mValue = std::move(t);
39 mSignal(mValue);
40 }
41 return *this;
42 }
43 operator T() const
44 {
45 return mValue;
46 }
47};
48
49template <>
51{
52 using T = bool;
53public:
54 using signal_t = ASignal<T> &;
55 using signal_sub_t = ASignal<> &;
56private:
57 signal_t mSignal;
58 signal_sub_t mSignalTrue;
59 signal_sub_t mSignalFalse;
60 T mValue;
61
62public:
63 AFieldSignalEmitter(signal_t signal, signal_sub_t signalTrue, signal_sub_t signalFalse, T initial = false)
64 : mSignal(signal),
65 mSignalTrue(signalTrue),
66 mSignalFalse(signalFalse),
67 mValue(initial)
68 {
69 }
70
71 AFieldSignalEmitter& set(AObject* emitter, const T& t)
72 {
73 if (mValue != t) {
74 mValue = t;
75 mSignal(t).invokeSignal(emitter);
76 if (t)
77 {
78 mSignalTrue().invokeSignal(emitter);
79 }
80 else
81 {
82 mSignalFalse().invokeSignal(emitter);
83 }
84 }
85 return *this;
86 }
87 operator bool() const
88 {
89 return mValue;
90 }
91};
Stores a value and observes it's changes, emitting a signal.
Definition: AFieldSignalEmitter.h:21
A base object class.
Definition: AObject.h:49