Merge "Add bundle cpp APIs" into tizen
[platform/core/base/bundle.git] / include / bundle_cpp.h
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef BUNDLE_CPP_H_
18 #define BUNDLE_CPP_H_
19
20 /**
21  * @file bundle_cpp.h
22  * @brief This file declares API of the bundle C++ library.
23  */
24
25 /**
26  * @addtogroup CORE_LIB_BUNDLE_CPP_MODULE
27  * @{
28  */
29
30 #include <bundle.h>
31 #include <dlog.h>
32
33 #include <cstdio>
34 #include <memory>
35 #include <string>
36 #include <utility>
37 #include <vector>
38
39 #ifdef LOG_TAG
40 #undef LOG_TAG
41 #endif
42
43 #define LOG_TAG "BUNDLE"
44
45 #ifndef EXPORT_API
46 #define EXPORT_API __attribute__((visibility("default")))
47 #endif
48
49 namespace tizen_base {
50
51 /**
52  * @brief The class for bundle APIs.
53  * @since_tizen 5.5
54  */
55 class EXPORT_API Bundle final {
56  public:
57   /**
58    * @brief The type for raw bundle.
59    * @since_tizen 5.5
60    */
61   using BundleRaw =
62       std::pair<std::unique_ptr<bundle_raw, decltype(std::free)*>, int>;
63
64   /**
65    * @brief The class for information of keys.
66    * @since_tizen 5.5
67    */
68   class KeyInfo final {
69    public:
70     /**
71      * @brief Constructor.
72      * @since_tizen 5.5
73      * @param[in] handle The handle for type bundle_keyval_t
74      * @param[in] name The key string
75      */
76     KeyInfo(const bundle_keyval_t* handle,  std::string name);
77
78     /**
79      * @brief Constructor.
80      * @since_tizen 5.5
81      */
82     KeyInfo();
83
84     /**
85      * @brief Destructor.
86      * @since_tizen 5.5
87      */
88     ~KeyInfo();
89
90     /**
91      * @brief Copy-constructor.
92      * @since_tizen 5.5
93      * @param[in] b The object to copy
94     */
95     KeyInfo(const KeyInfo& b);
96
97     /**
98      * @brief Assignment.
99      * @since_tizen 5.5
100      * @param[in] b The object to copy
101      */
102     KeyInfo& operator = (const KeyInfo& b);
103
104     /**
105      * @brief Move-constructor.
106      * @since_tizen 5.5
107      * @param[in] b The object to move
108     */
109     KeyInfo(KeyInfo&& b) noexcept;
110
111     /**
112      * @brief Assignment.
113      * @since_tizen 5.5
114      * @param[in] b The object to move
115      */
116     KeyInfo& operator = (KeyInfo&& b) noexcept;
117
118     /**
119      * @brief Gets the type of a key-value pair.
120      * @since_tizen 5.5
121      * @return The type
122      */
123     bundle_type GetType() const;
124
125     /**
126      * @brief Determines whether the type of a key-value pair is an array.
127      * @since_tizen 5.5
128      * @return True when it is an array
129      */
130     bool IsArray() const;
131
132     /**
133      * @brief Gets the key string.
134      * @since_tizen 5.5
135      * @return The key string
136      */
137     const std::string& GetName() const;
138
139    private:
140     class Impl;
141     std::unique_ptr<Impl> impl_;
142   };
143
144   /**
145    * @brief Constructor.
146    * @since_tizen 5.5
147    */
148   Bundle();
149
150   /**
151    * @brief Constructor.
152    * @since_tizen 5.5
153    * @param[in] raw The object for BundleRaw
154    */
155   explicit Bundle(BundleRaw raw);
156
157   /**
158    * @brief Constructor.
159    * @since_tizen 5.5
160    * @param[in] raw The string object for raw bundle
161    */
162   explicit Bundle(const std::string& raw);
163
164   /**
165    * @brief Constructor.
166    * @since_tizen 5.5
167    * @param[in] b The handle for bundle
168    * @param[in] copy True if this object wants to copy it from the handle
169    * @param[in] own True if this object owns the handle
170    */
171   explicit Bundle(bundle* b, bool copy = true, bool own = true);
172
173   /**
174    * @brief Destructor.
175    * @since_tizen 5.5
176    */
177   ~Bundle();
178
179   /**
180    * @brief Copy-constructor.
181    * @since_tizen 5.5
182    * @param[in] b The object to copy
183    */
184   Bundle(const Bundle& b);
185
186   /**
187    * @brief Assignment.
188    * @since_tizen 5.5
189    * @param[in] b The object to copy
190    */
191   Bundle& operator = (const Bundle& b);
192
193   /**
194    * @brief Move-constructor.
195    * @since_tizen 5.5
196    * @param[in] b The object to move
197    */
198   Bundle(Bundle&& b) noexcept;
199
200   /**
201    * @brief Assignment.
202    * @since_tizen 5.5
203    * @param[in] b The object to move
204    */
205   Bundle& operator = (Bundle&& b) noexcept;
206
207   /**
208    * @brief Gets keys in bundle object.
209    * @since_tizen 5.5
210    * @return A string array of object KeyInfo
211   */
212   std::vector<KeyInfo> GetKeys();
213
214   /**
215    * @brief Adds a string type key-value pair into a bundle.
216    * @since_tizen 5.5
217    * @param[in] key The string key
218    * @param[in] val The string value
219    * @return The operation result
220    * @retval BUNDLE_ERROR_NONE Success
221    * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
222    * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
223    * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
224   */
225   int Add(const std::string& key, const std::string& val);
226
227   /**
228    * @brief Adds a string type key-value pair into a bundle.
229    * @since_tizen 5.5
230    * @param[in] key The string key
231    * @param[in] val The array of strings
232    * @return The operation result
233    * @retval BUNDLE_ERROR_NONE Success
234    * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
235    * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
236    * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
237   */
238   int Add(const std::string& key, const std::vector<std::string>& val);
239
240   /**
241    * @brief Adds a string type key-value pair into a bundle.
242    * @since_tizen 5.5
243    * @param[in] key The string key
244    * @param[in] val The array of bytes
245    * @return The operation result
246    * @retval BUNDLE_ERROR_NONE Success
247    * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
248    * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
249    * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
250   */
251   int Add(const std::string& key, const std::vector<unsigned char>& val);
252
253   /**
254    * @brief Deletes a key-value object with the given key.
255    * @since_tizen 5.5
256    * @param[in] key The string key
257    * @return The operation result
258    * @retval BUNDLE_ERROR_NONE Success
259    * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
260    * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
261   */
262   int Delete(const std::string& key);
263
264   /**
265    * @brief Gets a string from the key.
266    * @since_tizen 5.5
267    * @param[in] key The string key
268    * @return The string
269   */
270   std::string GetString(const std::string& key) const;
271
272   /**
273    * @brief Gets strings from the key.
274    * @since_tizen 5.5
275    * @param[in] key The string key
276    * @return The array of strings
277   */
278   std::vector<std::string> GetStringArray(const std::string& key) const;
279
280   /**
281    * @brief Gets bytes from the key.
282    * @since_tizen 5.5
283    * @param[in] key The string key
284    * @return Bytes
285   */
286   std::vector<unsigned char> GetByte(const std::string& key) const;
287
288   /**
289    * @brief Converts this object to BundleRaw type.
290    * @since_tizen 5.5
291    * @return The object of BundleRaw
292   */
293   BundleRaw ToRaw();
294
295   /**
296    * @brief Gets the count of keys.
297    * @since_tizen 5.5
298    * @return The count
299   */
300   int GetCount() const;
301
302   /**
303    * @brief Gets the data type from the key.
304    * @since_tizen 5.5
305    * @param[in] key The string key
306    * @return The data type
307   */
308   bundle_type GetType(const std::string& key) const;
309
310   /**
311    * @brief Gets the handle for bundle APIs.
312    * @since_tizen 5.5
313    * @return The handle for bundle
314   */
315   bundle* GetHandle() const;
316
317   /**
318    * @brief Moves this object into the bundle handle.
319    * @since_tizen 5.5
320    * @return The handle for bundle
321   */
322   bundle* Detach();
323
324  private:
325   class Impl;
326   std::unique_ptr<Impl> impl_;
327 };
328
329 }  // namespace tizen_base
330
331 /**
332  * @}
333  */
334
335 #endif  // BUNDLE_CPP_H_