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_SRC_PROXY_OBJECT_H
17 #define META_SRC_PROXY_OBJECT_H
18 
19 #include <base/containers/unordered_map.h>
20 
21 #include <meta/api/event_handler.h>
22 #include <meta/api/property/default_value_bind.h>
23 #include <meta/base/interface_macros.h>
24 #include <meta/base/namespace.h>
25 #include <meta/ext/object.h>
26 #include <meta/interface/builtin_objects.h>
27 #include <meta/interface/intf_proxy_object.h>
28 
29 #include "object.h"
30 
META_BEGIN_NAMESPACE()31 META_BEGIN_NAMESPACE()
32 
33 namespace Internal {
34 
35 class ProxyObject final : public Internal::ObjectFwd<ProxyObject, META_NS::ClassId::ProxyObject, IProxyObject> {
36     using Super = Internal::ObjectFwd<ProxyObject, META_NS::ClassId::ProxyObject, IProxyObject>;
37 
38 public:
39     ProxyObject() = default;
40     ~ProxyObject() override;
41     META_NO_COPY_MOVE(ProxyObject)
42 
43 public: // ILifecycle
44     bool Build(const IMetadata::Ptr& data) override;
45 
46 public: // IMetadata
47     IProperty::Ptr GetPropertyByName(BASE_NS::string_view name) override;
48     IProperty::ConstPtr GetPropertyByName(BASE_NS::string_view name) const override;
49     void RemoveProperty(const IProperty::Ptr&) override;
50 
51     BASE_NS::vector<IProperty::Ptr> GetAllProperties() override;
52     BASE_NS::vector<IProperty::ConstPtr> GetAllProperties() const override;
53 
54 public: // IProxyObject
55     META_IMPLEMENT_INTERFACE_PROPERTY(IProxyObject, ProxyModeBitsValue, Mode, ProxyMode::REFLECT_PROXY_HIERARCHY)
56     META_IMPLEMENT_INTERFACE_PROPERTY(IProxyObject, bool, Dynamic, true);
57     const IObject::Ptr GetTarget() const override;
58     bool SetTarget(const IObject::Ptr& target) override;
59     BASE_NS::vector<IProperty::ConstPtr> GetOverrides() const override;
60     IProperty::ConstPtr GetOverride(BASE_NS::string_view name) const override;
61     IProperty::Ptr SetPropertyTarget(const IProperty::Ptr& property) override;
62     IProperty::ConstPtr GetProxyProperty(BASE_NS::string_view name) const override;
63 
64 private:
65     void ListenTargetChanges();
66     void ResetTargetListener();
67     void RefreshProperties();
68     IProperty::Ptr AddProxyProperty(const IProperty::ConstPtr& tp);
69     IProperty::Ptr AddProxyProperty(BASE_NS::string_view name);
70     void PopulateAllProperties();
71     bool ShouldSerialise(const IProperty::Ptr& p) const;
72     void UpdateSerializeState();
73     void ReflectHierarchy(const IObject::Ptr& target);
74     void ReflectTargetForProperty(const IMetadata::Ptr& m, BASE_NS::string_view name, const IProxyObject::Ptr& proxy);
75 
76     void OnPropertyAdded(const ChildChangedInfo& info);
77     void OnPropertyRemoved(const ChildChangedInfo& info);
78     void OnPropertyChanged(const IProperty::Ptr& p);
79 
80 private:
81     IObject::WeakPtr target_;
82     mutable BASE_NS::unordered_map<BASE_NS::string, DefaultValueBind> proxyProperties_;
83     EventHandler targetAddedListener_;
84     EventHandler targetRemovedListener_;
85     EventHandler metaAdded_;
86     EventHandler metaRemoved_;
87     bool updating_ {};
88 };
89 
90 } // namespace Internal
91 
92 META_END_NAMESPACE()
93 
94 #endif
95