From b3ba4509d744e82f767d67c38788bc453db0aa9a Mon Sep 17 00:00:00 2001 From: ohyzha Date: Thu, 19 Dec 2024 13:27:10 +0200 Subject: [PATCH] add constructor for array with iterators or pointers --- openVulkanoCpp/Data/Containers/Array.hpp | 14 ++++++++++++++ tests/Data/Containers/ArrayTest.cpp | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/openVulkanoCpp/Data/Containers/Array.hpp b/openVulkanoCpp/Data/Containers/Array.hpp index 2c7057e..61d9ed8 100644 --- a/openVulkanoCpp/Data/Containers/Array.hpp +++ b/openVulkanoCpp/Data/Containers/Array.hpp @@ -107,6 +107,17 @@ namespace OpenVulkano } } + template::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 + Array(InputIt first, InputIt last) -> Array::value_type>; } \ No newline at end of file diff --git a/tests/Data/Containers/ArrayTest.cpp b/tests/Data/Containers/ArrayTest.cpp index c03a323..1388a7a 100644 --- a/tests/Data/Containers/ArrayTest.cpp +++ b/tests/Data/Containers/ArrayTest.cpp @@ -237,4 +237,26 @@ TEST_CASE("Swap method", "[Array]") arr1.Swap(arr2); REQUIRE(arr1[0] == 4); REQUIRE(arr2[0] == 1); +} + +TEST_CASE("Iterator constructor", "[Array]") +{ + { + char data[] = "Hello world"; + Array arr(data, data + sizeof(data)); + REQUIRE(arr.Data() != data); + for (int i = 0; i < sizeof(data); i++) + { + REQUIRE(data[i] == arr[i]); + } + } + + { + Array arr = { 1, 2, 3, 4, 5 }; + Array arr2(arr.begin(), arr.end()); + for (int i = 0; i < arr.Size(); i++) + { + REQUIRE(arr[i] == arr2[i]); + } + } } \ No newline at end of file