Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Teuchos_ArrayViewDecl.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #ifndef TEUCHOS_ARRAY_VIEW_DECL_HPP
00043 #define TEUCHOS_ARRAY_VIEW_DECL_HPP
00044 
00045 
00046 #include "Teuchos_RCPNode.hpp"
00047 #include "Teuchos_ENull.hpp"
00048 #include "Teuchos_NullIteratorTraits.hpp"
00049 #include "Teuchos_ConstTypeTraits.hpp"
00050 
00051 
00052 namespace Teuchos {
00053 
00054 // Forward declaration; ArrayView uses ArrayRCP in debug mode.
00055 template<class T> class ArrayRCP;
00056 
00122 template<class T>
00123 class ArrayView {
00124 public:
00126 
00127 
00129   typedef Teuchos_Ordinal Ordinal;
00130 
00132   typedef Ordinal size_type;
00133 
00135   typedef Ordinal difference_type;
00136 
00138   typedef T value_type;
00139 
00143   typedef T* pointer;
00144 
00146   typedef const T* const_pointer;
00147 
00151   typedef T& reference;
00152 
00154   typedef const T& const_reference;
00155 
00156 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00157 
00158   typedef ArrayRCP<T> iterator;
00160   typedef ArrayRCP<const T> const_iterator;
00161 #else
00162 
00163   typedef pointer iterator;
00165   typedef const_pointer const_iterator;
00166 #endif
00167 
00169 
00170 
00171 
00173   ArrayView( ENull null_arg = null );
00174 
00190   ArrayView (T* p, size_type size, 
00191        const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
00192 
00203   ArrayView (const ArrayView<T>& array);
00204 
00206   ArrayView (std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
00207 
00209   ArrayView (const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
00210 
00212   ArrayView<T>& operator= (const ArrayView<T>& array);
00213 
00215   ~ArrayView();
00216 
00218 
00219 
00220 
00222   bool is_null() const;
00223 
00225   size_type size() const;
00226 
00228   std::string toString() const;
00229 
00231 
00232 
00233 
00235   inline T* getRawPtr() const;
00236 
00244   T& operator[](size_type i) const;
00245 
00247   T& front() const;
00248 
00250   T& back() const;
00251 
00253 
00254 
00255 
00270   ArrayView<T> view( size_type offset, size_type size ) const;
00271 
00275   ArrayView<T> operator()( size_type offset, size_type size ) const;
00276 
00278   const ArrayView<T>& operator() () const;
00279 
00281   ArrayView<const T> getConst() const;
00282 
00290   operator ArrayView<const T>() const;
00291 
00293 
00295 
00314   void assign (const ArrayView<const T>& array) const;
00315 
00317 
00318 
00319 
00332   iterator begin() const;
00333 
00346   iterator end() const;
00347 
00349 
00350 
00351 
00355   const ArrayView<T>& assert_not_null() const;
00356 
00361   const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
00362 
00364 
00365 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00366 
00367   // I should make these private but templated friends are not very portable.
00368   // Besides, if a client directly calls this it will not compile in an
00369   // optimized build.
00370 
00371   explicit ArrayView( const ArrayRCP<T> &arcp );
00372 
00373   explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
00374 
00375 #endif
00376 
00377 private:
00378   T *ptr_; //<! Pointer to the data
00379   int size_; //<! Number of entries in the view
00380 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00381   ArrayRCP<T> arcp_;
00382 #endif
00383 
00384   void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
00385 
00386   void debug_assert_not_null() const {
00387 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00388     assert_not_null();
00389 #endif
00390   }
00391 
00392   void debug_assert_in_range( size_type offset, size_type size_in ) const {
00393     (void)offset; (void)size_in;
00394 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00395     assert_in_range(offset, size_in);
00396 #endif
00397   }
00398 
00399   void debug_assert_valid_ptr() const {
00400 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00401     arcp_.access_private_node().assert_valid_ptr(*this);
00402 #endif
00403   }
00404 
00405 public: // Bad bad bad
00406 
00407   // This is a very bad breach of encapsulation but it exists to avoid
00408   // problems with portability of tempalted friends
00409   T* access_private_ptr() const
00410     { return ptr_; }
00411 
00412 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00413   ArrayRCP<T> access_private_arcp() const
00414     { return arcp_; }
00415 #endif
00416 
00417 };
00418 
00419 
00427 template<class T>
00428 class ArrayView<const T> {
00429 public:
00430   typedef Teuchos_Ordinal Ordinal;
00431   typedef Ordinal size_type;
00432   typedef Ordinal difference_type;
00433   typedef const T value_type;
00434   typedef const T* pointer;
00435   typedef const T* const_pointer;
00436   typedef const T& reference;
00437   typedef const T& const_reference;
00438 
00439 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00440   typedef ArrayRCP<const T> iterator;
00441   typedef ArrayRCP<const T> const_iterator;
00442 #else
00443   typedef pointer iterator;
00444   typedef const_pointer const_iterator;
00445 #endif
00446 
00447   ArrayView( ENull null_arg = null );
00448 
00449   ArrayView (const T* p, size_type size,
00450        const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
00451 
00452   ArrayView (const ArrayView<const T>& array);
00453 
00454   ArrayView (std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
00455 
00456   ArrayView (const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec);
00457 
00458   ArrayView<const T>& operator= (const ArrayView<const T>& array);
00459 
00460   ~ArrayView();
00461 
00462   bool is_null() const;
00463 
00464   size_type size() const;
00465 
00466   std::string toString() const;
00467 
00468   inline const T* getRawPtr() const;
00469 
00470   const T& operator[] (size_type i) const;
00471 
00472   const T& front() const;
00473 
00474   const T& back() const;
00475 
00476   ArrayView<const T> view( size_type offset, size_type size ) const;
00477 
00478   ArrayView<const T> operator()( size_type offset, size_type size ) const;
00479 
00480   const ArrayView<const T>& operator()() const;
00481 
00487   ArrayView<const T> getConst() const;
00488 
00489   iterator begin() const;
00490 
00491   iterator end() const;
00492 
00493   const ArrayView<const T>& assert_not_null() const;
00494 
00495   const ArrayView<const T>& assert_in_range( size_type offset, size_type size ) const;
00496 
00497 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00498 
00499   // I should make these private but templated friends are not very
00500   // portable.  Besides, if a client directly calls this it will not
00501   // compile in an optimized build.
00502 
00503   explicit ArrayView (const ArrayRCP<const T> &arcp );
00504 
00505   explicit ArrayView (const T* p, size_type size, const ArrayRCP<const T> &arcp );
00506 
00507 #endif
00508 
00509 private:
00510   const T* ptr_;
00511   int size_;
00512 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00513   ArrayRCP<const T> arcp_;
00514 #endif
00515 
00516   void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
00517 
00518   void debug_assert_not_null() const {
00519 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00520     assert_not_null();
00521 #endif
00522   }
00523 
00524   void debug_assert_in_range( size_type offset, size_type size_in ) const {
00525     (void)offset; (void)size_in;
00526 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00527     assert_in_range(offset, size_in);
00528 #endif
00529   }
00530 
00531   void debug_assert_valid_ptr() const {
00532 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00533     arcp_.access_private_node().assert_valid_ptr(*this);
00534 #endif
00535   }
00536 
00537 public: // Bad bad bad
00538 
00539   // This is a very bad breach of encapsulation but it exists to avoid
00540   // problems with portability of templated friends
00541   const T* access_private_ptr() const { return ptr_; }
00542 
00543 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00544   ArrayRCP<const T> access_private_arcp() const { return arcp_; }
00545 #endif
00546 };
00547 
00548 
00549 
00554 template<class T>
00555 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
00556 
00557 
00562 template<class T>
00563 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00564 
00565 
00570 template<class T>
00571 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00572 
00573 
00574 #ifndef __sun
00575 
00576 
00577 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00578 // a call to this function below is ambiguous.  However, if you just comment
00579 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00580 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00581 
00582 
00590 template<class T>
00591 std::vector<T> createVector( const ArrayView<T> &av );
00592 
00593 
00594 #endif // __sun
00595 
00596 
00604 template<class T>
00605 std::vector<T> createVector( const ArrayView<const T> &av );
00606 
00607 
00612 template<class T>
00613 bool is_null( const ArrayView<T> &av );
00614 
00615 
00620 template<class T>
00621 bool nonnull( const ArrayView<T> &av );
00622 
00623 
00631 template<class T>
00632 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
00633 
00634 
00643 template<class T2, class T1>
00644 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
00645 
00646 
00659 template<class T2, class T1>
00660 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
00661 
00662 
00663 } // end namespace Teuchos
00664 
00665 
00666 //
00667 // Inline members
00668 //
00669 
00670 
00671 // ToDo: Fill in!
00672 
00673 
00674 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines