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 BASE_EVENT_RAW_DECODE_INCLUDE_DECODED_PARAM_H
17 #define BASE_EVENT_RAW_DECODE_INCLUDE_DECODED_PARAM_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "base/raw_data_base_def.h"
23 #include "base/value_param.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace EventRaw {
28 class DecodedParam : public ValueParam {
29 
30 public:
DecodedParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)31     DecodedParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
32         : rawData_(rawData), maxLen_(maxLen), pos_(pos), key_(key)
33     {}
34 
~DecodedParam()35     virtual ~DecodedParam()
36     {}
37 
38 public:
39     virtual std::string GetKey();
40     virtual size_t GetPosition();
41 
42 public:
43     virtual bool AsUint64(uint64_t& dest) override;
44     virtual bool AsInt64(int64_t& dest) override;
45     virtual bool AsDouble(double& dest) override;
46     virtual bool AsString(std::string& dest) override;
47     virtual bool AsUint64Vec(std::vector<uint64_t>& dest) override;
48     virtual bool AsInt64Vec(std::vector<int64_t>& dest) override;
49     virtual bool AsDoubleVec(std::vector<double>& dest) override;
50     virtual bool AsStringVec(std::vector<std::string>& dest) override;
51 
52 public:
53     virtual bool DecodeValue() = 0;
54 
55 protected:
56     uint8_t* rawData_;
57     size_t maxLen_;
58     size_t pos_;
59     std::string key_;
60 };
61 
62 class UnsignedVarintDecodedParam : public DecodedParam {
63 public:
UnsignedVarintDecodedParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)64     UnsignedVarintDecodedParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
65         : DecodedParam(rawData, maxLen, pos, key) {}
66 
67     virtual bool DecodeValue() override;
68     virtual DataCodedType GetDataCodedType() override;
69     virtual bool AsUint64(uint64_t& dest) override;
70 
71 private:
72     uint64_t val_ = 0;
73 };
74 
75 class UnsignedVarintDecodedArrayParam : public DecodedParam {
76 public:
UnsignedVarintDecodedArrayParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)77     UnsignedVarintDecodedArrayParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
78         : DecodedParam(rawData, maxLen, pos, key) {}
79 
80     virtual bool DecodeValue() override;
81     virtual DataCodedType GetDataCodedType() override;
82     virtual bool AsUint64Vec(std::vector<uint64_t>& dest) override;
83 
84 private:
85     std::vector<uint64_t> vals_;
86 };
87 
88 class SignedVarintDecodedParam : public DecodedParam {
89 public:
SignedVarintDecodedParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)90     SignedVarintDecodedParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
91         : DecodedParam(rawData, maxLen, pos, key) {}
92 
93     virtual bool DecodeValue() override;
94     virtual DataCodedType GetDataCodedType() override;
95     virtual bool AsInt64(int64_t& dest) override;
96 
97 private:
98     int64_t val_ = 0;
99 };
100 
101 class SignedVarintDecodedArrayParam : public DecodedParam {
102 public:
SignedVarintDecodedArrayParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)103     SignedVarintDecodedArrayParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
104         : DecodedParam(rawData, maxLen, pos, key) {}
105 
106     virtual bool DecodeValue() override;
107     virtual DataCodedType GetDataCodedType() override;
108     virtual bool AsInt64Vec(std::vector<int64_t>& dest) override;
109 
110 private:
111     std::vector<int64_t> vals_;
112 };
113 
114 class FloatingNumberDecodedParam : public DecodedParam {
115 public:
FloatingNumberDecodedParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)116     FloatingNumberDecodedParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
117         : DecodedParam(rawData, maxLen, pos, key) {}
118 
119     virtual bool DecodeValue() override;
120     virtual DataCodedType GetDataCodedType() override;
121     virtual bool AsDouble(double& dest) override;
122 
123 private:
124     double val_ = 0.0;
125 };
126 
127 class FloatingNumberDecodedArrayParam : public DecodedParam {
128 public:
FloatingNumberDecodedArrayParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)129     FloatingNumberDecodedArrayParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
130         : DecodedParam(rawData, maxLen, pos, key) {}
131 
132     virtual bool DecodeValue() override;
133     virtual DataCodedType GetDataCodedType() override;
134     virtual bool AsDoubleVec(std::vector<double>& dest) override;
135 
136 private:
137     std::vector<double> vals_;
138 };
139 
140 class StringDecodedParam : public DecodedParam {
141 public:
StringDecodedParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)142     StringDecodedParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
143         : DecodedParam(rawData, maxLen, pos, key) {}
144 
145     virtual bool DecodeValue() override;
146     virtual DataCodedType GetDataCodedType() override;
147     virtual bool AsString(std::string& dest) override;
148 
149 private:
150     std::string val_;
151 };
152 
153 class StringDecodedArrayParam : public DecodedParam {
154 public:
StringDecodedArrayParam(uint8_t * rawData,const size_t maxLen,size_t & pos,const std::string & key)155     StringDecodedArrayParam(uint8_t* rawData, const size_t maxLen, size_t& pos, const std::string& key)
156         : DecodedParam(rawData, maxLen, pos, key) {}
157 
158     virtual bool DecodeValue() override;
159     virtual DataCodedType GetDataCodedType() override;
160     virtual bool AsStringVec(std::vector<std::string>& dest) override;
161 
162 private:
163     std::vector<std::string> vals_;
164 };
165 } // namespace EventRaw
166 } // namespace HiviewDFX
167 } // namespace OHOS
168 
169 #endif // BASE_EVENT_RAW_DECODE_INCLUDE_DECODED_PARAM_H