From 272e8f286a31060a6ab947a882839c9c48e30d03 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 18 Mar 2020 14:18:27 +1100 Subject: [PATCH] alloc/easy: add move initialisation for array allocations --- alloc/easy.hpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/alloc/easy.hpp b/alloc/easy.hpp index 5e749111..868155fb 100644 --- a/alloc/easy.hpp +++ b/alloc/easy.hpp @@ -3,7 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * - * Copyright 2018 Danny Robson + * Copyright 2018-2020, Danny Robson */ #pragma once @@ -11,9 +11,10 @@ #include "traits.hpp" +#include "../buffer/traits.hpp" +#include "../concepts.hpp" #include "../std.hpp" #include "../view.hpp" -#include "../buffer/traits.hpp" #include #include @@ -159,6 +160,26 @@ namespace cruft::alloc::easy { } + /// Allocates storage for an array of ValueT, moves the values from + /// ContainerT into this storage, and returns a view over the memory. + template < + concepts::iterable ContainerT + > + auto + array (ContainerT &&container) + { + using IteratorT = decltype (std::begin (container)); + using ValueT = typename std::iterator_traits::value_type; + + auto store = array (std::size (container)); + std::copy ( + std::move_iterator (std::begin (container)), + std::move_iterator (std::end (container)), + std::begin (store) + ); + + return store; + } /// Create an object of type `U` using `acquire` and wrap the result