1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef META_SRC_CONTAINER_OBSERVER_H
16 #define META_SRC_CONTAINER_OBSERVER_H
17
18 #include <base/containers/unordered_map.h>
19
20 #include <meta/base/namespace.h>
21 #include <meta/ext/event_impl.h>
22 #include <meta/interface/builtin_objects.h>
23 #include <meta/interface/intf_container_observer.h>
24
25 #include "object.h"
26
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28
29 class ContainerChangeListener final {
30 public:
31 META_NO_COPY(ContainerChangeListener)
32 ~ContainerChangeListener();
33 ContainerChangeListener() = delete;
34 ContainerChangeListener(ContainerChangeListener&& other);
35 ContainerChangeListener& operator=(ContainerChangeListener&& other);
36
37 explicit ContainerChangeListener(const IContainer::Ptr& container);
38 // Note the copy assignment implementation which is not directly applicable to all use cases
39 bool operator==(const ContainerChangeListener& other) const noexcept;
40 bool Subscribe(const IOnChildChanged::InterfaceTypePtr& onChanged);
41 void Unsubscribe();
42
43 private:
44 mutable BASE_NS::pair<IEvent::Token, IOnChildChanged::InterfaceTypePtr> changed_;
45 mutable IContainer::WeakPtr container_;
46 };
47
48 class ContainerObserver : public IntroduceInterfaces<MetaObject, IContainerObserver> {
49 META_OBJECT(ContainerObserver, ClassId::ContainerObserver, IntroduceInterfaces)
50 protected: // LifeCycle
51 bool Build(const IMetadata::Ptr&) override;
52
53 public: // IContainerObjserver
54 void SetContainer(const IContainer::Ptr& container) override;
55
56 META_BEGIN_STATIC_DATA()
57 META_STATIC_EVENT_DATA(IContainerObserver, IOnChildChanged, OnDescendantChanged)
58 META_END_STATIC_DATA()
59 META_IMPLEMENT_EVENT(IOnChildChanged, OnDescendantChanged)
60
61 private:
62 void Subscribe(const IContainer::Ptr& base);
63 void Unsubscribe(const IContainer::Ptr& base);
64 void InvokeChanged(const ChildChangedInfo& info);
65
66 IContainer::Ptr container_;
67 IOnChildChanged::InterfaceTypePtr changedCallable_;
68
69 struct Subscription {
SubscriptionSubscription70 Subscription(IContainer::WeakPtr container, ContainerChangeListener&& listener)
71 : container(BASE_NS::move(container)), listener(BASE_NS::move(listener))
72 {}
73 IContainer::WeakPtr container;
74 ContainerChangeListener listener;
75 };
76
77 BASE_NS::vector<Subscription> subscriptions_;
78 };
79
80 META_END_NAMESPACE()
81
82 #endif // META_SRC_CONTAINER_OBSERVER_H
83