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_ARRAY_UTIL_H
17 #define META_API_ARRAY_UTIL_H
18
19 #include <meta/api/util.h>
20 #include <meta/interface/property/array_property.h>
21
META_BEGIN_NAMESPACE()22 META_BEGIN_NAMESPACE()
23
24 /// Get shared_ptr of IInterface from an array any in given location, or nullptr if not compatible
25 inline BASE_NS::shared_ptr<CORE_NS::IInterface> GetPointerAt(IArrayAny::IndexType index, const IProperty::ConstPtr& p)
26 {
27 ArrayPropertyLock lock { p };
28 return lock ? GetPointer(lock->GetAnyAt(index)) : nullptr;
29 }
30
31 /// Get shared_ptr of IInterface from an array any in given location, or nullptr if not compatible
GetConstPointerAt(IArrayAny::IndexType index,const IProperty::ConstPtr & p)32 inline BASE_NS::shared_ptr<const CORE_NS::IInterface> GetConstPointerAt(
33 IArrayAny::IndexType index, const IProperty::ConstPtr& p)
34 {
35 ArrayPropertyLock lock { p };
36 return lock ? GetConstPointer(lock->GetAnyAt(index)) : nullptr;
37 }
38
39 /// Get interface pointer from an array any in given location, or nullptr if not compatible
40 template<typename Interface>
GetPointerAt(IArrayAny::IndexType index,const IProperty::ConstPtr & p)41 inline BASE_NS::shared_ptr<Interface> GetPointerAt(IArrayAny::IndexType index, const IProperty::ConstPtr& p)
42 {
43 return interface_pointer_cast<Interface>(GetPointerAt(index, p));
44 }
45
46 /// Get interface pointer from an array any in given location, or nullptr if not compatible
47 template<typename Interface>
GetConstPointerAt(IArrayAny::IndexType index,const IProperty::ConstPtr & p)48 inline BASE_NS::shared_ptr<Interface> GetConstPointerAt(IArrayAny::IndexType index, const IProperty::ConstPtr& p)
49 {
50 return interface_pointer_cast<Interface>(GetConstPointerAt(index, p));
51 }
52
53 /// Set interface pointer to an array property in given location, or nullptr if not compatible
SetPointerAt(IArrayAny::IndexType index,const IProperty::Ptr & p,const BASE_NS::shared_ptr<CORE_NS::IInterface> & ptr)54 inline bool SetPointerAt(
55 IArrayAny::IndexType index, const IProperty::Ptr& p, const BASE_NS::shared_ptr<CORE_NS::IInterface>& ptr)
56 {
57 if (ArrayPropertyLock lock { p }) {
58 if (auto any = lock->ConstructItemAny()) {
59 if (any->SetValue(ptr)) {
60 return lock->SetAnyAt(index, *any);
61 }
62 }
63 }
64 return false;
65 }
66
67 /// Set interface pointer to an array property in given location, or nullptr if not compatible
SetPointerAt(IArrayAny::IndexType index,const IProperty::Ptr & p,const BASE_NS::shared_ptr<const CORE_NS::IInterface> & ptr)68 inline bool SetPointerAt(
69 IArrayAny::IndexType index, const IProperty::Ptr& p, const BASE_NS::shared_ptr<const CORE_NS::IInterface>& ptr)
70 {
71 if (ArrayPropertyLock lock { p }) {
72 if (auto any = lock->ConstructItemAny()) {
73 if (any->SetValue(ptr)) {
74 return lock->SetAnyAt(index, *any);
75 }
76 }
77 }
78 return false;
79 }
80
81 /// Set interface pointer to an array property in given location, or nullptr if not compatible
82 template<typename Interface>
SetPointerAt(IArrayAny::IndexType index,const IProperty::Ptr & p,const BASE_NS::shared_ptr<Interface> & ptr)83 inline bool SetPointerAt(IArrayAny::IndexType index, const IProperty::Ptr& p, const BASE_NS::shared_ptr<Interface>& ptr)
84 {
85 return SetPointerAt(index, p, interface_pointer_cast<CORE_NS::IInterface>(ptr));
86 }
87
88 /// Check if property is an array property
IsArrayProperty(const IProperty::ConstPtr & p)89 inline bool IsArrayProperty(const IProperty::ConstPtr& p)
90 {
91 return IsArray(GetInternalAny(p));
92 }
93
94 /// Check if property is an array property
IsArrayProperty(const IProperty & p)95 inline bool IsArrayProperty(const IProperty& p)
96 {
97 return IsArray(GetInternalAny(p));
98 }
99
100 /// Check if array property item is compatible with given type
101 inline bool IsItemCompatible(
102 const IProperty& p, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
103 {
104 if (auto arr = interface_cast<IArrayAny>(GetInternalAny(p))) {
105 return IsItemCompatible(*arr, id, dir);
106 }
107 return false;
108 }
109
110 /// Check if array property item is compatible with given type
111 inline bool IsItemCompatible(
112 const IProperty::ConstPtr& p, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
113 {
114 return p && IsItemCompatible(*p, id, dir);
115 }
116
117 /// Check if array property contains pointers that can be extracted as constant IInterface
IsGetPointerArray(const IProperty::ConstPtr & p)118 inline bool IsGetPointerArray(const IProperty::ConstPtr& p)
119 {
120 return IsItemCompatible(p, GetTypeId<SharedPtrConstIInterface>(), CompatibilityDirection::GET);
121 }
122
123 /// Check if array property contains pointers that can be set as IInterface
IsSetPointerArray(const IProperty::ConstPtr & p)124 inline bool IsSetPointerArray(const IProperty::ConstPtr& p)
125 {
126 return IsItemCompatible(p, GetTypeId<SharedPtrIInterface>(), CompatibilityDirection::SET);
127 }
128
129 META_END_NAMESPACE()
130
131 #endif
132