• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace Internal {
30 
31 class ContainerChangeListener final {
32 public:
33     META_NO_COPY(ContainerChangeListener)
34     ~ContainerChangeListener();
35     ContainerChangeListener() = delete;
36     ContainerChangeListener(ContainerChangeListener&& other);
37     ContainerChangeListener& operator=(ContainerChangeListener&& other);
38 
39     explicit ContainerChangeListener(const IContainer::Ptr& container);
40     // Note the copy assignment implementation which is not directly applicable to all use cases
41     bool operator==(const ContainerChangeListener& other) const noexcept;
42     bool Subscribe(const IOnChildChanged::InterfaceTypePtr& onChanged);
43     void Unsubscribe();
44 
45 private:
46     mutable BASE_NS::pair<IEvent::Token, IOnChildChanged::InterfaceTypePtr> changed_;
47     mutable IContainer::WeakPtr container_;
48 };
49 
50 class ContainerObserver : public IntroduceInterfaces<MetaObject, IContainerObserver> {
51     META_OBJECT(ContainerObserver, ClassId::ContainerObserver, IntroduceInterfaces)
52 protected: // LifeCycle
53     bool Build(const IMetadata::Ptr&) override;
54 
55 public: // IContainerObjserver
56     void SetContainer(const IContainer::Ptr& container) override;
57 
58     META_BEGIN_STATIC_DATA()
59     META_STATIC_EVENT_DATA(IContainerObserver, IOnChildChanged, OnDescendantChanged)
60     META_END_STATIC_DATA()
61     META_IMPLEMENT_EVENT(IOnChildChanged, OnDescendantChanged)
62 
63 private:
64     void Subscribe(const IContainer::Ptr& base);
65     void Unsubscribe(const IContainer::Ptr& base);
66     void InvokeChanged(const ChildChangedInfo& info);
67 
68     IContainer::Ptr container_;
69     IOnChildChanged::InterfaceTypePtr changedCallable_;
70 
71     struct Subscription {
72         Subscription(IContainer::WeakPtr container, ContainerChangeListener&& listener)
73             : container(BASE_NS::move(container)), listener(BASE_NS::move(listener))
74         {}
75         IContainer::WeakPtr container;
76         ContainerChangeListener listener;
77     };
78 
79     BASE_NS::vector<Subscription> subscriptions_;
80 };
81 
82 } // namespace Internal
83 
84 META_END_NAMESPACE()
85 
86 #endif // META_SRC_CONTAINER_OBSERVER_H
87