* 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);
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);
THROW(ret);
}
-KeyInfo::~KeyInfo() = default;
-
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;
}
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;
return *this;
}
-bool KeyInfo::operator == (const KeyInfo& key_info) {
+bool KeyInfo::operator==(const KeyInfo& key_info) {
if (this == &key_info)
return true;
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;
}
}
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;
}
// total size
unsigned char* p = reinterpret_cast<unsigned char*>(&encoded_size);
- std::copy(p, p + sizeof(encoded_size), std::back_inserter(bytes));
+ bytes.insert(bytes.end(), p, p + sizeof(encoded_size));
// type
p = reinterpret_cast<unsigned char*>(&type_);
- std::copy(p, p + sizeof(type_), std::back_inserter(bytes));
+ bytes.insert(bytes.end(), p, p + sizeof(type_));
// key size
std::size_t key_length = key_.length() + 1;
p = reinterpret_cast<unsigned char*>(&key_length);
- std::copy(p, p + sizeof(key_length), std::back_inserter(bytes));
+ bytes.insert(bytes.end(), p, p + sizeof(key_length));
// key
- std::copy(key_.begin(), key_.end() + 1, std::back_inserter(bytes));
+ bytes.insert(bytes.end(), key_.begin(), key_.end() + 1);
if (type_ & BUNDLE_TYPE_ARRAY) {
// values size
std::size_t values_size = values_.size();
p = reinterpret_cast<unsigned char*>(&values_size);
- std::copy(p , p + sizeof(values_size), std::back_inserter(bytes));
+ bytes.insert(bytes.end(), p, p + sizeof(values_size));
}
// values
for (unsigned int i = 0; i < values_.size(); i++) {
std::size_t value_size = values_size_[i];
p = reinterpret_cast<unsigned char*>(&value_size);
- std::copy(p, p + sizeof(value_size), std::back_inserter(bytes));
+ bytes.insert(bytes.end(), p, p + sizeof(value_size));
- std::copy(values_[i].get(), values_[i].get() + value_size,
- std::back_inserter(bytes));
+ bytes.insert(bytes.end(), values_[i].get(), values_[i].get() + value_size);
}
return 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
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));
}
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;