• 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_UTIL_H
17 #define META_API_UTIL_H
18 
19 #include <meta/api/locking.h>
20 #include <meta/interface/animation/intf_interpolator.h>
21 #include <meta/interface/intf_any.h>
22 #include <meta/interface/intf_object_registry.h>
23 #include <meta/interface/intf_value.h>
24 #include <meta/interface/property/intf_modifier.h>
25 #include <meta/interface/property/property.h>
26 
META_BEGIN_NAMESPACE()27 META_BEGIN_NAMESPACE()
28 
29 inline BASE_NS::shared_ptr<CORE_NS::IInterface> GetPointer(const IAny& any)
30 {
31     BASE_NS::shared_ptr<CORE_NS::IInterface> ret;
32     any.GetValue(ret);
33     return ret;
34 }
35 
GetPointer(const IAny::Ptr & any)36 inline BASE_NS::shared_ptr<CORE_NS::IInterface> GetPointer(const IAny::Ptr& any)
37 {
38     return any ? GetPointer(*any) : nullptr;
39 }
GetConstPointer(const IAny & any)40 inline BASE_NS::shared_ptr<const CORE_NS::IInterface> GetConstPointer(const IAny& any)
41 {
42     BASE_NS::shared_ptr<const CORE_NS::IInterface> ret;
43     any.GetValue(ret);
44     return ret;
45 }
46 
GetPointer(const IAny::ConstPtr & any)47 inline BASE_NS::shared_ptr<const CORE_NS::IInterface> GetPointer(const IAny::ConstPtr& any)
48 {
49     return any ? GetConstPointer(*any) : nullptr;
50 }
51 template<typename Interface>
GetPointer(const IAny & any)52 inline BASE_NS::shared_ptr<Interface> GetPointer(const IAny& any)
53 {
54     return interface_pointer_cast<Interface>(GetPointer(any));
55 }
56 
57 /// Returns IIinterface pointer if property contains a pointer that can be converted to it.
GetPointer(const IProperty::ConstPtr & p)58 inline BASE_NS::shared_ptr<CORE_NS::IInterface> GetPointer(const IProperty::ConstPtr& p)
59 {
60     InterfaceSharedLock lock { p };
61     return lock ? GetPointer(p->GetValue()) : nullptr;
62 }
63 
64 template<typename Interface>
GetPointer(const IProperty::ConstPtr & p)65 inline BASE_NS::shared_ptr<Interface> GetPointer(const IProperty::ConstPtr& p)
66 {
67     return interface_pointer_cast<Interface>(GetPointer(p));
68 }
69 
GetInternalAny(const IProperty::ConstPtr & p)70 inline IAny::ConstPtr GetInternalAny(const IProperty::ConstPtr& p)
71 {
72     auto i = interface_cast<IPropertyInternalAny>(p);
73     return i ? i->GetInternalAny() : nullptr;
74 }
GetInternalAny(const IProperty & p)75 inline IAny::ConstPtr GetInternalAny(const IProperty& p)
76 {
77     auto i = interface_cast<IPropertyInternalAny>(&p);
78     return i ? i->GetInternalAny() : nullptr;
79 }
80 inline IProperty::Ptr DuplicatePropertyType(IObjectRegistry& obr, IProperty::ConstPtr p, BASE_NS::string_view name = {})
81 {
82     IProperty::Ptr dup;
83     PropertyLock lock { p };
84     if (auto obj = interface_cast<IObject>(p)) {
85         dup = obr.GetPropertyRegister().Create(obj->GetClassId(), name.empty() ? p->GetName() : name);
86         if (auto dupi = interface_cast<IPropertyInternalAny>(dup)) {
87             if (auto any = GetInternalAny(p)) {
88                     dupi->SetInternalAny(any->Clone(false));
89             }
90         }
91     }
92     return dup;
93 }
94 
95 template<typename Type>
96 Type GetValue(const Property<Type>& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
97 {
98     return p ? p->GetValue() : BASE_NS::move(defaultValue);
99 }
100 template<typename Type>
101 Type GetValue(const IProperty::ConstPtr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
102 {
103     return GetValue(Property<Type>(p), BASE_NS::move(defaultValue));
104 }
105 template<typename Type>
106 Type GetValue(const IProperty::ConstWeakPtr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
107 {
108     return GetValue<Type>(p.lock(), BASE_NS::move(defaultValue));
109 }
110 // to disambiguate
111 template<typename Type>
112 Type GetValue(const IProperty::Ptr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
113 {
114     return GetValue(Property<Type>(p), BASE_NS::move(defaultValue));
115 }
116 // to disambiguate
117 template<typename Type>
118 Type GetValue(const IProperty::WeakPtr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
119 {
120     return GetValue<Type>(p.lock(), BASE_NS::move(defaultValue));
121 }
122 
123 template<typename Type>
SetValue(Property<Type> property,const NonDeduced_t<Type> & value)124 bool SetValue(Property<Type> property, const NonDeduced_t<Type>& value) noexcept
125 {
126     return property && property->SetValue(value);
127 }
128 template<typename Type>
SetValue(IProperty::Ptr p,const NonDeduced_t<Type> & value)129 bool SetValue(IProperty::Ptr p, const NonDeduced_t<Type>& value) noexcept
130 {
131     return SetValue(Property<Type>(p), value);
132 }
133 template<typename Type>
SetValue(IProperty::WeakPtr p,const NonDeduced_t<Type> & value)134 bool SetValue(IProperty::WeakPtr p, const NonDeduced_t<Type>& value) noexcept
135 {
136     return SetValue<Type>(p.lock(), value);
137 }
138 
Copy(const IProperty::ConstPtr & src,const IProperty::Ptr & dst)139 inline bool Copy(const IProperty::ConstPtr& src, const IProperty::Ptr& dst)
140 {
141     PropertyLock source(src);
142     PropertyLock dest(dst);
143     return dest->SetValueAny(source->GetValueAny());
144 }
145 
146 inline bool IsCompatible(
147     const IProperty& prop, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
148 {
149     bool res = false;
150     if (auto iany = GetInternalAny(prop)) {
151             res = IsCompatible(*iany, id, dir);
152     }
153     return res;
154 }
155 inline bool IsCompatible(
156     const IProperty::ConstPtr& prop, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
157 {
158     return prop && IsCompatible(*prop, id, dir);
159 }
160 template<typename T>
161 inline bool IsCompatibleWith(const IProperty::ConstPtr& prop, CompatibilityDirection dir = CompatibilityDirection::BOTH)
162 {
163     return IsCompatible(prop, UidFromType<BASE_NS::remove_const_t<BASE_NS::remove_reference_t<T>>>(), dir);
164 }
165 
IsSetCompatible(const IProperty::ConstPtr & prop,const TypeId & id)166 inline bool IsSetCompatible(const IProperty::ConstPtr& prop, const TypeId& id)
167 {
168     return IsCompatible(prop, id, CompatibilityDirection::SET);
169 }
170 
IsGetCompatible(const IProperty::ConstPtr & prop,const TypeId & id)171 inline bool IsGetCompatible(const IProperty::ConstPtr& prop, const TypeId& id)
172 {
173     return IsCompatible(prop, id, CompatibilityDirection::GET);
174 }
175 
176 template<typename T>
IsSetCompatibleWith(const IProperty::ConstPtr & prop)177 inline bool IsSetCompatibleWith(const IProperty::ConstPtr& prop)
178 {
179     return IsCompatibleWith<T>(prop, CompatibilityDirection::SET);
180 }
181 
182 template<typename T>
IsGetCompatibleWith(const IProperty::ConstPtr & prop)183 inline bool IsGetCompatibleWith(const IProperty::ConstPtr& prop)
184 {
185     return IsCompatibleWith<T>(prop, CompatibilityDirection::GET);
186 }
187 
Interpolate(IInterpolator::ConstPtr inter,const IProperty::Ptr & output,const IAny::ConstPtr & from,const IAny::ConstPtr & to,float t)188 inline AnyReturnValue Interpolate(IInterpolator::ConstPtr inter, const IProperty::Ptr& output,
189     const IAny::ConstPtr& from, const IAny::ConstPtr& to, float t)
190 {
191     if (from && to) {
192         if (auto i = interface_cast<IPropertyInternalAny>(output)) {
193             PropertyLock lock { output };
194             if (auto iany = i->GetInternalAny()) {
195                 auto ret = inter->Interpolate(*iany, *from, *to, t);
196                 lock.SetValueAny(*iany);
197                 return ret;
198             }
199         }
200     }
201     return AnyReturn::FAIL;
202 }
203 
IsValueGetCompatible(const IAny & any,const IValue & value)204 inline bool IsValueGetCompatible(const IAny& any, const IValue& value)
205 {
206     for (auto&& t : any.GetCompatibleTypes(CompatibilityDirection::GET)) {
207         if (value.IsCompatible(t)) {
208             return true;
209         }
210     }
211     return false;
212 }
213 
IsModifierGetCompatible(const IAny & any,const IModifier & value)214 inline bool IsModifierGetCompatible(const IAny& any, const IModifier& value)
215 {
216     for (auto&& t : any.GetCompatibleTypes(CompatibilityDirection::GET)) {
217         if (value.IsCompatible(t)) {
218             return true;
219         }
220     }
221     return false;
222 }
223 
224 template<typename Interface>
GetFirstValueFromProperty(const IProperty::ConstPtr & p)225 typename Interface::Ptr GetFirstValueFromProperty(const IProperty::ConstPtr& p)
226 {
227     if (auto i = interface_cast<IStackProperty>(p)) {
228         PropertyLock lock { p };
229         TypeId ids[] = { Interface::UID };
230         auto values = i->GetValues(ids, false);
231         if (!values.empty()) {
232             return interface_pointer_cast<Interface>(values.back());
233         }
234     }
235     return nullptr;
236 }
237 
238 META_END_NAMESPACE()
239 
240 #endif
241