Remove unnecessary parcelable flag
[platform/core/base/bundle.git] / parcel / parcel.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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 "include/bundle_cpp.h"
18 #include "parcel/common.hh"
19 #include "parcel/log_private.hh"
20 #include "parcel/parcel.hh"
21 #include "parcel/parcel_implementation.hh"
22 #include "parcel/parcelable.hh"
23
24 namespace tizen_base {
25
26 Parcel::Impl::Impl(Parcel* parent) : parent_(parent) {
27 }
28
29 Parcel::Impl::~Impl() = default;
30
31 void Parcel::Impl::Write(const void* buf, uint32_t size) {
32   auto* p = reinterpret_cast<const uint8_t*>(buf);
33   std::copy(p, p + size, std::back_inserter(data_));
34 }
35
36 int Parcel::Impl::Read(void* buf, uint32_t size) {
37   if (data_.size() == 0)
38     return TIZEN_ERROR_NO_DATA;
39
40   if (reader_ + size > data_.size())
41     return TIZEN_ERROR_ILLEGAL_BYTE_SEQ;
42
43   auto* p = reinterpret_cast<uint8_t*>(buf);
44   std::copy(&data_[reader_], &data_[reader_] + size, p);
45   reader_ += size;
46   set_last_result(TIZEN_ERROR_NONE);
47   return TIZEN_ERROR_NONE;
48 }
49
50 const std::vector<uint8_t>& Parcel::Impl::GetRaw() {
51   return data_;
52 }
53
54 void Parcel::Impl::ResetReader() {
55   reader_ = 0;
56 }
57
58 void Parcel::Impl::Clear() {
59   data_.clear();
60   reader_ = 0;
61 }
62
63 void Parcel::Impl::Reset(const void* buf, uint32_t size) {
64   Clear();
65   Write(buf, size);
66 }
67
68 bool Parcel::Impl::IsEmpty() {
69   return data_.size() == 0;
70 }
71
72 void Parcel::Impl::WriteSize(uint32_t size) {
73   auto* p = reinterpret_cast<uint8_t*>(&size);
74   std::copy(p, p + sizeof(size), std::back_inserter(data_));
75 }
76
77 template <typename T>
78 void Parcel::Impl::Write(T d) {
79   auto* p = reinterpret_cast<uint8_t*>(&d);
80   std::copy(p, p + sizeof(T), std::back_inserter(data_));
81 }
82
83 int Parcel::Impl::ReadSize(uint32_t* size) {
84   if (data_.size() == 0)
85     return TIZEN_ERROR_NO_DATA;
86
87   if (reader_ + sizeof(uint32_t) > data_.size())
88     return TIZEN_ERROR_ILLEGAL_BYTE_SEQ;
89
90   auto* p = reinterpret_cast<uint8_t*>(size);
91   std::copy(&data_[reader_], &data_[reader_] + sizeof(uint32_t), p);
92   reader_ += sizeof(uint32_t);
93   return TIZEN_ERROR_NONE;
94 }
95
96 template <typename T>
97 int Parcel::Impl::Read(T* d) {
98   uint32_t size = sizeof(T);
99   if (reader_ + size > data_.size())
100     return TIZEN_ERROR_ILLEGAL_BYTE_SEQ;
101
102   auto* p = reinterpret_cast<uint8_t*>(d);
103   std::copy(&data_[reader_], &data_[reader_] + size, p);
104   reader_ += size;
105   return TIZEN_ERROR_NONE;
106 }
107
108 Parcel::Parcel() : impl_(new Impl(this)) {
109 }
110
111 Parcel::Parcel(const void* buf, uint32_t size) : impl_(new Impl(this)) {
112   impl_->Write(buf, size);
113 }
114
115 Parcel::~Parcel() = default;
116
117 Parcel::Parcel(const Parcel& p)
118   : impl_(new Impl(this)) {
119   std::copy(p.impl_->data_.begin(), p.impl_->data_.end(),
120       std::back_inserter(impl_->data_));
121   impl_->reader_ = p.impl_->reader_;
122 }
123
124 Parcel& Parcel::operator = (const Parcel& p) {
125   if (this != &p) {
126     std::copy(p.impl_->data_.begin(), p.impl_->data_.end(),
127         std::back_inserter(impl_->data_));
128     impl_->reader_ = p.impl_->reader_;
129   }
130   return *this;
131 }
132
133 Parcel::Parcel(Parcel&& p) noexcept {
134   impl_ = std::move(p.impl_);
135   impl_->parent_ = this;
136 }
137
138 Parcel& Parcel::operator = (Parcel&& p) noexcept {
139   if (this != &p) {
140     impl_->data_ = std::move(p.impl_->data_);
141     impl_->reader_ = p.impl_->reader_;
142     p.impl_->reader_ = 0;
143   }
144   return *this;
145 }
146
147 bool Parcel::IsEmpty() const noexcept {
148   return impl_->IsEmpty();
149 }
150
151 void Parcel::Write(const void* buf, uint32_t size) {
152   impl_->Write(buf, size);
153 }
154
155 int Parcel::Read(void* buf, uint32_t size) {
156   return impl_->Read(buf, size);
157 }
158
159 void Parcel::WriteBool(bool val) {
160   impl_->Write<bool>(val);
161 }
162
163 void Parcel::WriteByte(char val) {
164   impl_->Write<char>(val);
165 }
166
167 void Parcel::WriteUInt16(uint16_t val) {
168   impl_->Write<uint16_t>(val);
169 }
170
171 void Parcel::WriteUInt32(uint32_t val) {
172   impl_->Write<uint32_t>(val);
173 }
174
175 void Parcel::WriteUInt64(uint64_t val) {
176   impl_->Write<uint64_t>(val);
177 }
178
179 void Parcel::WriteInt16(int16_t val) {
180   impl_->Write<int16_t>(val);
181 }
182
183 void Parcel::WriteInt32(int32_t val) {
184   impl_->Write<int32_t>(val);
185 }
186
187 void Parcel::WriteInt64(int64_t val) {
188   impl_->Write<int64_t>(val);
189 }
190
191 void Parcel::WriteFloat(float val) {
192   impl_->Write<float>(val);
193 }
194
195 void Parcel::WriteDouble(double val) {
196   impl_->Write<double>(val);
197 }
198
199 void Parcel::WriteString(const std::string& str) {
200   impl_->WriteSize(str.length() + 1);
201   impl_->Write(str.c_str(), str.length() + 1);
202 }
203
204 void Parcel::WriteCString(const char* str) {
205   impl_->WriteSize(strlen(str) + 1);
206   impl_->Write(str, strlen(str) + 1);
207 }
208
209 int Parcel::ReadBool(bool* val) {
210   return impl_->Read<bool>(val);
211 }
212
213 int Parcel::ReadByte(char* val) {
214   return impl_->Read<char>(val);
215 }
216
217 int Parcel::ReadUInt16(uint16_t* val) {
218   return impl_->Read<uint16_t>(val);
219 }
220
221 int Parcel::ReadUInt32(uint32_t* val) {
222   return impl_->Read<uint32_t>(val);
223 }
224
225 int Parcel::ReadUInt64(uint64_t* val) {
226   return impl_->Read<uint64_t>(val);
227 }
228
229 int Parcel::ReadInt16(int16_t* val) {
230   return impl_->Read<int16_t>(val);
231 }
232
233 int Parcel::ReadInt32(int32_t* val) {
234   return impl_->Read<int32_t>(val);
235 }
236
237 int Parcel::ReadInt64(int64_t* val) {
238   return impl_->Read<int64_t>(val);
239 }
240
241 int Parcel::ReadFloat(float* val) {
242   return impl_->Read<float>(val);
243 }
244
245 int Parcel::ReadDouble(double* val) {
246   return impl_->Read<double>(val);
247 }
248
249 std::string Parcel::ReadString() {
250   uint32_t len = 0;
251   int ret = impl_->ReadSize(&len);
252   if (ret != TIZEN_ERROR_NONE) {
253     set_last_result(ret);
254     return {};
255   }
256
257   char* str = new (std::nothrow) char [len];
258   if (str == nullptr) {
259     set_last_result(TIZEN_ERROR_OUT_OF_MEMORY);
260     return {};
261   }
262
263   auto ptr = std::unique_ptr<char[]>(str);
264   ret = impl_->Read(str, len);
265   if (ret != TIZEN_ERROR_NONE) {
266     set_last_result(ret);
267     return {};
268   }
269
270   set_last_result(TIZEN_ERROR_NONE);
271   return std::string(str);
272 }
273
274 int Parcel::ReadCString(char** str) {
275   uint32_t len = 0;
276   int ret = impl_->ReadSize(&len);
277   if (ret != TIZEN_ERROR_NONE)
278     return ret;
279
280   char* val = static_cast<char*>(calloc(len, sizeof(char)));
281   if (val == nullptr)
282     return TIZEN_ERROR_OUT_OF_MEMORY;
283
284   ret = impl_->Read(val, len);
285   if (ret != TIZEN_ERROR_NONE) {
286     free(val);
287     return ret;
288   }
289
290   *str = val;
291   return TIZEN_ERROR_NONE;
292 }
293
294 const std::vector<uint8_t>& Parcel::GetRaw() {
295   return impl_->GetRaw();
296 }
297
298 void Parcel::ResetReader() {
299   impl_->ResetReader();
300 }
301
302 void Parcel::Clear() {
303   impl_->Clear();
304 }
305
306 void Parcel::Reset(const void* buf, uint32_t size) {
307   impl_->Reset(buf, size);
308 }
309
310 void Parcel::WriteParcelable(const Parcelable& parcelable) {
311   parcelable.WriteToParcel(this);
312 }
313
314 int Parcel::ReadParcelable(Parcelable* parcelable) {
315   parcelable->ReadFromParcel(this);
316   return TIZEN_ERROR_NONE;
317 }
318
319 }  // naemspace tizen_base