From: Peter Munch Date: Fri, 7 Feb 2020 17:54:44 +0000 (+0100) Subject: Add new constructor to ArrayView X-Git-Tag: v9.2.0-rc1~552^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F9492%2Fhead;p=dealii.git Add new constructor to ArrayView --- diff --git a/doc/news/changes/minor/20200207PeterMunch b/doc/news/changes/minor/20200207PeterMunch new file mode 100644 index 0000000000..886bd4b438 --- /dev/null +++ b/doc/news/changes/minor/20200207PeterMunch @@ -0,0 +1,4 @@ +Improved: Add a constructor to ArrayView that takes as argument +a reference to an arbitrary value type (like int, double, ...). +
+(Peter Munch, 2020/01/02) diff --git a/include/deal.II/base/array_view.h b/include/deal.II/base/array_view.h index 34e0d0f3ff..027b300aec 100644 --- a/include/deal.II/base/array_view.h +++ b/include/deal.II/base/array_view.h @@ -131,6 +131,11 @@ public: ArrayView(const ArrayView::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::reinit(value_type *starting_element, +template +inline ArrayView::ArrayView(ElementType &element) + : starting_element(&element) + , n_elements(1) +{} + + + template inline ArrayView::ArrayView( const ArrayView::type, MemorySpaceType> diff --git a/tests/base/array_view_15.cc b/tests/base/array_view_15.cc new file mode 100644 index 0000000000..100ea6748b --- /dev/null +++ b/tests/base/array_view_15.cc @@ -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 + +#include "../tests.h" + + +void +test() +{ + int v = 9; + + ArrayView 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 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 index 0000000000..0fd8fc12f0 --- /dev/null +++ b/tests/base/array_view_15.output @@ -0,0 +1,2 @@ + +DEAL::OK