Skip to content

Commit f5c1895

Browse files
committed
List: Add insert_empty() method
1 parent fd3cc6c commit f5c1895

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

include/scratchcpp/list.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,19 @@ class LIBSCRATCHCPP_EXPORT List : public Entity
9696
m_size--;
9797
}
9898

99-
/*! Inserts an item at index. */
100-
inline void insert(size_t index, const ValueData &value)
99+
/*! Inserts an empty item at index and returns the reference to it. Can be used for custom initialization. */
100+
inline ValueData &insertEmpty(size_t index)
101101
{
102102
assert(index >= 0 && index <= size());
103103
m_size++;
104104
reserve(getAllocSize(m_size));
105105
std::rotate(m_dataPtr->rbegin() + m_dataPtr->size() - m_size, m_dataPtr->rbegin() + m_dataPtr->size() - m_size + 1, m_dataPtr->rend() - index);
106-
value_assign_copy(&m_dataPtr->operator[](index), &value);
106+
return m_dataPtr->operator[](index);
107107
}
108108

109+
/*! Inserts an item at index. */
110+
inline void insert(size_t index, const ValueData &value) { value_assign_copy(&insertEmpty(index), &value); }
111+
109112
/*! Inserts an item at index. */
110113
inline void insert(size_t index, const Value &value) { insert(index, value.data()); }
111114

0 commit comments

Comments
 (0)