• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dk_record_field.h"
17 
18 namespace DriveKit {
DKRecordField()19 DKRecordField::DKRecordField() : type_(DKRecordFieldType::FIELD_TYPE_NULL) {}
20 
~DKRecordField()21 DKRecordField::~DKRecordField() {}
22 
DKRecordField(DKFieldValue fieldValue)23 DKRecordField::DKRecordField(DKFieldValue fieldValue) noexcept : value_(std::move(fieldValue))
24 {
25     type_ = DKRecordFieldType(value_.index());
26 }
27 
DKRecordField(DKRecordField && recordField)28 DKRecordField::DKRecordField(DKRecordField &&recordField) noexcept
29 {
30     if (this == &recordField) {
31         return;
32     }
33     type_ = recordField.type_;
34     value_ = std::move(recordField.value_);
35     recordField.type_ = DKRecordFieldType::FIELD_TYPE_NULL;
36 }
37 
DKRecordField(const DKRecordField & recordField)38 DKRecordField::DKRecordField(const DKRecordField &recordField)
39 {
40     if (this == &recordField) {
41         return;
42     }
43     type_ = recordField.type_;
44     value_ = recordField.value_;
45 }
46 
DKRecordField(int val)47 DKRecordField::DKRecordField(int val) : type_(DKRecordFieldType::FIELD_TYPE_INT)
48 {
49     value_ = static_cast<int64_t>(val);
50 }
DKRecordField(int64_t val)51 DKRecordField::DKRecordField(int64_t val) : type_(DKRecordFieldType::FIELD_TYPE_INT)
52 {
53     value_ = val;
54 }
DKRecordField(double val)55 DKRecordField::DKRecordField(double val) : type_(DKRecordFieldType::FIELD_TYPE_DOUBLE)
56 {
57     value_ = val;
58 }
DKRecordField(bool val)59 DKRecordField::DKRecordField(bool val) : type_(DKRecordFieldType::FIELD_TYPE_BOOL)
60 {
61     value_ = val;
62 }
DKRecordField(const char * val)63 DKRecordField::DKRecordField(const char *val) : type_(DKRecordFieldType::FIELD_TYPE_STRING)
64 {
65     value_ = std::string(val);
66 }
DKRecordField(const std::string & val)67 DKRecordField::DKRecordField(const std::string &val) : type_(DKRecordFieldType::FIELD_TYPE_STRING)
68 {
69     value_ = val;
70 }
DKRecordField(const std::vector<uint8_t> & val)71 DKRecordField::DKRecordField(const std::vector<uint8_t> &val) : type_(DKRecordFieldType::FIELD_TYPE_BLOB)
72 {
73     std::vector<uint8_t> blob = val;
74     value_ = blob;
75 }
DKRecordField(DKRecordFieldMap & val)76 DKRecordField::DKRecordField(DKRecordFieldMap &val) : type_(DKRecordFieldType::FIELD_TYPE_MAP)
77 {
78     value_ = val;
79 }
DKRecordField(DKRecordFieldList & val)80 DKRecordField::DKRecordField(DKRecordFieldList &val) : type_(DKRecordFieldType::FIELD_TYPE_LIST)
81 {
82     value_ = val;
83 }
DKRecordField(DKAsset & val)84 DKRecordField::DKRecordField(DKAsset &val) : type_(DKRecordFieldType::FIELD_TYPE_ASSET)
85 {
86     value_ = val;
87 }
DKRecordField(DKReference & val)88 DKRecordField::DKRecordField(DKReference &val) : type_(DKRecordFieldType::FIELD_TYPE_REFERENCE)
89 {
90     value_ = val;
91 }
operator =(DKRecordField && recordField)92 DKRecordField &DKRecordField::operator=(DKRecordField &&recordField) noexcept
93 {
94     if (this == &recordField) {
95         return *this;
96     }
97     type_ = recordField.type_;
98     value_ = std::move(recordField.value_);
99     recordField.type_ = DKRecordFieldType::FIELD_TYPE_NULL;
100     return *this;
101 }
operator =(const DKRecordField & recordField)102 DKRecordField &DKRecordField::operator=(const DKRecordField &recordField)
103 {
104     if (this == &recordField) {
105         return *this;
106     }
107     type_ = recordField.type_;
108     value_ = recordField.value_;
109     return *this;
110 }
GetType() const111 DKRecordFieldType DKRecordField::GetType() const
112 {
113     return type_;
114 }
GetFieldValue() const115 DKFieldValue DKRecordField::GetFieldValue() const
116 {
117     return value_;
118 }
StealDKFiledValue(DKFieldValue & value)119 void DKRecordField::StealDKFiledValue(DKFieldValue &value)
120 {
121     value = std::move(value_);
122     type_ = DKRecordFieldType::FIELD_TYPE_NULL;
123     return;
124 }
GetInt(int & val) const125 DKLocalErrorCode DKRecordField::GetInt(int &val) const
126 {
127     if (type_ != DKRecordFieldType::FIELD_TYPE_INT) {
128         return DKLocalErrorCode::DATA_TYPE_ERROR;
129     }
130 
131     int64_t v = std::get<int64_t>(value_);
132     val = static_cast<int>(v);
133     return DKLocalErrorCode::NO_ERROR;
134 }
GetLong(int64_t & val) const135 DKLocalErrorCode DKRecordField::GetLong(int64_t &val) const
136 {
137     if (type_ != DKRecordFieldType::FIELD_TYPE_INT) {
138         return DKLocalErrorCode::DATA_TYPE_ERROR;
139     }
140 
141     val = std::get<int64_t>(value_);
142     return DKLocalErrorCode::NO_ERROR;
143 }
GetDouble(double & val) const144 DKLocalErrorCode DKRecordField::GetDouble(double &val) const
145 {
146     if (type_ != DKRecordFieldType::FIELD_TYPE_DOUBLE) {
147         return DKLocalErrorCode::DATA_TYPE_ERROR;
148     }
149 
150     val = std::get<double>(value_);
151     return DKLocalErrorCode::NO_ERROR;
152 }
GetBool(bool & val) const153 DKLocalErrorCode DKRecordField::GetBool(bool &val) const
154 {
155     if (type_ != DKRecordFieldType::FIELD_TYPE_BOOL) {
156         return DKLocalErrorCode::DATA_TYPE_ERROR;
157     }
158 
159     val = std::get<bool>(value_);
160     return DKLocalErrorCode::NO_ERROR;
161 }
GetString(std::string & val) const162 DKLocalErrorCode DKRecordField::GetString(std::string &val) const
163 {
164     if (type_ != DKRecordFieldType::FIELD_TYPE_STRING) {
165         return DKLocalErrorCode::DATA_TYPE_ERROR;
166     }
167     val = std::get<std::string>(value_);
168     return DKLocalErrorCode::NO_ERROR;
169 }
GetBlob(std::vector<uint8_t> & val) const170 DKLocalErrorCode DKRecordField::GetBlob(std::vector<uint8_t> &val) const
171 {
172     if (type_ != DKRecordFieldType::FIELD_TYPE_BLOB) {
173         return DKLocalErrorCode::DATA_TYPE_ERROR;
174     }
175     val = std::get<std::vector<uint8_t>>(value_);
176     return DKLocalErrorCode::NO_ERROR;
177 }
GetRecordList(DKRecordFieldList & val) const178 DKLocalErrorCode DKRecordField::GetRecordList(DKRecordFieldList &val) const
179 {
180     if (type_ != DKRecordFieldType::FIELD_TYPE_LIST) {
181         return DKLocalErrorCode::DATA_TYPE_ERROR;
182     }
183     val = std::get<DKRecordFieldList>(value_);
184     return DKLocalErrorCode::NO_ERROR;
185 }
GetRecordMap(DKRecordFieldMap & val) const186 DKLocalErrorCode DKRecordField::GetRecordMap(DKRecordFieldMap &val) const
187 {
188     if (type_ != DKRecordFieldType::FIELD_TYPE_MAP) {
189         return DKLocalErrorCode::DATA_TYPE_ERROR;
190     }
191     val = std::get<DKRecordFieldMap>(value_);
192     return DKLocalErrorCode::NO_ERROR;
193 }
GetAsset(DKAsset & val) const194 DKLocalErrorCode DKRecordField::GetAsset(DKAsset &val) const
195 {
196     if (type_ != DKRecordFieldType::FIELD_TYPE_ASSET) {
197         return DKLocalErrorCode::DATA_TYPE_ERROR;
198     }
199     val = std::get<DKAsset>(value_);
200     return DKLocalErrorCode::NO_ERROR;
201 }
GetReference(DKReference & val) const202 DKLocalErrorCode DKRecordField::GetReference(DKReference &val) const
203 {
204     if (type_ != DKRecordFieldType::FIELD_TYPE_REFERENCE) {
205         return DKLocalErrorCode::DATA_TYPE_ERROR;
206     }
207     val = std::get<DKReference>(value_);
208     return DKLocalErrorCode::NO_ERROR;
209 }
210 }