AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AObject.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 "AObjectBase.h"
   15
   16namespace aui::detail {
   17  template<typename Object, typename Lambda>
   18  Lambda&& makeLambda(Object*, Lambda&& lambda) requires requires { std::is_class_v<Lambda>; } {
   19    return std::forward<Lambda>(lambda);
   20  }
   21
   22  template<typename Object1, typename Object2, typename Returns, typename... Args>
   23  auto makeLambda(Object1* object, Returns(Object2::*method)(Args...)) {
   24    return [object, method](Args... args) {
   25      (object->*method)(std::forward<Args>(args)...);
   26    };
   27  }
   28}
   29
   39class API_AUI_CORE AObject: public AObjectBase, public std::enable_shared_from_this<AObject>, public aui::noncopyable {
   40    friend class AAbstractSignal;
   41    template <typename ... Args>
   42    friend class ASignal;
   43
   44public:
   61    static constexpr AObjectBase* GENERIC_OBSERVER = nullptr;
   62
   63    AObject();
   64    virtual ~AObject() = default;
   65
   66    static void disconnect();
   67
   81    template <AAnySignal Signal, aui::derived_from<AObjectBase> Object, ACompatibleSlotFor<Signal> Function>
   82    static decltype(auto) connect(const Signal& signal, Object* object, Function&& function) {
   83        return const_cast<Signal&>(signal).connect(object, aui::detail::makeLambda(object, std::forward<Function>(function)));
   84    }
   85
  101    template <AAnyProperty Property, aui::derived_from<AObjectBase> Object, typename Function>
  102    static decltype(auto) connect(const Property& property, Object* object, Function&& function) {
  103        auto lambda = aui::detail::makeLambda(object, std::forward<Function>(function));
  104        property.changed.makeRawInvocable(lambda)(*property);
  105        return connect(property.changed, object, std::move(lambda));
  106    }
  107
  122    template <APropertyReadable PropertySource, APropertyWritable PropertyDestination>
  123    static void connect(PropertySource&& propertySource, PropertyDestination&& propertyDestination) requires requires {
  124            // source and destination properties must have compatible underlying types
  125            { *propertySource } -> aui::convertible_to<std::decay_t<decltype(*propertyDestination)>>;
  126        } {
  127        AObject::connect(propertySource,
  128                         propertyDestination.assignment());
  129    }
  130
  151    template <APropertyWritable PropertySource, APropertyWritable PropertyDestination>
  152    static void biConnect(PropertySource&& propertySource, PropertyDestination&& propertyDestination) requires requires {
  153            // source and destination properties must have compatible underlying types
  154            { *propertySource } -> aui::convertible_to<std::decay_t<decltype(*propertyDestination)>>;
  155            { *propertyDestination } -> aui::convertible_to<std::decay_t<decltype(*propertySource)>>;
  156        } {
  157        AObject::connect(propertySource,
  158                         propertyDestination.assignment());
  159        AObject::connect(propertyDestination.changed,
  160                         propertySource.assignment());
  161    }
  162
  176    template <AAnySignalOrProperty Connectable, aui::derived_from<AObjectBase> Object,
  177        ACompatibleSlotFor<Connectable> Function>
  178    static decltype(auto)
  179    connect(const Connectable& connectable, Object& object, Function&& function) {
  180        return connect(connectable, &object, std::forward<Function>(function));
  181    }
  182
  196    template <typename Connectable, ACompatibleSlotFor<Connectable> Function>
  197    decltype(auto) connect(const Connectable& connectable, Function&& function) {
  198        return connect(connectable, this, std::forward<Function>(function));
  199    }
  200
  219    template <AAnySignalOrProperty Connectable, aui::derived_from<AObjectBase> Object, ACompatibleSlotFor<Connectable> Function>
  220    static decltype(auto)
  221    connect(const Connectable& connectable, _<Object> object, Function&& function) {
  222        return connect(connectable, object.get(), std::forward<Function>(function));
  223    }
  224
  244    template <AAnySignalOrProperty Connectable, aui::derived_from<AObjectBase> Object, typename Function>
  245    static decltype(auto)
  246    connect(const Connectable& connectable, ASlotDef<Object*, Function> slotDef) {
  247        return connect(connectable, slotDef.boundObject, std::move(slotDef.invocable));
  248    }
  249
  267    template <AAnyProperty Property, typename Object, ACompatibleSlotFor<Property> Function>
  268    static void
  269    connect(const Property& property, _<Object> object, Function&& function)
  270        requires (!aui::derived_from<Object, AObject>)
  271    {
  272        property.changed.makeRawInvocable(function)(*property);
  273        connect(property.changed, object, std::forward<Function>(function));
  274        const_cast<std::decay_t<decltype(property.changed)>&>(property.changed).connectNonAObject(std::move(object), aui::detail::makeLambda(object,std::forward<Function>(function)));
  275    }
  276
  277    void setSignalsEnabled(bool enabled) { mSignalsEnabled = enabled; }
  278
  279    [[nodiscard]] bool isSignalsEnabled() const noexcept { return mSignalsEnabled; }
  280
  281    template <ASignalInvokable T>
  282    void operator^(T&& t) noexcept {
  283        if (mSignalsEnabled) {
  284            t.invokeSignal(this);
  285        }
  286    }
  287
  288    [[nodiscard]]
  289    const _<AAbstractThread>& getThread() const noexcept { return mAttachedThread; }
  290
  291    bool isSlotsCallsOnlyOnMyThread() const noexcept { return mSlotsCallsOnlyOnMyThread; }
  292
  293    static void moveToThread(aui::no_escape<AObject> object, _<AAbstractThread> thread);
  294
  295    void setSlotsCallsOnlyOnMyThread(bool slotsCallsOnlyOnMyThread) {
  296        mSlotsCallsOnlyOnMyThread = slotsCallsOnlyOnMyThread;
  297    }
  298
  299protected:
  303    void setThread(_<AAbstractThread> thread) { mAttachedThread = std::move(thread); }
  304
  305private:
  306    _<AAbstractThread> mAttachedThread;
  307    bool mSignalsEnabled = true;
  308
  315    bool mSlotsCallsOnlyOnMyThread = false;
  316
  317    static bool& isDisconnected();
  318};
  319
  340#define emit (*this) ^
  341
  367#define AUI_EMIT_FOREIGN(object, signal, ...) (*object) ^ object->signal(__VA_ARGS__)
  368
  369#include "SharedPtr.h"
static decltype(auto) connect(const Connectable &connectable, ASlotDef< Object *, Function > slotDef)
Connects signal to the slot of the specified object. Slot is packed to single argument.
Definition AObject.h:246
void setThread(_< AAbstractThread > thread)
Set thread of the object.
Definition AObject.h:303
static constexpr AObjectBase * GENERIC_OBSERVER
Indicates that a connection should not be explicitly linked to receiver's lifetime.
Definition AObject.h:61
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:215
Definition concepts.h:42
Definition concepts.h:25
type_of< T > t
Selects views that are of the specified C++ types.
Definition type_of.h:71
API_AUI_CORE const ACommandLineArgs & args() noexcept
static decltype(auto) connect(const Property &property, Object *object, Function &&function)
Connects property to the slot of the specified object.
Definition AObject.h:102
static void biConnect(PropertySource &&propertySource, PropertyDestination &&propertyDestination)
Connects source property to the destination property and opposite (bidirectionally).
Definition AObject.h:152
static void connect(const Property &property, _< Object > object, Function &&function)
Connects signal or property to the slot of the specified non-AObject type.
Definition AObject.h:269
static void connect(PropertySource &&propertySource, PropertyDestination &&propertyDestination)
Connects source property to the destination property.
Definition AObject.h:123
static decltype(auto) connect(const Connectable &connectable, Object &object, Function &&function)
Connects signal or property to the slot of the specified object.
Definition AObject.h:179
static decltype(auto) connect(const Signal &signal, Object *object, Function &&function)
Connects signal to the slot of the specified object.
Definition AObject.h:82
static decltype(auto) connect(const Connectable &connectable, _< Object > object, Function &&function)
Connects signal or property to the slot of the specified object.
Definition AObject.h:221
decltype(auto) connect(const Connectable &connectable, Function &&function)
Connects signal or property to slot of "this" object.
Definition AObject.h:197
Definition concepts.h:188
Forbids copy of your class.
Definition values.h:45