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 23 #include "asset_value.h" 24 #include "big_integer.h" 25 #include "rdb_visibility.h" 26 namespace OHOS { 27 namespace NativeRdb { 28 /** 29 * The ValueObject class of RDB. 30 */ 31 class API_EXPORT ValueObject { 32 public: 33 /** 34 * @brief Use Type replace std::variant. 35 */ 36 using Nil = std::monostate; 37 using Blob = std::vector<uint8_t>; 38 using Asset = AssetValue; 39 using Assets = std::vector<Asset>; 40 using BigInt = BigInteger; 41 using FloatVector = std::vector<float>; 42 using Type = std::variant<Nil, int64_t, double, std::string, bool, Blob, Asset, Assets, FloatVector, BigInt>; 43 template<typename Tp, typename... Types> 44 struct index_of : std::integral_constant<size_t, 0> {}; 45 46 template<typename Tp, typename... Types> 47 inline static constexpr size_t index_of_v = index_of<Tp, Types...>::value; 48 49 template<typename Tp, typename First, typename... Rest> 50 struct index_of<Tp, First, Rest...> 51 : std::integral_constant<size_t, std::is_same_v<Tp, First> ? 0 : index_of_v<Tp, Rest...> + 1> {}; 52 53 template<typename... Types> 54 struct variant_size_of { 55 static constexpr size_t value = sizeof...(Types); 56 }; 57 58 template<typename T, typename... Types> 59 struct variant_index_of { 60 static constexpr size_t value = index_of_v<T, Types...>; 61 }; 62 63 template<typename... Types> 64 static variant_size_of<Types...> variant_size_test(const std::variant<Types...> &); 65 66 template<typename T, typename... Types> 67 static variant_index_of<T, Types...> variant_index_test(const T &, const std::variant<Types...> &); 68 69 template<typename T> 70 inline constexpr static int32_t TYPE_INDEX = 71 decltype(variant_index_test(std::declval<T>(), std::declval<Type>()))::value; 72 73 inline constexpr static int32_t TYPE_MAX = decltype(variant_size_test(std::declval<Type>()))::value; 74 75 /** 76 * @brief Indicates the ValueObject {@link ValueObject} type. 77 * */ 78 enum TypeId : int32_t { 79 /** Indicates the ValueObject type is NULL.*/ 80 TYPE_NULL = TYPE_INDEX<Nil>, 81 /** Indicates the ValueObject type is int.*/ 82 TYPE_INT = TYPE_INDEX<int64_t>, 83 /** Indicates the ValueObject type is double.*/ 84 TYPE_DOUBLE = TYPE_INDEX<double>, 85 /** Indicates the ValueObject type is string.*/ 86 TYPE_STRING = TYPE_INDEX<std::string>, 87 /** Indicates the ValueObject type is bool.*/ 88 TYPE_BOOL = TYPE_INDEX<bool>, 89 /** Indicates the ValueObject type is blob.*/ 90 TYPE_BLOB = TYPE_INDEX<Blob>, 91 /** Indicates the ValueObject type is asset.*/ 92 TYPE_ASSET = TYPE_INDEX<Asset>, 93 /** Indicates the ValueObject type is assets.*/ 94 TYPE_ASSETS = TYPE_INDEX<Assets>, 95 /** Indicates the ValueObject type is vecs.*/ 96 TYPE_VECS = TYPE_INDEX<FloatVector>, 97 /** Indicates the ValueObject type is bigint.*/ 98 TYPE_BIGINT = TYPE_INDEX<BigInt>, 99 /** the BUTT.*/ 100 TYPE_BUTT = TYPE_MAX 101 }; 102 Type value; 103 104 /** 105 * @brief convert a std::variant input to another std::variant output with different (..._Types) 106 */ 107 template<typename T> 108 static inline std::enable_if_t<(TYPE_INDEX<T>) < TYPE_MAX, const char *> DeclType() 109 { 110 return DECLARE_TYPES[TYPE_INDEX<T>]; 111 } 112 /** 113 * @brief Constructor. 114 */ 115 API_EXPORT ValueObject(); 116 117 /** 118 * @brief Destructor. 119 */ 120 API_EXPORT ~ValueObject(); 121 122 /** 123 * @brief Constructor. 124 * 125 * A parameterized constructor used to create a ValueObject instance. 126 */ 127 API_EXPORT ValueObject(Type val) noexcept; 128 129 /** 130 * @brief Move constructor. 131 */ 132 API_EXPORT ValueObject(ValueObject &&val) noexcept; 133 134 /** 135 * @brief Copy constructor. 136 */ 137 API_EXPORT ValueObject(const ValueObject &val); 138 139 /** 140 * @brief Constructor. 141 * 142 * This constructor is used to convert the int input parameter to a value of type ValueObject. 143 * 144 * @param val Indicates an int input parameter. 145 */ 146 API_EXPORT ValueObject(int32_t val); 147 148 /** 149 * @brief Constructor. 150 * 151 * This constructor is used to convert the int64_t input parameter to a value of type ValueObject. 152 * 153 * @param val Indicates an int64_t input parameter. 154 */ 155 API_EXPORT ValueObject(int64_t val); 156 157 /** 158 * @brief Constructor. 159 * 160 * This constructor is used to convert the double input parameter to a value of type ValueObject. 161 * 162 * @param val Indicates an double input parameter. 163 */ 164 API_EXPORT ValueObject(double val); 165 166 /** 167 * @brief Constructor. 168 * 169 * This constructor is used to convert the bool input parameter to a value of type ValueObject. 170 * 171 * @param val Indicates an bool input parameter. 172 */ 173 API_EXPORT ValueObject(bool val); 174 175 /** 176 * @brief Constructor. 177 * 178 * This constructor is used to convert the string input parameter to a value of type ValueObject. 179 * 180 * @param val Indicates an string input parameter. 181 */ 182 API_EXPORT ValueObject(std::string val); 183 184 /** 185 * @brief Constructor. 186 * 187 * This constructor is used to convert the const char * input parameter to a value of type ValueObject. 188 * 189 * @param val Indicates an const char * input parameter. 190 */ 191 API_EXPORT ValueObject(const char *val); 192 193 /** 194 * @brief Constructor. 195 * 196 * This constructor is used to convert the vector<uint8_t> input parameter to a value of type ValueObject. 197 * 198 * @param val Indicates an vector<uint8_t> input parameter. 199 */ 200 API_EXPORT ValueObject(const std::vector<uint8_t> &blob); 201 202 /** 203 * @brief Constructor. 204 * 205 * This constructor is used to convert the Asset input parameter to a value of type ValueObject. 206 * 207 * @param val Indicates an Asset input parameter. 208 */ 209 API_EXPORT ValueObject(Asset val); 210 211 /** 212 * @brief Constructor. 213 * 214 * This constructor is used to convert the Assets input parameter to a value of type ValueObject. 215 * 216 * @param val Indicates an Assets input parameter. 217 */ 218 API_EXPORT ValueObject(Assets val); 219 220 /** 221 * @brief Constructor. 222 * 223 * This constructor is used to convert the Assets input parameter to a value of type ValueObject. 224 * 225 * @param val Indicates an Assets input parameter. 226 */ 227 API_EXPORT ValueObject(BigInt val); 228 229 /** 230 * @brief Constructor. 231 * This constructor is used to convert the FloatVector input parameter to a value of type ValueObject. 232 * 233 * @param val Indicates an FloatVector input parameter. 234 */ 235 API_EXPORT ValueObject(FloatVector val); 236 237 /** 238 * @brief Move assignment operator overloaded function. 239 */ 240 API_EXPORT ValueObject &operator=(ValueObject &&valueObject) noexcept; 241 242 /** 243 * @brief Copy assignment operator overloaded function. 244 */ 245 API_EXPORT ValueObject &operator=(const ValueObject &valueObject); 246 247 /** 248 * @brief Obtains the type in this {@code ValueObject} object. 249 */ 250 API_EXPORT TypeId GetType() const; 251 252 /** 253 * @brief Obtains the int value in this {@code ValueObject} object. 254 */ 255 API_EXPORT int GetInt(int &val) const; 256 257 /** 258 * @brief Obtains the long value in this {@code ValueObject} object. 259 */ 260 API_EXPORT int GetLong(int64_t &val) const; 261 262 /** 263 * @brief Obtains the double value in this {@code ValueObject} object. 264 */ 265 API_EXPORT int GetDouble(double &val) const; 266 267 /** 268 * @brief Obtains the bool value in this {@code ValueObject} object. 269 */ 270 API_EXPORT int GetBool(bool &val) const; 271 272 /** 273 * @brief Obtains the string value in this {@code ValueObject} object. 274 */ 275 API_EXPORT int GetString(std::string &val) const; 276 277 /** 278 * @brief Obtains the vector<uint8_t> value in this {@code ValueObject} object. 279 */ 280 API_EXPORT int GetBlob(std::vector<uint8_t> &val) const; 281 282 /** 283 * @brief Obtains the vector<uint8_t> value in this {@code ValueObject} object. 284 */ 285 API_EXPORT int GetAsset(Asset &val) const; 286 287 /** 288 * @brief Obtains the vector<uint8_t> value in this {@code ValueObject} object. 289 */ 290 API_EXPORT int GetAssets(Assets &val) const; 291 292 /** 293 * @brief Obtains the vector<float> value in this {@code ValueObject} object. 294 */ 295 API_EXPORT int GetVecs(FloatVector &val) const; 296 297 /** 298 * @brief Type conversion function. 299 * 300 * @return Returns the int type ValueObject. 301 */ 302 API_EXPORT operator int() const; 303 304 /** 305 * @brief Type conversion function. 306 * 307 * @return Returns the int64_t type ValueObject. 308 */ 309 API_EXPORT operator int64_t() const; 310 311 /** 312 * @brief Type conversion function. 313 * 314 * @return Returns the double type ValueObject. 315 */ 316 API_EXPORT operator double() const; 317 318 /** 319 * @brief Type conversion function. 320 * 321 * @return Returns the bool type ValueObject. 322 */ 323 API_EXPORT operator bool() const; 324 325 /** 326 * @brief Type conversion function. 327 * 328 * @return Returns the string type ValueObject. 329 */ 330 API_EXPORT operator std::string() const; 331 332 /** 333 * @brief Type conversion function. 334 * 335 * @return Returns the vector<uint8_t> type ValueObject. 336 */ 337 API_EXPORT operator Blob() const; 338 339 /** 340 * @brief Type conversion function. 341 * 342 * @return Returns the vector<uint8_t> type ValueObject. 343 */ 344 API_EXPORT operator Asset() const; 345 346 /** 347 * @brief Type conversion function. 348 * 349 * @return Returns the vector<uint8_t> type ValueObject. 350 */ 351 API_EXPORT operator Assets() const; 352 353 /** 354 * @brief Type conversion function. 355 * 356 * @return Returns the vector<float> type ValueObject. 357 */ 358 API_EXPORT operator FloatVector() const; 359 360 /** 361 * @brief Type conversion function. 362 * 363 * @return Returns the BigInt type ValueObject. 364 */ 365 API_EXPORT operator BigInt() const; 366 367 /** 368 * @brief Type conversion function. 369 * 370 * @return Returns the Type type ValueObject. 371 */ 372 operator Type() const 373 { 374 return value; 375 } 376 377 bool operator<(const ValueObject &rhs) const; 378 379 bool operator==(const ValueObject &rhs) const; 380 381 bool operator!=(const ValueObject &rhs) const; 382 383 private: 384 template<class T> 385 int Get(T &output) const; 386 static constexpr const char *DECLARE_TYPES[TypeId::TYPE_BUTT] = { 387 /** Indicates the ValueObject type is NULL.*/ 388 "", 389 /** Indicates the ValueObject type is int.*/ 390 "INT", 391 /** Indicates the ValueObject type is double.*/ 392 "REAL", 393 /** Indicates the ValueObject type is string.*/ 394 "TEXT", 395 /** Indicates the ValueObject type is bool.*/ 396 "BOOL", 397 /** Indicates the ValueObject type is blob.*/ 398 "BLOB", 399 /** Indicates the ValueObject type is asset.*/ 400 "ASSET", 401 /** Indicates the ValueObject type is assets.*/ 402 "ASSETS", 403 /** Indicates the ValueObject type is vecs.*/ 404 "FLOATVECTOR", 405 /** Indicates the ValueObject type is bigint.*/ 406 "UNLIMITED INT" 407 }; 408 }; 409 using ValueObjectType = ValueObject::TypeId; 410 } // namespace NativeRdb 411 } // namespace OHOS 412 #endif 413