*/
using const_iterator = const ElementType *;
+ /**
+ * Default constructor.
+ */
+ ArrayView();
+
/**
* Constructor.
*
*/
ArrayView(std::vector<typename std::remove_cv<value_type>::type> &vector);
+ /**
+ * Reinitialize a view.
+ *
+ * @param[in] starting_element A pointer to the first element of the array
+ * this object should represent.
+ * @param[in] n_elements The length (in elements) of the chunk of memory
+ * this object should represent.
+ *
+ * @note The object that is constructed from these arguments has no
+ * knowledge how large the object into which it points really is. As a
+ * consequence, whenever you call ArrayView::operator[], the array view can
+ * check that the given index is within the range of the view, but it can't
+ * check that the view is indeed a subset of the valid range of elements of
+ * the underlying object that allocated that range. In other words, you need
+ * to ensure that the range of the view specified by the two arguments to
+ * this constructor is in fact a subset of the elements of the array into
+ * which it points. The appropriate way to do this is to use the
+ * make_array_view() functions.
+ */
+ void
+ reinit(value_type *starting_element, const std::size_t n_elements);
+
/**
* Compare two ArrayView objects of the same type. Two objects are considered
* equal if they have the same size and the same starting pointer.
* A pointer to the first element of the range of locations in memory that
* this object represents.
*/
- value_type *const starting_element;
+ value_type *starting_element;
/**
* The length of the array this object represents.
*/
- const std::size_t n_elements;
+ std::size_t n_elements;
friend class ArrayView<const ElementType, MemorySpaceType>;
};
+template <typename ElementType, typename MemorySpaceType>
+inline ArrayView<ElementType, MemorySpaceType>::ArrayView()
+ : starting_element(nullptr)
+ , n_elements(0)
+{}
+
+
+
template <typename ElementType, typename MemorySpaceType>
inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
value_type * starting_element,
+template <typename ElementType, typename MemorySpaceType>
+inline void
+ArrayView<ElementType, MemorySpaceType>::reinit(value_type *starting_element,
+ const std::size_t n_elements)
+{
+ *this = ArrayView(starting_element, n_elements);
+}
+
+
+
template <typename ElementType, typename MemorySpaceType>
inline ArrayView<ElementType, MemorySpaceType>::ArrayView(
const ArrayView<typename std::remove_cv<value_type>::type, MemorySpaceType>
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2019 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
+// same as array_view_01.cc but uses default constructor and reinit()
+
+#include <deal.II/base/array_view.h>
+
+#include "../tests.h"
+
+
+void
+test()
+{
+ std::vector<int> v(10);
+
+ ArrayView<int> a;
+ a.reinit(&v[4], 3); // writable view
+ a[2] = 42;
+
+ Assert(a[2] == 42, ExcInternalError());
+ Assert(v[6] == 42, ExcInternalError());
+
+ ArrayView<const int> a2;
+ a2.reinit(&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();
+}