Add Parcel Library
[platform/core/base/bundle.git] / parcel / parcel.hh
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 #ifndef PARCEL_PARCEL_HH_
18 #define PARCEL_PARCEL_HH_
19
20 /**
21  * @addtogroup CORE_LIB_PARCEL_MODULE
22  * @{
23  */
24
25 #include <bundle_cpp.h>
26 #include <tizen.h>
27
28 #include <memory>
29 #include <string>
30 #include <vector>
31
32 #include "parcel/common.hh"
33 #include "parcel/parcelable.hh"
34
35 namespace tizen_base {
36
37 /**
38  * @brief The class for Parcel API.
39  * @since_tizen 6.5
40  */
41 class EXPORT Parcel final {
42  public:
43   /**
44    * @brief Enumeration for parcel error.
45    * @since_tizen 6.5
46    */
47   enum Error {
48     None = TIZEN_ERROR_NONE, /**< Successful */
49     IllegalByteSeq = TIZEN_ERROR_ILLEGAL_BYTE_SEQ, /**< Illegal byte sequence */
50     InvalidParameter = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
51     NoData = TIZEN_ERROR_NO_DATA, /**< No data available */
52     OutOfMemory = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
53   };
54
55   /**
56    * @brief Constructor.
57    * @since_tizen 6.5
58    */
59   Parcel();
60
61   /**
62    * @brief Constructor
63    * @since_tizen 6.5
64    * @param[in] buf The bytes to write
65    * @param[in] size the size of bytes to write
66    */
67   Parcel(const void* buf, uint32_t size);
68
69   /**
70    * @brief Destructor
71    * @since_tizen 6.5
72    */
73   ~Parcel();
74
75   /**
76    * @brief Copy-constructor.
77    * @since_tizen 6.5
78    * @param[in] p The object to copy
79    */
80   Parcel(const Parcel& p);
81
82   /**
83    * @brief Assignment.
84    * @since_tizen 6.5
85    * @param[in] b The object to copy
86    */
87   Parcel& operator = (const Parcel& p);
88
89   /**
90    * @brief Move-constructor.
91    * @since_tizen 6.5
92    * @param[in] p The object to move
93    */
94   Parcel(Parcel&& p) noexcept;
95
96   /**
97    * @brief Assignment.
98    * @since_tizen 6.5
99    * @param[in] p The object to move
100    */
101   Parcel& operator = (Parcel&& p) noexcept;
102
103   /**
104    * @brief Checks the parcel is empty or not.
105    * @since_tizen 6.5
106    * @return true if the parcel is empty
107    */
108   bool IsEmpty() const noexcept;
109
110   /**
111    * @brief Writes bytes into the parcel.
112    * @since_tizen 6.5
113    * @param[in] buf The bytes to write
114    * @param[in] size The size of bytes to write
115    */
116   void Write(const void* buf, uint32_t size);
117
118   /**
119    * @brief Reads bytes from the parcel.
120    * @since_tizen 6.5
121    * @param[out] buf The bytes to read
122    * @param[in] size The size of bytes to read
123    * @return @c 0 on success,
124    *         otherwise a negative error value
125    */
126   int Read(void* buf, uint32_t size);
127
128   /**
129    * @brief Writes a boolean value into the parcel.
130    * @since_tizen 6.5
131    * @param[in] val The boolean value
132    */
133   void WriteBool(bool val);
134
135   /**
136    * @brief Writes a byte value into the parcel.
137    * @since_tizen 6.5
138    * @param[in] val The unsigned byte value
139    */
140   void WriteByte(char byte);
141
142   /**
143    * @brief Writes an unsigned short value into the parcel.
144    * @since_tizen 6.5
145    * @param[in] val The unsigned short value
146    */
147   void WriteUInt16(uint16_t val);
148
149   /**
150    * @brief Writes an unsigned integer value into the parcel.
151    * @since_tizen 6.5
152    * @param[in] val The unsigned integer value
153    */
154   void WriteUInt32(uint32_t val);
155
156   /**
157    * @brief Writes an unsigned long long integer into the parcel.
158    * @since_tizen 6.5
159    * @param[in] val The unsigned long long integer value
160    */
161   void WriteUInt64(uint64_t val);
162
163   /**
164    * @brief Writes a signed short value into the parcel.
165    * @since_tizen 6.5
166    * @param[in] val The signed short value
167    */
168   void WriteInt16(int16_t val);
169
170   /**
171    * @brief Writes a signed integer value into the parcel.
172    * @since_tizen 6.5
173    * @param[in] val The signed integer value
174    */
175   void WriteInt32(int32_t val);
176
177   /**
178    * @brief Writes a signed long long integer value into the parcel.
179    * @since_tizen 6.5
180    * @param[in] val The signed long long integer value
181    */
182   void WriteInt64(int64_t val);
183
184   /**
185    * @brief Writes a floating point value into the parcel.
186    * @since_tizen 6.5
187    * @param[in] val The floating point value
188    */
189   void WriteFloat(float val);
190
191   /**
192    * @brief Writes a double precision floating point value into the parcel.
193    * @since_tizen 6.5
194    * @param[in] val The double precision floating point value
195    */
196   void WriteDouble(double val);
197
198   /**
199    * @brief Writes a string data into the parcel.
200    * @since_tizen 6.5
201    * @param[in] str The string data
202    */
203   void WriteString(const std::string& str);
204
205   /**
206    * @brief Writes a string data into the parcel.
207    * @since_tizen 6.5
208    * @param[in] str The string data
209    */
210   void WriteCString(const char* str);
211
212   /**
213    * @brief Writes a bundle data into the parcel.
214    * @since_tizen 6.5
215    * @param[in] b The bundle data
216    */
217   void WriteCBundle(bundle* b);
218
219   /**
220    * @brief Writes a bundle data into the parcel.
221    * @since_tizen 6.5
222    * @param[in] b The bundle data
223    */
224   void WriteBundle(const Bundle& b);
225
226   /**
227    * @brief Reads a boolean value from the parcel.
228    * @since_tizen 6.5
229    * @param[out] val The boolean value
230    * @return @c 0 on success,
231    *         otherwise a negative error value
232    */
233   int ReadBool(bool* val);
234
235   /**
236    * @brief Reads a byte value from the parcel.
237    * @since_tizen 6.5
238    * @param[out] val The unsigned byte value
239    * @return @c 0 on success,
240    *         otherwise a negative error value
241    */
242   int ReadByte(char* val);
243
244   /**
245    * @breif Reads an unsigned short value from the parcel.
246    * @since_tizen 6.5
247    * @param[out] val The unsigned short value
248    * @return @c 0 on success,
249    *         otherwise a negative error value
250    */
251   int ReadUInt16(uint16_t* val);
252
253   /**
254    * @brief Reads an unsigned integer value from the parcel.
255    * @since_tizen 6.5
256    * @param[out] val The unsigned integer value
257    * @return @c 0 on success,
258    *         otherwise a negative error value
259    */
260   int ReadUInt32(uint32_t* val);
261
262   /**
263    * @brief Reads an unsigned long long integer value from the parcel.
264    * @since_tizen 6.5
265    * @param[out] val The unsigned long long integer value
266    * @return @c 0 on success,
267    *         otherwise a negative error value
268    */
269   int ReadUInt64(uint64_t* val);
270
271   /**
272    * @brief Reads a signed byte value from the parcel.
273    * @since_tizen 6.5
274    * @param[out] val The signed byte value
275    * @return @c 0 on success,
276    *         otherwise a negative error value
277    */
278   int ReadInt8(int8_t* val);
279
280   /**
281    * @brief Reads a signed short value from the parcel.
282    * @since_tizen 6.5
283    * @param[out] val The signed short value
284    * @return @c 0 on success,
285    *         otherwise a negative error value
286    */
287   int ReadInt16(int16_t* val);
288
289   /**
290    * @brief Reads a signed integer value from the parcel.
291    * @since_tizen 6.5
292    * @param[out] val The signed integer value
293    * @return @c 0 on success,
294    *         otherwise a negative error value
295    */
296   int ReadInt32(int32_t* val);
297
298   /**
299    * @brief Reads a signed long long integer value from the parcel.
300    * @since_tizen 6.5
301    * @param[out] val The signed long long integer value
302    * @return @c 0 on success,
303    *         otherwise a negative error value
304    */
305   int ReadInt64(int64_t* val);
306
307   /**
308    * @brief Reads a floating point value from the parcel.
309    * @since_tizen 6.5
310    * @param[out] val The floating point value
311    * @return @c 0 on success,
312    *         otherwise a negative error value
313    */
314   int ReadFloat(float* val);
315
316   /**
317    * @brief Reads a double precision floating point value from the parcel.
318    * @since_tizen 6.5
319    * @param[out] val The double precision floating point value
320    * @return @c 0 on success,
321    *         otherwise a negative error value
322    */
323   int ReadDouble(double* val);
324
325   /**
326    * @brief Reads a string data from the parcel.
327    * @since_tizen 6.5
328    * @return The string data
329    * @remarks Before using the returned value, you should check the result using get_last_result().
330    * @see get_last_result()
331    */
332   std::string ReadString();
333
334   /**
335    * @brief Reads a string data from the parcel.
336    * @since_tizen 6.5
337    * @remarks You should release @a str using free().
338    * @param[out] str The string data
339    * @return @c 0 on success,
340    *         otherwise a negative error value
341    */
342   int ReadCString(char** str);
343
344   /**
345    * @brief Reads a bundle data from the parcel.
346    * @since_tizen 6.5
347    * @remarks You should release @a b using bundle_free().
348    * @param[out] b The bundle data
349    * @return @c 0 on success,
350    *         otherwise a negative error value
351    */
352   int ReadCBundle(bundle** b);
353
354   /**
355    * @brief Reads a bundle data from the parcel.
356    * @since_tizen 6.5
357    * @return The bundle data
358    * @remarks Before using the returned value, you should check the result using get_last_result().
359    * @see get_last_result();
360    */
361   Bundle ReadBundle();
362
363   /**
364    * @brief Gets the raw data of the parcel.
365    * @since_tizen 6.5
366    * @return The raw data
367    */
368   const std::vector<uint8_t>& GetRaw();
369
370   /**
371    * @brief Resets the reader pointer of the parcel to the start.
372    * @since_tizen 6.5
373    */
374   void ResetReader();
375
376   /**
377    * @brief Clears the data of the parcel.
378    * @since_tizen 6.5
379    */
380   void Clear();
381
382   /**
383    * @brief Resets the parcel.
384    * @since_tizen 6.5
385    * @param[in] buf The bytes to write
386    * @param[in] size The size of bytes to write
387    */
388   void Reset(const void* buf, uint32_t size);
389
390   /**
391    * @brief Writes the data using @a parcelable into the parcel.
392    * @since_tizen 6.5
393    * @param[in] parcelable The interface to write the data into the parcel
394    */
395   void WriteParcelable(const Parcelable& parcelable);
396
397   /**
398    * @brief Reads the data using @a parcelable from the parcel.
399    * @since_tizen 6.5
400    * @param[out] parcelable The interface to get data from the parcel
401    * @return @c 0 on success,
402    *         otherwise a negative error value
403    */
404   int ReadParcelable(Parcelable* parcelable);
405
406  private:
407   class Impl;
408   std::unique_ptr<Impl> impl_;
409 };
410
411 }  // naemspace tizen_base
412
413 /**
414  * @}
415  */
416
417 #endif  // PARCEL_PARCEL_HH_