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_fwd.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 IntroduceInterfaces<MetaObject, IProxyObject> {
36 META_OBJECT(ProxyObject, META_NS::ClassId::ProxyObject, IntroduceInterfaces)
37 public:
38 ProxyObject() = default;
39 ~ProxyObject() override;
40 META_NO_COPY_MOVE(ProxyObject)
41
42 public: // ILifecycle
43 bool Build(const IMetadata::Ptr& data) override;
44
45 META_BEGIN_STATIC_DATA()
46 META_STATIC_PROPERTY_DATA(IProxyObject, ProxyModeBitsValue, Mode, ProxyMode::REFLECT_PROXY_HIERARCHY)
47 META_STATIC_PROPERTY_DATA(IProxyObject, bool, Dynamic, true)
48 META_END_STATIC_DATA()
49 META_IMPLEMENT_PROPERTY(ProxyModeBitsValue, Mode)
50 META_IMPLEMENT_PROPERTY(bool, Dynamic)
51
52 public: // IMetadata
53 using IMetadata::GetProperty;
54 IProperty::Ptr GetProperty(BASE_NS::string_view name, MetadataQuery) override;
55 IProperty::ConstPtr GetProperty(BASE_NS::string_view name, MetadataQuery) const override;
56 bool RemoveProperty(const IProperty::Ptr&) override;
57
58 BASE_NS::vector<IProperty::Ptr> GetProperties() override;
59 BASE_NS::vector<IProperty::ConstPtr> GetProperties() const override;
60
61 public: // IProxyObject
62 const IObject::Ptr GetTarget() const override;
63 bool SetTarget(const IObject::Ptr& target) override;
64 BASE_NS::vector<IProperty::ConstPtr> GetOverrides() const override;
65 IProperty::ConstPtr GetOverride(BASE_NS::string_view name) const override;
66 IProperty::Ptr SetPropertyTarget(const IProperty::Ptr& property) override;
67 IProperty::ConstPtr GetProxyProperty(BASE_NS::string_view name) const override;
68 void AddInternalProxy(BASE_NS::string_view propertyPropertyName, BASE_NS::string_view sourcePropertyName) override;
69
70 private:
71 void ListenTargetChanges();
72 void ResetTargetListener();
73 void RefreshProperties();
74 IProperty::Ptr AddProxyProperty(BASE_NS::string_view name, const IProperty::ConstPtr& tp);
75 IProperty::Ptr AddProxyProperty(BASE_NS::string_view name);
76 void RemoveProxyProperty(const BASE_NS::string& name);
77 void RemoveAllProxyProperties();
78 void PopulateAllProperties();
79 bool ShouldSerialise(const IProperty::Ptr& p) const;
80 void UpdateSerializeState();
81 void ReflectHierarchy(const IObject::Ptr& target);
82 void ReflectTargetForProperty(const IMetadata::Ptr& m, BASE_NS::string_view name, const IProxyObject::Ptr& proxy);
83
84 void OnPropertyAdded(const ChildChangedInfo& info);
85 void OnPropertyRemoved(const ChildChangedInfo& info);
86 void OnPropertyChanged(const IProperty::Ptr& p);
87
88 private:
89 IObject::WeakPtr target_;
90 mutable BASE_NS::unordered_map<BASE_NS::string, DefaultValueBind> proxyProperties_;
91 mutable BASE_NS::unordered_map<BASE_NS::string, BASE_NS::string> internalBindings_;
92 EventHandler targetAddedListener_;
93 EventHandler targetRemovedListener_;
94 EventHandler metaAdded_;
95 EventHandler metaRemoved_;
96 bool updating_ {};
97 META_NS::IMetadata* meta_ {};
98 };
99
100 } // namespace Internal
101
102 META_END_NAMESPACE()
103
104 #endif
105