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 NATIVE_RDB_VALUE_OBJECT_H 17 #define NATIVE_RDB_VALUE_OBJECT_H 18 19 #include <string> 20 #include <variant> 21 #include <vector> 22 #include <parcel.h> 23 24 namespace OHOS { 25 namespace NativeRdb { 26 27 enum class ValueObjectType { 28 TYPE_NULL = 0, 29 TYPE_INT, 30 TYPE_INT64, 31 TYPE_DOUBLE, 32 TYPE_STRING, 33 TYPE_BLOB, 34 TYPE_BOOL, 35 }; 36 37 class ValueObject : public virtual OHOS::Parcelable { 38 public: 39 ValueObject(); 40 ~ValueObject(); 41 ValueObject(ValueObject &&valueObject) noexcept; 42 ValueObject(const ValueObject &valueObject); 43 explicit ValueObject(int val); 44 explicit ValueObject(int64_t val); 45 explicit ValueObject(double val); 46 explicit ValueObject(bool val); 47 explicit ValueObject(const std::string &val); 48 explicit ValueObject(const std::vector<uint8_t> &blob); 49 ValueObject &operator=(ValueObject &&valueObject) noexcept; 50 ValueObject &operator=(const ValueObject &valueObject); 51 52 ValueObjectType GetType() const; 53 int GetInt(int &val) const; 54 int GetLong(int64_t &val) const; 55 int GetDouble(double &val) const; 56 int GetBool(bool &val) const; 57 int GetString(std::string &val) const; 58 int GetBlob(std::vector<uint8_t> &val) const; 59 60 bool Marshalling(Parcel &parcel) const override; 61 static ValueObject *Unmarshalling(Parcel &parcel); 62 63 operator int () const 64 { 65 return static_cast<int>(std::get<int64_t>(value)); 66 } int64_t()67 operator int64_t () const 68 { 69 return std::get<int64_t>(value); 70 } 71 operator double () const 72 { 73 return std::get<double>(value); 74 } 75 operator bool () const 76 { 77 return std::get<bool>(value); 78 } string()79 operator std::string () const 80 { 81 return std::get<std::string>(value); 82 } 83 operator std::vector<uint8_t> () const 84 { 85 return std::get<std::vector<uint8_t>>(value); 86 } 87 88 private: 89 ValueObjectType type; 90 std::variant<int64_t, double, std::string, bool, std::vector<uint8_t>> value; 91 }; 92 93 } // namespace NativeRdb 94 } // namespace OHOS 95 #endif 96