Remove unused constructor
[platform/core/base/bundle.git] / src / bundle_cpp.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <dlog.h>
18
19 #include <memory>
20
21 #include "bundle_cpp_implementation.h"
22 #include "bundle_cpp.h"
23 #include "bundle_internal.h"
24
25
26 #ifdef LOG_TAG
27 #undef LOG_TAG
28 #endif
29
30 #define LOG_TAG "BUNDLE"
31
32 namespace tizen_base {
33 Bundle::Impl::Impl(Bundle* parent, bool copy, bool own)
34   : copy_(copy), own_(own), parent_(parent) {
35 }
36
37 Bundle::Impl::Impl(Bundle* parent) : parent_(parent) {
38 }
39
40 Bundle::Impl::~Impl() = default;
41
42 Bundle::Bundle()
43   : impl_(new Impl(this)) {
44   impl_->handle_ = bundle_create();
45   if (impl_->handle_ == nullptr)
46     throw std::bad_alloc();
47 }
48
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();
54 }
55
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();
62 }
63
64 Bundle::Bundle(bundle* b, bool copy, bool own)
65   : impl_(new Impl(this, copy, own)) {
66   if (b == nullptr)
67     throw std::invalid_argument("b cannot be null");
68
69   if (!impl_->copy_) {
70     impl_->handle_ = b;
71   } else {
72     impl_->handle_ = bundle_dup(b);
73     if (impl_->handle_ == nullptr)
74       throw std::bad_alloc();
75   }
76 }
77
78 Bundle::~Bundle() {
79   if (impl_->handle_ && (impl_->own_ || impl_->copy_))
80     bundle_free(impl_->handle_);
81 }
82
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();
88 }
89
90 Bundle::KeyInfo::KeyInfo(const bundle_keyval_t* handle, std::string name)
91   : impl_(new Impl(this, handle, std::move(name))) {
92 }
93
94 Bundle::KeyInfo::~KeyInfo() {
95 }
96
97 Bundle::KeyInfo::Impl::~Impl() = default;
98 Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent,
99                             const bundle_keyval_t* handle,
100                             std::string name)
101   : handle_(handle), name_(name), parent_(parent) {
102 }
103
104 Bundle::KeyInfo::Impl::Impl(Bundle::KeyInfo* parent) : parent_(parent) {
105 }
106
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();
113 }
114
115 Bundle::KeyInfo& Bundle::KeyInfo::operator = (const Bundle::KeyInfo& k) {
116   if (this != &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();
121   }
122   return *this;
123 }
124
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;
130   k.impl_->name_ = "";
131 }
132
133 Bundle::KeyInfo& Bundle::KeyInfo::operator = (Bundle::KeyInfo&& k) noexcept {
134   if (this != &k) {
135     impl_->handle_ = k.impl_->handle_;
136     impl_->name_ = k.impl_->name_;
137     k.impl_->handle_ = nullptr;
138     k.impl_->name_ = "";
139   }
140   return *this;
141 }
142
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_)));
146 }
147
148 bool Bundle::KeyInfo::IsArray() const {
149   return bundle_keyval_type_is_array(const_cast<bundle_keyval_t*>(
150       impl_->handle_));
151 }
152
153 const std::string& Bundle::KeyInfo::GetName() const {
154   return impl_->name_;
155 }
156
157 Bundle& Bundle::operator = (const Bundle& b) {
158   if (this != &b) {
159     impl_->handle_ = bundle_dup(b.impl_->handle_);
160     if (impl_->handle_ == nullptr)
161       throw std::bad_alloc();
162   }
163   return *this;
164 }
165
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;
170 }
171
172 Bundle& Bundle::operator = (Bundle&& b) noexcept {
173   if (this != &b) {
174     impl_->handle_ = b.impl_->handle_;
175     b.impl_->handle_ = nullptr;
176   }
177   return *this;
178 }
179
180 std::vector<Bundle::KeyInfo> Bundle::GetKeys() {
181   std::vector<Bundle::KeyInfo> v;
182
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);
187       }, &v);
188
189   return v;
190 }
191
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);
196   return ret;
197 }
198
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());
203   }
204
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);
209
210   return ret;
211 }
212
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);
217   return ret;
218 }
219
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);
224   return ret;
225 }
226
227 std::string Bundle::GetString(const std::string& key) const {
228   char* str = nullptr;
229   bundle_get_str(impl_->handle_, key.c_str(), &str);
230
231   if (!str)
232     return "";
233
234   return std::string(str);
235 }
236
237 std::vector<std::string> Bundle::GetStringArray(const std::string& key) const {
238   std::vector<std::string> v;
239
240   const char** str_array = nullptr;
241   int len = 0;
242
243   str_array = bundle_get_str_array(impl_->handle_, key.c_str(), &len);
244
245   for (int i = 0; i < len; i++) {
246     v.emplace_back(str_array[i]);
247   }
248
249   return v;
250 }
251
252 std::vector<unsigned char> Bundle::GetByte(const std::string& key) const {
253   size_t size;
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);
258 }
259
260 Bundle::BundleRaw Bundle::ToRaw() {
261   bundle_raw* raw = nullptr;
262   int len = 0;
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();
267   }
268
269   return BundleRaw(
270       std::unique_ptr<bundle_raw, decltype(std::free)*>(raw, std::free), len);
271 }
272
273 int Bundle::GetCount() const {
274   return bundle_get_count(impl_->handle_);
275 }
276
277 bundle_type Bundle::GetType(const std::string& key) const {
278   return static_cast<bundle_type>(bundle_get_type(impl_->handle_, key.c_str()));
279 }
280
281 bundle* Bundle::GetHandle() const {
282   return impl_->handle_;
283 }
284
285 bundle* Bundle::Detach() {
286   auto* h = impl_->handle_;
287   impl_->handle_ = nullptr;
288   return h;
289 }
290
291 }  // namespace tizen_base