18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci#ifndef META_SRC_CONTAINER_OBSERVER_H
168bf80f4bSopenharmony_ci#define META_SRC_CONTAINER_OBSERVER_H
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <base/containers/unordered_map.h>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ci#include <meta/ext/event_impl.h>
218bf80f4bSopenharmony_ci#include <meta/interface/builtin_objects.h>
228bf80f4bSopenharmony_ci#include <meta/interface/intf_container_observer.h>
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_ci#include "object.h"
258bf80f4bSopenharmony_ci
268bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
278bf80f4bSopenharmony_ci
288bf80f4bSopenharmony_ciclass ContainerChangeListener final {
298bf80f4bSopenharmony_cipublic:
308bf80f4bSopenharmony_ci    META_NO_COPY(ContainerChangeListener)
318bf80f4bSopenharmony_ci    ~ContainerChangeListener();
328bf80f4bSopenharmony_ci    ContainerChangeListener() = delete;
338bf80f4bSopenharmony_ci    ContainerChangeListener(ContainerChangeListener&& other);
348bf80f4bSopenharmony_ci    ContainerChangeListener& operator=(ContainerChangeListener&& other);
358bf80f4bSopenharmony_ci
368bf80f4bSopenharmony_ci    explicit ContainerChangeListener(const IContainer::Ptr& container);
378bf80f4bSopenharmony_ci    // Note the copy assignment implementation which is not directly applicable to all use cases
388bf80f4bSopenharmony_ci    bool operator==(const ContainerChangeListener& other) const noexcept;
398bf80f4bSopenharmony_ci    bool Subscribe(const IOnChildChanged::InterfaceTypePtr& onAdded, const IOnChildChanged::InterfaceTypePtr& onRemoved,
408bf80f4bSopenharmony_ci        const IOnChildMoved::InterfaceTypePtr& onMoved);
418bf80f4bSopenharmony_ci    void Unsubscribe();
428bf80f4bSopenharmony_ci
438bf80f4bSopenharmony_ciprivate:
448bf80f4bSopenharmony_ci    mutable BASE_NS::pair<IEvent::Token, IOnChildChanged::InterfaceTypePtr> added_;
458bf80f4bSopenharmony_ci    mutable BASE_NS::pair<IEvent::Token, IOnChildChanged::InterfaceTypePtr> removed_;
468bf80f4bSopenharmony_ci    mutable BASE_NS::pair<IEvent::Token, IOnChildMoved::InterfaceTypePtr> moved_;
478bf80f4bSopenharmony_ci    mutable IContainer::WeakPtr container_;
488bf80f4bSopenharmony_ci};
498bf80f4bSopenharmony_ci
508bf80f4bSopenharmony_ciclass ContainerObserver
518bf80f4bSopenharmony_ci    : public Internal::MetaObjectFwd<ContainerObserver, ClassId::ContainerObserver, IContainerObserver> {
528bf80f4bSopenharmony_ci    using Super = Internal::MetaObjectFwd<ContainerObserver, ClassId::ContainerObserver, IContainerObserver>;
538bf80f4bSopenharmony_ci    using Super::Super;
548bf80f4bSopenharmony_ci
558bf80f4bSopenharmony_ciprotected: // LifeCycle
568bf80f4bSopenharmony_ci    bool Build(const IMetadata::Ptr&) override;
578bf80f4bSopenharmony_ci
588bf80f4bSopenharmony_ciprotected: // IContainerObjserver
598bf80f4bSopenharmony_ci    void SetContainer(const IContainer::Ptr& container) override;
608bf80f4bSopenharmony_ci
618bf80f4bSopenharmony_ci    META_IMPLEMENT_INTERFACE_EVENT(IContainerObserver, IOnChildChanged, OnDescendantAdded)
628bf80f4bSopenharmony_ci    META_IMPLEMENT_INTERFACE_EVENT(IContainerObserver, IOnChildChanged, OnDescendantRemoved)
638bf80f4bSopenharmony_ci    META_IMPLEMENT_INTERFACE_EVENT(IContainerObserver, IOnChildMoved, OnDescendantMoved)
648bf80f4bSopenharmony_ci
658bf80f4bSopenharmony_ciprivate:
668bf80f4bSopenharmony_ci    void Subscribe(const IContainer::Ptr& base);
678bf80f4bSopenharmony_ci    void Unsubscribe(const IContainer::Ptr& base);
688bf80f4bSopenharmony_ci    void InvokeAdded(const ChildChangedInfo& info);
698bf80f4bSopenharmony_ci    void InvokeRemoved(const ChildChangedInfo& info);
708bf80f4bSopenharmony_ci    void InvokeMoved(const ChildMovedInfo& info);
718bf80f4bSopenharmony_ci    IContainer::Ptr container_;
728bf80f4bSopenharmony_ci    IOnChildChanged::InterfaceTypePtr addedCallable_;
738bf80f4bSopenharmony_ci    IOnChildChanged::InterfaceTypePtr removedCallable_;
748bf80f4bSopenharmony_ci    IOnChildMoved::InterfaceTypePtr movedCallable_;
758bf80f4bSopenharmony_ci
768bf80f4bSopenharmony_ci    struct Subscription {
778bf80f4bSopenharmony_ci        Subscription(IContainer::WeakPtr container, ContainerChangeListener&& listener)
788bf80f4bSopenharmony_ci            : container(BASE_NS::move(container)), listener(BASE_NS::move(listener))
798bf80f4bSopenharmony_ci        {}
808bf80f4bSopenharmony_ci        IContainer::WeakPtr container;
818bf80f4bSopenharmony_ci        ContainerChangeListener listener;
828bf80f4bSopenharmony_ci    };
838bf80f4bSopenharmony_ci
848bf80f4bSopenharmony_ci    BASE_NS::vector<Subscription> subscriptions_;
858bf80f4bSopenharmony_ci};
868bf80f4bSopenharmony_ci
878bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
888bf80f4bSopenharmony_ci
898bf80f4bSopenharmony_ci#endif // META_SRC_CONTAINER_OBSERVER_H
90