From: Hwankyu Jhun Date: Wed, 16 Feb 2022 01:18:38 +0000 (+0900) Subject: Fix bundle implementation X-Git-Tag: accepted/tizen/unified/20220221.080310~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F43%2F271143%2F1;p=platform%2Fcore%2Fbase%2Fbundle.git Fix bundle implementation - Add const keyword to Exception& - Use std::move() Change-Id: Ia58d1ae51fee24b69a48f30af287d49f7e717e60 Signed-off-by: Hwankyu Jhun --- diff --git a/src/bundle-internal.cc b/src/bundle-internal.cc index d30240e..3d14ace 100644 --- a/src/bundle-internal.cc +++ b/src/bundle-internal.cc @@ -111,7 +111,7 @@ void Bundle::Add(std::shared_ptr key_info) { THROW(BUNDLE_ERROR_KEY_EXISTS); map_[key_info->GetKey()] = key_info; - list_.push_back(key_info); + list_.push_back(std::move(key_info)); } void Bundle::Set(const std::string& key, int index, @@ -120,7 +120,7 @@ void Bundle::Set(const std::string& key, int index, if (iter == map_.end()) THROW(BUNDLE_ERROR_KEY_NOT_AVAILABLE); - int ret = iter->second->SetValue(index, value); + int ret = iter->second->SetValue(index, std::move(value)); if (ret != BUNDLE_ERROR_NONE) THROW(ret); } @@ -150,7 +150,7 @@ unsigned char* Bundle::Encode() { unsigned char* raw; try { raw = EncodeRaw(&size); - } catch (Exception& e) { + } catch (const Exception& e) { THROW(e.GetErrorCode()); } @@ -246,8 +246,8 @@ int Bundle::DecodeRaw(unsigned char* raw, int size) { KeyInfo* new_key_info; try { - new_key_info = new KeyInfo(encoded_bytes); - } catch (Exception& e) { + new_key_info = new KeyInfo(std::move(encoded_bytes)); + } catch (const Exception& e) { return e.GetErrorCode(); } catch (const std::bad_alloc& ba) { return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -259,7 +259,7 @@ int Bundle::DecodeRaw(unsigned char* raw, int size) { continue; map_[key_info->GetKey()] = key_info; - list_.push_back(key_info); + list_.push_back(std::move(key_info)); } return BUNDLE_ERROR_NONE; @@ -282,7 +282,7 @@ std::vector Bundle::Export() { std::unique_ptr base64_bytes_ptr(base64_bytes, g_free); - argv.push_back(base64_bytes); + argv.push_back(std::move(base64_bytes)); } return argv; @@ -301,8 +301,8 @@ int Bundle::Import(int argc, char** argv) { KeyInfo* new_key_info; try { - new_key_info = new KeyInfo(Type::String, argv[idx], value); - } catch (Exception& e) { + new_key_info = new KeyInfo(Type::String, argv[idx], std::move(value)); + } catch (const Exception& e) { return e.GetErrorCode(); } catch (const std::bad_alloc& ba) { return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -335,8 +335,8 @@ int Bundle::Import(int argc, char** argv) { KeyInfo* new_key_info; try { - new_key_info = new KeyInfo(decoded_bytes); - } catch (Exception& e) { + new_key_info = new KeyInfo(std::move(decoded_bytes)); + } catch (const Exception& e) { return e.GetErrorCode(); } catch (const std::bad_alloc& ba) { return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -348,7 +348,7 @@ int Bundle::Import(int argc, char** argv) { continue; map_[key_info->GetKey()] = key_info; - list_.push_back(key_info); + list_.push_back(std::move(key_info)); } return BUNDLE_ERROR_NONE; diff --git a/src/exception-internal.h b/src/exception-internal.h index 2020fbe..4ce8690 100644 --- a/src/exception-internal.h +++ b/src/exception-internal.h @@ -42,7 +42,7 @@ class Exception : public std::exception { return message_.c_str(); } - int GetErrorCode() { + int GetErrorCode() const { return error_code_; } diff --git a/src/json-internal.cc b/src/json-internal.cc index a6bcdd3..be83a5a 100644 --- a/src/json-internal.cc +++ b/src/json-internal.cc @@ -143,12 +143,12 @@ void Json::OnJsonObjectMember(JsonObject* object, const char* key, auto* p = reinterpret_cast(const_cast(val)); std::vector value; std::copy(p, p + (strlen(val) + 1), std::back_inserter(value)); - values.push_back(value); + values.push_back(std::move(value)); } try { - key_info = new KeyInfo(Bundle::Type::StringArray, key, values); - } catch (Exception& e) { + key_info = new KeyInfo(Bundle::Type::StringArray, key, std::move(values)); + } catch (const Exception& e) { _E("Error(%d)", e.GetErrorCode()); return; } catch (const std::bad_alloc& ba) { @@ -165,8 +165,8 @@ void Json::OnJsonObjectMember(JsonObject* object, const char* key, std::copy(p, p + (strlen(val) + 1), std::back_inserter(value)); try { - key_info = new KeyInfo(Bundle::Type::String, key, value); - } catch (Exception& e) { + key_info = new KeyInfo(Bundle::Type::String, key, std::move(value)); + } catch (const Exception& e) { _E("Error(%d)", e.GetErrorCode()); return; } catch (const std::bad_alloc& ba) { @@ -177,7 +177,7 @@ void Json::OnJsonObjectMember(JsonObject* object, const char* key, try { b->Add(std::shared_ptr(key_info)); - } catch (Exception& e) { + } catch (const Exception& e) { _W("Add() is failed. error(%d)", e.GetErrorCode()); return; } diff --git a/src/stub.cc b/src/stub.cc index f8b94c0..81815ee 100644 --- a/src/stub.cc +++ b/src/stub.cc @@ -57,8 +57,8 @@ extern "C" EXPORT_API int bundle_add_str(bundle* b, KeyInfo* key_info; try { - key_info = new KeyInfo(BUNDLE_TYPE_STR, key, value); - } catch (Exception& e) { + key_info = new KeyInfo(BUNDLE_TYPE_STR, key, std::move(value)); + } catch (const Exception& e) { return e.GetErrorCode(); } catch (const std::bad_alloc& ba) { return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -66,7 +66,7 @@ extern "C" EXPORT_API int bundle_add_str(bundle* b, try { h->Add(std::shared_ptr(key_info)); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -82,7 +82,7 @@ extern "C" EXPORT_API int bundle_get_str(bundle* b, std::shared_ptr key_info; try { key_info = h->Get(key); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -110,7 +110,7 @@ extern "C" EXPORT_API int bundle_del(bundle* b, const char* key) { auto* h = reinterpret_cast(b); try { h->Remove(key); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -268,7 +268,7 @@ extern "C" EXPORT_API bundle_keyval_t* bundle_keyval_dup( KeyInfo* k; try { k = new KeyInfo(*key_info); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return nullptr; } catch (const std::bad_alloc& ba) { @@ -336,7 +336,7 @@ extern "C" EXPORT_API bundle* bundle_decode(const bundle_raw* r, try { auto* raw = const_cast(r); b = new Bundle(static_cast(raw), data_size); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return nullptr; } catch (const std::bad_alloc& ba) { @@ -356,7 +356,7 @@ extern "C" EXPORT_API int bundle_encode_raw(bundle* b, bundle_raw** r, auto* h = reinterpret_cast(b); try { *r = reinterpret_cast(h->EncodeRaw(len)); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } return BUNDLE_ERROR_NONE; @@ -373,7 +373,7 @@ extern "C" EXPORT_API bundle* bundle_decode_raw(const bundle_raw* r, try { auto* raw = const_cast(r); b = new Bundle(static_cast(raw), data_size, false); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return nullptr; } catch (const std::bad_alloc& ba) { @@ -395,7 +395,7 @@ extern "C" EXPORT_API int bundle_get_type(bundle* b, const char* key) { std::shared_ptr key_info; try { key_info = h->Get(key); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return BUNDLE_TYPE_NONE; } @@ -423,13 +423,13 @@ extern "C" EXPORT_API int bundle_add_str_array(bundle* b, const char* key, try { key_info = new KeyInfo((BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY), key, values); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } try { h->Add(std::shared_ptr(key_info)); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -447,7 +447,7 @@ extern "C" EXPORT_API const char** bundle_get_str_array(bundle* b, std::shared_ptr key_info; try { key_info = h->Get(key); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return nullptr; } @@ -485,8 +485,8 @@ extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key, KeyInfo* key_info; try { - key_info = new KeyInfo(BUNDLE_TYPE_BYTE, key, value); - } catch (Exception& e) { + key_info = new KeyInfo(BUNDLE_TYPE_BYTE, key, std::move(value)); + } catch (const Exception& e) { return e.GetErrorCode(); } catch (const std::bad_alloc& ba) { return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -494,7 +494,7 @@ extern "C" EXPORT_API int bundle_add_byte(bundle* b, const char* key, try { h->Add(std::shared_ptr(key_info)); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -510,7 +510,7 @@ extern "C" EXPORT_API int bundle_get_byte(bundle* b, const char* key, std::shared_ptr key_info; try { key_info = h->Get(key); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -546,7 +546,7 @@ extern "C" EXPORT_API int bundle_export_to_argv(bundle* b, char*** argv) { std::vector exported_vt; try { exported_vt = h->Export(); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return -1; } @@ -603,7 +603,7 @@ extern "C" EXPORT_API bundle* bundle_import_from_argv(int argc, char** argv) { Bundle* b = nullptr; try { b = new (std::nothrow) Bundle(argc, argv); - } catch (Exception& e) { + } catch (const Exception& e) { set_last_result(e.GetErrorCode()); return nullptr; } @@ -635,8 +635,8 @@ extern "C" EXPORT_API int bundle_set_str_array_element(bundle* b, auto* h = reinterpret_cast(b); try { - h->Set(key, idx, value); - } catch (Exception& e) { + h->Set(key, idx, std::move(value)); + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -659,7 +659,7 @@ extern "C" EXPORT_API int bundle_init_byte_array(bundle* b, try { std::vector> values(len); key_info = new KeyInfo(Bundle::Type::ByteArray, key, std::move(values)); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } catch (const std::bad_alloc& ba) { return BUNDLE_ERROR_OUT_OF_MEMORY; @@ -669,7 +669,7 @@ extern "C" EXPORT_API int bundle_init_byte_array(bundle* b, try { h->Add(std::shared_ptr(key_info)); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -686,7 +686,7 @@ extern "C" EXPORT_API int bundle_get_byte_array(bundle* b, std::shared_ptr key_info; try { key_info = h->Get(key); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -727,8 +727,8 @@ extern "C" EXPORT_API int bundle_set_byte_array_element(bundle* b, auto* h = reinterpret_cast(b); try { - h->Set(key, idx, value); - } catch (Exception& e) { + h->Set(key, idx, std::move(value)); + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -743,7 +743,7 @@ extern "C" EXPORT_API int bundle_to_json(bundle* b, char** json) { Json js(h); try { *json = strdup(js.ToString().c_str()); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); } @@ -757,7 +757,7 @@ extern "C" EXPORT_API int bundle_from_json(const char* json, bundle** b) { Json js(json); try { *b = reinterpret_cast(js.ToBundle()); - } catch (Exception& e) { + } catch (const Exception& e) { return e.GetErrorCode(); }