AUI Framework  master
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AObjectBase.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 <AUI/Thread/AMutex.h>
   15#include <AUI/Traits/concepts.h>
   16#include <AUI/Traits/members.h>
   17
   18#include "AUI/Common/AVector.h"
   19#include "AUI/Core.h"
   20#include "AUI/Traits/values.h"
   21#include "SharedPtrTypes.h"
   22#include <AUI/Common/AAbstractSignal.h>
   23
   24class API_AUI_CORE AObjectBase {
   25    /* ASignal <-> AObject implementation stuff */
   26    friend class AAbstractSignal;
   27
   28    /* tests */
   29    friend class SignalSlotTest;
   30    friend class PropertyTest;
   31    friend class PropertyPrecomputedTest;
   32
   33public:
   34    virtual ~AObjectBase() = default;
   35    AObjectBase() = default;
   36
   37    static ASpinlockMutex SIGNAL_SLOT_GLOBAL_SYNC;
   38
   39    AObjectBase(AObjectBase&& rhs) noexcept {
   40        operator=(std::move(rhs));
   41    }
   42
   43    AObjectBase(const AObjectBase& rhs) noexcept {
   44        // mIngoingConnections are not borrowed on copy operation.
   45    }
   46
   47    AObjectBase& operator=(const AObjectBase& rhs) noexcept {
   48        // mIngoingConnections are not borrowed on copy operation.
   49        return *this;
   50    }
   51
   52    AObjectBase& operator=(AObjectBase&& rhs) noexcept {
   53        if (this == &rhs) {
   54            return *this;
   55        }
   56        AUI_ASSERTX(rhs.mIngoingConnections.empty(), "AObjectBase move is valid only if no signals connected to it");
   57        return *this;
   58    }
   59
   60protected:
   61    void clearAllIngoingConnections() noexcept;
   62
   66    virtual void handleSlotException(std::exception_ptr exception);
   67
   68private:
   72    struct ReceiverConnectionOwner {
   73        _<AAbstractSignal::Connection> value = nullptr;
   74
   75        ReceiverConnectionOwner() = default;
   76        explicit ReceiverConnectionOwner(_<AAbstractSignal::Connection> connection) noexcept
   77          : value(std::move(connection)) {}
   78        ReceiverConnectionOwner(const ReceiverConnectionOwner&) = default;
   79        ReceiverConnectionOwner(ReceiverConnectionOwner&&) noexcept = default;
   80        ReceiverConnectionOwner& operator=(const ReceiverConnectionOwner& rhs) {
   81            if (this == &rhs) {
   82                return *this;
   83            }
   84            release();
   85            value = rhs.value;
   86            return *this;
   87        }
   88
   89        ReceiverConnectionOwner& operator=(ReceiverConnectionOwner&& rhs) noexcept {
   90            if (this == &rhs) {
   91                return *this;
   92            }
   93            release();
   94            value = std::move(rhs.value);
   95            return *this;
   96        }
   97
   98        ~ReceiverConnectionOwner() {
   99            release();
  100        }
  101
  102    private:
  103        void release() noexcept {
  104            if (!value) {
  105                return;
  106            }
  107            value->onBeforeReceiverSideDestroyed();
  108        }
  109    };
  110
  111    AVector<ReceiverConnectionOwner> mIngoingConnections;
  112};
virtual void handleSlotException(std::exception_ptr exception)
Called then an exception has thrown during slot processing of the signal emitted by this object.
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
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74