• 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_API_METADATA_UTIL_H
16 #define META_API_METADATA_UTIL_H
17 
18 #include <meta/interface/intf_metadata.h>
19 #include <meta/interface/intf_object_flags.h>
20 #include <meta/interface/property/property.h>
21 
META_BEGIN_NAMESPACE()22 META_BEGIN_NAMESPACE()
23 
24 inline bool IsValueSet(const IProperty& p)
25 {
26     if (p.IsDefaultValue()) {
27         return false;
28     }
29     if (auto stack = interface_cast<IStackProperty>(&p)) {
30         // only way to compare anys for now
31         if (auto copy = p.GetValue().Clone()) {
32             return copy->CopyFrom(stack->GetDefaultValue()) != AnyReturn::NOTHING_TO_DO;
33         }
34     }
35     return true;
36 }
37 
IsValueSame(const IProperty & p1,const IProperty & p2)38 inline bool IsValueSame(const IProperty& p1, const IProperty& p2)
39 {
40     // only way to compare anys for now
41     if (auto copy = p1.GetValue().Clone()) {
42         return copy->CopyFrom(p2.GetValue()) == AnyReturn::NOTHING_TO_DO;
43     }
44     return false;
45 }
46 
47 inline void CloneToDefaultIfSet(const IProperty::ConstPtr& p, IMetadata& out, BASE_NS::string_view name = {})
48 {
49     if (PropertyLock lock { p }) {
50         if (IsValueSet(*p)) {
51             if (auto copy = DuplicatePropertyType(GetObjectRegistry(), p, name)) {
52                 if (auto sc = interface_cast<IStackProperty>(copy)) {
53                     sc->SetDefaultValue(p->GetValue());
54                 }
55                 out.AddProperty(copy);
56             }
57         }
58     }
59 }
60 
61 inline void CloneToDefault(const IProperty::ConstPtr& p, IMetadata& out, BASE_NS::string_view name = {})
62 {
63     if (PropertyLock lock { p }) {
64         if (auto copy = DuplicatePropertyType(GetObjectRegistry(), p, name)) {
65             if (auto sc = interface_cast<IStackProperty>(copy)) {
66                 sc->SetDefaultValue(p->GetValue());
67             }
68             out.AddProperty(copy);
69         }
70     }
71 }
72 
73 inline void Clone(const IProperty::ConstPtr& p, IMetadata& out, BASE_NS::string_view name = {})
74 {
75     if (PropertyLock lock { p }) {
76         if (auto copy = DuplicatePropertyType(GetObjectRegistry(), p, name)) {
77             copy->SetValue(p->GetValue());
78             out.AddProperty(copy);
79         }
80     }
81 }
82 
CloneToDefaultIfSet(const IMetadata & in,IMetadata & out)83 inline void CloneToDefaultIfSet(const IMetadata& in, IMetadata& out)
84 {
85     for (auto&& p : in.GetProperties()) {
86         CloneToDefaultIfSet(p, out);
87     }
88 }
89 
CloneToDefault(const IMetadata & in,IMetadata & out)90 inline void CloneToDefault(const IMetadata& in, IMetadata& out)
91 {
92     for (auto&& p : in.GetProperties()) {
93         CloneToDefault(p, out);
94     }
95 }
96 
Clone(const IMetadata & in,IMetadata & out)97 inline void Clone(const IMetadata& in, IMetadata& out)
98 {
99     for (auto&& p : in.GetProperties()) {
100         Clone(p, out);
101     }
102 }
103 
CopyValue(const IProperty::ConstPtr & p,IProperty & out)104 inline void CopyValue(const IProperty::ConstPtr& p, IProperty& out)
105 {
106     if (PropertyLock plock { p }) {
107         if (PropertyLock lock { &out }) {
108             if (!out.SetValue(p->GetValue())) {
109                 CORE_LOG_W("Failed to set property value [%s]", out.GetName().c_str());
110             }
111         }
112     }
113 }
114 
115 inline void CopyValue(const IProperty::ConstPtr& p, IMetadata& out, BASE_NS::string_view name = {})
116 {
117     BASE_NS::string n(name.empty() ? p->GetName() : name);
118     if (auto target = out.GetProperty(n)) {
119         CopyValue(p, *target);
120     } else {
121         CORE_LOG_W("No such property [%s]", n.c_str());
122     }
123 }
124 
CopyToDefaultAndReset(const IProperty::ConstPtr & p,IProperty & out)125 inline void CopyToDefaultAndReset(const IProperty::ConstPtr& p, IProperty& out)
126 {
127     if (PropertyLock plock { p }) {
128         if (PropertyLock lock { &out }) {
129             if (auto sc = interface_cast<IStackProperty>(&out)) {
130                 sc->SetDefaultValue(p->GetValue());
131             }
132             out.ResetValue();
133         }
134     }
135 }
136 
137 inline void CopyToDefaultAndReset(const IProperty::ConstPtr& p, IMetadata& out, BASE_NS::string_view name = {})
138 {
139     if (PropertyLock plock { p }) {
140         BASE_NS::string n(name.empty() ? p->GetName() : name);
141         if (auto target = out.GetProperty(n)) {
142             CopyToDefaultAndReset(p, *target);
143         } else {
144             CORE_LOG_W("No such property [%s]", n.c_str());
145         }
146     }
147 }
148 
CopyValue(const IMetadata & in,IMetadata & out)149 inline void CopyValue(const IMetadata& in, IMetadata& out)
150 {
151     for (auto&& p : in.GetProperties()) {
152         CopyValue(p, out);
153     }
154 }
155 
CopyToDefaultAndReset(const IMetadata & in,IMetadata & out)156 inline void CopyToDefaultAndReset(const IMetadata& in, IMetadata& out)
157 {
158     for (auto&& p : in.GetProperties()) {
159         CopyToDefaultAndReset(p, out);
160     }
161 }
162 
163 inline bool CreatePropertiesFromStaticMeta(const ObjectId& id, IMetadata& out, bool includeReadOnly = true)
164 {
165     auto& r = META_NS::GetObjectRegistry();
166     auto fac = r.GetObjectFactory(id);
167     if (!fac) {
168         return false;
169     }
170     if (auto sm = fac->GetClassStaticMetadata()) {
171         for (auto&& v : GetAllStaticMetadata(*sm, MetadataType::PROPERTY)) {
172             if (!v.readOnly || includeReadOnly) {
173                 if (v.data && v.data->runtimeValue) {
174                     if (auto def = v.data->runtimeValue()) {
175                         if (auto p = ConstructPropertyAny(v.data->name, *def)) {
176                             out.AddProperty(p);
177                         }
178                     }
179                 }
180             }
181         }
182     }
183     return true;
184 }
185 
186 META_END_NAMESPACE()
187 
188 #endif
189