mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 22:31:14 +00:00
Implement SharedVector::clear() for C++
This commit is contained in:
parent
856a049627
commit
511027dad0
2 changed files with 44 additions and 0 deletions
|
@ -127,6 +127,21 @@ struct SharedVector
|
|||
inner->size++;
|
||||
}
|
||||
|
||||
/// Clears the vector and removes all elements. The capacity remains unaffected.
|
||||
void clear()
|
||||
{
|
||||
// Detach first to retain capacity, so that the begin() call doesn't detach
|
||||
// (which it would to inner->size instead of capacity)
|
||||
detach(inner->capacity);
|
||||
{
|
||||
auto b = begin(), e = end();
|
||||
inner->size = 0;
|
||||
for (auto it = b; it < e; ++it) {
|
||||
it->~T();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the vector \a a has the same number of elements as \a b
|
||||
/// and all the elements also compare equal; false otherwise.
|
||||
friend bool operator==(const SharedVector &a, const SharedVector &b)
|
||||
|
@ -139,6 +154,9 @@ struct SharedVector
|
|||
/// and all the elements also compare equal; true otherwise.
|
||||
friend bool operator!=(const SharedVector &a, const SharedVector &b) { return !(a == b); }
|
||||
|
||||
/// \private
|
||||
std::size_t capacity() const { return inner->capacity; }
|
||||
|
||||
private:
|
||||
void detach(std::size_t expected_capacity)
|
||||
{
|
||||
|
|
|
@ -94,3 +94,29 @@ TEST_CASE("Image")
|
|||
REQUIRE(size.height == 128.);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("SharedVector")
|
||||
{
|
||||
using namespace sixtyfps;
|
||||
|
||||
SharedVector<SharedString> vec;
|
||||
vec.push_back("Hello");
|
||||
vec.push_back("World");
|
||||
vec.push_back("of");
|
||||
vec.push_back("Vectors");
|
||||
|
||||
auto copy = vec;
|
||||
|
||||
REQUIRE(vec.size() == 4);
|
||||
auto orig_cap = vec.capacity();
|
||||
REQUIRE(orig_cap >= vec.size());
|
||||
vec.clear();
|
||||
REQUIRE(vec.size() == 0);
|
||||
REQUIRE(vec.capacity() == orig_cap);
|
||||
vec.push_back("Welcome back");
|
||||
REQUIRE(vec.size() == 1);
|
||||
REQUIRE(vec.capacity() == orig_cap);
|
||||
|
||||
REQUIRE(copy.size() == 4);
|
||||
REQUIRE(copy.capacity() == orig_cap);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue