2 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "bundle_cpp_implementation.h"
22 #include "bundle_cpp.h"
23 #include "bundle_internal.h"
30 #define LOG_TAG "BUNDLE"
32 namespace tizen_base {
33 Bundle::Impl::Impl(Bundle* parent, bool copy, bool own)
34 : copy_(copy), own_(own), parent_(parent) {
37 Bundle::Impl::Impl(Bundle* parent) : parent_(parent) {
40 Bundle::Impl::~Impl() = default;
43 : impl_(new Impl(this)) {
44 impl_->handle_ = bundle_create();
45 if (impl_->handle_ == nullptr)
46 throw std::bad_alloc();
49 Bundle::Bundle(BundleRaw raw)
50 : impl_(new Impl(this)) {
51 impl_->handle_ = bundle_decode(raw.first.get(), raw.second);
52 if (impl_->handle_ == nullptr)
53 throw std::bad_alloc();
56 Bundle::Bundle(const std::string& raw)
57 : impl_(new Impl(this)) {
58 impl_->handle_ = bundle_decode(reinterpret_cast<const bundle_raw*>(
59 raw.c_str()), raw.length());
60 if (impl_->handle_ == nullptr)
61 throw std::bad_alloc();
64 Bundle::Bundle(bundle* b, bool copy, bool own)
65 : impl_(new Impl(this, copy, own)) {
67 throw std::invalid_argument("b cannot be null");
72 impl_->handle_ = bundle_dup(b);
73 if (impl_->handle_ == nullptr)
74 throw std::bad_alloc();
79 if (impl_->handle_ && (impl_->own_ || impl_->copy_))
80 bundle_free(impl_->handle_);
83 Bundle::Bundle(const Bundle& b)
84 : impl_(new Impl(this)) {
85 impl_->handle_ = bundle_dup(b.impl_->handle_);
86 if (impl_->handle_ == nullptr)
87 throw std::bad_alloc();
90 Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name)
91 : impl_(new Impl(this, handle, std::move(name))) {
94 Bundle::KeyInfo::~KeyInfo() {
97 Bundle::KeyInfo::Impl::~Impl() = default;
98 Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent,
99 const bundle_keyval_t* handle,
101 : handle_(handle), name_(name), parent_(parent) {
104 Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent) : parent_(parent) {
107 Bundle::KeyInfo::KeyInfo(const KeyInfo& k)
108 : impl_(new Impl(this)) {
109 impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
110 impl_->name_ = k.impl_->name_;
111 if (impl_->handle_ == nullptr)
112 throw std::bad_alloc();
115 Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) {
117 impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
118 impl_->name_ = k.impl_->name_;
119 if (impl_->handle_ == nullptr)
120 throw std::bad_alloc();
125 Bundle::KeyInfo::KeyInfo(Bundle::KeyInfo&& k) noexcept {
126 impl_ = std::unique_ptr<Impl>(new Impl(this));
127 impl_->handle_ = k.impl_->handle_;
128 impl_->name_ = k.impl_->name_;
129 k.impl_->handle_ = nullptr;
133 Bundle::KeyInfo& Bundle::KeyInfo::operator = (Bundle::KeyInfo&& k) noexcept {
135 impl_->handle_ = k.impl_->handle_;
136 impl_->name_ = k.impl_->name_;
137 k.impl_->handle_ = nullptr;
143 bundle_type Bundle::KeyInfo::GetType() const {
144 return static_cast<bundle_type>(
145 bundle_keyval_get_type(const_cast<bundle_keyval_t*>(impl_->handle_)));
148 bool Bundle::KeyInfo::IsArray() const {
149 return bundle_keyval_type_is_array(const_cast<bundle_keyval_t*>(
153 const std::string& Bundle::KeyInfo::GetName() const {
157 Bundle& Bundle::operator = (const Bundle& b) {
159 impl_->handle_ = bundle_dup(b.impl_->handle_);
160 if (impl_->handle_ == nullptr)
161 throw std::bad_alloc();
166 Bundle::Bundle(Bundle&& b) noexcept {
167 impl_ = std::unique_ptr<Impl>(new Impl(this));
168 impl_->handle_ = b.impl_->handle_;
169 b.impl_->handle_ = nullptr;
172 Bundle& Bundle::operator = (Bundle&& b) noexcept {
174 impl_->handle_ = b.impl_->handle_;
175 b.impl_->handle_ = nullptr;
180 std::vector<Bundle::KeyInfo> Bundle::GetKeys() {
181 std::vector<Bundle::KeyInfo> v;
183 bundle_foreach(impl_->handle_, [](const char *key, const int type,
184 const bundle_keyval_t *kv, void *user_data) {
185 auto* v = static_cast<std::vector<KeyInfo>*>(user_data);
186 v->emplace_back(kv, key);
192 int Bundle::Add(const std::string& key, const std::string& val) {
193 int ret = bundle_add_str(impl_->handle_, key.c_str(), val.c_str());
194 if (ret != BUNDLE_ERROR_NONE)
195 LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(), ret);
199 int Bundle::Add(const std::string& key, const std::vector<std::string>& val) {
200 std::vector<const char*> v;
201 for (auto& i : val) {
202 v.push_back(i.c_str());
205 int ret = bundle_add_str_array(
206 impl_->handle_, key.c_str(), v.data(), v.size());
207 if (ret != BUNDLE_ERROR_NONE)
208 LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
213 int Bundle::Add(const std::string& key, const std::vector<unsigned char>& val) {
214 int ret = bundle_add_byte(impl_->handle_, key.c_str(), val.data(), val.size());
215 if (ret != BUNDLE_ERROR_NONE)
216 LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
220 int Bundle::Delete(const std::string& key) {
221 int ret = bundle_del(impl_->handle_, key.c_str());
222 if (ret != BUNDLE_ERROR_NONE)
223 LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
227 std::string Bundle::GetString(const std::string& key) const {
229 bundle_get_str(impl_->handle_, key.c_str(), &str);
234 return std::string(str);
237 std::vector<std::string> Bundle::GetStringArray(const std::string& key) const {
238 std::vector<std::string> v;
240 const char** str_array = nullptr;
243 str_array = bundle_get_str_array(impl_->handle_, key.c_str(), &len);
245 for (int i = 0; i < len; i++) {
246 v.emplace_back(str_array[i]);
252 std::vector<unsigned char> Bundle::GetByte(const std::string& key) const {
254 unsigned char* bytes = nullptr;
255 bundle_get_byte(impl_->handle_, key.c_str(),
256 reinterpret_cast<void**>(&bytes), &size);
257 return std::vector<unsigned char>(bytes, bytes + size);
260 Bundle::BundleRaw Bundle::ToRaw() {
261 bundle_raw* raw = nullptr;
263 int ret = bundle_encode(impl_->handle_, &raw, &len);
264 if (raw == nullptr) {
265 LOGE("Fail to encode data (%d)", ret);
266 throw std::bad_alloc();
270 std::unique_ptr<bundle_raw, decltype(std::free)*>(raw, std::free), len);
273 int Bundle::GetCount() const {
274 return bundle_get_count(impl_->handle_);
277 bundle_type Bundle::GetType(const std::string& key) const {
278 return static_cast<bundle_type>(bundle_get_type(impl_->handle_, key.c_str()));
281 bundle* Bundle::GetHandle() const {
282 return impl_->handle_;
285 bundle* Bundle::Detach() {
286 auto* h = impl_->handle_;
287 impl_->handle_ = nullptr;
291 } // namespace tizen_base