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_INTERFACE_PROPERTY_CONSTRUCT_ARRAY_PROPERTY_H
17 #define META_INTERFACE_PROPERTY_CONSTRUCT_ARRAY_PROPERTY_H
18
19 #include <meta/interface/intf_object_flags.h>
20 #include <meta/interface/intf_object_registry.h>
21 #include <meta/interface/property/array_property.h>
22
META_BEGIN_NAMESPACE()23 META_BEGIN_NAMESPACE()
24
25 /**
26 * @brief Construct array property
27 * @param obr object registry instance
28 * @param name Name of the property
29 * @param value Default value for the array property
30 * @param flags Object flags for the array property
31 * @return Typed array property
32 */
33 template<typename T>
34 ArrayProperty<T> ConstructArrayProperty(IObjectRegistry& obr, BASE_NS::string_view name,
35 BASE_NS::array_view<const T> value = {}, ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE)
36 {
37 ArrayProperty<T> p(NOCHECK, obr.GetPropertyRegister().Create(ClassId::StackProperty, name));
38 if (auto i = interface_cast<IPropertyInternalAny>(p.GetProperty())) {
39 i->SetInternalAny(ConstructArrayAny<T>(BASE_NS::move(value)));
40 }
41 SetObjectFlags(p.GetProperty(), flags, true);
42 return p;
43 }
44 /**
45 * @brief Construct array property
46 * @param name Name of the property
47 * @param value Default value for the array property
48 * @param flags Object flags for the array property
49 * @return Typed array property
50 */
51 template<typename T>
52 ArrayProperty<T> ConstructArrayProperty(BASE_NS::string_view name, BASE_NS::array_view<const T> value = {},
53 ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE)
54 {
55 return ConstructArrayProperty(GetObjectRegistry(), name, BASE_NS::move(value), flags);
56 }
57 /**
58 * @brief Construct array property with IAny default value
59 * @param name Name of the property
60 * @param value Default value for the array property
61 * @param flags Object flags for the array property
62 * @return Typed array property
63 */
64 template<typename T>
65 ArrayProperty<T> ConstructArrayPropertyAny(
66 BASE_NS::string_view name, const IAny& value, ObjectFlagBitsValue flags = ObjectFlagBits::SERIALIZE)
67 {
68 auto p = ConstructArrayProperty<T>(name, {}, flags);
69 if (p) {
70 p.GetUnlockedAccess().SetDefaultValueAny(value);
71 }
72 return p;
73 }
74
75 // helpers to handle default values with Value Ptr stuff
76 template<typename T>
ConstructArrayAnyHelper(BASE_NS::vector<T> value)77 IAny::Ptr ConstructArrayAnyHelper(BASE_NS::vector<T> value)
78 {
79 return ConstructArrayAny(BASE_NS::move(value));
80 }
81 template<typename T, typename Param = typename T::TypePtr, typename = typename T::TypePtr>
ConstructArrayAnyHelper(BASE_NS::vector<Param> value)82 IAny::Ptr ConstructArrayAnyHelper(BASE_NS::vector<Param> value)
83 {
84 return ConstructArrayAny(BASE_NS::move(value));
85 }
86
87 META_END_NAMESPACE()
88
89 #endif
90