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"
24 #include "bundle_log.h"
26 namespace tizen_base {
27 Bundle::Impl::Impl(Bundle* parent, bool copy, bool own)
28 : copy_(copy), own_(own), parent_(parent) {
31 Bundle::Impl::Impl(Bundle* parent) : parent_(parent) {
34 Bundle::Impl::~Impl() = default;
37 : impl_(new Impl(this)) {
38 impl_->handle_ = bundle_create();
39 if (impl_->handle_ == nullptr)
40 throw std::bad_alloc();
43 Bundle::Bundle(BundleRaw raw)
44 : impl_(new Impl(this)) {
45 impl_->handle_ = bundle_decode(raw.first.get(), raw.second);
46 if (impl_->handle_ == nullptr)
47 throw std::bad_alloc();
50 Bundle::Bundle(const std::string& raw)
51 : impl_(new Impl(this)) {
52 impl_->handle_ = bundle_decode(reinterpret_cast<const bundle_raw*>(
53 raw.c_str()), raw.length());
54 if (impl_->handle_ == nullptr)
55 throw std::bad_alloc();
58 Bundle::Bundle(bundle* b, bool copy, bool own)
59 : impl_(new Impl(this, copy, own)) {
61 throw std::invalid_argument("b cannot be null");
66 impl_->handle_ = bundle_dup(b);
67 if (impl_->handle_ == nullptr)
68 throw std::bad_alloc();
73 if (impl_->handle_ && (impl_->own_ || impl_->copy_))
74 bundle_free(impl_->handle_);
77 Bundle::Bundle(const Bundle& b)
78 : impl_(new Impl(this)) {
79 impl_->handle_ = bundle_dup(b.impl_->handle_);
80 if (impl_->handle_ == nullptr)
81 throw std::bad_alloc();
84 Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name)
85 : impl_(new Impl(this, handle, std::move(name))) {
88 Bundle::KeyInfo::~KeyInfo() {
91 Bundle::KeyInfo::Impl::~Impl() = default;
92 Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent,
93 const bundle_keyval_t* handle,
95 : handle_(handle), name_(name), parent_(parent) {
98 Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent) : parent_(parent) {
101 Bundle::KeyInfo::KeyInfo(const KeyInfo& k)
102 : impl_(new Impl(this)) {
103 impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
104 impl_->name_ = k.impl_->name_;
105 if (impl_->handle_ == nullptr)
106 throw std::bad_alloc();
109 Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) {
111 impl_->handle_ = bundle_keyval_dup(k.impl_->handle_);
112 impl_->name_ = k.impl_->name_;
113 if (impl_->handle_ == nullptr)
114 throw std::bad_alloc();
119 Bundle::KeyInfo::KeyInfo(Bundle::KeyInfo&& k) noexcept {
120 impl_ = std::unique_ptr<Impl>(new Impl(this));
121 impl_->handle_ = k.impl_->handle_;
122 impl_->name_ = k.impl_->name_;
123 k.impl_->handle_ = nullptr;
127 Bundle::KeyInfo& Bundle::KeyInfo::operator = (Bundle::KeyInfo&& k) noexcept {
129 impl_->handle_ = k.impl_->handle_;
130 impl_->name_ = k.impl_->name_;
131 k.impl_->handle_ = nullptr;
137 bundle_type Bundle::KeyInfo::GetType() const {
138 return static_cast<bundle_type>(
139 bundle_keyval_get_type(const_cast<bundle_keyval_t*>(impl_->handle_)));
142 bool Bundle::KeyInfo::IsArray() const {
143 return bundle_keyval_type_is_array(const_cast<bundle_keyval_t*>(
147 const std::string& Bundle::KeyInfo::GetName() const {
151 Bundle& Bundle::operator = (const Bundle& b) {
153 impl_->handle_ = bundle_dup(b.impl_->handle_);
154 if (impl_->handle_ == nullptr)
155 throw std::bad_alloc();
160 Bundle::Bundle(Bundle&& b) noexcept {
161 impl_ = std::unique_ptr<Impl>(new Impl(this));
162 impl_->handle_ = b.impl_->handle_;
163 b.impl_->handle_ = nullptr;
166 Bundle& Bundle::operator = (Bundle&& b) noexcept {
168 impl_->handle_ = b.impl_->handle_;
169 b.impl_->handle_ = nullptr;
174 std::vector<Bundle::KeyInfo> Bundle::GetKeys() {
175 std::vector<Bundle::KeyInfo> v;
177 bundle_foreach(impl_->handle_, [](const char *key, const int type,
178 const bundle_keyval_t *kv, void *user_data) {
179 auto* v = static_cast<std::vector<KeyInfo>*>(user_data);
180 v->emplace_back(kv, key);
186 int Bundle::Add(const std::string& key, const std::string& val) {
187 int ret = bundle_add_str(impl_->handle_, key.c_str(), val.c_str());
188 if (ret != BUNDLE_ERROR_NONE)
189 LOGE("Add fail key(%s), val(%s), ret(%d)", key.c_str(), val.c_str(), ret);
193 int Bundle::Add(const std::string& key, const std::vector<std::string>& val) {
194 std::vector<const char*> v;
195 for (auto& i : val) {
196 v.push_back(i.c_str());
199 int ret = bundle_add_str_array(
200 impl_->handle_, key.c_str(), v.data(), v.size());
201 if (ret != BUNDLE_ERROR_NONE)
202 LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
207 int Bundle::Add(const std::string& key, const std::vector<unsigned char>& val) {
208 int ret = bundle_add_byte(impl_->handle_, key.c_str(), val.data(), val.size());
209 if (ret != BUNDLE_ERROR_NONE)
210 LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
214 int Bundle::Delete(const std::string& key) {
215 int ret = bundle_del(impl_->handle_, key.c_str());
216 if (ret != BUNDLE_ERROR_NONE)
217 LOGE("Add fail key(%s), ret(%d)", key.c_str(), ret);
221 std::string Bundle::GetString(const std::string& key) const {
223 bundle_get_str(impl_->handle_, key.c_str(), &str);
228 return std::string(str);
231 std::vector<std::string> Bundle::GetStringArray(const std::string& key) const {
232 std::vector<std::string> v;
234 const char** str_array = nullptr;
237 str_array = bundle_get_str_array(impl_->handle_, key.c_str(), &len);
239 for (int i = 0; i < len; i++) {
240 v.emplace_back(str_array[i]);
246 std::vector<unsigned char> Bundle::GetByte(const std::string& key) const {
248 unsigned char* bytes = nullptr;
249 bundle_get_byte(impl_->handle_, key.c_str(),
250 reinterpret_cast<void**>(&bytes), &size);
251 return std::vector<unsigned char>(bytes, bytes + size);
254 Bundle::BundleRaw Bundle::ToRaw() {
255 bundle_raw* raw = nullptr;
257 int ret = bundle_encode(impl_->handle_, &raw, &len);
258 if (raw == nullptr) {
259 LOGE("Fail to encode data (%d)", ret);
260 throw std::bad_alloc();
264 std::unique_ptr<bundle_raw, decltype(std::free)*>(raw, std::free), len);
267 int Bundle::GetCount() const {
268 return bundle_get_count(impl_->handle_);
271 bundle_type Bundle::GetType(const std::string& key) const {
272 return static_cast<bundle_type>(bundle_get_type(impl_->handle_, key.c_str()));
275 bundle* Bundle::GetHandle() const {
276 return impl_->handle_;
279 bundle* Bundle::Detach() {
280 auto* h = impl_->handle_;
281 impl_->handle_ = nullptr;
285 } // namespace tizen_base