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 DATASHARE_VALUEBUCKET_CONVERT_H
17 #define DATASHARE_VALUEBUCKET_CONVERT_H
18 
19 #include "datashare_value_object.h"
20 #include "datashare_values_bucket.h"
21 #include "datashare_observer.h"
22 #include "traits.h"
23 
24 namespace OHOS::DataShare {
25 using Value = DataShareObserver::ChangeInfo::Value;
26 using VBucket = DataShareObserver::ChangeInfo::VBucket;
27 using VBuckets = DataShareObserver::ChangeInfo::VBuckets;
28 
29 template<typename T, typename O>
GetItem(T && input,O & output)30 static bool GetItem(T&& input, O& output)
31 {
32     return false;
33 }
34 
35 template<typename T, typename O, typename First, typename... Rest>
GetItem(T && input,O & output)36 static bool GetItem(T&& input, O& output)
37 {
38     auto val = Traits::get_if<First>(&input);
39     if (val != nullptr) {
40         output = std::move(*val);
41         return true;
42     }
43     return GetItem<T, O, Rest...>(std::move(input), output);
44 }
45 
46 template<typename T, typename... Types>
Convert(T && input,std::variant<Types...> & output)47 static bool Convert(T&& input, std::variant<Types...>& output)
48 {
49     return GetItem<T, decltype(output), Types...>(std::move(input), output);
50 }
51 }
52 
53 namespace OHOS::DataShare {
54 class ValueProxy {
55 public:
56     static VBuckets Convert(std::vector<DataShare::DataShareValuesBucket> &&dataShareValuesBuckets);
57     static std::vector<DataShare::DataShareValuesBucket> Convert(VBuckets &&vBuckets);
58 };
59 }
60 #endif // DATASHARE_VALUEBUCKET_CONVERT_H