Add a constructor to use initializer list 95/258595/1
authorjh9216.park <jh9216.park@samsung.com>
Fri, 21 May 2021 02:27:24 +0000 (22:27 -0400)
committerjh9216.park <jh9216.park@samsung.com>
Fri, 21 May 2021 02:27:24 +0000 (22:27 -0400)
- example
 Bundle b = {
   {"key1", "val1"},
   {"key2", "val2"},
   {"key3", "val3"}
 };

Change-Id: Ib16d58eba57d0958b985cfa24ac0d19bd49af734
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
include/bundle_cpp.h
src/bundle_cpp.cc
tests/bundle_unittests/src/test_bundle_cpp.cc

index 86c8af9..997da06 100644 (file)
@@ -30,6 +30,7 @@
 #include <bundle.h>
 
 #include <cstdio>
+#include <initializer_list>
 #include <memory>
 #include <string>
 #include <utility>
@@ -137,6 +138,14 @@ class EXPORT_API Bundle final {
 
   /**
    * @brief Constructor.
+   * @since_tizen 6.5
+   * @param[in] key_values The list of key-value pair
+   */
+  Bundle(std::initializer_list<
+      std::pair<std::string, std::string>> key_values);
+
+  /**
+   * @brief Constructor.
    * @since_tizen 5.5
    * @param[in] raw The object for BundleRaw
    * @param[in] base64 @c true, @a raw is the encoded raw data using base64-encoding
index a2ac393..20c34db 100644 (file)
@@ -38,6 +38,16 @@ Bundle::Bundle()
     throw std::bad_alloc();
 }
 
+Bundle::Bundle(std::initializer_list<
+    std::pair<std::string, std::string>> key_values)
+  : impl_(new Impl(this)) {
+  impl_->handle_ = bundle_create();
+  if (impl_->handle_ == nullptr)
+    throw std::bad_alloc();
+  for (auto& i : key_values)
+    Add(i.first, i.second);
+}
+
 Bundle::Bundle(BundleRaw raw, bool base64)
   : impl_(new Impl(this)) {
   if (base64)
index ef2f93d..605eacf 100644 (file)
@@ -29,6 +29,13 @@ TEST(Bundle, CtorDtor) {
   Bundle bundle;
 }
 
+TEST(Bundle, CtorDtor2) {
+  Bundle bundle = { {"key1", "val1"}, {"key2", "val2"}, {"key3", "val3"} };
+  EXPECT_EQ(bundle.GetString("key1"), "val1");
+  EXPECT_EQ(bundle.GetString("key2"), "val2");
+  EXPECT_EQ(bundle.GetString("key3"), "val3");
+}
+
 TEST(Bundle, CopyCtor) {
   Bundle bundle;
   bundle.Add("TestKey", "TestVal");