if (impl_->data_ == nullptr) throw std::bad_alloc();
}
-Parcel::Parcel(const void* buf, uint32_t size, bool copy)
+Parcel::Parcel(const void* buf, uint32_t size, bool copy, bool ownership)
: impl_(new Impl(
this, size,
copy ? nullptr : static_cast<uint8_t*>(const_cast<void*>(buf)))) {
if (impl_->data_ == nullptr) throw std::bad_alloc();
if (copy) impl_->Write(buf, size);
+ else impl_->ownership_ = ownership;
}
Parcel::Parcel(const Parcel& parcel, uint32_t start_pos, uint32_t size)
* @param[in] buf The bytes to write
* @param[in] size the size of bytes to write
* @param[in] copy If @c is true, this object copies bytes.
- * If @c is false, this object takes ownership of @buf.
+ * If @c is false, this object takes ownership of @buf if @ownership is true
+ * @param[in] ownership Only meaningful when the @copy is false
+ * If @c is false, this object doesn't have ownership of buf.
*/
- Parcel(const void* buf, uint32_t size, bool copy = true);
+ Parcel(const void* buf, uint32_t size, bool copy = true, bool ownership = true);
/**
* @brief Construct from Parcel object.
EXPECT_THROW(Parcel w1(p, 0, 100), std::bad_alloc);
}
+
+TEST_F(ParcelCppTest, NonOwnershipParcel) {
+ auto& p = GetHandle();
+ int pos0 = p.GetDataSize();
+
+ p.WriteString("Test non_ownership_parcel1 1");
+ p.WriteString("Test non_ownership_parcel1 2");
+ int pos1 = p.GetDataSize();
+ p.WriteString("Test non_ownership_parcel2 1");
+ p.WriteString("Test non_ownership_parcel2 2");
+ int pos2 = p.GetDataSize();
+ p.WriteString("Test non_ownership_parcel3 1");
+ p.WriteString("Test non_ownership_parcel3 2");
+ int pos3 = p.GetDataSize();
+ p.WriteString("Test non_ownership_parcel4 1");
+ p.WriteString("Test non_ownership_parcel4 2");
+ int pos4 = p.GetDataSize();
+
+ Parcel r1(p.GetData() + pos0, pos1 - pos0, false, false);
+ Parcel r2(p.GetData() + pos1, pos2 - pos1, false, false);
+ Parcel r3(p.GetData() + pos2, pos3 - pos2, false, false);
+ Parcel r4(p.GetData() + pos3, pos4 - pos3, false, false);
+
+ ASSERT_EQ(r1.ReadString(), "Test non_ownership_parcel1 1");
+ ASSERT_EQ(r1.ReadString(), "Test non_ownership_parcel1 2");
+
+ ASSERT_EQ(r3.ReadString(), "Test non_ownership_parcel3 1");
+ ASSERT_EQ(r3.ReadString(), "Test non_ownership_parcel3 2");
+
+ ASSERT_EQ(r2.ReadString(), "Test non_ownership_parcel2 1");
+ ASSERT_EQ(r2.ReadString(), "Test non_ownership_parcel2 2");
+
+ ASSERT_EQ(r4.ReadString(), "Test non_ownership_parcel4 1");
+ ASSERT_EQ(r4.ReadString(), "Test non_ownership_parcel4 2");
+}