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_COMPATIBLE_VALUE_UTIL_H
17 #define META_API_COMPATIBLE_VALUE_UTIL_H
18
19 #include <meta/api/util.h>
20
META_BEGIN_NAMESPACE()21 META_BEGIN_NAMESPACE()
22
23 namespace Internal {
24 template<typename Type, typename Out>
25 AnyReturnValue ExtractAnyValue(const IAny& value, Out& out)
26 {
27 Type v {};
28 auto res = value.GetValue<Type>(v);
29 if (res) {
30 out = static_cast<Out>(v);
31 }
32 return res;
33 }
34 } // namespace Internal
35
36 /**
37 * @brief Try to get value from property using list of known types
38 * @param value Any to get value from
39 * @param out Output value, type of this has to be compatible with the list of types given (conversion with static_cast)
40 * @param TypeList Type list of types which are tried for the extraction
41 * @return Result of getting the value.
42 */
43 template<typename Type, typename... Builtins>
GetCompatibleValue(const IAny & value,Type & out,TypeList<Builtins...>)44 AnyReturnValue GetCompatibleValue(const IAny& value, Type& out, TypeList<Builtins...>)
45 {
46 AnyReturnValue res = AnyReturn::FAIL;
47 [[maybe_unused]] bool r = ((META_NS::IsCompatible(value, GetTypeId<Builtins>())
48 ? (res = Internal::ExtractAnyValue<Builtins>(value, out), true)
49 : false) ||
50 ...);
51 return res;
52 }
53 /**
54 * @brief Try to set value to property using list of known types
55 * @param value, type of this has to be compatible with the list of types given (conversion with static_cast)
56 * @param any Any to set value to
57 * @param TypeList Type list of types which are tried for the insertion
58 * @return Result of setting the value.
59 */
60 template<typename Type, typename... Builtins>
SetCompatibleValue(const Type & value,IAny & any,TypeList<Builtins...>)61 AnyReturnValue SetCompatibleValue(const Type& value, IAny& any, TypeList<Builtins...>)
62 {
63 AnyReturnValue res = AnyReturn::FAIL;
64 [[maybe_unused]] bool r =
65 ((META_NS::IsCompatible(any, GetTypeId<Builtins>()) ? (res = any.SetValue(static_cast<Builtins>(value)), true)
66 : false) ||
67 ...);
68 return res;
69 }
70
71 META_END_NAMESPACE()
72
73 #endif
74