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 DRIVE_KIT_RECORD_FIELD_H 17 #define DRIVE_KIT_RECORD_FIELD_H 18 #include <map> 19 #include <string> 20 #include <variant> 21 #include <vector> 22 23 #include "dk_asset.h" 24 #include "dk_error.h" 25 #include "dk_reference.h" 26 27 namespace DriveKit { 28 enum class DKRecordFieldType { 29 FIELD_TYPE_NULL = 0, 30 FIELD_TYPE_INT, 31 FIELD_TYPE_DOUBLE, 32 FIELD_TYPE_STRING, 33 FIELD_TYPE_BOOL, 34 FIELD_TYPE_BLOB, 35 FIELD_TYPE_LIST, 36 FIELD_TYPE_MAP, 37 FIELD_TYPE_ASSET, 38 FIELD_TYPE_REFERENCE, 39 }; 40 class DKRecordField; 41 using DKRecordFieldList = std::vector<DKRecordField>; 42 using DKRecordFieldMap = std::map<std::string, DKRecordField>; 43 using DKFieldValue = std::variant<std::monostate, 44 int64_t, 45 double, 46 std::string, 47 bool, 48 std::vector<uint8_t>, 49 DKRecordFieldList, 50 DKRecordFieldMap, 51 DKAsset, 52 DKReference>; 53 class DKRecordField { 54 public: 55 DKRecordField(); 56 ~DKRecordField(); 57 DKRecordField(DKFieldValue fieldValue) noexcept; 58 DKRecordField(DKRecordField &&recordField) noexcept; 59 DKRecordField(const DKRecordField &recordField); 60 61 explicit DKRecordField(int val); 62 explicit DKRecordField(int64_t val); 63 explicit DKRecordField(double val); 64 explicit DKRecordField(bool val); 65 explicit DKRecordField(const char *val); 66 explicit DKRecordField(const std::string &val); 67 explicit DKRecordField(const std::vector<uint8_t> &val); 68 explicit DKRecordField(DKRecordFieldMap &val); 69 explicit DKRecordField(DKRecordFieldList &val); 70 explicit DKRecordField(DKAsset &val); 71 explicit DKRecordField(DKReference &val); 72 DKRecordField &operator=(DKRecordField &&recordField) noexcept; 73 DKRecordField &operator=(const DKRecordField &recordField); 74 DKRecordFieldType GetType() const; 75 DKFieldValue GetFieldValue() const; 76 void StealDKFiledValue(DKFieldValue &value); 77 DKLocalErrorCode GetInt(int &val) const; 78 DKLocalErrorCode GetLong(int64_t &val) const; 79 DKLocalErrorCode GetDouble(double &val) const; 80 DKLocalErrorCode GetBool(bool &val) const; 81 DKLocalErrorCode GetString(std::string &val) const; 82 DKLocalErrorCode GetBlob(std::vector<uint8_t> &val) const; 83 DKLocalErrorCode GetRecordList(DKRecordFieldList &val) const; 84 DKLocalErrorCode GetRecordMap(DKRecordFieldMap &val) const; 85 DKLocalErrorCode GetAsset(DKAsset &val) const; 86 DKLocalErrorCode GetReference(DKReference &val) const; 87 88 operator int() const 89 { 90 return static_cast<int>(int64_t(std::get<int64_t>(value_))); 91 } int64_t()92 operator int64_t() const 93 { 94 return std::get<int64_t>(value_); 95 } 96 operator double() const 97 { 98 return std::get<double>(value_); 99 } 100 operator bool() const 101 { 102 return std::get<bool>(value_); 103 } string()104 operator std::string() const 105 { 106 return std::get<std::string>(value_); 107 } 108 operator std::vector<uint8_t>() const 109 { 110 return std::get<std::vector<uint8_t>>(value_); 111 } DKRecordFieldList()112 operator DKRecordFieldList() const 113 { 114 return std::get<DKRecordFieldList>(value_); 115 } DKRecordFieldMap()116 operator DKRecordFieldMap() const 117 { 118 return std::get<DKRecordFieldMap>(value_); 119 } DKAsset()120 operator DKAsset() const 121 { 122 return std::get<DKAsset>(value_); 123 } DKReference()124 operator DKReference() const 125 { 126 return std::get<DKReference>(value_); 127 } DKFieldValue()128 operator DKFieldValue() const 129 { 130 return value_; 131 } 132 133 private: 134 DKRecordFieldType type_; 135 DKFieldValue value_; 136 }; 137 } // namespace DriveKit 138 #endif 139