/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef META_INTERFACE_PROPERTY_CONSTRUCT_PROPERTY_H #define META_INTERFACE_PROPERTY_CONSTRUCT_PROPERTY_H #include #include #include #include META_BEGIN_NAMESPACE() struct ValuePtrBase {}; template struct ValuePtrInstance : ValuePtrBase { static constexpr ObjectId ID = ClassInfo.Id(); using TypePtr = typename Type::Ptr; static_assert(IsInterfacePtr_v, "Invalid type for ValuePtr"); }; template struct ValuePtrImpl { template using Instance = ValuePtrInstance; }; template using ValuePtr = ValuePtrInstance; template struct PropertyType { using Type = T; }; template struct PropertyType> { using Type = typename ValuePtr::TypePtr; }; template using PropertyType_v = typename PropertyType::Type; // NOLINT(readability-identifier-naming) template Property ConstructProperty(IObjectRegistry& obr, BASE_NS::string_view name, const T& value = {}, ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE) { Property p(NOCHECK, obr.GetPropertyRegister().Create(ClassId::StackProperty, name)); if (auto i = interface_cast(p.GetProperty())) { i->SetInternalAny(IAny::Ptr(new Any)); } auto access = p.GetUnlockedAccess(); access.SetDefaultValue(value); if (auto f = interface_cast(p.GetProperty())) { f->SetObjectFlags(flags); } return p; } template>> Property ConstructProperty( BASE_NS::string_view name, const T& value = {}, ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE) { return ConstructProperty(GetObjectRegistry(), name, value, flags); } template Property ConstructProperty( BASE_NS::string_view name, const Param& value = {}, ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE) { auto& obr = GetObjectRegistry(); Property p(NOCHECK, obr.GetPropertyRegister().Create(ClassId::StackProperty, name)); if (auto i = interface_cast(p.GetProperty())) { auto obj = obr.Create(T::ID); if (!obj) { CORE_LOG_E("invalid class id for ValuePtr"); return nullptr; } i->SetInternalAny(obj); } auto access = p.GetUnlockedAccess(); access.SetDefaultValueAny(Any(value)); if (auto f = interface_cast(p.GetProperty())) { f->SetObjectFlags(flags); } return p; } inline IProperty::Ptr ConstructPropertyAny( BASE_NS::string_view name, const IAny& value, ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE) { auto p = GetObjectRegistry().GetPropertyRegister().Create(ClassId::StackProperty, name); if (auto i = interface_cast(p)) { i->SetInternalAny(value.Clone(true)); } if (auto f = interface_cast(p)) { f->SetObjectFlags(flags); } return p; } META_END_NAMESPACE() #endif