Teuchos_ArrayViewDecl.hpp

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 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_ARRAY_VIEW_DECL_HPP
00030 #define TEUCHOS_ARRAY_VIEW_DECL_HPP
00031 
00032 
00033 #include "Teuchos_RCPNode.hpp"
00034 #include "Teuchos_ENull.hpp"
00035 #include "Teuchos_NullIteratorTraits.hpp"
00036 #include "Teuchos_ConstTypeTraits.hpp"
00037 
00038 
00039 namespace Teuchos {
00040 
00041 
00042 template<class T> class ArrayRCP;
00043 
00044 
00063 template<class T>
00064 class ArrayView {
00065 public:
00066 
00069 
00071   typedef Teuchos_Ordinal Ordinal;
00072 
00074   typedef Ordinal size_type;
00076   typedef Ordinal difference_type;
00078   typedef T value_type;
00080   typedef T* pointer;
00082   typedef const T* const_pointer;
00084   typedef T& reference;
00086   typedef const T& const_reference;
00087 
00088 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00089 
00090   typedef ArrayRCP<T> iterator;
00092   typedef ArrayRCP<const T> const_iterator;
00093 #else
00094 
00095   typedef pointer iterator;
00097   typedef const_pointer const_iterator;
00098 #endif
00099 
00101 
00103 
00104 
00106   ArrayView( ENull null_arg = null );
00107 
00127   ArrayView( T* p, size_type size );
00128 
00143   ArrayView(const ArrayView<T>& array);
00144 
00146   ArrayView(
00147     std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00148     );
00149 
00151   ArrayView(
00152     const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00153     );
00154   
00156   ArrayView<T>& operator=(const ArrayView<T>& array);
00157 
00160   ~ArrayView();
00161 
00163 
00165 
00166 
00168   bool is_null() const;
00169 
00171   size_type size() const;
00172 
00174   std::string toString() const;
00175 
00177 
00179 
00180 
00182   inline T* getRawPtr() const;
00183 
00191   T& operator[](size_type i) const;
00192 
00194   T& front() const;
00195 
00197   T& back() const;
00198 
00200 
00202 
00203 
00215   ArrayView<T> view( size_type offset, size_type size ) const;
00216 
00219   ArrayView<T> operator()( size_type offset, size_type size ) const;
00220 
00223   const ArrayView<T>& operator()() const;
00224 
00230   ArrayView<const T> getConst() const;
00231 
00232 // 2009/06/30: rabartl: Disable Intel compiler warning #597 about the function
00233 // below not ever being called.  This is a bogus warning and if you comment
00234 // out this function, the Teuchos unit tests for this class will not compile
00235 // (see Trilinos bug 4457).
00236 #ifdef __INTEL_COMPILER
00237 #  pragma warning(disable : 597)
00238 #endif
00239 
00245   operator ArrayView<const T>() const;
00246 
00248 
00251 
00268   void assign(const ArrayView<const T>& array) const;
00269 
00271 
00273 
00274 
00287   iterator begin() const;
00288 
00301   iterator end() const;
00302 
00304 
00306 
00307 
00311   const ArrayView<T>& assert_not_null() const;
00312 
00317   const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
00318 
00320 
00321 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00322 
00323   // I should make these private but templated friends are not very portable.
00324   // Besides, if a client directly calls this it will not compile in an
00325   // optimized build.
00326 
00327   explicit ArrayView( const ArrayRCP<T> &arcp );
00328 
00329   explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
00330 
00331 #endif
00332 
00333 private:
00334 
00335   // ///////////////////////
00336   // Private data members
00337 
00338   T *ptr_;
00339   int size_;
00340 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00341   ArrayRCP<T> arcp_;
00342 #endif
00343 
00344   void setUpIterators();
00345 
00346   // ///////////////////////
00347   // Private member functions
00348 
00349   void debug_assert_not_null() const
00350     {
00351 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00352       assert_not_null();
00353 #endif
00354     }
00355 
00356   void debug_assert_in_range( size_type offset, size_type size_in ) const
00357     {
00358       (void)offset; (void)size_in;
00359 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00360       assert_in_range(offset, size_in);
00361 #endif
00362     }
00363 
00364   void debug_assert_valid_ptr() const
00365     {
00366 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00367       arcp_.access_private_node().assert_valid_ptr(*this);
00368 #endif
00369     }
00370 
00371 public: // Bad bad bad
00372 
00373   // This is a very bad breach of encapsulation but it exists to avoid
00374   // problems with portability of tempalted friends
00375   T* access_private_ptr() const
00376     { return ptr_; }
00377 
00378 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00379   ArrayRCP<T> access_private_arcp() const
00380     { return arcp_; }
00381 #endif
00382 
00383 };
00384 
00385 
00390 template<class T>
00391 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
00392 
00393 
00398 template<class T>
00399 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00400 
00401 
00406 template<class T>
00407 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00408 
00409 
00410 #ifndef __sun
00411 
00412 
00413 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00414 // a call to this function below is ambiguous.  However, if you just comment
00415 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00416 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00417 
00418 
00426 template<class T>
00427 std::vector<T> createVector( const ArrayView<T> &av );
00428 
00429 
00430 #endif // __sun
00431 
00432 
00440 template<class T>
00441 std::vector<T> createVector( const ArrayView<const T> &av );
00442 
00443 
00448 template<class T>
00449 bool is_null( const ArrayView<T> &av );
00450 
00451 
00456 template<class T>
00457 bool nonnull( const ArrayView<T> &av );
00458 
00459 
00467 template<class T>
00468 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
00469 
00470 
00479 template<class T2, class T1>
00480 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
00481 
00482 
00495 template<class T2, class T1>
00496 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
00497 
00498 
00499 } // end namespace Teuchos
00500 
00501 
00502 //
00503 // Inline members
00504 //
00505 
00506 
00507 // ToDo: Fill in!
00508 
00509 
00510 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP

Generated on Tue Jul 13 09:22:59 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7