]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Automatically allow converting std::vector to ArrayView.
authorWolfgang Bangerth <bangerth@colostate.edu>
Mon, 11 Sep 2017 23:23:48 +0000 (17:23 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Tue, 12 Sep 2017 12:40:32 +0000 (06:40 -0600)
include/deal.II/base/array_view.h

index d0d5374658e587f8a851a8e1727a633f7d21fae8..68f12651da16b942da529bbdeb2f63fb999a6d68 100644 (file)
@@ -125,6 +125,39 @@ public:
    */
   ArrayView (const ArrayView<typename std::remove_cv<value_type>::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 <code>ArrayView@<const double@></code>.
+   *   You cannot initialize ArrayView objects to non-@p const memory with
+   *   such arguments, such as <code>ArrayView@<double@></code>.
+   */
+  ArrayView (const std::vector<typename std::remove_cv<value_type>::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
+   *   <code>ArrayView@<const double@></code>, or to non-@p const memory,
+   *   such as <code>ArrayView@<double@></code>.
+   */
+  ArrayView (std::vector<typename std::remove_cv<value_type>::type> &vector);
+
+
   /**
    * Return the size (in elements) of the view of memory this object
    * represents.
@@ -194,8 +227,9 @@ ArrayView<ElementType>::ArrayView()
 
 template <typename ElementType>
 inline
-ArrayView<ElementType>::ArrayView(value_type        *starting_element,
-                                  const std::size_t  n_elements)
+ArrayView<ElementType>::
+ArrayView(value_type        *starting_element,
+          const std::size_t  n_elements)
   :
   starting_element (starting_element),
   n_elements(n_elements)
@@ -205,7 +239,8 @@ ArrayView<ElementType>::ArrayView(value_type        *starting_element,
 
 template <typename ElementType>
 inline
-ArrayView<ElementType>::ArrayView(const ArrayView<typename std::remove_cv<value_type>::type> &view)
+ArrayView<ElementType>::
+ArrayView(const ArrayView<typename std::remove_cv<value_type>::type> &view)
   :
   starting_element (view.starting_element),
   n_elements(view.n_elements)
@@ -213,6 +248,44 @@ ArrayView<ElementType>::ArrayView(const ArrayView<typename std::remove_cv<value_
 
 
 
+template <typename ElementType>
+inline
+ArrayView<ElementType>::
+ArrayView (const std::vector<typename std::remove_cv<value_type>::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_type>::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 <typename ElementType>
+inline
+ArrayView<ElementType>::
+ArrayView (std::vector<typename std::remove_cv<value_type>::type> &vector)
+  :
+  // use delegating constructor
+  ArrayView (vector.data(), vector.size())
+{}
+
+
+
+
 template <typename ElementType>
 inline
 std::size_t

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.