2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
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.
17 #ifndef PARCEL_PARCEL_HH_
18 #define PARCEL_PARCEL_HH_
21 * @addtogroup CORE_LIB_PARCEL_MODULE
31 #include "parcel/common.hh"
32 #include "parcel/parcelable.hh"
34 namespace tizen_base {
37 * @brief The class for Parcel API.
40 class EXPORT Parcel final {
43 * @brief Enumeration for parcel error.
47 None = TIZEN_ERROR_NONE, /**< Successful */
48 IllegalByteSeq = TIZEN_ERROR_ILLEGAL_BYTE_SEQ, /**< Illegal byte sequence */
49 InvalidParameter = TIZEN_ERROR_INVALID_PARAMETER, /**< Invalid parameter */
50 NoData = TIZEN_ERROR_NO_DATA, /**< No data available */
51 OutOfMemory = TIZEN_ERROR_OUT_OF_MEMORY, /**< Out of memory */
57 * @param[in] big_endian @c true, if byte order of the parcel is big-endian
59 Parcel(bool big_endian = false);
64 * @param[in] buf The bytes to write
65 * @param[in] size the size of bytes to write
66 * @param[in] big_endian @c true, if byte order of the parcel is big-endian
68 Parcel(const void* buf, uint32_t size, bool big_endian = false);
77 * @brief Copy-constructor.
79 * @param[in] p The object to copy
81 Parcel(const Parcel& p);
86 * @param[in] b The object to copy
88 Parcel& operator = (const Parcel& p);
91 * @brief Move-constructor.
93 * @param[in] p The object to move
95 Parcel(Parcel&& p) noexcept;
100 * @param[in] p The object to move
102 Parcel& operator = (Parcel&& p) noexcept;
105 * @brief Checks the parcel is empty or not.
107 * @return true if the parcel is empty
109 bool IsEmpty() const noexcept;
112 * @brief Writes bytes into the parcel.
114 * @param[in] buf The bytes to write
115 * @param[in] size The size of bytes to write
117 void Write(const void* buf, uint32_t size);
120 * @brief Reads bytes from the parcel.
122 * @param[out] buf The bytes to read
123 * @param[in] size The size of bytes to read
124 * @return @c 0 on success,
125 * otherwise a negative error value
127 int Read(void* buf, uint32_t size);
130 * @brief Writes a boolean value into the parcel.
132 * @param[in] val The boolean value
134 void WriteBool(bool val);
137 * @brief Writes a byte value into the parcel.
139 * @param[in] val The unsigned byte value
141 void WriteByte(char byte);
144 * @brief Writes an unsigned short value into the parcel.
146 * @param[in] val The unsigned short value
148 void WriteUInt16(uint16_t val);
151 * @brief Writes an unsigned integer value into the parcel.
153 * @param[in] val The unsigned integer value
155 void WriteUInt32(uint32_t val);
158 * @brief Writes an unsigned long long integer into the parcel.
160 * @param[in] val The unsigned long long integer value
162 void WriteUInt64(uint64_t val);
165 * @brief Writes a signed short value into the parcel.
167 * @param[in] val The signed short value
169 void WriteInt16(int16_t val);
172 * @brief Writes a signed integer value into the parcel.
174 * @param[in] val The signed integer value
176 void WriteInt32(int32_t val);
179 * @brief Writes a signed long long integer value into the parcel.
181 * @param[in] val The signed long long integer value
183 void WriteInt64(int64_t val);
186 * @brief Writes a floating point value into the parcel.
188 * @param[in] val The floating point value
190 void WriteFloat(float val);
193 * @brief Writes a double precision floating point value into the parcel.
195 * @param[in] val The double precision floating point value
197 void WriteDouble(double val);
200 * @brief Writes a string data into the parcel.
202 * @param[in] str The string data
204 void WriteString(const std::string& str);
207 * @brief Writes a string data into the parcel.
209 * @param[in] str The string data
211 void WriteCString(const char* str);
214 * @brief Reads a boolean value from the parcel.
216 * @param[out] val The boolean value
217 * @return @c 0 on success,
218 * otherwise a negative error value
220 int ReadBool(bool* val);
223 * @brief Reads a byte value from the parcel.
225 * @param[out] val The unsigned byte value
226 * @return @c 0 on success,
227 * otherwise a negative error value
229 int ReadByte(char* val);
232 * @breif Reads an unsigned short value from the parcel.
234 * @param[out] val The unsigned short value
235 * @return @c 0 on success,
236 * otherwise a negative error value
238 int ReadUInt16(uint16_t* val);
241 * @brief Reads an unsigned integer value from the parcel.
243 * @param[out] val The unsigned integer value
244 * @return @c 0 on success,
245 * otherwise a negative error value
247 int ReadUInt32(uint32_t* val);
250 * @brief Reads an unsigned long long integer value from the parcel.
252 * @param[out] val The unsigned long long integer value
253 * @return @c 0 on success,
254 * otherwise a negative error value
256 int ReadUInt64(uint64_t* val);
259 * @brief Reads a signed byte value from the parcel.
261 * @param[out] val The signed byte value
262 * @return @c 0 on success,
263 * otherwise a negative error value
265 int ReadInt8(int8_t* val);
268 * @brief Reads a signed short value from the parcel.
270 * @param[out] val The signed short value
271 * @return @c 0 on success,
272 * otherwise a negative error value
274 int ReadInt16(int16_t* val);
277 * @brief Reads a signed integer value from the parcel.
279 * @param[out] val The signed integer value
280 * @return @c 0 on success,
281 * otherwise a negative error value
283 int ReadInt32(int32_t* val);
286 * @brief Reads a signed long long integer value from the parcel.
288 * @param[out] val The signed long long integer value
289 * @return @c 0 on success,
290 * otherwise a negative error value
292 int ReadInt64(int64_t* val);
295 * @brief Reads a floating point value from the parcel.
297 * @param[out] val The floating point value
298 * @return @c 0 on success,
299 * otherwise a negative error value
301 int ReadFloat(float* val);
304 * @brief Reads a double precision floating point value from the parcel.
306 * @param[out] val The double precision floating point value
307 * @return @c 0 on success,
308 * otherwise a negative error value
310 int ReadDouble(double* val);
313 * @brief Reads a string data from the parcel.
315 * @return The string data
316 * @remarks Before using the returned value, you should check the result using get_last_result().
317 * @see get_last_result()
319 std::string ReadString();
322 * @brief Reads a string data from the parcel.
324 * @remarks You should release @a str using free().
325 * @param[out] str The string data
326 * @return @c 0 on success,
327 * otherwise a negative error value
329 int ReadCString(char** str);
332 * @brief Gets the raw data of the parcel.
334 * @return The raw data
336 const std::vector<uint8_t>& GetRaw();
339 * @brief Resets the reader pointer of the parcel to the start.
345 * @brief Clears the data of the parcel.
351 * @brief Resets the parcel.
353 * @param[in] buf The bytes to write
354 * @param[in] size The size of bytes to write
356 void Reset(const void* buf, uint32_t size);
359 * @brief Writes the data using @a parcelable into the parcel.
361 * @param[in] parcelable The interface to write the data into the parcel
363 void WriteParcelable(const Parcelable& parcelable);
366 * @brief Reads the data using @a parcelable from the parcel.
368 * @param[out] parcelable The interface to get data from the parcel
369 * @return @c 0 on success,
370 * otherwise a negative error value
372 int ReadParcelable(Parcelable* parcelable);
375 * @brief Sets byte order of the parcel.
377 * @param[in] big_endian @c true, if byte order of the parcel is big-endian
379 void SetByteOrder(bool big_endian);
383 std::unique_ptr<Impl> impl_;
386 } // naemspace tizen_base
392 #endif // PARCEL_PARCEL_HH_