]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Enable ArrayView for std::array 10632/head
authorPeter Munch <peterrmuench@gmail.com>
Mon, 29 Jun 2020 22:16:10 +0000 (00:16 +0200)
committerPeter Munch <peterrmuench@gmail.com>
Mon, 29 Jun 2020 22:19:45 +0000 (00:19 +0200)
doc/news/changes/minor/20200629Munch [new file with mode: 0644]
include/deal.II/base/array_view.h
tests/base/array_view_16.cc [new file with mode: 0644]
tests/base/array_view_16.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20200629Munch b/doc/news/changes/minor/20200629Munch
new file mode 100644 (file)
index 0000000..7b8ee8d
--- /dev/null
@@ -0,0 +1,3 @@
+New: The class ArrayView can now also be constructed from std::array.
+<br>
+(Peter Munch, 2020/06/29)
index 9bf37a5c317543514db9dccb3218920de5b504d4..823cc429bea7738e9b50e61e694402ba01d079cc 100644 (file)
@@ -168,6 +168,29 @@ public:
    */
   ArrayView(std::vector<typename std::remove_cv<value_type>::type> &vector);
 
+  /**
+   * A constructor that automatically creates a view from a std::array 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::array.
+   */
+  template <std::size_t N>
+  ArrayView(
+    const std::array<typename std::remove_cv<value_type>::type, N> &vector);
+
+  /**
+   * A constructor that automatically creates a view from a std::array 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::array.
+   */
+  template <std::size_t N>
+  ArrayView(std::array<typename std::remove_cv<value_type>::type, N> &vector);
+
   /**
    * Reinitialize a view.
    *
@@ -422,6 +445,41 @@ inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
 
 
 
+template <typename ElementType, typename MemorySpaceType>
+template <std::size_t N>
+inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
+  const std::array<typename std::remove_cv<value_type>::type, N> &vector)
+  : // use delegating constructor
+  ArrayView(vector.data(), vector.size())
+{
+  // the following static_assert is not strictly necessary because,
+  // if we got a const std::array 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::array.");
+}
+
+
+
+template <typename ElementType, typename MemorySpaceType>
+template <std::size_t N>
+inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
+  std::array<typename std::remove_cv<value_type>::type, N> &vector)
+  : // use delegating constructor
+  ArrayView(vector.data(), vector.size())
+{}
+
+
+
 template <typename ElementType, typename MemorySpaceType>
 inline bool
 ArrayView<ElementType, MemorySpaceType>::
diff --git a/tests/base/array_view_16.cc b/tests/base/array_view_16.cc
new file mode 100644 (file)
index 0000000..0e432a8
--- /dev/null
@@ -0,0 +1,52 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2015 - 2018 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE.md at
+// the top level directory of deal.II.
+//
+// ---------------------------------------------------------------------
+
+
+// test for class ArrayView with std::array
+
+#include <deal.II/base/array_view.h>
+
+#include "../tests.h"
+
+
+void
+test()
+{
+  std::array<int, 10> v;
+
+  ArrayView<int> a(&v[4], 3); // writable view
+  a[2] = 42;
+
+  Assert(a[2] == 42, ExcInternalError());
+  Assert(v[6] == 42, ExcInternalError());
+
+  ArrayView<const int> a2(&v[4], 3); // readable view
+  Assert(a2[2] == 42, ExcInternalError());
+
+  ArrayView<const int> a3(a); // readable view, converted from 'a'
+  Assert(a3[2] == 42, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test();
+}
diff --git a/tests/base/array_view_16.output b/tests/base/array_view_16.output
new file mode 100644 (file)
index 0000000..0fd8fc1
--- /dev/null
@@ -0,0 +1,2 @@
+
+DEAL::OK

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.