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     using Floats = ValueObject::FloatVector;
28     template<typename T, typename... Rest>
29     static bool Convert(T input, std::variant<Rest...> &output);
30 
31     static size_t ParserRawData(const uint8_t *data, size_t length, Asset &asset);
32     static size_t ParserRawData(const uint8_t *data, size_t length, Assets &assets);
33     static size_t ParserRawData(const uint8_t *data, size_t length, std::map<std::string, Asset> &assets);
34     static size_t ParserRawData(const uint8_t *data, size_t length, BigInteger &bigint);
35     static size_t ParserRawData(const uint8_t *data, size_t length, Floats &floats);
36 
37     static std::vector<uint8_t> PackageRawData(const Asset &asset);
38     static std::vector<uint8_t> PackageRawData(const Assets &assets);
39     static std::vector<uint8_t> PackageRawData(const std::map<std::string, Asset> &assets);
40     static std::vector<uint8_t> PackageRawData(const BigInteger &bigint);
41     static std::vector<uint8_t> PackageRawData(const Floats &floats);
42 
43 private:
44     struct InnerAsset : public Serializable {
45         Asset &asset_;
InnerAssetInnerAsset46         explicit InnerAsset(Asset &asset) : asset_(asset) {}
47 
48         bool Marshal(json &node) const override;
49         bool Unmarshal(const json &node) override;
50     };
51 
52     template<typename T, typename O>
Get(T && input,O & output)53     static bool Get(T &&input, O &output)
54     {
55         return false;
56     }
57 
58     template<typename T, typename O, typename First, typename... Rest>
Get(T && input,O & output)59     static bool Get(T &&input, O &output)
60     {
61         auto *val = Traits::get_if<First>(&input);
62         if (val != nullptr) {
63             output = std::move(*val);
64             return true;
65         }
66         return Get<T, O, Rest...>(std::move(input), output);
67     }
68 
69     static constexpr const uint32_t ASSET_MAGIC = 0x41534554;
70     static constexpr const uint32_t ASSETS_MAGIC = 0x41534553;
71     static constexpr const uint32_t FLOUT32_ARRAY = 0x46333241;
72     static constexpr const uint32_t BIG_INT = 0x42494749;
73 };
74 
75 template<typename T, typename... Rest>
Convert(T input,std::variant<Rest...> & output)76 bool RawDataParser::Convert(T input, std::variant<Rest...> &output)
77 {
78     return Get<T, decltype(output), Rest...>(std::move(input), output);
79 }
80 }
81 
82 #endif // OHOS_DISTRIBUTED_DATA_RELATIONAL_STORE_FRAMEWORKS_NATIVE_RDB_PARSER_H
83