]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add new constructor to ArrayView 9492/head
authorPeter Munch <peterrmuench@gmail.com>
Fri, 7 Feb 2020 17:54:44 +0000 (18:54 +0100)
committerPeter Munch <peterrmuench@gmail.com>
Fri, 7 Feb 2020 18:10:42 +0000 (19:10 +0100)
doc/news/changes/minor/20200207PeterMunch [new file with mode: 0644]
include/deal.II/base/array_view.h
tests/base/array_view_15.cc [new file with mode: 0644]
tests/base/array_view_15.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20200207PeterMunch b/doc/news/changes/minor/20200207PeterMunch
new file mode 100644 (file)
index 0000000..886bd4b
--- /dev/null
@@ -0,0 +1,4 @@
+Improved: Add a constructor to ArrayView that takes as argument
+a reference to an arbitrary value type (like int, double, ...).
+<br>
+(Peter Munch, 2020/01/02)
index 34e0d0f3ffb305ce862d2cd8b6367af6e97b435b..027b300aec7e38e0df10d7c8c969c60f2ef96b97 100644 (file)
@@ -131,6 +131,11 @@ public:
   ArrayView(const ArrayView<typename std::remove_cv<value_type>::type,
                             MemorySpaceType> &view);
 
+  /**
+   * A constructor that automatically creates a view from a value_type object.
+   */
+  explicit ArrayView(value_type &element);
+
   /**
    * A constructor that automatically creates a view from a std::vector object.
    * The view encompasses all elements of the given vector.
@@ -367,6 +372,14 @@ ArrayView<ElementType, MemorySpaceType>::reinit(value_type *starting_element,
 
 
 
+template <typename ElementType, typename MemorySpaceType>
+inline ArrayView<ElementType, MemorySpaceType>::ArrayView(ElementType &element)
+  : starting_element(&element)
+  , n_elements(1)
+{}
+
+
+
 template <typename ElementType, typename MemorySpaceType>
 inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
   const ArrayView<typename std::remove_cv<value_type>::type, MemorySpaceType>
diff --git a/tests/base/array_view_15.cc b/tests/base/array_view_15.cc
new file mode 100644 (file)
index 0000000..100ea67
--- /dev/null
@@ -0,0 +1,54 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2020 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 for scalar type
+
+#include <deal.II/base/array_view.h>
+
+#include "../tests.h"
+
+
+void
+test()
+{
+  int v = 9;
+
+  ArrayView<int> a1(v); // writable view
+
+  Assert(a1.size() == 1, ExcInternalError());
+  Assert(a1[0] == 9, ExcInternalError());
+  v = 10;
+  Assert(a1[0] == 10, ExcInternalError());
+  a1[0] = 11;
+  Assert(a1[0] == 11, ExcInternalError());
+
+  ArrayView<int> a2(v); // writable view
+
+  Assert(a2.size() == 1, ExcInternalError());
+  Assert(a2[0] == 11, ExcInternalError());
+
+  deallog << "OK" << std::endl;
+}
+
+
+
+int
+main()
+{
+  initlog();
+
+  test();
+}
diff --git a/tests/base/array_view_15.output b/tests/base/array_view_15.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.