add constructor for array with iterators or pointers

This commit is contained in:
ohyzha
2024-12-19 13:27:10 +02:00
parent 2334d48856
commit b3ba4509d7
2 changed files with 36 additions and 0 deletions

View File

@@ -107,6 +107,17 @@ namespace OpenVulkano
}
}
template<typename InputIt, std::enable_if_t<!std::is_same_v<typename std::iterator_traits<InputIt>::value_type, void>, int> = 0>
Array(InputIt first, InputIt last) : size(std::distance(first, last)), data(MakeBuffer(size))
{
size_t i = 0;
while (first != last)
{
new (&data[i++]) T(*first);
++first;
}
}
~Array() noexcept
{
ClearData();
@@ -354,4 +365,7 @@ namespace OpenVulkano
{
return !(rhs < lhs);
}
template<typename InputIt>
Array(InputIt first, InputIt last) -> Array<typename std::iterator_traits<InputIt>::value_type>;
}

View File

@@ -238,3 +238,25 @@ TEST_CASE("Swap method", "[Array]")
REQUIRE(arr1[0] == 4);
REQUIRE(arr2[0] == 1);
}
TEST_CASE("Iterator constructor", "[Array]")
{
{
char data[] = "Hello world";
Array<char> arr(data, data + sizeof(data));
REQUIRE(arr.Data() != data);
for (int i = 0; i < sizeof(data); i++)
{
REQUIRE(data[i] == arr[i]);
}
}
{
Array<int> arr = { 1, 2, 3, 4, 5 };
Array<double> arr2(arr.begin(), arr.end());
for (int i = 0; i < arr.Size(); i++)
{
REQUIRE(arr[i] == arr2[i]);
}
}
}