#include <memory>
#include <stdexcept>
+#include "bundle-internal.h"
#include "bundle_cpp.h"
#include "bundle_cpp_implementation.h"
-#include "bundle_internal.h"
+#include "exception-internal.h"
+#include "key-info-internal.h"
#include "log-private.h"
namespace tizen_base {
Bundle::Bundle()
: impl_(new Impl(this)) {
- impl_->handle_ = bundle_create();
- if (impl_->handle_ == nullptr)
+ impl_->handle_ = new (std::nothrow) internal::Bundle();
+ if (impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
throw std::bad_alloc();
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
}
-Bundle::Bundle(std::initializer_list<
- std::pair<std::string, std::string>> key_values)
+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)
+ impl_->handle_ = new (std::nothrow) internal::Bundle();
+ if (impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
throw std::bad_alloc();
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
+
for (auto& i : key_values)
Add(i.first, i.second);
}
Bundle::Bundle(BundleRaw raw, bool base64)
: impl_(new Impl(this)) {
- if (base64)
- impl_->handle_ = bundle_decode(raw.first.get(), raw.second);
- else
- impl_->handle_ = bundle_decode_raw(raw.first.get(), raw.second);
- if (impl_->handle_ == nullptr)
+ auto& [raw_data, raw_size] = raw;
+ try {
+ impl_->handle_ = new internal::Bundle(raw_data.get(), raw_size, base64);
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
throw std::bad_alloc();
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ throw std::bad_alloc();
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
}
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)
+ try {
+ impl_->handle_ = new internal::Bundle(
+ reinterpret_cast<bundle_raw*>(const_cast<char*>(raw.data())),
+ raw.length(), true);
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
throw std::bad_alloc();
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ throw std::bad_alloc();
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
}
Bundle::Bundle(bundle* b, bool copy, bool own)
throw std::invalid_argument("b cannot be null");
if (!impl_->copy_) {
- impl_->handle_ = b;
+ impl_->handle_ = reinterpret_cast<internal::Bundle*>(b);
} else {
- impl_->handle_ = bundle_dup(b);
- if (impl_->handle_ == nullptr)
+ try {
+ impl_->handle_ =
+ new internal::Bundle(*reinterpret_cast<internal::Bundle*>(b));
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ throw std::bad_alloc();
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
throw std::bad_alloc();
+ }
}
+ set_last_result(BUNDLE_ERROR_NONE);
}
Bundle::~Bundle() {
if (impl_ && impl_->handle_ && (impl_->own_ || impl_->copy_))
- bundle_free(impl_->handle_);
+ delete impl_->handle_;
}
Bundle::Bundle(const Bundle& b)
: impl_(new Impl(this)) {
- impl_->handle_ = bundle_dup(b.impl_->handle_);
- if (impl_->handle_ == nullptr)
+ if (b.impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ throw std::bad_alloc();
+ }
+
+ try {
+ impl_->handle_ = new internal::Bundle(*(b.impl_->handle_));
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ throw std::bad_alloc();
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
throw std::bad_alloc();
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
}
Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name,
Bundle::KeyInfo::~KeyInfo() {
if (impl_ && impl_->handle_ && impl_->own_)
- bundle_keyval_free(const_cast<bundle_keyval_t*>(impl_->handle_));
+ delete impl_->handle_;
}
Bundle::KeyInfo::Impl::~Impl() = default;
const bundle_keyval_t* handle,
std::string name,
bool own)
- : handle_(handle), name_(name), parent_(parent), own_(own) {
+ : handle_(const_cast<internal::KeyInfo*>(
+ reinterpret_cast<const internal::KeyInfo*>(handle))),
+ name_(name), parent_(parent), own_(own) {
}
Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent) : parent_(parent) {
Bundle::KeyInfo::KeyInfo(const KeyInfo& k)
: impl_(new Impl(this)) {
- impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
+ if (k.impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ throw std::bad_alloc();
+ }
+
+ try {
+ impl_->handle_ = new internal::KeyInfo(*(k.impl_->handle_));
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ throw std::bad_alloc();
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ throw std::bad_alloc();
+ }
+
impl_->name_ = k.impl_->name_;
impl_->own_ = true;
- if (impl_->handle_ == nullptr)
- throw std::bad_alloc();
}
Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) {
if (this != &k) {
- if (impl_->handle_ && impl_->own_)
- bundle_keyval_free(const_cast<bundle_keyval_t*>(impl_->handle_));
-
- impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
- impl_->name_ = k.impl_->name_;
- if (impl_->handle_ == nullptr)
+ if (impl_->handle_ && impl_->own_) {
+ delete impl_->handle_;
+ }
+ try {
+ impl_->handle_ = new internal::KeyInfo(*(k.impl_->handle_));
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
throw std::bad_alloc();
+ } catch (const std::bad_alloc& ba) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ throw std::bad_alloc();
+ }
+ impl_->name_ = k.impl_->name_;
impl_->own_ = true;
}
return *this;
Bundle::KeyInfo& Bundle::KeyInfo::operator = (Bundle::KeyInfo&& k) noexcept {
if (this != &k) {
- if (impl_->handle_ && impl_->own_)
- bundle_keyval_free(const_cast<bundle_keyval_t*>(impl_->handle_));
+ if (impl_->handle_ && impl_->own_) {
+ delete impl_->handle_;
+ }
impl_->handle_ = k.impl_->handle_;
impl_->name_ = k.impl_->name_;
}
bundle_type Bundle::KeyInfo::GetType() const {
- return static_cast<bundle_type>(
- bundle_keyval_get_type(const_cast<bundle_keyval_t*>(impl_->handle_)));
+ if (impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ return bundle_type::BUNDLE_TYPE_NONE;
+ }
+
+ set_last_result(BUNDLE_ERROR_NONE);
+ return static_cast<bundle_type>(impl_->handle_->GetType());
}
bool Bundle::KeyInfo::IsArray() const {
- return bundle_keyval_type_is_array(const_cast<bundle_keyval_t*>(
- impl_->handle_));
+ if (impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ return true;
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
+ return (impl_->handle_->GetType() & BUNDLE_TYPE_ARRAY);
}
const std::string& Bundle::KeyInfo::GetName() const {
Bundle& Bundle::operator = (const Bundle& b) {
if (this != &b) {
if (impl_->handle_ && (impl_->own_ || impl_->copy_))
- bundle_free(impl_->handle_);
+ delete impl_->handle_;
- impl_->handle_ = bundle_dup(b.impl_->handle_);
- if (impl_->handle_ == nullptr)
+ if(b.impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
throw std::bad_alloc();
+ }
+
+ try {
+ impl_->handle_ = new internal::Bundle(*(b.impl_->handle_));
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ throw std::bad_alloc();
+ } catch (const std::bad_alloc& e) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ throw std::bad_alloc();
+ }
impl_->own_ = true;
impl_->copy_ = true;
+ set_last_result(BUNDLE_ERROR_NONE);
}
return *this;
}
impl_ = std::move(b.impl_);
impl_->parent_ = this;
b.impl_.reset(new Impl(&b));
- b.impl_->handle_ = bundle_create();
+ b.impl_->handle_ = new (std::nothrow) internal::Bundle();
+ set_last_result(BUNDLE_ERROR_NONE);
+ if (b.impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_OUT_OF_MEMORY);
+ }
}
Bundle& Bundle::operator = (Bundle&& b) noexcept {
if (this != &b) {
if (impl_->handle_ && (impl_->own_ || impl_->copy_))
- bundle_free(impl_->handle_);
+ delete impl_->handle_;
impl_->handle_ = b.impl_->handle_;
b.impl_->handle_ = nullptr;
}
bool Bundle::operator == (const Bundle& b) const {
- return (bundle_compare(impl_->handle_, b.impl_->handle_) == 0);
+ return (*(impl_->handle_) == *(b.impl_->handle_));
}
bool Bundle::operator != (const Bundle& b) const {
}
bool Bundle::IsEmpty() const noexcept {
- return (bundle_get_count(impl_->handle_) == 0);
+ if (impl_->handle_ == nullptr)
+ return true;
+ set_last_result(BUNDLE_ERROR_NONE);
+ return (impl_->handle_->GetSize() == 0);
}
std::vector<Bundle::KeyInfo> Bundle::GetKeys() {
std::vector<Bundle::KeyInfo> v;
+ if (impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ return v;
+ }
- bundle_foreach(impl_->handle_, [](const char *key, const int type,
- const bundle_keyval_t *kv, void *user_data) {
- auto* v = static_cast<std::vector<KeyInfo>*>(user_data);
- v->emplace_back(kv, key, false);
- }, &v);
+ auto it = impl_->handle_->GetMap().begin();
+ while (it != impl_->handle_->GetMap().end()) {
+ auto& key_info = it->second;
+ v.emplace_back(reinterpret_cast<bundle_keyval_t*>(key_info.get()),
+ key_info->GetKey(), false);
+ ++it;
+ }
+ set_last_result(BUNDLE_ERROR_NONE);
return v;
}
int Bundle::Add(const std::string& key, const std::string& val) {
- 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;
+ if (impl_->handle_ == nullptr || key.empty() || val.empty()) {
+ LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(),
+ BUNDLE_ERROR_INVALID_PARAMETER);
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+ }
+
+ std::vector<unsigned char> value(val.begin(),val.end()+1);
+ try {
+ auto key_info = std::make_shared<internal::KeyInfo>(BUNDLE_TYPE_STR,
+ key, std::move(value));
+ impl_->handle_->Add(key_info);
+ } catch (const internal::Exception& e) {
+ LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(),
+ e.GetErrorCode());
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
+ LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(),
+ BUNDLE_ERROR_OUT_OF_MEMORY);
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
+ return BUNDLE_ERROR_NONE;
}
int Bundle::Add(const std::string& key, const std::vector<std::string>& val) {
- std::vector<const char*> v;
- for (auto& i : val) {
- v.push_back(i.c_str());
+ if (impl_->handle_ == nullptr || key.empty()) {
+ LOGE("Add fail key(%s), ret(%d)", key.c_str(),
+ BUNDLE_ERROR_INVALID_PARAMETER);
+ return BUNDLE_ERROR_INVALID_PARAMETER;
}
- 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);
+ std::vector<std::vector<unsigned char>> values;
+ for (const auto& str : val) {
+ values.emplace_back(str.begin(), str.end()+1);
+ }
- return ret;
+ try {
+ auto key_info = std::make_shared<internal::KeyInfo>(
+ (BUNDLE_TYPE_STR_ARRAY | BUNDLE_TYPE_ARRAY), key, values);
+ impl_->handle_->Add(std::move(key_info));
+ } catch (const internal::Exception& e) {
+ LOGE("Add fail key(%s), ret(%d)", key.c_str(), e.GetErrorCode());
+ return e.GetErrorCode();
+ } catch (const std::bad_alloc& ba) {
+ LOGE("Add fail key(%s), ret(%d)", key.c_str(), BUNDLE_ERROR_OUT_OF_MEMORY);
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
+ return BUNDLE_ERROR_NONE;
}
int Bundle::Add(const std::string& key, const std::vector<unsigned char>& val) {
- 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;
+ if (impl_->handle_ == nullptr || key.empty()) {
+ LOGE("Add fail key(%s), ret(%d)", key.c_str(),
+ BUNDLE_ERROR_INVALID_PARAMETER);
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+ }
+
+ try {
+ auto key_info =
+ std::make_shared<internal::KeyInfo>(BUNDLE_TYPE_BYTE, key, val);
+ impl_->handle_->Add(std::move(key_info));
+ } catch (const internal::Exception& e) {
+ LOGE("Add fail key(%s), ret(%d)", key.c_str(), e.GetErrorCode());
+ } catch (const std::bad_alloc& ba) {
+ LOGE("Add fail key(%s), ret(%d)", key.c_str(), BUNDLE_ERROR_OUT_OF_MEMORY);
+ return BUNDLE_ERROR_OUT_OF_MEMORY;
+ }
+ return BUNDLE_ERROR_NONE;
}
int Bundle::Delete(const std::string& key) {
- return bundle_del(impl_->handle_, key.c_str());
+ if (impl_->handle_ == nullptr || key.empty())
+ return BUNDLE_ERROR_INVALID_PARAMETER;
+
+ try {
+ impl_->handle_->Remove(key);
+ }
+ catch (const internal::Exception& e) {
+ return e.GetErrorCode();
+ }
+ return BUNDLE_ERROR_NONE;
}
std::string Bundle::GetString(const std::string& key) const {
- char* str = nullptr;
- bundle_get_str(impl_->handle_, key.c_str(), &str);
-
- if (!str)
+ if (impl_->handle_ == nullptr || key.empty())
return "";
- return std::string(str);
+ std::shared_ptr<internal::KeyInfo> key_info;
+ try {
+ key_info = impl_->handle_->Get(key);
+ } catch (const internal::Exception& e) {
+ return "";
+ }
+ if( key_info->GetType() != BUNDLE_TYPE_STR ){
+ return "";
+ }
+ return reinterpret_cast<char*>(key_info->GetValues()[0].get());
}
std::vector<std::string> Bundle::GetStringArray(const std::string& key) const {
- std::vector<std::string> v;
+ if (key.empty() || (impl_->handle_ == nullptr)) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ return std::vector<std::string>();
+ }
- const char** str_array = nullptr;
- int len = 0;
+ std::vector<std::string> v;
+ std::shared_ptr<internal::KeyInfo> key_info;
- str_array = bundle_get_str_array(impl_->handle_, key.c_str(), &len);
+ try {
+ key_info = impl_->handle_->Get(key);
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ return std::vector<std::string>();
+ }
- for (int i = 0; i < len; i++) {
- v.emplace_back(str_array[i]);
+ if (key_info->GetType() != BUNDLE_TYPE_STR_ARRAY) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ return std::vector<std::string>();
}
+ set_last_result(BUNDLE_ERROR_NONE);
+ auto& raw_values = key_info->GetValues();
+ for (const std::unique_ptr<unsigned char[]>& raw_value : raw_values) {
+ v.emplace_back(reinterpret_cast<char*>(raw_value.get()));
+ }
return v;
}
std::vector<unsigned char> Bundle::GetByte(const std::string& key) const {
- size_t size;
- unsigned char* bytes = nullptr;
- 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");
+ if (impl_->handle_ == nullptr || key.empty()) {
+ LOGE("Bundle::GetByte() is failed");
+ return {};
+ }
+
+ std::shared_ptr<internal::KeyInfo> key_info;
+ try {
+ key_info = impl_->handle_->Get(key);
+ } catch (const internal::Exception& e) {
+ LOGE("Bundle::GetByte() is failed");
return {};
}
+ if (key_info->GetType() != BUNDLE_TYPE_BYTE) {
+ LOGE("Bundle::GetByte() is failed");
+ return {};
+ }
+
+ auto& raw_values = key_info->GetValues();
+ unsigned char* bytes = nullptr;
+ if (!raw_values.empty())
+ bytes = reinterpret_cast<unsigned char*>(raw_values[0].get());
+ auto size = reinterpret_cast<size_t>(key_info->GetValuesSize()[0]);
return std::vector<unsigned char>(bytes, bytes + size);
}
Bundle::BundleRaw Bundle::ToRaw(bool base64) {
- bundle_raw* raw = nullptr;
- int len = 0;
- int ret;
- if (base64)
- ret = bundle_encode(impl_->handle_, &raw, &len);
- else
- ret = bundle_encode_raw(impl_->handle_, &raw, &len);
- if (raw == nullptr) {
- LOGE("Fail to encode data (%d)", ret);
+ if (impl_->handle_ == nullptr) {
+ LOGE("Fail to encode data (%d)", BUNDLE_ERROR_INVALID_PARAMETER);
throw std::bad_alloc();
}
+ bundle_raw* raw = nullptr;
+ int len = 0;
+ if (base64){
+ try {
+ raw = impl_->handle_->Encode();
+ } catch (const internal::Exception& e) {
+ LOGE("Fail to encode data (%d)", e.GetErrorCode());
+ throw std::bad_alloc();
+ }
+ len = strlen(reinterpret_cast<char*>(raw));
+ } else {
+ try {
+ raw = impl_->handle_->EncodeRaw(&len);
+ } catch (const internal::Exception & e) {
+ LOGE("Fail to encode data (%d)", e.GetErrorCode());
+ throw std::bad_alloc();
+ }
+ }
return BundleRaw(
std::unique_ptr<bundle_raw, decltype(std::free)*>(raw, std::free), len);
}
int Bundle::GetCount() const {
- return bundle_get_count(impl_->handle_);
+ if (impl_->handle_ == nullptr)
+ return 0;
+ set_last_result(BUNDLE_ERROR_NONE);
+ return impl_->handle_->GetSize();
}
bundle_type Bundle::GetType(const std::string& key) const {
- return static_cast<bundle_type>(bundle_get_type(impl_->handle_, key.c_str()));
+ if (impl_->handle_ == nullptr || key.empty()) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ return BUNDLE_TYPE_NONE;
+ }
+
+ std::shared_ptr<internal::KeyInfo> key_info;
+ try {
+ key_info = impl_->handle_->Get(key);
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ return BUNDLE_TYPE_NONE;
+ }
+
+ set_last_result(BUNDLE_ERROR_NONE);
+ return static_cast<bundle_type>(key_info->GetType());
}
bundle* Bundle::GetHandle() const {
- return impl_->handle_;
+ return reinterpret_cast<bundle*>(impl_->handle_);
}
bundle* Bundle::Detach() {
auto* h = impl_->handle_;
impl_->handle_ = nullptr;
- return h;
+ return reinterpret_cast<bundle*>(h);
}
std::vector<std::string> Bundle::Export() const {
- char** argv = nullptr;
- int argc = bundle_export_to_argv(impl_->handle_, &argv);
- if (argc < 0) {
- LOGE("bundle_export_to_argv() is failed");
+ if (impl_->handle_ == nullptr) {
+ set_last_result(BUNDLE_ERROR_INVALID_PARAMETER);
+ LOGE("bundle_export_to_argv() is failed.");
return {};
}
- std::vector<std::string> args(1);
- for (int i = 1; i < argc; ++i)
- args.push_back(argv[i]);
-
- bundle_free_exported_argv(argc, &argv);
+ std::vector<std::string> args;
+ try {
+ args = impl_->handle_->Export();
+ args[1] = std::string(internal::TAG_IMPORT_EXPORT_CHECK);
+ } catch (const internal::Exception& e) {
+ set_last_result(e.GetErrorCode());
+ LOGE("bundle_export_to_argv() is failed");
+ return {};
+ }
return args;
}