Skip to content

Commit

Permalink
Make vector<bool> work again (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
dunhor authored Jul 29, 2020
1 parent 95c342e commit 820fed5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions strings/base_collections_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ namespace winrt::impl
struct removed_value
{
// Trivially destructible; okay to run destructor under lock
void assign(T&) {}
template <typename U>
void assign(U&&) {}
};

template <typename T>
struct removed_value<T, std::enable_if_t<std::is_move_constructible_v<T> && !std::is_trivially_destructible_v<T>>>
{
std::optional<T> m_value;

void assign(T& value)
template <typename U>
void assign(U&& value)
{
m_value.emplace(std::move(value));
}
Expand Down Expand Up @@ -313,7 +315,7 @@ WINRT_EXPORT namespace winrt
}

this->increment_version();
auto& pos = static_cast<D&>(*this).get_container()[index];
auto&& pos = static_cast<D&>(*this).get_container()[index];
oldValue.assign(pos);
pos = static_cast<D const&>(*this).wrap_value(value);
}
Expand Down
30 changes: 30 additions & 0 deletions test/old_tests/UnitTests/single_threaded_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,33 @@ TEST_CASE("test_single_threaded_vector")
test_vector(single_threaded_vector<int>());
test_vector(single_threaded_observable_vector<int>());
}

TEST_CASE("single_threaded_vector of bool")
{
auto values = single_threaded_vector<bool>();
values.Append(true);
values.ReplaceAll({ false, true, false, true });
values.InsertAt(1, false);
values.SetAt(2, false);
REQUIRE(values.Size() == 5);
REQUIRE(!values.GetAt(0));
uint32_t index;
REQUIRE((values.IndexOf(true, index) && (index == 4)));

auto itr = values.First();
REQUIRE(itr.HasCurrent());
REQUIRE(!itr.Current());
REQUIRE(itr.MoveNext());
REQUIRE(itr.MoveNext());
bool temp[5];
REQUIRE(itr.GetMany(temp) == 3);
REQUIRE((!temp[0] && !temp[1] && temp[2]));

values.RemoveAt(0);
values.RemoveAtEnd();
REQUIRE(values.Size() == 3);
REQUIRE(values.GetMany(0, temp) == 3);
REQUIRE((!temp[0] && !temp[1] && !temp[2]));
values.Clear();
REQUIRE(values.Size() == 0);
}

0 comments on commit 820fed5

Please sign in to comment.