2 * Copyright (c) 2019 - 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * @brief This file declares API of the bundle C++ library.
26 * @addtogroup CORE_LIB_BUNDLE_CPP_MODULE
33 #include <initializer_list>
40 #define EXPORT_API __attribute__((visibility("default")))
43 namespace tizen_base {
46 * @brief The class for bundle APIs.
49 class EXPORT_API Bundle final {
52 * @brief The type for raw bundle.
56 std::pair<std::unique_ptr<bundle_raw, decltype(std::free)*>, int>;
59 * @brief The class for information of keys.
67 * @param[in] handle The handle for type bundle_keyval_t
68 * @param[in] name The key string
69 * @param[in] own True if this object owns the handle
71 KeyInfo(const bundle_keyval_t* handle, std::string name, bool own = false);
80 * @brief Copy-constructor.
82 * @param[in] b The object to copy
84 KeyInfo(const KeyInfo& b);
89 * @param[in] b The object to copy
91 KeyInfo& operator = (const KeyInfo& b);
94 * @brief Move-constructor.
96 * @param[in] b The object to move
98 KeyInfo(KeyInfo&& b) noexcept;
103 * @param[in] b The object to move
105 KeyInfo& operator = (KeyInfo&& b) noexcept;
108 * @brief Gets the type of a key-value pair.
112 bundle_type GetType() const;
115 * @brief Determines whether the type of a key-value pair is an array.
117 * @return True when it is an array
119 bool IsArray() const;
122 * @brief Gets the key string.
124 * @return The key string
126 const std::string& GetName() const;
130 std::unique_ptr<Impl> impl_;
134 * @brief Constructor.
140 * @brief Constructor.
142 * @param[in] key_values The list of key-value pair
144 Bundle(std::initializer_list<
145 std::pair<std::string, std::string>> key_values);
148 * @brief Constructor.
150 * @param[in] raw The object for BundleRaw
151 * @param[in] base64 @c true, @a raw is the encoded raw data using base64-encoding
153 explicit Bundle(BundleRaw raw, bool base64 = true);
156 * @brief Constructor.
158 * @param[in] raw The string object for raw bundle
160 explicit Bundle(const std::string& raw);
163 * @brief Constructor.
165 * @param[in] b The handle for bundle
166 * @param[in] copy True if this object wants to copy it from the handle
167 * @param[in] own True if this object owns the handle
169 explicit Bundle(bundle* b, bool copy = true, bool own = true);
178 * @brief Copy-constructor.
180 * @param[in] b The object to copy
182 Bundle(const Bundle& b);
187 * @param[in] b The object to copy
189 Bundle& operator = (const Bundle& b);
192 * @brief Move-constructor.
194 * @param[in] b The object to move
196 Bundle(Bundle&& b) noexcept;
201 * @param[in] b The object to move
203 Bundle& operator = (Bundle&& b) noexcept;
206 * @brief Check the bundle is empty or not.
208 * @return true if the bundle is empty
210 bool IsEmpty() const noexcept;
213 * @brief Gets keys in bundle object.
215 * @return A string array of object KeyInfo
217 std::vector<KeyInfo> GetKeys();
220 * @brief Adds a string type key-value pair into a bundle.
222 * @param[in] key The string key
223 * @param[in] val The string value
224 * @return The operation result
225 * @retval BUNDLE_ERROR_NONE Success
226 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
227 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
228 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
230 int Add(const std::string& key, const std::string& val);
233 * @brief Adds a string type key-value pair into a bundle.
235 * @param[in] key The string key
236 * @param[in] val The array of strings
237 * @return The operation result
238 * @retval BUNDLE_ERROR_NONE Success
239 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
240 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
241 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
243 int Add(const std::string& key, const std::vector<std::string>& val);
246 * @brief Adds a string type key-value pair into a bundle.
248 * @param[in] key The string key
249 * @param[in] val The array of bytes
250 * @return The operation result
251 * @retval BUNDLE_ERROR_NONE Success
252 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
253 * @retval BUNDLE_ERROR_KEY_EXISTS Key already exists
254 * @retval BUNDLE_ERROR_OUT_OF_MEMORY Out of memory
256 int Add(const std::string& key, const std::vector<unsigned char>& val);
259 * @brief Deletes a key-value object with the given key.
261 * @param[in] key The string key
262 * @return The operation result
263 * @retval BUNDLE_ERROR_NONE Success
264 * @retval BUNDLE_ERROR_INVALID_PARAMETER Invalid parameter
265 * @retval BUNDLE_ERROR_KEY_NOT_AVAILABLE Key not available
267 int Delete(const std::string& key);
270 * @brief Gets a string from the key.
272 * @param[in] key The string key
275 std::string GetString(const std::string& key) const;
278 * @brief Gets strings from the key.
280 * @param[in] key The string key
281 * @return The array of strings
283 std::vector<std::string> GetStringArray(const std::string& key) const;
286 * @brief Gets bytes from the key.
288 * @param[in] key The string key
291 std::vector<unsigned char> GetByte(const std::string& key) const;
294 * @brief Converts this object to BundleRaw type.
296 * @param[in] base64 @c true, the BundleRaw will be encoded using base64-encoding.
297 * @return The object of BundleRaw
299 BundleRaw ToRaw(bool base64 = true);
302 * @brief Gets the count of keys.
306 int GetCount() const;
309 * @brief Gets the data type from the key.
311 * @param[in] key The string key
312 * @return The data type
314 bundle_type GetType(const std::string& key) const;
317 * @brief Gets the handle for bundle APIs.
319 * @return The handle for bundle
321 bundle* GetHandle() const;
324 * @brief Moves this object into the bundle handle.
326 * @return The handle for bundle
331 * @brief Exports bundle to an argument vector.
333 * @return The argument vector
335 std::vector<std::string> Export() const;
339 std::unique_ptr<Impl> impl_;
342 } // namespace tizen_base
348 #endif // BUNDLE_CPP_H_