AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AAbstractSignal.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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 <exception>
   15
   16#include <AUI/Core.h>
   17#include <AUI/Common/SharedPtrTypes.h>
   18#include <AUI/Traits/values.h>
   19
   20class AObject;
   21
  358
  366class API_AUI_CORE AAbstractSignal {
  367    friend class AObject;
  368
  369public:
  370    virtual ~AAbstractSignal() = default;
  371
  375    struct Connection {
  376        friend class API_AUI_CORE AObjectBase;
  380        virtual void disconnect() = 0;
  381
  382    private:
  390        virtual void onBeforeReceiverSideDestroyed() = 0;
  391    };
  392
  396    struct AutoDestroyedConnection {
  397        _<Connection> value = nullptr;
  398
  399        AutoDestroyedConnection() = default;
  400        AutoDestroyedConnection(_<Connection> connection) noexcept : value(std::move(connection)) {}
  401        AutoDestroyedConnection(const AutoDestroyedConnection&) = default;
  402        AutoDestroyedConnection(AutoDestroyedConnection&&) noexcept = default;
  403
  404        AutoDestroyedConnection& operator=(_<Connection> rhs) noexcept {
  405            if (value == rhs) {
  406                return *this;
  407            }
  408            release();
  409            value = std::move(rhs);
  410            return *this;
  411        }
  412
  413        AutoDestroyedConnection& operator=(const AutoDestroyedConnection& rhs) {
  414            return operator=(rhs.value);
  415        }
  416
  417        AutoDestroyedConnection& operator=(AutoDestroyedConnection&& rhs) noexcept {
  418            return operator=(std::move(rhs.value));
  419        }
  420
  421        ~AutoDestroyedConnection() {
  422            release();
  423        }
  424
  425    private:
  426        void release() noexcept {
  427            if (!value) {
  428                return;
  429            }
  430            value->disconnect();
  431        }
  432    };
  433
  437    virtual void clearAllOutgoingConnections() const noexcept = 0;
  438
  443    virtual void clearAllOutgoingConnectionsWith(aui::no_escape<AObjectBase> receiver) const noexcept = 0;
  444
  449    virtual bool hasOutgoingConnectionsWith(aui::no_escape<AObjectBase> receiver) const noexcept = 0;
  450
  456    virtual _<Connection> addGenericObserver(AObjectBase* receiver, std::function<void()> observer) = 0;
  457
  458protected:
  459    /* some handy functions accessed from public headers */
  460
  461    static _weak<AObject> weakPtrFromObject(AObject* object);
  462
  466    static void addIngoingConnectionIn(aui::no_escape<AObjectBase> object, _<Connection> connection);
  467
  471    static void removeIngoingConnectionIn(aui::no_escape<AObjectBase> object, Connection& connection, std::unique_lock<ASpinlockMutex>& lock);
  472};
Base class for signal.
Definition AAbstractSignal.h:366
virtual _< Connection > addGenericObserver(AObjectBase *receiver, std::function< void()> observer)=0
Creates generic connection (without arguments).
static void addIngoingConnectionIn(aui::no_escape< AObjectBase > object, _< Connection > connection)
Adds a connection to the specified object.
static void removeIngoingConnectionIn(aui::no_escape< AObjectBase > object, Connection &connection, std::unique_lock< ASpinlockMutex > &lock)
Removes a connection from the specified object.
virtual void clearAllOutgoingConnections() const noexcept=0
Destroys all connections of this signal, if any.
virtual bool hasOutgoingConnectionsWith(aui::no_escape< AObjectBase > receiver) const noexcept=0
virtual void clearAllOutgoingConnectionsWith(aui::no_escape< AObjectBase > receiver) const noexcept=0
Destroys all connections with passed receiver, if any.
Definition AObjectBase.h:24
A base object class.
Definition AObject.h:39
Synchronization primitive that is implemented with atomic values instead of doing syscalls.
Definition AMutex.h:65
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
std::add_lvalue_reference_t< T > value() const noexcept
Dereferences the stored pointer.
Definition SharedPtrTypes.h:294
Connection handle.
Definition AAbstractSignal.h:375
virtual void disconnect()=0
Breaks connection.
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:52