Add IsEmpty() method
[platform/core/base/bundle.git] / src / bundle_cpp.cc
index 5a5001d..6d1163c 100644 (file)
 #include "bundle_cpp_implementation.h"
 #include "bundle_cpp.h"
 #include "bundle_internal.h"
-
-
-#ifdef LOG_TAG
-#undef LOG_TAG
-#endif
-
-#define LOG_TAG "BUNDLE"
+#include "bundle_log.h"
 
 namespace tizen_base {
 Bundle::Impl::Impl(Bundle* parent, bool copy, bool own)
@@ -42,45 +36,55 @@ Bundle::Impl::~Impl() = default;
 Bundle::Bundle()
   : impl_(new Impl(this)) {
   impl_->handle_ = bundle_create();
+  if (impl_->handle_ == nullptr)
+    throw std::bad_alloc();
 }
 
 Bundle::Bundle(BundleRaw raw)
   : impl_(new Impl(this)) {
   impl_->handle_ = bundle_decode(raw.first.get(), raw.second);
+  if (impl_->handle_ == nullptr)
+    throw std::bad_alloc();
 }
 
 Bundle::Bundle(const std::string& raw)
   : impl_(new Impl(this)) {
   impl_->handle_ = bundle_decode(reinterpret_cast<const bundle_raw*>(
       raw.c_str()), raw.length());
+  if (impl_->handle_ == nullptr)
+    throw std::bad_alloc();
 }
 
 Bundle::Bundle(bundle* b, bool copy, bool own)
   : impl_(new Impl(this, copy, own)) {
-  if (!impl_->copy_)
+  if (b == nullptr)
+    throw std::invalid_argument("b cannot be null");
+
+  if (!impl_->copy_) {
     impl_->handle_ = b;
-  else
+  } else {
     impl_->handle_ = bundle_dup(b);
+    if (impl_->handle_ == nullptr)
+      throw std::bad_alloc();
+  }
 }
 
 Bundle::~Bundle() {
-  if (impl_->handle_ && (impl_->own_ || impl_->copy_))
+  if (impl_ && impl_->handle_ && (impl_->own_ || impl_->copy_))
     bundle_free(impl_->handle_);
 }
 
 Bundle::Bundle(const Bundle& b)
   : impl_(new Impl(this)) {
   impl_->handle_ = bundle_dup(b.impl_->handle_);
+  if (impl_->handle_ == nullptr)
+    throw std::bad_alloc();
 }
 
 Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name)
   : impl_(new Impl(this, handle, std::move(name))) {
 }
 
-Bundle::KeyInfo::KeyInfo()
-  : impl_(new Impl(this)) {
-}
-
 Bundle::KeyInfo::~KeyInfo() {
 }
 
@@ -98,12 +102,16 @@ Bundle::KeyInfo::KeyInfo(const KeyInfo& k)
     : impl_(new Impl(this)) {
   impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
   impl_->name_ = k.impl_->name_;
+  if (impl_->handle_ == nullptr)
+    throw std::bad_alloc();
 }
 
 Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) {
   if (this != &k) {
     impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
     impl_->name_ = k.impl_->name_;
+    if (impl_->handle_ == nullptr)
+      throw std::bad_alloc();
   }
   return *this;
 }
@@ -143,14 +151,15 @@ const std::string& Bundle::KeyInfo::GetName() const {
 Bundle& Bundle::operator = (const Bundle& b) {
   if (this != &b) {
     impl_->handle_ = bundle_dup(b.impl_->handle_);
+    if (impl_->handle_ == nullptr)
+      throw std::bad_alloc();
   }
   return *this;
 }
 
 Bundle::Bundle(Bundle&& b) noexcept {
-  impl_ = std::unique_ptr<Impl>(new Impl(this));
-  impl_->handle_ = b.impl_->handle_;
-  b.impl_->handle_ = nullptr;
+  impl_ = std::move(b.impl_);
+  impl_->parent_ = this;
 }
 
 Bundle& Bundle::operator = (Bundle&& b) noexcept {
@@ -161,6 +170,10 @@ Bundle& Bundle::operator = (Bundle&& b) noexcept {
   return *this;
 }
 
+bool Bundle::IsEmpty() const noexcept {
+  return (bundle_get_count(impl_->handle_) == 0) ? true : false;
+}
+
 std::vector<Bundle::KeyInfo> Bundle::GetKeys() {
   std::vector<Bundle::KeyInfo> v;
 
@@ -174,7 +187,10 @@ std::vector<Bundle::KeyInfo> Bundle::GetKeys() {
 }
 
 int Bundle::Add(const std::string& key, const std::string& val) {
-  return bundle_add_str(impl_->handle_, key.c_str(), val.c_str());
+  int ret = bundle_add_str(impl_->handle_, key.c_str(), val.c_str());
+  if (ret != BUNDLE_ERROR_NONE)
+    LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(), ret);
+  return ret;
 }
 
 int Bundle::Add(const std::string& key, const std::vector<std::string>& val) {
@@ -183,15 +199,26 @@ int Bundle::Add(const std::string& key, const std::vector<std::string>& val) {
     v.push_back(i.c_str());
   }
 
-  return bundle_add_str_array(impl_->handle_, key.c_str(), v.data(), v.size());
+  int ret = bundle_add_str_array(
+    impl_->handle_, key.c_str(), v.data(), v.size());
+  if (ret != BUNDLE_ERROR_NONE)
+    LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
+
+  return ret;
 }
 
 int Bundle::Add(const std::string& key, const std::vector<unsigned char>& val) {
-  return bundle_add_byte(impl_->handle_, key.c_str(), val.data(), val.size());
+  int ret = bundle_add_byte(impl_->handle_, key.c_str(), val.data(), val.size());
+  if (ret != BUNDLE_ERROR_NONE)
+    LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
+  return ret;
 }
 
 int Bundle::Delete(const std::string& key) {
-  return bundle_del(impl_->handle_, key.c_str());
+  int ret = bundle_del(impl_->handle_, key.c_str());
+  if (ret != BUNDLE_ERROR_NONE)
+    LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
+  return ret;
 }
 
 std::string Bundle::GetString(const std::string& key) const {
@@ -222,15 +249,24 @@ std::vector<std::string> Bundle::GetStringArray(const std::string& key) const {
 std::vector<unsigned char> Bundle::GetByte(const std::string& key) const {
   size_t size;
   unsigned char* bytes = nullptr;
-  bundle_get_byte(impl_->handle_, key.c_str(),
+  int ret = bundle_get_byte(impl_->handle_, key.c_str(),
       reinterpret_cast<void**>(&bytes), &size);
+  if (ret != BUNDLE_ERROR_NONE) {
+    LOGE("bundle_get_byte() is failed");
+    return {};
+  }
+
   return std::vector<unsigned char>(bytes, bytes + size);
 }
 
 Bundle::BundleRaw Bundle::ToRaw() {
   bundle_raw* raw = nullptr;
   int len = 0;
-  bundle_encode(impl_->handle_, &raw, &len);
+  int ret = bundle_encode(impl_->handle_, &raw, &len);
+  if (raw == nullptr) {
+    LOGE("Fail to encode data (%d)", ret);
+    throw std::bad_alloc();
+  }
 
   return BundleRaw(
       std::unique_ptr<bundle_raw, decltype(std::free)*>(raw, std::free), len);