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 // 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_Index Ordinal;
00072 
00074   typedef T value_type;
00076   typedef T* pointer;
00078   typedef const T* const_pointer;
00080   typedef T& reference;
00082   typedef const T& const_reference;
00083 
00084 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00085 
00086   typedef ArrayRCP<T> iterator;
00087 #else
00088 
00089   typedef pointer iterator;
00090 #endif
00091 
00093   typedef size_t size_type;
00095   typedef ptrdiff_t difference_type;
00096 
00098 
00100 
00101 
00103   ArrayView( ENull null_arg = null );
00104 
00124   ArrayView( T* p, Ordinal size );
00125 
00140   ArrayView(const ArrayView<T>& array);
00141 
00143   ArrayView(
00144     std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00145     );
00146 
00148   ArrayView(
00149     const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00150     );
00151   
00153   ArrayView<T>& operator=(const ArrayView<T>& array);
00154 
00157   ~ArrayView();
00158 
00160 
00162 
00163 
00165   bool is_null() const;
00166 
00168   Ordinal size() const;
00169 
00171   std::string toString() const;
00172 
00174 
00176 
00177 
00179   inline T* getRawPtr() const;
00180 
00188   T& operator[](Ordinal i) const;
00189 
00191   T& front() const;
00192 
00194   T& back() const;
00195 
00197 
00199 
00200 
00212   ArrayView<T> view( Ordinal offset, Ordinal size ) const;
00213 
00216   ArrayView<T> operator()( Ordinal offset, Ordinal size ) const;
00217 
00220   const ArrayView<T>& operator()() const;
00221 
00227   ArrayView<const T> getConst() const;
00228 
00229 // 2009/06/30: rabartl: Disable Intel compiler warning #597 about the function
00230 // below not ever being called.  This is a bogus warning and if you comment
00231 // out this function, the Teuchos unit tests for this class will not compile
00232 // (see Trilinos bug 4457).
00233 #ifdef __INTEL_COMPILER
00234 #  pragma warning(disable : 597)
00235 #endif
00236 
00242   operator ArrayView<const T>() const;
00243 
00245 
00248 
00265   void assign(const ArrayView<const T>& array) const;
00266 
00268 
00270 
00271 
00284   iterator begin() const;
00285 
00298   iterator end() const;
00299 
00301 
00303 
00304 
00308   const ArrayView<T>& assert_not_null() const;
00309 
00314   const ArrayView<T>& assert_in_range( Ordinal offset, Ordinal size ) const;
00315 
00317 
00318 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00319 
00320   // I should make these private but templated friends are not very portable.
00321   // Besides, if a client directly calls this it will not compile in an
00322   // optimized build.
00323 
00324   explicit ArrayView( const ArrayRCP<T> &arcp );
00325 
00326   explicit ArrayView( T* p, Ordinal size, const ArrayRCP<T> &arcp );
00327 
00328 #endif
00329 
00330 private:
00331 
00332   // ///////////////////////
00333   // Private data members
00334 
00335   T *ptr_;
00336   int size_;
00337 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00338   ArrayRCP<T> arcp_;
00339 #endif
00340 
00341   void setUpIterators();
00342 
00343   // ///////////////////////
00344   // Private member functions
00345 
00346   void debug_assert_not_null() const
00347     {
00348 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00349       assert_not_null();
00350 #endif
00351     }
00352 
00353   void debug_assert_in_range( Ordinal offset, Ordinal size_in ) const
00354     {
00355       (void)offset; (void)size_in;
00356 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00357       assert_in_range(offset, size_in);
00358 #endif
00359     }
00360 
00361   void debug_assert_valid_ptr() const
00362     {
00363 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00364       arcp_.access_private_node().assert_valid_ptr(*this);
00365 #endif
00366     }
00367 
00368 public: // Bad bad bad
00369 
00370   // This is a very bad breach of encapsulation but it exists to avoid
00371   // problems with portability of tempalted friends
00372   T* access_private_ptr() const
00373     { return ptr_; }
00374 
00375 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00376   ArrayRCP<T> access_private_arcp() const
00377     { return arcp_; }
00378 #endif
00379 
00380 };
00381 
00382 
00387 template<class T>
00388 ArrayView<T> arrayView( T* p, typename ArrayView<T>::Ordinal size );
00389 
00390 
00395 template<class T>
00396 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00397 
00398 
00403 template<class T>
00404 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00405 
00406 
00407 #ifndef __sun
00408 
00409 
00410 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00411 // a call to this function below is ambiguous.  However, if you just comment
00412 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00413 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00414 
00415 
00423 template<class T>
00424 std::vector<T> createVector( const ArrayView<T> &av );
00425 
00426 
00427 #endif // __sun
00428 
00429 
00437 template<class T>
00438 std::vector<T> createVector( const ArrayView<const T> &av );
00439 
00440 
00445 template<class T>
00446 bool is_null( const ArrayView<T> &av );
00447 
00448 
00456 template<class T>
00457 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
00458 
00459 
00472 template<class T2, class T1>
00473 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
00474 
00475 
00476 } // end namespace Teuchos
00477 
00478 
00479 //
00480 // Inline members
00481 //
00482 
00483 
00484 // ToDo: Fill in!
00485 
00486 
00487 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1