From 53bdfa8752c64cca0e640034063d4237442bc7e1 Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Sat, 9 Feb 2019 15:20:44 +0100 Subject: [PATCH] Add make_const_array_view --- doc/news/changes/minor/20190209Arndt | 3 ++ include/deal.II/base/array_view.h | 19 +++++++++++ tests/base/array_view_07.cc | 50 ++++++++++++++++++++++++++++ tests/base/array_view_07.output | 3 ++ 4 files changed, 75 insertions(+) create mode 100644 doc/news/changes/minor/20190209Arndt create mode 100644 tests/base/array_view_07.cc create mode 100644 tests/base/array_view_07.output diff --git a/doc/news/changes/minor/20190209Arndt b/doc/news/changes/minor/20190209Arndt new file mode 100644 index 0000000000..d8bdf8fff5 --- /dev/null +++ b/doc/news/changes/minor/20190209Arndt @@ -0,0 +1,3 @@ +New: make_const_array_view creates a constant view from a non-const object. +
+(Daniel Arndt, 2019/02/09) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 9fdcbb3b6b..130e693f4b 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -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` where `T` is the + * element type of the container. + * + * @relatesalso ArrayView + */ +template +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 index 0000000000..06d844ad8d --- /dev/null +++ b/tests/base/array_view_07.cc @@ -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 + +#include "../tests.h" + +template +void +const_foo(const ArrayView> &view) +{ + AssertThrow(view[0][0] == 1, ExcInternalError()); + deallog << "OK" << std::endl; +} + +template +void +foo(const ArrayView> &view) +{ + AssertThrow(view[0][0] == 1, ExcInternalError()); + deallog << "OK" << std::endl; +} + +int +main() +{ + initlog(); + std::vector> v(1, std::vector(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 index 0000000000..8b3b075900 --- /dev/null +++ b/tests/base/array_view_07.output @@ -0,0 +1,3 @@ + +DEAL::OK +DEAL::OK -- 2.39.5