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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_ARRAY_VIEW_DECL_HPP
43 #define TEUCHOS_ARRAY_VIEW_DECL_HPP
44 
45 
46 #include "Teuchos_RCPNode.hpp"
47 #include "Teuchos_ENull.hpp"
49 #include <vector>
50 
51 namespace Teuchos {
52 
53 // Forward declaration; ArrayView uses ArrayRCP in debug mode.
54 template<class T> class ArrayRCP;
55 
121 template<class T>
122 class ArrayView {
123 public:
125 
126 
129 
132 
135 
137  typedef T value_type;
138 
142  typedef T* pointer;
143 
145  typedef const T* const_pointer;
146 
150  typedef T& reference;
151 
153  typedef const T& const_reference;
154 
155 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
156  typedef ArrayRCP<T> iterator;
160 #else
161  typedef pointer iterator;
165 #endif
166 
168 
170 
172  ArrayView( ENull null_arg = null );
173 
189  ArrayView (T* p, size_type size,
190  const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
191 
202  ArrayView (const ArrayView<T>& array);
203 
205  ArrayView (std::vector<typename std::remove_const_t<T>>& vec);
206 
208  ArrayView (const std::vector<typename std::remove_const_t<T>>& vec);
209 
211  ArrayView<T>& operator= (const ArrayView<T>& array);
212 
214  ~ArrayView();
215 
217 
219 
221  bool is_null() const;
222 
224  size_type size() const;
225 
227  std::string toString() const;
228 
230 
232 
234  inline T* getRawPtr() const;
235 
239  inline T* data() const;
240 
248  T& operator[](size_type i) const;
249 
251  T& front() const;
252 
254  T& back() const;
255 
257 
259 
274  ArrayView<T> view( size_type offset, size_type size ) const;
275 
280 
282  const ArrayView<T>& operator() () const;
283 
286 
294  operator ArrayView<const T>() const;
295 
297 
299 
318  void assign (const ArrayView<const T>& array) const;
319 
321 
323 
336  iterator begin() const;
337 
350  iterator end() const;
351 
353 
355 
359  const ArrayView<T>& assert_not_null() const;
360 
365  const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
366 
368 
369 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
370 
371  // I should make these private but templated friends are not very portable.
372  // Besides, if a client directly calls this it will not compile in an
373  // optimized build.
374 
375  explicit ArrayView( const ArrayRCP<T> &arcp );
376 
377  explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
378 
379 #endif
380 
381 private:
382  T *ptr_; //<! Pointer to the data
383  size_type size_; //<! Number of entries in the view
384 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
385  ArrayRCP<T> arcp_;
386 #endif
387 
388  void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
389 
390  void debug_assert_not_null() const {
391 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
392  assert_not_null();
393 #endif
394  }
395 
396  void debug_assert_in_range( size_type offset, size_type size_in ) const {
397  (void)offset; (void)size_in;
398 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
399  assert_in_range(offset, size_in);
400 #endif
401  }
402 
403  void debug_assert_valid_ptr() const {
404 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
405  arcp_.access_private_node().assert_valid_ptr(*this);
406 #endif
407  }
408 
409 public: // Bad bad bad
410 
411  // This is a very bad breach of encapsulation but it exists to avoid
412  // problems with portability of tempalted friends
414  { return ptr_; }
415 
416 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
417  ArrayRCP<T> access_private_arcp() const
418  { return arcp_; }
419 #endif
420 
421 };
422 
423 
431 template<class T>
432 class ArrayView<const T> {
433 public:
437  typedef const T value_type;
438  typedef const T* pointer;
439  typedef const T* const_pointer;
440  typedef const T& reference;
441  typedef const T& const_reference;
442 
443 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
444  typedef ArrayRCP<const T> iterator;
446 #else
447  typedef pointer iterator;
449 #endif
450 
451  ArrayView( ENull null_arg = null );
452 
453  ArrayView (const T* p, size_type size,
454  const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
455 
456  ArrayView (const ArrayView<const T>& array);
457 
458  ArrayView (std::vector<typename std::remove_const_t<T>>& vec);
459 
460  ArrayView (const std::vector<typename std::remove_const_t<T>>& vec);
461 
463 
464  ~ArrayView();
465 
466  bool is_null() const;
467 
468  size_type size() const;
469 
470  std::string toString() const;
471 
472  inline const T* getRawPtr() const;
473 
474  inline const T* data() const;
475 
476  const T& operator[] (size_type i) const;
477 
478  const T& front() const;
479 
480  const T& back() const;
481 
483 
485 
486  const ArrayView<const T>& operator()() const;
487 
494 
495  iterator begin() const;
496 
497  iterator end() const;
498 
499  const ArrayView<const T>& assert_not_null() const;
500 
502 
503 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
504 
505  // I should make these private but templated friends are not very
506  // portable. Besides, if a client directly calls this it will not
507  // compile in an optimized build.
508 
509  explicit ArrayView (const ArrayRCP<const T> &arcp );
510 
511  explicit ArrayView (const T* p, size_type size, const ArrayRCP<const T> &arcp );
512 
513 #endif
514 
515 private:
516  const T* ptr_;
518 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
519  ArrayRCP<const T> arcp_;
520 #endif
521 
522  void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
523 
524  void debug_assert_not_null() const {
525 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
526  assert_not_null();
527 #endif
528  }
529 
530  void debug_assert_in_range( size_type offset, size_type size_in ) const {
531  (void)offset; (void)size_in;
532 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
533  assert_in_range(offset, size_in);
534 #endif
535  }
536 
537  void debug_assert_valid_ptr() const {
538 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
539  arcp_.access_private_node().assert_valid_ptr(*this);
540 #endif
541  }
542 
543 public: // Bad bad bad
544 
545  // This is a very bad breach of encapsulation but it exists to avoid
546  // problems with portability of templated friends
547  const T* access_private_ptr() const { return ptr_; }
548 
549 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
550  ArrayRCP<const T> access_private_arcp() const { return arcp_; }
551 #endif
552 };
553 
554 
555 
560 template<class T>
561 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
562 
563 
568 template<class T>
569 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
570 
571 
576 template<class T>
577 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
578 
579 
580 #ifndef __sun
581 
582 
583 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
584 // a call to this function below is ambiguous. However, if you just comment
585 // the function out, then the code on g++ (3.4.6 at least) will not compile.
586 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
587 
588 
596 template<class T>
597 std::vector<T> createVector( const ArrayView<T> &av );
598 
599 
600 #endif // __sun
601 
602 
610 template<class T>
611 std::vector<T> createVector( const ArrayView<const T> &av );
612 
613 
618 template<class T>
619 bool is_null( const ArrayView<T> &av );
620 
621 
626 template<class T>
627 bool nonnull( const ArrayView<T> &av );
628 
629 
637 template<class T>
638 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
639 
640 
649 template<class T2, class T1>
650 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
651 
652 
665 template<class T2, class T1>
666 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
667 
668 
669 } // end namespace Teuchos
670 
671 
672 //
673 // Inline members
674 //
675 
676 
677 // ToDo: Fill in!
678 
679 
680 #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.