Commit Graph

3 Commits

Author SHA1 Message Date
Abtin Keshavarzian 14373d5543 [heap] introduce Move() and unify TakeFrom() for move semantics (#11972)
This change enhances the move semantics for heap-allocated container
classes (`Array`, `Data`, and `String`) by introducing a consistent
pattern.

A new `Move()` method is added to `Heap::Array`, `Heap::Data`, and
`Heap::String`. This method returns an rvalue reference to the object,
making the intent to transfer ownership explicit at the call site.

The existing `TakeFrom()` methods are updated to accept an rvalue
reference and now include a check to prevent self-assignment, which
improves robustness.

For consistency, `Heap::Data::SetFrom(Data&&)` and
`Heap::String::Set(String&&)` are renamed to `TakeFrom()`.

All call sites are updated to use the new `foo.TakeFrom(bar.Move())`
pattern, replacing the more verbose and less clear
`static_cast<...&&>(bar)`.

Unit tests are updated to validate the new `TakeFrom()` and `Move()`
semantics, including tests for self-assignment and moving from a
null (empty) container
2025-09-29 18:04:56 -07:00
Jonathan Hui 9c467a23ae [clang-format] apply v14 changes (#8490) 2022-12-07 16:23:20 -08:00
Abtin Keshavarzian 4b31f57014 [common] adding Heap::Array<Type> a heap allocated flexible-length array (#7420)
This commit adds `Heap::Array<Type>` class which allocates the
buffer to store array elements from the heap. The `Array`
implementation automatically grows the buffer when new entries
are added. It also provides optional method `ReserveCapacity()`
to allow user to allocate and reserve a certain capacity
(number of elements user expects to add).

The `Array` can safely be used with element `Type`s that are
themselves heap allocated (manage allocated items). The `Array`
implementation uses the move constructor and destructor of the
`Type` class to ensure that copying the entries when growing the
array is performed efficiently and that the removed entries are
properly deleted.

The `Array` implementation also provides helper methods to search
in the array, e.g., `Find()`, `FindMatching()`, `Contains()`, and
`ContainsMatching()`. It also supports range-based `for` loop
iteration.

This commit also adds a detailed unit test `test_heap_array` which
covers behavior of `Array` with a simple `uint16_t` entry type and
a more complex entry type (validating the constructors and destructor
of entry are properly invoked by `Array` implementation).
2022-03-08 17:55:02 -08:00