• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FORMAT_H
17 #define FORMAT_H
18 
19 #include <string>
20 #include <map>
21 #include <vector>
22 
23 namespace OHOS {
24 namespace Media {
25 enum FormatDataType : uint32_t {
26     /* None */
27     FORMAT_TYPE_NONE,
28     /* Int32 */
29     FORMAT_TYPE_INT32,
30     /* Int64 */
31     FORMAT_TYPE_INT64,
32     /* Float */
33     FORMAT_TYPE_FLOAT,
34     /* Double */
35     FORMAT_TYPE_DOUBLE,
36     /* String */
37     FORMAT_TYPE_STRING,
38     /* Addr */
39     FORMAT_TYPE_ADDR,
40 };
41 
42 struct FormatData {
43     FormatDataType type = FORMAT_TYPE_NONE;
44     union Val {
45         int32_t int32Val;
46         int64_t int64Val;
47         float floatVal;
48         double doubleVal;
49     } val = {0};
50     std::string stringVal = "";
51     uint8_t *addr = nullptr;
52     size_t size = 0;
53 };
54 
55 class __attribute__((visibility("default"))) Format {
56 public:
57     Format() = default;
58     ~Format();
59 
60     Format(const Format &rhs);
61     Format(Format &&rhs) noexcept;
62     Format &operator=(const Format &rhs);
63     Format &operator=(Format &&rhs) noexcept;
64 
65     /**
66      * @brief Sets metadata of the integer type.
67      *
68      * @param key Indicates the metadata key.
69      * @param value Indicates the metadata value, which is a 32-bit integer.
70      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
71      * @since 1.0
72      * @version 1.0
73      */
74     bool PutIntValue(const std::string_view &key, int32_t value);
75 
76     /**
77      * @brief Sets metadata of the long integer type.
78      *
79      * @param key Indicates the metadata key.
80      * @param value Indicates the metadata value, which is a 64-bit integer.
81      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
82      * @since 1.0
83      * @version 1.0
84      */
85     bool PutLongValue(const std::string_view &key, int64_t value);
86 
87     /**
88      * @brief Sets metadata of the single-precision floating-point type.
89      *
90      * @param key Indicates the metadata key.
91      * @param value Indicates the metadata value, which is a single-precision floating-point number.
92      * @return Returns <b>true</b> if the metadata is successfully set; returns <b>false</b> otherwise.
93      * @since 1.0
94      * @version 1.0
95      */
96     bool PutFloatValue(const std::string_view &key, float value);
97 
98     /**
99      * @brief Sets metadata of the double-precision floating-point type.
100      *
101      * @param key Indicates the metadata key.
102      * @param value Indicates the metadata value, which is a double-precision floating-point number.
103      * @return Returns <b>true</b> if the setting is successful; returns <b>false</b> otherwise.
104      * @since 1.0
105      * @version 1.0
106      */
107     bool PutDoubleValue(const std::string_view &key, double value);
108 
109     /**
110      * @brief Sets metadata of the string type.
111      *
112      * @param key Indicates the metadata key.
113      * @param value Indicates the metadata value, which is a string.
114      * @return Returns <b>true</b> if the metadata is successfully set; returns <b>false</b> otherwise.
115      * @since 1.0
116      * @version 1.0
117      */
118     bool PutStringValue(const std::string_view &key, const std::string_view &value);
119 
120     /**
121      * @brief Sets metadata of the string type.
122      *
123      * @param key Indicates the metadata key.
124      * @param addr Indicates the metadata addr, which is a uint8_t *.
125      * @param size Indicates the metadata addr size, which is a size_t.
126      * @return Returns <b>true</b> if the metadata is successfully set; returns <b>false</b> otherwise.
127      * @since 1.0
128      * @version 1.0
129      */
130     bool PutBuffer(const std::string_view &key, const uint8_t *addr, size_t size);
131 
132     /**
133      * @brief Sets metadata of the format vector type.
134      *
135      * @param key Indicates the metadata key.
136      * @param value Indicates the metadata value, which is a format vector.
137      * @return Returns <b>true</b> if the format vector is successfully set; returns <b>false</b> otherwise.
138      * @since 1.0
139      * @version 1.0
140      */
141     bool PutFormatVector(const std::string_view &key, std::vector<Format> &value);
142 
143     /**
144      * @brief Obtains the metadata value of the integer type.
145      *
146      * @param key Indicates the metadata key.
147      * @param value Indicates the metadata value to obtain, which is a 32-bit integer.
148      * @return Returns <b>true</b> if the integer is successfully obtained; returns <b>false</b> otherwise.
149      * @since 1.0
150      * @version 1.0
151      */
152     bool GetIntValue(const std::string_view &key, int32_t &value) const;
153 
154     /**
155      * @brief Obtains the metadata value of the long integer type.
156      *
157      * @param key Indicates the metadata key.
158      * @param value Indicates the metadata value to obtain, which is a 64-bit long integer.
159      * @return Returns <b>true</b> if the integer is successfully obtained; returns <b>false</b> otherwise.
160      * @since 1.0
161      * @version 1.0
162      */
163     bool GetLongValue(const std::string_view &key, int64_t &value) const;
164 
165     /**
166      * @brief Obtains the metadata value of the single-precision floating-point type.
167      *
168      * @param key Indicates the metadata key.
169      * @param value Indicates the metadata value to obtain, which is a single-precision floating-point number.
170      * @return Returns <b>true</b> if the single-precision number is successfully obtained; returns
171      * <b>false</b> otherwise.
172      * @since 1.0
173      * @version 1.0
174      */
175     bool GetFloatValue(const std::string_view &key, float &value) const;
176 
177     /**
178      * @brief Obtains the metadata value of the double-precision floating-point type.
179      *
180      * @param key Indicates the metadata key.
181      * @param value Indicates the metadata value to obtain, which is a double-precision floating-point number.
182      * @return Returns <b>true</b> if the double-precision number is successfully obtained; returns
183      * <b>false</b> otherwise.
184      * @since 1.0
185      * @version 1.0
186      */
187     bool GetDoubleValue(const std::string_view &key, double &value) const;
188 
189     /**
190      * @brief Obtains the metadata value of the string type.
191      *
192      * @param key Indicates the metadata key.
193      * @param value Indicates the metadata value to obtain, which is a string.
194      * @return Returns <b>true</b> if the string is successfully obtained; returns <b>false</b> otherwise.
195      * @since 1.0
196      * @version 1.0
197      */
198     bool GetStringValue(const std::string_view &key, std::string &value) const;
199 
200     /**
201      * @brief Obtains the metadata value of the string type.
202      *
203      * @param key Indicates the metadata key.
204      * @param addr Indicates the metadata addr to obtain, which is a uint8_t **.
205      * @param size Indicates the metadata addr size to obtain, which is a size_t.
206      * @return Returns <b>true</b> if the string is successfully obtained; returns <b>false</b> otherwise.
207      * @since 1.0
208      * @version 1.0
209      */
210     bool GetBuffer(const std::string_view &key, uint8_t **addr, size_t &size) const;
211 
212     /**
213      * @brief Obtains the metadata value of the format vector type.
214      *
215      * @param key Indicates the metadata key.
216      * @param value Indicates the metadata value to obtain, which is a format vector.
217      * @return Returns <b>true</b> if the format vector is successfully obtained; returns <b>false</b> otherwise.
218      * @since 1.0
219      * @version 1.0
220      */
221     bool GetFormatVector(const std::string_view &key, std::vector<Format> &value) const;
222 
223     /**
224      * @brief Query whether the key exists in this Format.
225      *
226      * @param key Indicates the metadata key.
227      * @return true
228      * @return false
229      */
230     bool ContainKey(const std::string_view &key) const;
231 
232     /**
233      * @brief Get the value type for the key if the key exists in this Format.
234      *
235      * @param key Indicates the metadata key.
236      * @return FormatDataType. If the key does not exists, return FORMAT_TYPE_NONE.
237      */
238     FormatDataType GetValueType(const std::string_view &key) const;
239 
240     /**
241      * @brief Remove the key from the Format
242      *
243      * @param keys the key will be removed.
244      */
245     void RemoveKey(const std::string_view &key);
246 
247     /**
248      * @brief A trick to enable the comparision between the std::string and std::string_view for
249      * std::map, the trick called Transparent Comparator.
250      *
251      */
252     using FormatDataMap = std::map<std::string, FormatData, std::less<>>;
253 
254     /**
255      * @brief Obtains the metadata map.
256      *
257      * @return Returns the map object.
258      * @since 1.0
259      * @version 1.0
260      */
261     const FormatDataMap &GetFormatMap() const;
262 
263     /**
264      * @brief A trick to enable the comparision between the std::string and std::string_view for
265      * std::map, the trick called Transparent Comparator.
266      *
267      */
268     using FormatVectorMap = std::map<std::string, std::vector<Format>, std::less<>>;
269 
270     /**
271      * @brief Obtains the metadata vector map.
272      *
273      * @return Returns the map object.
274      * @since 1.0
275      * @version 1.0
276      */
277     const FormatVectorMap &GetFormatVectorMap() const;
278 
279     /**
280      * @brief Convert the metadata map to string.
281      *
282      * @return Returns a converted string.
283      * @since 1.0
284      * @version 1.0
285      */
286     std::string Stringify() const;
287 
288 private:
289     FormatDataMap formatMap_;
290     FormatVectorMap formatVecMap_;
291 };
292 } // namespace Media
293 } // namespace OHOS
294 #endif // FORMAT_H
295