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 #include "rs_profiler_capturedata.h"
17 
18 #include "rs_profiler_archive.h"
19 
20 namespace OHOS::Rosen {
21 
22 RSCaptureData::RSCaptureData() = default;
23 
24 RSCaptureData::~RSCaptureData() = default;
25 
Reset()26 void RSCaptureData::Reset()
27 {
28     time_ = 0.0f;
29     properties_.clear();
30 }
31 
SetTime(float time)32 void RSCaptureData::SetTime(float time)
33 {
34     time_ = time;
35 }
36 
GetTime() const37 float RSCaptureData::GetTime() const
38 {
39     return time_;
40 }
41 
SetProperty(const std::string & name,const std::string & value)42 void RSCaptureData::SetProperty(const std::string& name, const std::string& value)
43 {
44     properties_[name] = value;
45 }
46 
GetProperty(const std::string & name) const47 const std::string& RSCaptureData::GetProperty(const std::string& name) const
48 {
49     static const std::string DEFAULT_STR = "0";
50     const auto found = properties_.find(name);
51     return (found != properties_.end()) ? found->second : DEFAULT_STR;
52 }
53 
GetPropertyFloat(const std::string & name) const54 float RSCaptureData::GetPropertyFloat(const std::string& name) const
55 {
56     return std::stof(GetProperty(name));
57 }
58 
GetPropertyDouble(const std::string & name) const59 double RSCaptureData::GetPropertyDouble(const std::string& name) const
60 {
61     return std::stod(GetProperty(name));
62 }
63 
GetPropertyInt8(const std::string & name) const64 int8_t RSCaptureData::GetPropertyInt8(const std::string& name) const
65 {
66     return static_cast<int8_t>(std::stoi(GetProperty(name)));
67 }
68 
GetPropertyUint8(const std::string & name) const69 uint8_t RSCaptureData::GetPropertyUint8(const std::string& name) const
70 {
71     return static_cast<uint8_t>(std::stoul(GetProperty(name)));
72 }
73 
GetPropertyInt16(const std::string & name) const74 int16_t RSCaptureData::GetPropertyInt16(const std::string& name) const
75 {
76     return static_cast<int16_t>(std::stoi(GetProperty(name)));
77 }
78 
GetPropertyUint16(const std::string & name) const79 uint16_t RSCaptureData::GetPropertyUint16(const std::string& name) const
80 {
81     return static_cast<uint16_t>(std::stoul(GetProperty(name)));
82 }
83 
GetPropertyInt32(const std::string & name) const84 int32_t RSCaptureData::GetPropertyInt32(const std::string& name) const
85 {
86     return static_cast<int32_t>(std::stol(GetProperty(name)));
87 }
88 
GetPropertyUint32(const std::string & name) const89 uint32_t RSCaptureData::GetPropertyUint32(const std::string& name) const
90 {
91     return static_cast<uint32_t>(std::stoul(GetProperty(name)));
92 }
93 
GetPropertyInt64(const std::string & name) const94 int64_t RSCaptureData::GetPropertyInt64(const std::string& name) const
95 {
96     return static_cast<int64_t>(std::stoll(GetProperty(name)));
97 }
98 
GetPropertyUint64(const std::string & name) const99 uint64_t RSCaptureData::GetPropertyUint64(const std::string& name) const
100 {
101     return static_cast<uint64_t>(std::stoull(GetProperty(name)));
102 }
103 
Serialize(std::vector<char> & out)104 void RSCaptureData::Serialize(std::vector<char>& out)
105 {
106     DataWriter archive(out);
107     Serialize(archive);
108 }
109 
Deserialize(const std::vector<char> & in)110 void RSCaptureData::Deserialize(const std::vector<char>& in)
111 {
112     Reset();
113 
114     DataReader archive(in);
115     Serialize(archive);
116 }
117 
Serialize(Archive & archive)118 void RSCaptureData::Serialize(Archive& archive)
119 {
120     archive.Serialize(time_);
121 
122     size_t size = properties_.size();
123     archive.Serialize(size);
124 
125     if (archive.IsReading()) {
126         std::string first;
127         std::string second;
128 
129         for (size_t i = 0; i < size; i++) {
130             archive.Serialize(first);
131             archive.Serialize(second);
132 
133             properties_[first] = second;
134         }
135     } else {
136         for (auto& pair : properties_) {
137             archive.Serialize(const_cast<std::string&>(pair.first));
138             archive.Serialize(const_cast<std::string&>(pair.second));
139         }
140     }
141 }
142 
143 } // namespace OHOS::Rosen