AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AObject.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 <AUI/Thread/AMutex.h>
15#include <AUI/Traits/concepts.h>
16#include <AUI/Traits/members.h>
17
18#include "AUI/Common/ASet.h"
19#include "AUI/Core.h"
20#include "AUI/Traits/values.h"
21#include "SharedPtrTypes.h"
22
23class AString;
24class AAbstractSignal;
25class AAbstractThread;
26class API_AUI_CORE AObject;
27
28template <typename T>
29concept AAnySignal = requires(T) {
30 std::is_base_of_v<AAbstractSignal, T>;
31 typename T::args_t;
32};
33
34template <typename C>
35concept ASignalInvokable = requires(C&& c) { c.invokeSignal(std::declval<AObject*>()); };
36
37template <typename F, typename Signal>
38concept ACompatibleSlotFor = true; // TODO
39
49class API_AUI_CORE AObject : public aui::noncopyable, public std::enable_shared_from_this<AObject> {
50 friend class AAbstractSignal;
51
52 public:
53 AObject();
54 virtual ~AObject();
55
56 static void disconnect();
57
58 [[nodiscard]] _<AObject> sharedPtr() { return std::enable_shared_from_this<AObject>::shared_from_this(); }
59
60 [[nodiscard]] _weak<AObject> weakPtr() { return std::enable_shared_from_this<AObject>::weak_from_this(); }
61
62 void clearSignals() noexcept;
63
76 template <AAnySignal Signal, aui::derived_from<AObject> Object, ACompatibleSlotFor<Signal> Function>
77 static void connect(Signal& signal, Object* object, Function&& function) {
78 signal.connect(object, std::forward<Function>(function));
79 }
80
93 template <AAnySignal Signal, aui::derived_from<AObject> Object, ACompatibleSlotFor<Signal> Function>
94 static void connect(Signal& signal, Object& object, Function&& function) {
95 signal.connect(&object, std::forward<Function>(function));
96 }
97
109 template <AAnySignal Signal, ACompatibleSlotFor<Signal> Function>
110 void connect(Signal& signal, Function&& function) {
111 signal.connect(this, std::forward<Function>(function));
112 }
113
126 template <AAnySignal Signal, aui::derived_from<AObject> Object, ACompatibleSlotFor<Signal> Function>
127 static void connect(Signal& signal, _<Object> object, Function&& function) {
128 signal.connect(object.get(), std::forward<Function>(function));
129 }
130
131 void setSignalsEnabled(bool enabled) { mSignalsEnabled = enabled; }
132
133 [[nodiscard]] bool isSignalsEnabled() const noexcept { return mSignalsEnabled; }
134
135 template <ASignalInvokable T>
136 void operator^(T&& t) {
137 if (mSignalsEnabled) {
138 t.invokeSignal(this);
139 }
140 }
141
142 _<AAbstractThread> getThread() const { return mAttachedThread; }
143
144 bool isSlotsCallsOnlyOnMyThread() const noexcept { return mSlotsCallsOnlyOnMyThread; }
145
146 static void moveToThread(aui::no_escape<AObject> object, _<AAbstractThread> thread);
147
148 void setSlotsCallsOnlyOnMyThread(bool slotsCallsOnlyOnMyThread) {
149 mSlotsCallsOnlyOnMyThread = slotsCallsOnlyOnMyThread;
150 }
151
152 protected:
156 void setThread(_<AAbstractThread> thread) { mAttachedThread = std::move(thread); }
157
158 private:
159 _<AAbstractThread> mAttachedThread;
160 AMutex mSignalsLock;
161 ASet<AAbstractSignal*> mSignals;
162 bool mSignalsEnabled = true;
163
164 /*
165 * @brief Allows cross-thread signal call through event loop.
166 * @details
167 * If the object is sensitive to calls from other threads (i.e. view), it may set this flag to true in order to
168 * force signals to pass through the object's native thread instead of calls from the other threads.
169 */
170 bool mSlotsCallsOnlyOnMyThread = false;
171
172 static bool& isDisconnected();
173};
174
196#define emit (*this) ^
197
224#define AUI_EMIT_FOREIGN(object, signal, ...) (*object) ^ object->signal(__VA_ARGS__)
225
226#include "SharedPtr.h"
Base class for signal.
Definition: AAbstractSignal.h:368
Represents an abstract thread. Not all threads are created through AThread - these are interfaced wit...
Definition: AThread.h:33
A base object class.
Definition: AObject.h:49
void setThread(_< AAbstractThread > thread)
Set thread of the object.
Definition: AObject.h:156
A std::set with AUI extensions.
Definition: ASet.h:25
Represents a Unicode character string.
Definition: AString.h:37
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Basic syscall-based synchronization primitive.
Definition: AMutex.h:33
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:51
Does not allow escaping, allowing to accept lvalue ref, rvalue ref, shared_ptr and etc without overhe...
Definition: values.h:127
Forbids copy of your class.
Definition: values.h:40