18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#ifndef META_API_UTIL_H
178bf80f4bSopenharmony_ci#define META_API_UTIL_H
188bf80f4bSopenharmony_ci
198bf80f4bSopenharmony_ci#include <meta/api/locking.h>
208bf80f4bSopenharmony_ci#include <meta/interface/animation/intf_interpolator.h>
218bf80f4bSopenharmony_ci#include <meta/interface/intf_any.h>
228bf80f4bSopenharmony_ci#include <meta/interface/intf_object_registry.h>
238bf80f4bSopenharmony_ci#include <meta/interface/intf_value.h>
248bf80f4bSopenharmony_ci#include <meta/interface/property/intf_modifier.h>
258bf80f4bSopenharmony_ci#include <meta/interface/property/property.h>
268bf80f4bSopenharmony_ci
278bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE()
288bf80f4bSopenharmony_ci
298bf80f4bSopenharmony_ciinline BASE_NS::shared_ptr<CORE_NS::IInterface> GetPointer(const IAny& any)
308bf80f4bSopenharmony_ci{
318bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<CORE_NS::IInterface> ret;
328bf80f4bSopenharmony_ci    any.GetValue(ret);
338bf80f4bSopenharmony_ci    return ret;
348bf80f4bSopenharmony_ci}
358bf80f4bSopenharmony_ci
368bf80f4bSopenharmony_ciinline BASE_NS::shared_ptr<const CORE_NS::IInterface> GetConstPointer(const IAny& any)
378bf80f4bSopenharmony_ci{
388bf80f4bSopenharmony_ci    BASE_NS::shared_ptr<const CORE_NS::IInterface> ret;
398bf80f4bSopenharmony_ci    any.GetValue(ret);
408bf80f4bSopenharmony_ci    return ret;
418bf80f4bSopenharmony_ci}
428bf80f4bSopenharmony_ci
438bf80f4bSopenharmony_citemplate<typename Interface>
448bf80f4bSopenharmony_ciinline BASE_NS::shared_ptr<Interface> GetPointer(const IAny& any)
458bf80f4bSopenharmony_ci{
468bf80f4bSopenharmony_ci    return interface_pointer_cast<Interface>(GetPointer(any));
478bf80f4bSopenharmony_ci}
488bf80f4bSopenharmony_ci
498bf80f4bSopenharmony_ci/// Returns IIinterface pointer if property contains a pointer that can be converted to it.
508bf80f4bSopenharmony_ciinline BASE_NS::shared_ptr<CORE_NS::IInterface> GetPointer(const IProperty::ConstPtr& p)
518bf80f4bSopenharmony_ci{
528bf80f4bSopenharmony_ci    InterfaceSharedLock lock { p };
538bf80f4bSopenharmony_ci    return GetPointer(p->GetValue());
548bf80f4bSopenharmony_ci}
558bf80f4bSopenharmony_ci
568bf80f4bSopenharmony_citemplate<typename Interface>
578bf80f4bSopenharmony_ciinline BASE_NS::shared_ptr<Interface> GetPointer(const IProperty::ConstPtr& p)
588bf80f4bSopenharmony_ci{
598bf80f4bSopenharmony_ci    return interface_pointer_cast<Interface>(GetPointer(p));
608bf80f4bSopenharmony_ci}
618bf80f4bSopenharmony_ci
628bf80f4bSopenharmony_ciinline IProperty::Ptr DuplicatePropertyType(IObjectRegistry& obr, IProperty::ConstPtr p, BASE_NS::string_view name = {})
638bf80f4bSopenharmony_ci{
648bf80f4bSopenharmony_ci    IProperty::Ptr dup;
658bf80f4bSopenharmony_ci    PropertyLock lock { p };
668bf80f4bSopenharmony_ci    if (auto obj = interface_cast<IObject>(p)) {
678bf80f4bSopenharmony_ci        dup = obr.GetPropertyRegister().Create(obj->GetClassId(), name.empty() ? p->GetName() : name);
688bf80f4bSopenharmony_ci        if (auto dupi = interface_cast<IPropertyInternalAny>(dup)) {
698bf80f4bSopenharmony_ci            if (auto i = interface_cast<IPropertyInternalAny>(p)) {
708bf80f4bSopenharmony_ci                if (auto&& any = i->GetInternalAny()) {
718bf80f4bSopenharmony_ci                    dupi->SetInternalAny(any->Clone(false));
728bf80f4bSopenharmony_ci                }
738bf80f4bSopenharmony_ci            }
748bf80f4bSopenharmony_ci        }
758bf80f4bSopenharmony_ci    }
768bf80f4bSopenharmony_ci    return dup;
778bf80f4bSopenharmony_ci}
788bf80f4bSopenharmony_ci
798bf80f4bSopenharmony_citemplate<typename Type>
808bf80f4bSopenharmony_ciType GetValue(const Property<Type>& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
818bf80f4bSopenharmony_ci{
828bf80f4bSopenharmony_ci    return p ? p->GetValue() : BASE_NS::move(defaultValue);
838bf80f4bSopenharmony_ci}
848bf80f4bSopenharmony_citemplate<typename Type>
858bf80f4bSopenharmony_ciType GetValue(const IProperty::ConstPtr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
868bf80f4bSopenharmony_ci{
878bf80f4bSopenharmony_ci    return GetValue(Property<Type>(p), BASE_NS::move(defaultValue));
888bf80f4bSopenharmony_ci}
898bf80f4bSopenharmony_citemplate<typename Type>
908bf80f4bSopenharmony_ciType GetValue(const IProperty::ConstWeakPtr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
918bf80f4bSopenharmony_ci{
928bf80f4bSopenharmony_ci    return GetValue<Type>(p.lock(), BASE_NS::move(defaultValue));
938bf80f4bSopenharmony_ci}
948bf80f4bSopenharmony_ci// to disambiguate
958bf80f4bSopenharmony_citemplate<typename Type>
968bf80f4bSopenharmony_ciType GetValue(const IProperty::Ptr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
978bf80f4bSopenharmony_ci{
988bf80f4bSopenharmony_ci    return GetValue(Property<Type>(p), BASE_NS::move(defaultValue));
998bf80f4bSopenharmony_ci}
1008bf80f4bSopenharmony_ci// to disambiguate
1018bf80f4bSopenharmony_citemplate<typename Type>
1028bf80f4bSopenharmony_ciType GetValue(const IProperty::WeakPtr& p, NonDeduced_t<BASE_NS::remove_const_t<Type>> defaultValue = {}) noexcept
1038bf80f4bSopenharmony_ci{
1048bf80f4bSopenharmony_ci    return GetValue<Type>(p.lock(), BASE_NS::move(defaultValue));
1058bf80f4bSopenharmony_ci}
1068bf80f4bSopenharmony_ci
1078bf80f4bSopenharmony_citemplate<typename Type>
1088bf80f4bSopenharmony_cibool SetValue(Property<Type> property, const NonDeduced_t<Type>& value) noexcept
1098bf80f4bSopenharmony_ci{
1108bf80f4bSopenharmony_ci    return property && property->SetValue(value);
1118bf80f4bSopenharmony_ci}
1128bf80f4bSopenharmony_citemplate<typename Type>
1138bf80f4bSopenharmony_cibool SetValue(IProperty::Ptr p, const NonDeduced_t<Type>& value) noexcept
1148bf80f4bSopenharmony_ci{
1158bf80f4bSopenharmony_ci    return SetValue(Property<Type>(p), value);
1168bf80f4bSopenharmony_ci}
1178bf80f4bSopenharmony_citemplate<typename Type>
1188bf80f4bSopenharmony_cibool SetValue(IProperty::WeakPtr p, const NonDeduced_t<Type>& value) noexcept
1198bf80f4bSopenharmony_ci{
1208bf80f4bSopenharmony_ci    return SetValue<Type>(p.lock(), value);
1218bf80f4bSopenharmony_ci}
1228bf80f4bSopenharmony_ci
1238bf80f4bSopenharmony_ciinline bool Copy(const IProperty::ConstPtr& src, const IProperty::Ptr& dst)
1248bf80f4bSopenharmony_ci{
1258bf80f4bSopenharmony_ci    PropertyLock source(src);
1268bf80f4bSopenharmony_ci    PropertyLock dest(dst);
1278bf80f4bSopenharmony_ci    return dest->SetValueAny(source->GetValueAny());
1288bf80f4bSopenharmony_ci}
1298bf80f4bSopenharmony_ci
1308bf80f4bSopenharmony_ciinline bool IsCompatible(
1318bf80f4bSopenharmony_ci    const IProperty::ConstPtr& prop, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
1328bf80f4bSopenharmony_ci{
1338bf80f4bSopenharmony_ci    bool res = false;
1348bf80f4bSopenharmony_ci    if (auto i = interface_cast<IPropertyInternalAny>(prop)) {
1358bf80f4bSopenharmony_ci        if (auto iany = i->GetInternalAny()) {
1368bf80f4bSopenharmony_ci            res = IsCompatible(*iany, id, dir);
1378bf80f4bSopenharmony_ci        }
1388bf80f4bSopenharmony_ci    }
1398bf80f4bSopenharmony_ci    return res;
1408bf80f4bSopenharmony_ci}
1418bf80f4bSopenharmony_ci
1428bf80f4bSopenharmony_citemplate<typename T>
1438bf80f4bSopenharmony_ciinline bool IsCompatibleWith(const IProperty::ConstPtr& prop, CompatibilityDirection dir = CompatibilityDirection::BOTH)
1448bf80f4bSopenharmony_ci{
1458bf80f4bSopenharmony_ci    return IsCompatible(prop, UidFromType<BASE_NS::remove_const_t<BASE_NS::remove_reference_t<T>>>(), dir);
1468bf80f4bSopenharmony_ci}
1478bf80f4bSopenharmony_ci
1488bf80f4bSopenharmony_ciinline bool IsSetCompatible(const IProperty::ConstPtr& prop, const TypeId& id)
1498bf80f4bSopenharmony_ci{
1508bf80f4bSopenharmony_ci    return IsCompatible(prop, id, CompatibilityDirection::SET);
1518bf80f4bSopenharmony_ci}
1528bf80f4bSopenharmony_ci
1538bf80f4bSopenharmony_ciinline bool IsGetCompatible(const IProperty::ConstPtr& prop, const TypeId& id)
1548bf80f4bSopenharmony_ci{
1558bf80f4bSopenharmony_ci    return IsCompatible(prop, id, CompatibilityDirection::GET);
1568bf80f4bSopenharmony_ci}
1578bf80f4bSopenharmony_ci
1588bf80f4bSopenharmony_citemplate<typename T>
1598bf80f4bSopenharmony_ciinline bool IsSetCompatibleWith(const IProperty::ConstPtr& prop)
1608bf80f4bSopenharmony_ci{
1618bf80f4bSopenharmony_ci    return IsCompatibleWith<T>(prop, CompatibilityDirection::SET);
1628bf80f4bSopenharmony_ci}
1638bf80f4bSopenharmony_ci
1648bf80f4bSopenharmony_citemplate<typename T>
1658bf80f4bSopenharmony_ciinline bool IsGetCompatibleWith(const IProperty::ConstPtr& prop)
1668bf80f4bSopenharmony_ci{
1678bf80f4bSopenharmony_ci    return IsCompatibleWith<T>(prop, CompatibilityDirection::GET);
1688bf80f4bSopenharmony_ci}
1698bf80f4bSopenharmony_ci
1708bf80f4bSopenharmony_ciinline AnyReturnValue Interpolate(IInterpolator::ConstPtr inter, const IProperty::Ptr& output,
1718bf80f4bSopenharmony_ci    const IAny::ConstPtr& from, const IAny::ConstPtr& to, float t)
1728bf80f4bSopenharmony_ci{
1738bf80f4bSopenharmony_ci    if (from && to) {
1748bf80f4bSopenharmony_ci        if (auto i = interface_cast<IPropertyInternalAny>(output)) {
1758bf80f4bSopenharmony_ci            PropertyLock lock { output };
1768bf80f4bSopenharmony_ci            if (auto iany = i->GetInternalAny()) {
1778bf80f4bSopenharmony_ci                auto ret = inter->Interpolate(*iany, *from, *to, t);
1788bf80f4bSopenharmony_ci                lock.SetValueAny(*iany);
1798bf80f4bSopenharmony_ci                return ret;
1808bf80f4bSopenharmony_ci            }
1818bf80f4bSopenharmony_ci        }
1828bf80f4bSopenharmony_ci    }
1838bf80f4bSopenharmony_ci    return AnyReturn::FAIL;
1848bf80f4bSopenharmony_ci}
1858bf80f4bSopenharmony_ci
1868bf80f4bSopenharmony_ciinline bool IsValueGetCompatible(const IAny& any, const IValue& value)
1878bf80f4bSopenharmony_ci{
1888bf80f4bSopenharmony_ci    for (auto&& t : any.GetCompatibleTypes(CompatibilityDirection::GET)) {
1898bf80f4bSopenharmony_ci        if (value.IsCompatible(t)) {
1908bf80f4bSopenharmony_ci            return true;
1918bf80f4bSopenharmony_ci        }
1928bf80f4bSopenharmony_ci    }
1938bf80f4bSopenharmony_ci    return false;
1948bf80f4bSopenharmony_ci}
1958bf80f4bSopenharmony_ci
1968bf80f4bSopenharmony_ciinline bool IsModifierGetCompatible(const IAny& any, const IModifier& value)
1978bf80f4bSopenharmony_ci{
1988bf80f4bSopenharmony_ci    for (auto&& t : any.GetCompatibleTypes(CompatibilityDirection::GET)) {
1998bf80f4bSopenharmony_ci        if (value.IsCompatible(t)) {
2008bf80f4bSopenharmony_ci            return true;
2018bf80f4bSopenharmony_ci        }
2028bf80f4bSopenharmony_ci    }
2038bf80f4bSopenharmony_ci    return false;
2048bf80f4bSopenharmony_ci}
2058bf80f4bSopenharmony_ci
2068bf80f4bSopenharmony_ciMETA_END_NAMESPACE()
2078bf80f4bSopenharmony_ci
2088bf80f4bSopenharmony_ci#endif
209