diff --git a/lib/base/array.cpp b/lib/base/array.cpp index a1ef7aeb6..d3bb34f0d 100644 --- a/lib/base/array.cpp +++ b/lib/base/array.cpp @@ -108,6 +108,22 @@ size_t Array::GetLength(void) const return m_Data.size(); } +/** + * Insert the given value at the specified index + * + * @param index The index + * @param value The value to add + */ +void Array::Insert(unsigned int index, const Value& value) +{ + ASSERT(!OwnsLock()); + ObjectLock olock(this); + + ASSERT(index <= m_Data.size()); + + m_Data.insert(m_Data.begin() + index, value); +} + /** * Removes the specified index from the array. * diff --git a/lib/base/array.h b/lib/base/array.h index de2e49cee..53cf0a4a9 100644 --- a/lib/base/array.h +++ b/lib/base/array.h @@ -51,6 +51,7 @@ public: size_t GetLength(void) const; + void Insert(unsigned int index, const Value& value); void Remove(unsigned int index); void Remove(Iterator it); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index baee4592b..f4867e128 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,6 +26,7 @@ add_boost_test(base LIBRARIES base config icinga TESTS base_array/construct base_array/getset + base_array/insert base_array/remove base_array/foreach base_array/clone diff --git a/test/base-array.cpp b/test/base-array.cpp index dc1172c6a..9e0448eec 100644 --- a/test/base-array.cpp +++ b/test/base-array.cpp @@ -53,6 +53,27 @@ BOOST_AUTO_TEST_CASE(getset) BOOST_CHECK(array->Get(1) == 5); } +BOOST_AUTO_TEST_CASE(insert) +{ + Array::Ptr array = make_shared(); + + array->Insert(0, 11); + array->Insert(1, 22); + BOOST_CHECK(array->GetLength() == 2); + BOOST_CHECK(array->Get(1) == 22); + + array->Insert(0, 33); + BOOST_CHECK(array->GetLength() == 3); + BOOST_CHECK(array->Get(0) == 33); + BOOST_CHECK(array->Get(1) == 11); + + array->Insert(1, 44); + BOOST_CHECK(array->GetLength() == 4); + BOOST_CHECK(array->Get(0) == 33); + BOOST_CHECK(array->Get(1) == 44); + BOOST_CHECK(array->Get(2) == 11); +} + BOOST_AUTO_TEST_CASE(remove) { Array::Ptr array = make_shared();