1 /*
2  * Copyright (c) 2022 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 "scan_option_value.h"
17 #include "scan_log.h"
18 
19 namespace OHOS::Scan {
ScanOptionValue()20 ScanOptionValue::ScanOptionValue() : valueType_(SCAN_VALUE_NONE
21     ), valueSize_(0), numValue_(0), strValue_(""), boolValue_(false)
22 {
23     numListValue_.clear();
24 }
25 
ScanOptionValue(const ScanOptionValue & right)26 ScanOptionValue::ScanOptionValue(const ScanOptionValue &right)
27 {
28     valueType_ = right.valueType_;
29     valueSize_ = right.valueSize_;
30     numValue_ = right.numValue_;
31     strValue_ = right.strValue_;
32     boolValue_ = right.boolValue_;
33     numListValue_.assign(right.numListValue_.begin(), right.numListValue_.end());
34 }
35 
operator =(const ScanOptionValue & right)36 ScanOptionValue &ScanOptionValue::operator=(const ScanOptionValue &right)
37 {
38     if (this != &right) {
39         valueType_ = right.valueType_;
40         valueSize_ = right.valueSize_;
41         numValue_ = right.numValue_;
42         strValue_ = right.strValue_;
43         boolValue_ = right.boolValue_;
44         numListValue_.assign(right.numListValue_.begin(), right.numListValue_.end());
45     }
46     return *this;
47 }
48 
~ScanOptionValue()49 ScanOptionValue::~ScanOptionValue()
50 {}
51 
Reset()52 void ScanOptionValue::Reset()
53 {
54     valueType_ = SCAN_VALUE_NONE;
55     valueSize_ = 0;
56     numValue_ = 0;
57     strValue_ = "";
58     boolValue_ = false;
59     numListValue_.clear();
60 }
61 
SetScanOptionValueType(const ScanOptionValueType & valueType)62 void ScanOptionValue::SetScanOptionValueType(const ScanOptionValueType &valueType)
63 {
64     valueType_ = valueType;
65 }
66 
SetValueSize(const int32_t & valueSize)67 void ScanOptionValue::SetValueSize(const int32_t &valueSize)
68 {
69     valueSize_ = valueSize;
70 }
71 
SetNumValue(const int32_t & numValue)72 void ScanOptionValue::SetNumValue(const int32_t &numValue)
73 {
74     numValue_ = numValue;
75 }
76 
SetNumListValue(const std::vector<int32_t> & numListValue)77 void ScanOptionValue::SetNumListValue(const std::vector<int32_t> &numListValue)
78 {
79     numListValue_.assign(numListValue.begin(), numListValue.end());
80 }
81 
SetStrValue(const std::string & strValue)82 void ScanOptionValue::SetStrValue(const std::string &strValue)
83 {
84     strValue_ = strValue;
85 }
86 
SetBoolValue(const bool & boolValue)87 void ScanOptionValue::SetBoolValue(const bool &boolValue)
88 {
89     boolValue_ = boolValue;
90 }
91 
GetScanOptionValueType() const92 ScanOptionValueType ScanOptionValue::GetScanOptionValueType() const
93 {
94     return valueType_;
95 }
96 
GetValueSize() const97 int32_t ScanOptionValue::GetValueSize() const
98 {
99     return valueSize_;
100 }
101 
GetNumValue() const102 int32_t ScanOptionValue::GetNumValue() const
103 {
104     return numValue_;
105 }
106 
GetNumListValue(std::vector<int32_t> & numListValue) const107 void ScanOptionValue::GetNumListValue(std::vector<int32_t> &numListValue) const
108 {
109     numListValue.assign(numListValue_.begin(), numListValue_.end());
110 }
111 
GetStrValue() const112 std::string ScanOptionValue::GetStrValue() const
113 {
114     return strValue_;
115 }
116 
GetBoolValue() const117 bool ScanOptionValue::GetBoolValue() const
118 {
119     return boolValue_;
120 }
121 
ReadFromParcel(Parcel & parcel)122 void ScanOptionValue::ReadFromParcel(Parcel &parcel)
123 {
124     SetScanOptionValueType((ScanOptionValueType)parcel.ReadUint32());
125     SetValueSize(parcel.ReadInt32());
126     if (valueType_ == SCAN_VALUE_NUM) {
127         SetNumValue(parcel.ReadInt32());
128     } else if (valueType_ == SCAN_VALUE_NUM_LIST) {
129         parcel.ReadInt32Vector(&numListValue_);
130     } else if (valueType_ == SCAN_VALUE_STR) {
131         SetStrValue(parcel.ReadString());
132     } else if (valueType_ == SCAN_VALUE_BOOL) {
133         SetBoolValue(parcel.ReadBool());
134     }
135 }
136 
Marshalling(Parcel & parcel) const137 bool ScanOptionValue::Marshalling(Parcel &parcel) const
138 {
139     parcel.WriteUint32(valueType_);
140     parcel.WriteInt32(valueSize_);
141     if (valueType_ == SCAN_VALUE_NUM) {
142         parcel.WriteInt32(numValue_);
143     } else if (valueType_ == SCAN_VALUE_NUM_LIST) {
144         parcel.WriteInt32Vector(numListValue_);
145     } else if (valueType_ == SCAN_VALUE_STR) {
146         parcel.WriteString(strValue_);
147     } else if (valueType_ == SCAN_VALUE_BOOL) {
148         parcel.WriteBool(boolValue_);
149     }
150     return true;
151 }
152 
Unmarshalling(Parcel & parcel)153 std::shared_ptr<ScanOptionValue> ScanOptionValue::Unmarshalling(Parcel &parcel)
154 {
155     auto nativeObj = std::make_shared<ScanOptionValue>();
156     nativeObj->ReadFromParcel(parcel);
157     return nativeObj;
158 }
159 
Dump()160 void ScanOptionValue::Dump()
161 {
162     SCAN_HILOGE("ValueType = %{public}d", valueType_);
163     SCAN_HILOGE("ValueSize = %{public}d", valueSize_);
164     if (valueType_ == SCAN_VALUE_NUM) {
165         SCAN_HILOGE("NumValue = %{public}d", numValue_);
166     } else if (valueType_ == SCAN_VALUE_NUM_LIST) {
167         for (auto &num : numListValue_) {
168             SCAN_HILOGE("NumValue = %{public}d", num);
169         }
170     } else if (valueType_ == SCAN_VALUE_STR) {
171         SCAN_HILOGE("StrValue = %{public}s", strValue_.c_str());
172     } else if (valueType_ == SCAN_VALUE_BOOL) {
173         SCAN_HILOGE("BoolValue = %{public}d", boolValue_);
174     }
175 }
176 } // namespace OHOS::Scan
177