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 #ifndef DATASHARE_PREDICATES_OBJECTS_H
17 #define DATASHARE_PREDICATES_OBJECTS_H
18 
19 #include <variant>
20 #include <string>
21 #include <vector>
22 
23 namespace OHOS {
24 namespace DataShare {
25 /**
26  * @brief DataShare Predicates Object Type .
27  */
28 enum class DataSharePredicatesObjectsType {
29     /** Predicates Object Types is null.*/
30     TYPE_NULL = 0x06,
31     /** Predicates Object Types is int.*/
32     TYPE_INT_VECTOR,
33     /** Predicates Object Types is long.*/
34     TYPE_LONG_VECTOR,
35     /** Predicates Object Types is double.*/
36     TYPE_DOUBLE_VECTOR,
37     /** Predicates Object Types is string.*/
38     TYPE_STRING_VECTOR,
39 };
40 
41 /**
42  * @brief Use ObjectType replace DataSharePredicatesObjectsType namespace.
43  */
44 using ObjectsType = DataSharePredicatesObjectsType;
45 class MutliValue {
46 public:
47 
48     /**
49      * @brief Use Type replace variant namespace.
50      */
51     using Type = std::variant<std::monostate, std::vector<int>, std::vector<int64_t>,
52         std::vector<std::string>, std::vector<double>>;
53     Type value;
54 
55     /**
56      * @brief Constructor.
57      */
58     MutliValue() = default;
59 
60     /**
61      * @brief Destructor.
62      */
63     ~MutliValue() = default;
64 
65     /**
66      * @brief Move Constructor.
67      */
MutliValue(MutliValue::Type val)68     MutliValue(MutliValue::Type val) noexcept : value(std::move(val))
69     {
70     }
71 
72     /**
73      * @brief Copy constructor.
74      */
MutliValue(const MutliValue & val)75     MutliValue(const MutliValue &val) : value(val.value) {}
76     MutliValue &operator=(MutliValue &&object) noexcept
77     {
78         if (this == &object) {
79             return *this;
80         }
81         value = std::move(object.value);
82         return *this;
83     }
84     MutliValue &operator=(const MutliValue &object)
85     {
86         if (this == &object) {
87             return *this;
88         }
89         value = object.value;
90         return *this;
91     }
92 
93     /**
94      * @brief constructor.
95      *
96      * @param int Specifies the parameter of the type.
97      */
MutliValue(const std::vector<int> & val)98     MutliValue(const std::vector<int> &val) : value(val) {}
99 
100     /**
101      * @brief constructor.
102      *
103      * @param int64_t Specifies the parameter of the type.
104      */
MutliValue(const std::vector<int64_t> & val)105     MutliValue(const std::vector<int64_t> &val) : value(val) {}
106 
107     /**
108      * @brief constructor.
109      *
110      * @param double Specifies the parameter of the type.
111      */
MutliValue(const std::vector<double> & val)112     MutliValue(const std::vector<double> &val) : value(val) {}
113 
114     /**
115      * @brief constructor.
116      *
117      * @param string Specifies the parameter of the type.
118      */
MutliValue(const std::vector<std::string> & val)119     MutliValue(const std::vector<std::string> &val) : value(val) {}
120 
vector()121     operator std::vector<int> () const
122     {
123         if (std::get_if<std::vector<int>>(&value) != nullptr) {
124             return std::get<std::vector<int>>(value);
125         } else {
126             return {};
127         }
128     }
vector()129     operator std::vector<int64_t> () const
130     {
131         if (std::get_if<std::vector<int64_t>>(&value) != nullptr) {
132             return std::get<std::vector<int64_t>>(value);
133         } else {
134             return {};
135         }
136     }
vector()137     operator std::vector<double> () const
138     {
139         if (std::get_if<std::vector<double>>(&value) != nullptr) {
140             return std::get<std::vector<double>>(value);
141         } else {
142             return {};
143         }
144     }
vector()145     operator std::vector<std::string> () const
146     {
147         if (std::get_if<std::vector<std::string>>(&value) != nullptr) {
148             return std::get<std::vector<std::string>>(value);
149         } else {
150             return {};
151         }
152     }
153 };
154 } // namespace DataShare
155 } // namespace OHOS
156 #endif