From c66c6be939d7ceffb63ea2a50986315fbeac52ae Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Mon, 11 Sep 2017 17:23:48 -0600 Subject: [PATCH] Automatically allow converting std::vector to ArrayView. --- include/deal.II/base/array_view.h | 79 +++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index d0d5374658..68f12651da 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -125,6 +125,39 @@ public: */ ArrayView (const ArrayView::type> &view); + /** + * A constructor that automatically creates a view from a std::vector object. + * The view encompasses all elements of the given vector. + * + * This implicit conversion constructor is particularly useful when calling + * a function that takes an ArrayView object as argument, and passing in + * a std::vector. + * + * @note This constructor takes a reference to a @p const vector as argument. + * It can only be used to initialize ArrayView objects that point to + * @p const memory locations, such as ArrayView@. + * You cannot initialize ArrayView objects to non-@p const memory with + * such arguments, such as ArrayView@. + */ + ArrayView (const std::vector::type> &vector); + + /** + * A constructor that automatically creates a view from a std::vector object. + * The view encompasses all elements of the given vector. + * + * This implicit conversion constructor is particularly useful when calling + * a function that takes an ArrayView object as argument, and passing in + * a std::vector. + * + * @note This constructor takes a reference to a non-@p const vector as + * argument. It can be used to initialize ArrayView objects that point to + * either @p const memory locations, such as + * ArrayView@, or to non-@p const memory, + * such as ArrayView@. + */ + ArrayView (std::vector::type> &vector); + + /** * Return the size (in elements) of the view of memory this object * represents. @@ -194,8 +227,9 @@ ArrayView::ArrayView() template inline -ArrayView::ArrayView(value_type *starting_element, - const std::size_t n_elements) +ArrayView:: +ArrayView(value_type *starting_element, + const std::size_t n_elements) : starting_element (starting_element), n_elements(n_elements) @@ -205,7 +239,8 @@ ArrayView::ArrayView(value_type *starting_element, template inline -ArrayView::ArrayView(const ArrayView::type> &view) +ArrayView:: +ArrayView(const ArrayView::type> &view) : starting_element (view.starting_element), n_elements(view.n_elements) @@ -213,6 +248,44 @@ ArrayView::ArrayView(const ArrayView +inline +ArrayView:: +ArrayView (const std::vector::type> &vector) + : + // use delegating constructor + ArrayView (vector.data(), vector.size()) +{ + // the following static_assert is not strictly necessary because, + // if we got a const std::vector reference argument but ElementType + // is not itself const, then the call to the forwarding constructor + // above will already have failed: vector.data() will have returned + // a const pointer, but we need a non-const pointer. + // + // nevertheless, leave the static_assert in since it provides a + // more descriptive error message that will simply come after the first + // error produced above + static_assert (std::is_const::value == true, + "This constructor may only be called if the ArrayView " + "object has a const value_type. In other words, you can " + "only create an ArrayView to const values from a const " + "std::vector."); +} + + + +template +inline +ArrayView:: +ArrayView (std::vector::type> &vector) + : + // use delegating constructor + ArrayView (vector.data(), vector.size()) +{} + + + + template inline std::size_t -- 2.39.5