Add resize_initialized and resize_uninitialized to Vector. These functions serve as replacements for resize, to make sure the caller understands whether elements need to be initialized 'by hand' after the call.

This commit is contained in:
Lukas Tenbrink
2025-05-23 00:20:22 +02:00
parent 6c9765d87e
commit 4cb8a0c77e
24 changed files with 62 additions and 44 deletions

View File

@@ -99,8 +99,26 @@ public:
_FORCE_INLINE_ T get(Size p_index) { return _cowdata.get(p_index); }
_FORCE_INLINE_ const T &get(Size p_index) const { return _cowdata.get(p_index); }
_FORCE_INLINE_ void set(Size p_index, const T &p_elem) { _cowdata.set(p_index, p_elem); }
Error resize(Size p_size) { return _cowdata.template resize<!std::is_trivially_constructible_v<T>>(p_size); }
Error resize_zeroed(Size p_size) { return _cowdata.template resize<true>(p_size); }
/// Resize the vector.
/// Elements are initialized (or not) depending on what the default C++ behavior for this type is.
_FORCE_INLINE_ Error resize(Size p_size) {
return _cowdata.template resize<!std::is_trivially_constructible_v<T>>(p_size);
}
/// Resize and set all values to 0 / false / nullptr.
/// This is only available for zero constructible types.
_FORCE_INLINE_ Error resize_initialized(Size p_size) {
return _cowdata.template resize<true>(p_size);
}
/// Resize and set all values to 0 / false / nullptr.
/// This is only available for trivially destructible types (otherwise, trivial resize might be UB).
_FORCE_INLINE_ Error resize_uninitialized(Size p_size) {
// resize() statically asserts that T is compatible, no need to do it ourselves.
return _cowdata.template resize<false>(p_size);
}
_FORCE_INLINE_ const T &operator[](Size p_index) const { return _cowdata.get(p_index); }
// Must take a copy instead of a reference (see GH-31736).
Error insert(Size p_pos, T p_val) { return _cowdata.insert(p_pos, p_val); }