• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 PROTO_READER_FIELD_H
17 #define PROTO_READER_FIELD_H
18 
19 #include <stdint.h>
20 #include <string.h>
21 #include <vector>
22 #include "proto_reader_help.h"
23 #include "string_help.h"
24 namespace SysTuning {
25 namespace ProtoReader {
26 class DataArea {
27 public:
DataAreaValid()28     bool DataAreaValid() const
29     {
30         return dataAreaID_ != 0;
31     }
DataAreaId()32     uint16_t DataAreaId() const
33     {
34         return dataAreaID_;
35     }
36     explicit operator bool() const
37     {
38         return DataAreaValid();
39     }
40 
Type()41     ProtoWireType Type() const
42     {
43         auto res = static_cast<ProtoWireType>(type_);
44         return res;
45     }
46 
ToBool()47     bool ToBool() const
48     {
49         return static_cast<bool>(intValue_);
50     }
51 
ToUint32()52     uint32_t ToUint32() const
53     {
54         return static_cast<uint32_t>(intValue_);
55     }
56 
ToInt32()57     int32_t ToInt32() const
58     {
59         return static_cast<int32_t>(intValue_);
60     }
61 
ToSint32()62     int32_t ToSint32() const
63     {
64         return ZigZagDecode(static_cast<uint32_t>(intValue_));
65     }
66 
ToUint64()67     uint64_t ToUint64() const
68     {
69         return intValue_;
70     }
71 
ToInt64()72     int64_t ToInt64() const
73     {
74         return static_cast<int64_t>(intValue_);
75     }
76 
ToSint64()77     int64_t ToSint64() const
78     {
79         return ZigZagDecode(static_cast<uint64_t>(intValue_));
80     }
81 
ToFloat()82     float ToFloat() const
83     {
84         float res;
85         uint32_t value32 = static_cast<uint32_t>(intValue_);
86         (void)memcpy_s(&res, sizeof(res), &value32, sizeof(value32));
87         return res;
88     }
89 
ToDouble()90     double ToDouble() const
91     {
92         double res;
93         (void)memcpy_s(&res, sizeof(res), &intValue_, sizeof(intValue_));
94         return res;
95     }
96 
ToString()97     CharsView ToString() const
98     {
99         return CharsView{reinterpret_cast<const char*>(Data()), size_};
100     }
101 
ToStdString()102     std::string ToStdString() const
103     {
104         return ToString().ToStdString();
105     }
106 
ToBytes()107     BytesView ToBytes() const
108     {
109         return BytesView(Data(), size_);
110     }
111 
Data()112     const uint8_t* Data() const
113     {
114         return reinterpret_cast<const uint8_t*>(intValue_);
115     }
116 
Size()117     size_t Size() const
118     {
119         return size_;
120     }
121 
RawIntValue()122     uint64_t RawIntValue() const
123     {
124         return intValue_;
125     }
126 
SetDataAreaId(uint16_t id)127     void SetDataAreaId(uint16_t id)
128     {
129         dataAreaID_ = id;
130     }
SetDataAreaType(uint8_t type)131     void SetDataAreaType(uint8_t type)
132     {
133         type_ = type;
134     }
SetDataAreaIntValue(uint64_t intValue)135     void SetDataAreaIntValue(uint64_t intValue)
136     {
137         intValue_ = intValue;
138     }
SetDataAreaSize(uint32_t size)139     void SetDataAreaSize(uint32_t size)
140     {
141         size_ = size;
142     }
Initialize(uint16_t id,uint8_t type,uint64_t intValue,uint32_t size)143     void Initialize(uint16_t id, uint8_t type, uint64_t intValue, uint32_t size)
144     {
145         dataAreaID_ = id;
146         type_ = type;
147         intValue_ = intValue;
148         size_ = size;
149     }
150 
GetValue(bool * val)151     void GetValue(bool* val) const
152     {
153         *val = ToBool();
154     }
GetValue(uint32_t * val)155     void GetValue(uint32_t* val) const
156     {
157         *val = ToUint32();
158     }
GetValue(int32_t * val)159     void GetValue(int32_t* val) const
160     {
161         *val = ToInt32();
162     }
GetValue(uint64_t * val)163     void GetValue(uint64_t* val) const
164     {
165         *val = ToUint64();
166     }
GetValue(int64_t * val)167     void GetValue(int64_t* val) const
168     {
169         *val = ToInt64();
170     }
GetValue(float * val)171     void GetValue(float* val) const
172     {
173         *val = ToFloat();
174     }
GetValue(double * val)175     void GetValue(double* val) const
176     {
177         *val = ToDouble();
178     }
GetValue(std::string * val)179     void GetValue(std::string* val) const
180     {
181         *val = ToStdString();
182     }
GetValue(CharsView * val)183     void GetValue(CharsView* val) const
184     {
185         *val = ToString();
186     }
GetValue(BytesView * val)187     void GetValue(BytesView* val) const
188     {
189         *val = ToBytes();
190     }
GetSignedValue(int32_t * val)191     void GetSignedValue(int32_t* val) const
192     {
193         *val = ToSint32();
194     }
GetSignedValue(int64_t * val)195     void GetSignedValue(int64_t* val) const
196     {
197         *val = ToSint64();
198     }
199 
200     template <typename T, typename = typename std::enable_if<std::is_enum<T>::value, T>::type>
getT(T * val)201     void getT(T* val) const
202     {
203         *val = static_cast<T>(ToInt32());
204     }
205 
206 private:
207     uint64_t intValue_;
208     uint32_t size_;
209     uint16_t dataAreaID_;
210     uint8_t type_;
211 };
212 } // namespace ProtoReader
213 } // namespace SysTuning
214 #endif // PROTO_READER_FIELD_H
215