From: pjh9216 Date: Wed, 23 Oct 2024 04:46:49 +0000 (+0900) Subject: Add new internal API for parcel X-Git-Tag: accepted/tizen/unified/20241108.105505~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fa12eae5355e4bfb5183c3a9dcc47351dac9077c;p=platform%2Fcore%2Fbase%2Fbundle.git Add new internal API for parcel Change-Id: I61797a337dd9bcb50908db92379cf40f3576c07f Signed-off-by: pjh9216 --- diff --git a/src/parcel/api/parcel.h b/src/parcel/api/parcel.h index d16909c..87642b8 100644 --- a/src/parcel/api/parcel.h +++ b/src/parcel/api/parcel.h @@ -460,6 +460,18 @@ int parcel_read_string(parcel_h parcel, char **str); */ int parcel_reset_reader(parcel_h parcel); +/** + * @brief Sets the reader pointer of the handle to the given position. + * @since_tizen 10.0 + * @param[in] parcel The parcel handle + * @param[in] pos The position to set + * @return @c 0 on success, + * otherwise a negative error value + * @retval #PARCEL_ERROR_NONE Successful + * @retval #PARCEL_ERROR_INVALID_PARAMETER Invalid parameter + */ +int parcel_set_reader(parcel_h parcel, uint32_t pos); + /** * @brief Clears the data of the parcel handle. * @since_tizen 6.5 diff --git a/src/parcel/parcel.cc b/src/parcel/parcel.cc index 343ae9d..fc1e873 100644 --- a/src/parcel/parcel.cc +++ b/src/parcel/parcel.cc @@ -77,6 +77,13 @@ void Parcel::Impl::ResetReader() { reader_ = 0; } +bool Parcel::Impl::SetReader(uint32_t pos) { + if (pos > data_size_) + return false; + reader_ = pos; + return true; +} + void Parcel::Impl::Clear() { data_size_ = 0; reader_ = 0; @@ -507,6 +514,10 @@ void Parcel::ResetReader() { impl_->ResetReader(); } +bool Parcel::SetReader(uint32_t pos) { + return impl_->SetReader(pos); +} + void Parcel::Clear() { impl_->Clear(); } diff --git a/src/parcel/parcel.hh b/src/parcel/parcel.hh index 965a208..e934f3f 100644 --- a/src/parcel/parcel.hh +++ b/src/parcel/parcel.hh @@ -343,6 +343,15 @@ class EXPORT Parcel final { */ void ResetReader(); + /** + * @brief Sets the reader pointer of the handle to the given position. + * @since_tizen 10.0 + * @param[in] pos The position to set + * @return @c true on success, + * otherwise false + */ + bool SetReader(uint32_t pos); + /** * @brief Clears the data of the parcel. * @since_tizen 6.5 diff --git a/src/parcel/parcel_implementation.hh b/src/parcel/parcel_implementation.hh index d6951f1..dbf076a 100644 --- a/src/parcel/parcel_implementation.hh +++ b/src/parcel/parcel_implementation.hh @@ -32,6 +32,7 @@ class Parcel::Impl { void Write(const void* buf, uint32_t size); int Read(void* buf, uint32_t size); void ResetReader(); + bool SetReader(uint32_t pos); void Clear(); void Reset(const void* buf, uint32_t size); bool IsEmpty(); diff --git a/src/parcel/stub.cc b/src/parcel/stub.cc index bf2a78c..da4ca6e 100644 --- a/src/parcel/stub.cc +++ b/src/parcel/stub.cc @@ -346,6 +346,18 @@ extern "C" EXPORT int parcel_reset_reader(parcel_h parcel) { return PARCEL_ERROR_NONE; } +extern "C" EXPORT int parcel_set_reader(parcel_h parcel, uint32_t pos) { + if (parcel == nullptr) { + _E("Invalid parameter"); + return PARCEL_ERROR_INVALID_PARAMETER; + } + + auto* h = static_cast(parcel); + if (h->SetReader(pos)) + return PARCEL_ERROR_NONE; + return PARCEL_ERROR_INVALID_PARAMETER; +} + extern "C" EXPORT int parcel_clear(parcel_h parcel) { if (parcel == nullptr) { _E("Invalid parameter"); diff --git a/tests/parcel_unittests/test_parcel.cc b/tests/parcel_unittests/test_parcel.cc index 28c049d..ef08bb3 100644 --- a/tests/parcel_unittests/test_parcel.cc +++ b/tests/parcel_unittests/test_parcel.cc @@ -437,6 +437,27 @@ TEST_F(ParcelTest, parcel_reset_reader_N) { ASSERT_EQ(ret, PARCEL_ERROR_INVALID_PARAMETER); } +TEST_F(ParcelTest, parcel_set_reader_P) { + parcel_write_uint32(GetHandle(), 1); + parcel_write_uint32(GetHandle(), 2); + parcel_write_uint32(GetHandle(), 3); + int ret = parcel_set_reader(GetHandle(), 4); + int32_t val = 0; + parcel_read_int32(GetHandle(), &val); + ASSERT_EQ(ret, PARCEL_ERROR_NONE); + ASSERT_EQ(val, 2); +} + +TEST_F(ParcelTest, parcel_set_reader_N1) { + int ret = parcel_set_reader(nullptr, 0); + ASSERT_EQ(ret, PARCEL_ERROR_INVALID_PARAMETER); +} + +TEST_F(ParcelTest, parcel_set_reader_N2) { + int ret = parcel_set_reader(GetHandle(), 100); + ASSERT_EQ(ret, PARCEL_ERROR_INVALID_PARAMETER); +} + TEST_F(ParcelTest, parcel_clear_P) { int ret = parcel_clear(GetHandle()); ASSERT_EQ(ret, PARCEL_ERROR_NONE);