• 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_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 interface pointer from an array any in given location, or nullptr if not compatible
32 template<typename Interface>
GetPointerAt(IArrayAny::IndexType index,const IProperty::ConstPtr & p)33 inline BASE_NS::shared_ptr<Interface> GetPointerAt(IArrayAny::IndexType index, const IProperty::ConstPtr& p)
34 {
35     return interface_pointer_cast<Interface>(GetPointerAt(index, p));
36 }
37 /// Check if property is an array property
IsArrayProperty(const IProperty::ConstPtr & p)38 inline bool IsArrayProperty(const IProperty::ConstPtr& p)
39 {
40     return IsArray(GetInternalAny(p));
41 }
42 
43 /// Check if property is an array property
IsArrayProperty(const IProperty & p)44 inline bool IsArrayProperty(const IProperty& p)
45 {
46     return IsArray(GetInternalAny(p));
47 }
48 
49 /// Check if array property item is compatible with given type
50 inline bool IsItemCompatible(
51     const IProperty& p, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
52 {
53     if (auto arr = interface_cast<IArrayAny>(GetInternalAny(p))) {
54         return IsItemCompatible(*arr, id, dir);
55     }
56     return false;
57 }
58 
59 /// Check if array property item is compatible with given type
60 inline bool IsItemCompatible(
61     const IProperty::ConstPtr& p, const TypeId& id, CompatibilityDirection dir = CompatibilityDirection::BOTH)
62 {
63     return p && IsItemCompatible(*p, id, dir);
64 }
65 
66 META_END_NAMESPACE()
67 
68 #endif
69