• 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 
16 #ifndef META_API_CONTAINER_OBSERVER_H
17 #define META_API_CONTAINER_OBSERVER_H
18 
19 #include <meta/api/make_callback.h>
20 #include <meta/api/object.h>
21 #include <meta/interface/intf_container_observer.h>
22 #include <meta/interface/intf_object_hierarchy_observer.h>
23 
META_BEGIN_NAMESPACE()24 META_BEGIN_NAMESPACE()
25 
26 /**
27  * @brief Wrapper class for objects which implement IObjectHierarchyObserver.
28  */
29 class ObjectHierarchyObserver : public InterfaceObject<IObjectHierarchyObserver> {
30 public:
31     META_INTERFACE_OBJECT(ObjectHierarchyObserver, InterfaceObject<IObjectHierarchyObserver>, IObjectHierarchyObserver)
32     META_INTERFACE_OBJECT_INSTANTIATE(ObjectHierarchyObserver, ClassId::ObjectHierarchyObserver)
33     void SetTarget(const IObject::Ptr& root, HierarchyChangeModeValue mode)
34     {
35         META_INTERFACE_OBJECT_CALL_PTR(SetTarget(root, mode));
36     }
37     void SetTarget(const IObject::Ptr& root)
38     {
39         META_INTERFACE_OBJECT_CALL_PTR(SetTarget(root));
40     }
41     auto& Target(const IObject::Ptr& root)
42     {
43         META_INTERFACE_OBJECT_CALL_PTR(SetTarget(root));
44         return *this;
45     }
46     auto GetTarget() const
47     {
48         return META_INTERFACE_OBJECT_CALL_PTR(GetTarget());
49     }
50     template<typename Type = IObject>
51     auto GetAllObserved() const
52     {
53         return META_INTERFACE_OBJECT_CALL_PTR(template GetAllObserved<Type>());
54     }
55     auto OnHierarchyChanged() const
56     {
57         return META_INTERFACE_OBJECT_CALL_PTR(OnHierarchyChanged());
58     }
59     /**
60      * @brief Subscribe to IObjectHierarchyObserver::OnHierarchyChanged
61      * @param callback Handler function for the event.
62      * @return Event token for the handler which can be used to unsubscribe the handler. ContainerObserver does not
63      * automatically unsubscribe the event at destruction.
64      */
65     template<typename Callback>
66     auto OnHierarchyChanged(Callback&& callback) const
67     {
68         auto p = OnHierarchyChanged();
69         return p ? p->AddHandler(MakeCallback<IOnHierarchyChanged>(BASE_NS::forward<Callback>(callback)))
70                  : IEvent::Token {};
71     }
72 };
73 
74 /**
75  * @brief Wrapper class for objects which implement IContainerObserver.
76  */
77 class ContainerObserver : public InterfaceObject<IContainerObserver> {
78 public:
META_INTERFACE_OBJECT(ContainerObserver,InterfaceObject<IContainerObserver>,IContainerObserver)79     META_INTERFACE_OBJECT(ContainerObserver, InterfaceObject<IContainerObserver>, IContainerObserver)
80     META_INTERFACE_OBJECT_INSTANTIATE(ContainerObserver, ClassId::ContainerObserver)
81     void SetContainer(const IContainer::Ptr& container)
82     {
83         META_INTERFACE_OBJECT_CALL_PTR(SetContainer(container));
84     }
Container(const IContainer::Ptr & container)85     auto& Container(const IContainer::Ptr& container)
86     {
87         META_INTERFACE_OBJECT_CALL_PTR(SetContainer(container));
88         return *this;
89     }
OnDescendantChanged()90     auto OnDescendantChanged() const
91     {
92         return META_INTERFACE_OBJECT_CALL_PTR(OnDescendantChanged());
93     }
94     /**
95      * @brief Subscribe to IContainerObserver::OnDescendantChanged
96      * @param callback Handler function for the event.
97      * @return Event token for the handler which can be used to unsubscribe the handler. ContainerObserver does not
98      * automatically unsubscribe the event at destruction.
99      */
100     template<typename Callback>
OnDescendantChanged(Callback && callback)101     auto OnDescendantChanged(Callback&& callback) const
102     {
103         auto p = OnDescendantChanged();
104         return p ? p->AddHandler(MakeCallback<IOnChildChanged>(BASE_NS::forward<Callback>(callback)))
105                  : META_NS::IEvent::Token {};
106     }
107 };
108 
109 /// Returns a default object which implements IObjectHierarchyObserver
110 template<>
111 inline auto CreateObjectInstance<IObjectHierarchyObserver>()
112 {
113     return ObjectHierarchyObserver(CreateNew);
114 }
115 /// Returns a default object which implements IObjectHierarchyObserver
116 template<>
117 inline auto CreateObjectInstance<IContainerObserver>()
118 {
119     return ContainerObserver(CreateNew);
120 }
121 
122 META_END_NAMESPACE()
123 
124 #endif // META_API_CONTAINER_OBSERVER_H
125