From b9e9ba76d11c8bc38bae094df2f669fd5bda74e5 Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Thu, 12 Oct 2023 10:39:49 -0600 Subject: [PATCH] Make ArrayView objects from boost::container::small_vector. --- include/deal.II/base/array_view.h | 75 +++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 7b6173ccd1..7bf75706c5 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -23,6 +23,8 @@ #include #include +#include + #include #include #include @@ -180,6 +182,44 @@ public: */ ArrayView(std::vector> &vector); + /** + * A constructor that automatically creates a view from a + * boost::container::small_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 boost::container::small_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@. + */ + template + ArrayView(const boost::container::small_vector, + N> &vector); + + /** + * A constructor that automatically creates a view from a + * boost::container::small_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 boost::container::small_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@. + */ + template + ArrayView( + boost::container::small_vector, N> &vector); + /** * A constructor that automatically creates a view for a given C-style array. * This constructor can be used as follows: @@ -529,6 +569,41 @@ inline ArrayView::ArrayView( +template +template +inline ArrayView::ArrayView( + const boost::container::small_vector, N> &vector) + : // use delegating constructor + ArrayView(vector.data(), vector.size()) +{ + // the following static_assert is not strictly necessary because, + // if we got a const boost::container::small_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_v == 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 +template +inline ArrayView::ArrayView( + boost::container::small_vector, N> &vector) + : // use delegating constructor + ArrayView(vector.data(), vector.size()) +{} + + + template template inline ArrayView::ArrayView( -- 2.39.5