Refactor bundle implementation
[platform/core/base/bundle.git] / src / key-info-internal.cc
index e7c7f6f..e5d0b03 100644 (file)
@@ -14,7 +14,9 @@
  * limitations under the License.
  */
 
+#include <algorithm>
 #include <cstring>
+#include <utility>
 
 #include "include/bundle.h"
 
 namespace tizen_base {
 namespace internal {
 
-KeyInfo::KeyInfo(int type, std::string key,
-    std::vector<unsigned char> value)
-  : type_(type),
-    key_(std::move(key)) {
+KeyInfo::KeyInfo(int type, std::string key, std::vector<unsigned char> value)
+    : type_(type), key_(std::move(key)) {
   int ret = SetValue(value);
   if (ret != BUNDLE_ERROR_NONE)
     THROW(ret);
@@ -35,8 +35,7 @@ KeyInfo::KeyInfo(int type, std::string key,
 
 KeyInfo::KeyInfo(int type, std::string key,
     std::vector<std::vector<unsigned char>> values)
-  : type_(type),
-    key_(std::move(key)) {
+    : type_(type), key_(std::move(key)) {
   int ret = SetValues(values);
   if (ret != BUNDLE_ERROR_NONE)
     THROW(ret);
@@ -49,37 +48,49 @@ KeyInfo::KeyInfo(std::vector<unsigned char> encoded_bytes) {
 }
 
 KeyInfo::KeyInfo(const KeyInfo& key_info) {
+  values_.reserve(key_info.values_.size());
+  try {
+    for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
+      auto new_value =
+          std::make_unique<unsigned char[]>(key_info.values_size_[i]);
+      std::copy(key_info.values_[i].get(),
+          key_info.values_[i].get() + key_info.values_size_[i],
+          new_value.get());
+      values_.push_back(std::move(new_value));
+    }
+  } catch (const std::bad_alloc& e) {
+    THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
+  }
+
   type_ = key_info.type_;
   key_ = key_info.key_;
   values_size_ = key_info.values_size_;
   uvalues_size_ = key_info.uvalues_size_;
-  for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
-    auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
-    if (new_value == nullptr)
-      THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
-
-    std::copy(key_info.values_[i].get(),
-        key_info.values_[i].get() + values_size_[i], new_value);
-    values_.emplace_back(new_value);
-  }
 }
 
-KeyInfo& KeyInfo::operator = (const KeyInfo& key_info) {
+KeyInfo& KeyInfo::operator=(const KeyInfo& key_info) {
   if (this != &key_info) {
+    decltype(values_) new_values;
+    try {
+      for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
+        auto new_value =
+            std::make_unique<unsigned char[]>(key_info.values_size_[i]);
+        std::copy(key_info.values_[i].get(),
+            key_info.values_[i].get() + key_info.values_size_[i],
+            new_value.get());
+        new_values.push_back(std::move(new_value));
+      }
+    } catch (const std::bad_alloc& e) {
+      THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
+    }
+
     type_ = key_info.type_;
     key_ = key_info.key_;
     values_size_ = key_info.values_size_;
     uvalues_size_ = key_info.uvalues_size_;
-    for (unsigned int i = 0; i < key_info.values_.size(); ++i) {
-      auto* new_value = new (std::nothrow) unsigned char[values_size_[i]];
-      if (new_value == nullptr)
-        THROW(BUNDLE_ERROR_OUT_OF_MEMORY);
-
-      std::copy(key_info.values_[i].get(),
-          key_info.values_[i].get() + values_size_[i], new_value);
-      values_.emplace_back(new_value);
-    }
+    values_ = std::move(new_values);
   }
+
   return *this;
 }
 
@@ -92,7 +103,7 @@ KeyInfo::KeyInfo(KeyInfo&& key_info) noexcept {
   uvalues_size_ = std::move(key_info.uvalues_size_);
 }
 
-KeyInfo& KeyInfo::operator = (KeyInfo&& key_info) noexcept {
+KeyInfo& KeyInfo::operator=(KeyInfo&& key_info) noexcept {
   if (this != &key_info) {
     type_ = key_info.type_;
     key_info.type_ = 0;
@@ -104,7 +115,7 @@ KeyInfo& KeyInfo::operator = (KeyInfo&& key_info) noexcept {
   return *this;
 }
 
-bool KeyInfo::operator == (const KeyInfo& key_info) {
+bool KeyInfo::operator==(const KeyInfo& key_info) {
   if (this == &key_info)
     return true;
 
@@ -118,12 +129,12 @@ bool KeyInfo::operator == (const KeyInfo& key_info) {
     return false;
 
   for (unsigned int i = 0; i < values_.size(); ++i) {
-    if (values_size_[i] == key_info.values_size_[i])
+    if (values_size_[i] != key_info.values_size_[i])
       return false;
 
     int ret = std::memcmp(values_[i].get(), key_info.values_[i].get(),
         values_size_[i]);
-    if (ret == 0)
+    if (ret != 0)
       return false;
   }
 
@@ -158,28 +169,32 @@ const std::vector<unsigned int>& KeyInfo::GetUValuesSize() {
 }
 
 int KeyInfo::SetValue(const std::vector<unsigned char>& value) {
-  auto* new_value = new (std::nothrow) unsigned char[value.size()];
-  if (new_value == nullptr)
+  try {
+    auto new_value = std::make_unique<unsigned char[]>(value.size());
+    std::copy(value.begin(), value.end(), new_value.get());
+    values_.emplace_back(std::move(new_value));
+  } catch (const std::bad_alloc& e) {
     return BUNDLE_ERROR_OUT_OF_MEMORY;
+  }
 
-  std::copy(value.begin(), value.end(), new_value);
-  values_.emplace_back(new_value);
   values_size_.push_back(value.size());
   uvalues_size_.push_back(static_cast<unsigned int>(value.size()));
   return BUNDLE_ERROR_NONE;
 }
 
 int KeyInfo::SetValues(const std::vector<std::vector<unsigned char>>& values) {
-  for (unsigned int i = 0; i< values.size(); ++i) {
-    auto* new_value = new (std::nothrow) unsigned char[values[i].size()];
-    if (new_value == nullptr)
-      return BUNDLE_ERROR_OUT_OF_MEMORY;
-
-    std::copy(values[i].begin(), values[i].end(), new_value);
-    values_.emplace_back(new_value);
-    values_size_.push_back(values[i].size());
-    uvalues_size_.push_back(values[i].size());
+  try {
+    for (unsigned int i = 0; i < values.size(); ++i) {
+      auto new_value = std::make_unique<unsigned char[]>(values[i].size());
+      std::copy(values[i].begin(), values[i].end(), new_value.get());
+      values_.push_back(std::move(new_value));
+      values_size_.push_back(values[i].size());
+      uvalues_size_.push_back(values[i].size());
+    }
+  } catch (const std::bad_alloc& e) {
+    return BUNDLE_ERROR_OUT_OF_MEMORY;
   }
+
   return BUNDLE_ERROR_NONE;
 }
 
@@ -210,7 +225,7 @@ std::vector<unsigned char> KeyInfo::Encode() {
     // values size
     std::size_t values_size = values_.size();
     p = reinterpret_cast<unsigned char*>(&values_size);
-    bytes.insert(bytes.end(), p , p + sizeof(values_size));
+    bytes.insert(bytes.end(), p, p + sizeof(values_size));
   }
 
   // values
@@ -290,7 +305,7 @@ int KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
     return BUNDLE_ERROR_INVALID_PARAMETER;
 
   p = reinterpret_cast<unsigned char*>(&type_);
-  std::copy(&bytes[reader], &bytes[reader] + sizeof(type_),  p);
+  std::copy(&bytes[reader], &bytes[reader] + sizeof(type_), p);
   reader += sizeof(type_);
 
   // key size
@@ -340,14 +355,14 @@ int KeyInfo::Decode(const std::vector<unsigned char>& bytes) {
     if ((reader + value_size) > bytes.size())
       return BUNDLE_ERROR_INVALID_PARAMETER;
 
-    auto* new_value = new (std::nothrow) unsigned char[value_size];
-    if (new_value == nullptr)
+    try {
+      auto new_value = std::make_unique<unsigned char[]>(value_size);
+      std::copy(&bytes[reader], &bytes[reader] + value_size, new_value.get());
+      reader += value_size;
+      values_.push_back(std::move(new_value));
+    } catch (const std::bad_alloc& e) {
       return BUNDLE_ERROR_OUT_OF_MEMORY;
-
-    std::copy(&bytes[reader], &bytes[reader] + value_size, new_value);
-    reader += value_size;
-
-    values_.emplace_back(new_value);
+    }
     values_size_.push_back(value_size);
     uvalues_size_.push_back(static_cast<unsigned int>(value_size));
   }
@@ -362,14 +377,16 @@ int KeyInfo::SetValue(int index, const std::vector<unsigned char>& value) {
   if (!IsArray())
     return BUNDLE_ERROR_INVALID_PARAMETER;
 
-  auto* new_value = new (std::nothrow) unsigned char[value.size()];
-  if (new_value == nullptr)
-    return BUNDLE_ERROR_OUT_OF_MEMORY;
+  try {
+    auto new_value = std::make_unique<unsigned char[]>(value.size());
+    if (value.size() != 0)
+      std::copy(value.begin(), value.end(), new_value.get());
 
-  if (value.size() != 0)
-    std::copy(value.begin(), value.end(), new_value);
+    values_[index] = std::move(new_value);
+  } catch (const std::bad_alloc& e) {
+    return BUNDLE_ERROR_OUT_OF_MEMORY;
+  }
 
-  values_[index].reset(new_value);
   values_size_[index] = value.size();
   uvalues_size_[index] = static_cast<unsigned int>(value.size());
   return BUNDLE_ERROR_NONE;