[base] Add Value::Insert
In order to prepare for the switch of the return value of
Value::GetList() from Value::ListStorage& to span<Value> this change
adds an Insert API to Value, allowing callers to insert Values at
arbitrary positiions in the list. The signature of the API is inspired
by std::vector::insert, taking a insertion position and a Value, and
returning an iterator pointing to the just inserted element.
Bug: 646113
Change-Id: Ibacc9e1c22ee60b6f35661b66de951746e978b76
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1887694
Commit-Queue: Jan Wilken Dörrie <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Cr-Commit-Position: refs/heads/master@{#710725}
diff --git a/base/values.h b/base/values.h
index ccf87bfd..cd79ebc 100644
--- a/base/values.h
+++ b/base/values.h
@@ -198,9 +198,20 @@
void Append(StringPiece16 value);
void Append(Value&& value);
+ // Inserts |value| before |pos|.
+ // Note: These CHECK that type() is Type::LIST.
+ // TODO(crbug.com/990059): Remove ListStorage::const_iterator overload once
+ // mutable GetList() returns a base::span.
+ ListStorage::iterator Insert(ListStorage::const_iterator pos, Value&& value);
+ CheckedContiguousIterator<Value> Insert(
+ CheckedContiguousConstIterator<Value> pos,
+ Value&& value);
+
// Erases the Value pointed to by |iter|. Returns false if |iter| is out of
// bounds.
// Note: This CHECKs that type() is Type::LIST.
+ // TODO(crbug.com/990059): Remove ListStorage::const_iterator overload once
+ // mutable GetList() returns a base::span.
bool EraseListIter(ListStorage::const_iterator iter);
bool EraseListIter(CheckedContiguousConstIterator<Value> iter);
@@ -866,9 +877,10 @@
// DEPRECATED, use std::find() with Value::Append() instead.
bool AppendIfNotPresent(std::unique_ptr<Value> in_value);
+ using Value::Insert;
// Insert a Value at index.
// Returns true if successful, or false if the index was out of range.
- // DEPRECATED, use GetList()::insert() instead.
+ // DEPRECATED, use Value::Insert() instead.
bool Insert(size_t index, std::unique_ptr<Value> in_value);
// Searches for the first instance of |value| in the list using the Equals