]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add make_const_array_view
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Sat, 9 Feb 2019 14:20:44 +0000 (15:20 +0100)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Mon, 11 Feb 2019 09:19:45 +0000 (10:19 +0100)
doc/news/changes/minor/20190209Arndt [new file with mode: 0644]
include/deal.II/base/array_view.h
tests/base/array_view_07.cc [new file with mode: 0644]
tests/base/array_view_07.output [new file with mode: 0644]

diff --git a/doc/news/changes/minor/20190209Arndt b/doc/news/changes/minor/20190209Arndt
new file mode 100644 (file)
index 0000000..d8bdf8f
--- /dev/null
@@ -0,0 +1,3 @@
+New: make_const_array_view creates a constant view from a non-const object.
+<br>
+(Daniel Arndt, 2019/02/09)
index 9fdcbb3b6b20734e23666685bf655eea4e2d1c9b..130e693f4bef88230d49655f281e6a30d3bf864a 100644 (file)
@@ -1134,6 +1134,25 @@ make_array_view(const Table<2, ElementType> &                   table,
 
 
 
+/*
+ * Create a view that doesn't allow the container it points to to be modified.
+ * This is useful if the object passed in is not `const` already and a function
+ * requires a view to constant memory in its signature.
+ *
+ * This function returns an object of type `ArrayView<const T>` where `T` is the
+ * element type of the container.
+ *
+ * @relatesalso ArrayView
+ */
+template <typename Container>
+inline auto
+make_const_array_view(const Container &container)
+  -> decltype(make_array_view(container))
+{
+  return make_array_view(container);
+}
+
+
 DEAL_II_NAMESPACE_CLOSE
 
 #endif
diff --git a/tests/base/array_view_07.cc b/tests/base/array_view_07.cc
new file mode 100644 (file)
index 0000000..06d844a
--- /dev/null
@@ -0,0 +1,50 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 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 make_const_array_view
+
+#include <deal.II/base/array_view.h>
+
+#include "../tests.h"
+
+template <typename ElementType>
+void
+const_foo(const ArrayView<const std::vector<ElementType>> &view)
+{
+  AssertThrow(view[0][0] == 1, ExcInternalError());
+  deallog << "OK" << std::endl;
+}
+
+template <typename ElementType>
+void
+foo(const ArrayView<std::vector<ElementType>> &view)
+{
+  AssertThrow(view[0][0] == 1, ExcInternalError());
+  deallog << "OK" << std::endl;
+}
+
+int
+main()
+{
+  initlog();
+  std::vector<std::vector<int>> v(1, std::vector<int>(1, 1));
+  // this doesn't work
+  // const_foo(make_array_view(v));
+  const_foo(make_const_array_view(v));
+  foo(make_array_view(v));
+  // this doesn't work
+  // foo(make_const_array_view(v));
+}
diff --git a/tests/base/array_view_07.output b/tests/base/array_view_07.output
new file mode 100644 (file)
index 0000000..8b3b075
--- /dev/null
@@ -0,0 +1,3 @@
+
+DEAL::OK
+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.