• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PARSER_H
17 #define OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PARSER_H
18 
19 #include "serializable.h"
20 #include "traits.h"
21 #include "value_object.h"
22 namespace OHOS::NativeRdb {
23 class RawDataParser final {
24 public:
25     using Asset = ValueObject::Asset;
26     using Assets = ValueObject::Assets;
27     template<typename T, typename... Rest>
28     static bool Convert(T input, std::variant<Rest...> &output);
29     static size_t ParserRawData(const uint8_t *data, size_t length, Asset &asset);
30     static size_t ParserRawData(const uint8_t *data, size_t length, Assets &assets);
31     static size_t ParserRawData(const uint8_t *data, size_t length, std::map<std::string, Asset> &assets);
32     static std::vector<uint8_t> PackageRawData(const Asset &asset);
33     static std::vector<uint8_t> PackageRawData(const Assets &assets);
34     static std::vector<uint8_t> PackageRawData(const std::map<std::string, Asset> &assets);
35 
36 private:
37     struct InnerAsset : public Serializable {
38         Asset &asset_;
InnerAssetInnerAsset39         explicit InnerAsset(Asset &asset) : asset_(asset) {}
40 
41         bool Marshal(json &node) const override;
42         bool Unmarshal(const json &node) override;
43     };
44 
45     template<typename T, typename O>
Get(T && input,O & output)46     static bool Get(T &&input, O &output)
47     {
48         return false;
49     }
50 
51     template<typename T, typename O, typename First, typename... Rest>
Get(T && input,O & output)52     static bool Get(T &&input, O &output)
53     {
54         auto *val = Traits::get_if<First>(&input);
55         if (val != nullptr) {
56             output = std::move(*val);
57             return true;
58         }
59         return Get<T, O, Rest...>(std::move(input), output);
60     }
61     static constexpr const uint32_t ASSET_MAGIC = 0x41534554;
62     static constexpr const uint32_t ASSETS_MAGIC = 0x41534553;
63 };
64 
65 template<typename T, typename... Rest>
Convert(T input,std::variant<Rest...> & output)66 bool RawDataParser::Convert(T input, std::variant<Rest...> &output)
67 {
68     return Get<T, decltype(output), Rest...>(std::move(input), output);
69 }
70 }
71 
72 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PARSER_H
73