Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ArrayViewDecl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_ARRAY_VIEW_DECL_HPP
11 #define TEUCHOS_ARRAY_VIEW_DECL_HPP
12 
13 
14 #include "Teuchos_RCPNode.hpp"
15 #include "Teuchos_ENull.hpp"
17 #include <vector>
18 
19 namespace Teuchos {
20 
21 // Forward declaration; ArrayView uses ArrayRCP in debug mode.
22 template<class T> class ArrayRCP;
23 
89 template<class T>
90 class ArrayView {
91 public:
93 
94 
97 
99  typedef Ordinal size_type;
100 
103 
105  typedef T value_type;
106 
110  typedef T* pointer;
111 
113  typedef const T* const_pointer;
114 
118  typedef T& reference;
119 
121  typedef const T& const_reference;
122 
123 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
124  typedef ArrayRCP<T> iterator;
128 #else
129  typedef pointer iterator;
133 #endif
134 
136 
138 
140  ArrayView( ENull null_arg = null );
141 
157  ArrayView (T* p, size_type size,
158  const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
159 
170  ArrayView (const ArrayView<T>& array);
171 
173  ArrayView (std::vector<typename std::remove_const_t<T>>& vec);
174 
176  ArrayView (const std::vector<typename std::remove_const_t<T>>& vec);
177 
179  ArrayView<T>& operator= (const ArrayView<T>& array);
180 
182  ~ArrayView();
183 
185 
187 
189  bool is_null() const;
190 
192  size_type size() const;
193 
195  std::string toString() const;
196 
198 
200 
202  inline T* getRawPtr() const;
203 
207  inline T* data() const;
208 
216  T& operator[](size_type i) const;
217 
219  T& front() const;
220 
222  T& back() const;
223 
225 
227 
242  ArrayView<T> view( size_type offset, size_type size ) const;
243 
248 
250  const ArrayView<T>& operator() () const;
251 
254 
262  operator ArrayView<const T>() const;
263 
265 
267 
286  void assign (const ArrayView<const T>& array) const;
287 
289 
291 
304  iterator begin() const;
305 
318  iterator end() const;
319 
321 
323 
327  const ArrayView<T>& assert_not_null() const;
328 
333  const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
334 
336 
337 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
338 
339  // I should make these private but templated friends are not very portable.
340  // Besides, if a client directly calls this it will not compile in an
341  // optimized build.
342 
343  explicit ArrayView( const ArrayRCP<T> &arcp );
344 
345  explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
346 
347 #endif
348 
349 private:
350  T *ptr_; //<! Pointer to the data
351  size_type size_; //<! Number of entries in the view
352 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
353  ArrayRCP<T> arcp_;
354 #endif
355 
356  void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
357 
358  void debug_assert_not_null() const {
359 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
360  assert_not_null();
361 #endif
362  }
363 
364  void debug_assert_in_range( size_type offset, size_type size_in ) const {
365  (void)offset; (void)size_in;
366 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
367  assert_in_range(offset, size_in);
368 #endif
369  }
370 
371  void debug_assert_valid_ptr() const {
372 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
373  arcp_.access_private_node().assert_valid_ptr(*this);
374 #endif
375  }
376 
377 public: // Bad bad bad
378 
379  // This is a very bad breach of encapsulation but it exists to avoid
380  // problems with portability of tempalted friends
382  { return ptr_; }
383 
384 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
385  ArrayRCP<T> access_private_arcp() const
386  { return arcp_; }
387 #endif
388 
389 };
390 
391 
399 template<class T>
400 class ArrayView<const T> {
401 public:
405  typedef const T value_type;
406  typedef const T* pointer;
407  typedef const T* const_pointer;
408  typedef const T& reference;
409  typedef const T& const_reference;
410 
411 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
412  typedef ArrayRCP<const T> iterator;
414 #else
415  typedef pointer iterator;
417 #endif
418 
419  ArrayView( ENull null_arg = null );
420 
421  ArrayView (const T* p, size_type size,
422  const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
423 
424  ArrayView (const ArrayView<const T>& array);
425 
426  ArrayView (std::vector<typename std::remove_const_t<T>>& vec);
427 
428  ArrayView (const std::vector<typename std::remove_const_t<T>>& vec);
429 
431 
432  ~ArrayView();
433 
434  bool is_null() const;
435 
436  size_type size() const;
437 
438  std::string toString() const;
439 
440  inline const T* getRawPtr() const;
441 
442  inline const T* data() const;
443 
444  const T& operator[] (size_type i) const;
445 
446  const T& front() const;
447 
448  const T& back() const;
449 
451 
453 
454  const ArrayView<const T>& operator()() const;
455 
462 
463  iterator begin() const;
464 
465  iterator end() const;
466 
467  const ArrayView<const T>& assert_not_null() const;
468 
470 
471 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
472 
473  // I should make these private but templated friends are not very
474  // portable. Besides, if a client directly calls this it will not
475  // compile in an optimized build.
476 
477  explicit ArrayView (const ArrayRCP<const T> &arcp );
478 
479  explicit ArrayView (const T* p, size_type size, const ArrayRCP<const T> &arcp );
480 
481 #endif
482 
483 private:
484  const T* ptr_;
486 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
487  ArrayRCP<const T> arcp_;
488 #endif
489 
490  void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
491 
492  void debug_assert_not_null() const {
493 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
494  assert_not_null();
495 #endif
496  }
497 
498  void debug_assert_in_range( size_type offset, size_type size_in ) const {
499  (void)offset; (void)size_in;
500 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
501  assert_in_range(offset, size_in);
502 #endif
503  }
504 
505  void debug_assert_valid_ptr() const {
506 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
507  arcp_.access_private_node().assert_valid_ptr(*this);
508 #endif
509  }
510 
511 public: // Bad bad bad
512 
513  // This is a very bad breach of encapsulation but it exists to avoid
514  // problems with portability of templated friends
515  const T* access_private_ptr() const { return ptr_; }
516 
517 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
518  ArrayRCP<const T> access_private_arcp() const { return arcp_; }
519 #endif
520 };
521 
522 
523 
528 template<class T>
529 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
530 
531 
536 template<class T>
537 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
538 
539 
544 template<class T>
545 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
546 
547 
548 #ifndef __sun
549 
550 
551 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
552 // a call to this function below is ambiguous. However, if you just comment
553 // the function out, then the code on g++ (3.4.6 at least) will not compile.
554 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
555 
556 
564 template<class T>
565 std::vector<T> createVector( const ArrayView<T> &av );
566 
567 
568 #endif // __sun
569 
570 
578 template<class T>
579 std::vector<T> createVector( const ArrayView<const T> &av );
580 
581 
586 template<class T>
587 bool is_null( const ArrayView<T> &av );
588 
589 
594 template<class T>
595 bool nonnull( const ArrayView<T> &av );
596 
597 
605 template<class T>
606 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
607 
608 
617 template<class T2, class T1>
618 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
619 
620 
633 template<class T2, class T1>
634 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
635 
636 
637 } // end namespace Teuchos
638 
639 
640 //
641 // Inline members
642 //
643 
644 
645 // ToDo: Fill in!
646 
647 
648 #endif // TEUCHOS_ARRAY_VIEW_DECL_HPP
T & reference
Type of a reference to an array element.
void debug_assert_in_range(size_type offset, size_type size_in) const
std::string toString() const
Convert an ArrayView&lt;T&gt; to an std::string
void setUpIterators(const ERCPNodeLookup rcpNodeLookup=RCP_ENABLE_NODE_LOOKUP)
T & front() const
Get the first element.
const T * const_pointer
Type of a const pointer to an array element.
pointer iterator
Type of a nonconst iterator.
bool is_null() const
Returns true if the underlying pointer is null.
const ArrayView< T > & assert_in_range(size_type offset, size_type size) const
Throws NullReferenceError if this-&gt;get()==NULL orthis-&gt;get()!=NULL, throws RangeError if (offset &lt; 0 ...
Partial specialization of ArrayRCP for const T.
const T & const_reference
Type of a const reference to an array element.
iterator begin() const
Return an iterator to beginning of the array of data.
bool nonnull(const std::shared_ptr< T > &p)
Returns true if p.get()!=NULL.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
const_pointer const_iterator
Type of a const iterator.
Ordinal size_type
Type representing the number of elements in an ArrayRCP or view thereof.
size_type size() const
The total number of items in the managed array.
ERCPNodeLookup
Used to determine if RCPNode lookup is performed or not.
ENull
Used to initialize a RCP object to NULL using an implicit conversion!
T * data() const
Return a raw pointer to beginning of array.
TEUCHOS_ORDINAL_TYPE Teuchos_Ordinal
void assign(const ArrayView< const T > &array) const
Copy the data from one array view object to this array view object.
Teuchos_Ordinal Ordinal
Integer index type used throughout ArrayView.
void debug_assert_in_range(size_type offset, size_type size_in) const
void debug_assert_not_null() const
T * pointer
Type of a pointer to an array element.
T * getRawPtr() const
Return a raw pointer to beginning of array or NULL if unsized.
const ArrayView< T > & assert_not_null() const
Throws NullReferenceError if this-&gt;get()==NULL, otherwise returns reference to *this.
void debug_assert_valid_ptr() const
T value_type
Type of each array element.
const ArrayView< T > & operator()() const
Return *this (just for compatibility with Array and ArrayPtr).
Nonowning array view.
ArrayView(ENull null_arg=null)
Constructor that initializes to NULL (implicitly or explicitly).
iterator end() const
Return an iterator to past the end of the array of data.
ArrayView< T > view(size_type offset, size_type size) const
Return a view of a contiguous range of elements.
Partial specialization of ArrayView for const T.
T & operator[](size_type i) const
Random object access.
Reference-counted pointer node classes.
Ordinal difference_type
Type representing the difference between two size_type values.
ArrayView< T > & operator=(const ArrayView< T > &array)
Shallow copy assignment operator.
ArrayView< const T > getConst() const
Return a const view of a possibly nonconst view.
T & back() const
Get the last element.
Reference-counted smart pointer for managing arrays.