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 
00076 template<class T>
00077 class ArrayView {
00078 public:
00079 
00082 
00084   typedef Teuchos_Index Ordinal;
00085 
00087   typedef T value_type;
00089   typedef T* pointer;
00091   typedef const T* const_pointer;
00093   typedef T& reference;
00095   typedef const T& const_reference;
00096 
00097 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00098 
00099   typedef ArrayRCP<T> iterator;
00100 #else
00101 
00102   typedef pointer iterator;
00103 #endif
00104 
00106   typedef size_t size_type;
00108   typedef ptrdiff_t difference_type;
00109 
00111 
00113 
00114 
00123   ArrayView( ENull null_arg = null );
00124 
00144   ArrayView( T* p, Ordinal size );
00145 
00160   ArrayView(const ArrayView<T>& array);
00161 
00163   ArrayView(
00164     std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00165     );
00166 
00168   ArrayView(
00169     const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00170     );
00171 
00174   ~ArrayView();
00175 
00177 
00179 
00180 
00182   Ordinal size() const;
00183 
00185   std::string toString() const;
00186 
00188 
00190 
00191 
00193   inline T* getRawPtr() const;
00194 
00202   T& operator[](Ordinal i) const;
00203 
00205   T& front() const;
00206 
00208   T& back() const;
00209 
00211 
00213 
00214 
00226   ArrayView<T> view( Ordinal offset, Ordinal size ) const;
00227 
00230   ArrayView<T> operator()( Ordinal offset, Ordinal size ) const;
00231 
00234   const ArrayView<T>& operator()() const;
00235 
00241   ArrayView<const T> getConst() const;
00242 
00248   operator ArrayView<const T>() const;
00249 
00251 
00254 
00265   void assign(const ArrayView<const T>& array) const;
00266 
00269   ArrayView<T>& operator=(const ArrayView<T>&);
00270 
00272 
00274 
00275 
00288   iterator begin() const;
00289 
00302   iterator end() const;
00303 
00305 
00307 
00308 
00312   const ArrayView<T>& assert_not_null() const;
00313 
00318   const ArrayView<T>& assert_in_range( Ordinal offset, Ordinal size ) const;
00319 
00321 
00322 
00323 private:
00324 
00325   // ///////////////////////
00326   // Private data members
00327 
00328   T *ptr_;
00329   int size_;
00330 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00331   ArrayRCP<T> arcp_;
00332 #endif
00333 
00334 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00335   void setUpIterators();
00336 #endif
00337 
00338   // ///////////////////////
00339   // Private member functions
00340 
00341   // Disable dynamic allocation
00342   static void* operator new(size_t);
00343 #ifndef TEUCHOS_PRIVIATE_DELETE_NOT_SUPPORTED
00344   //static void operator delete(void*);
00345   // 2008/09/19: rabartl: Above, I commented this out to address bug 4191.
00346   // Making operator delete() private is just meant to help users avoid bad
00347   // coding practices but there is nothing fundamentally illegal about
00348   // dynamically creating an ArrayView object (but it is never advised to do
00349   // so).  Even with this change, Chris Baker is not going to be dynamically
00350   // allocateding ArrayView objects.
00351 #endif
00352 
00353 };
00354 
00355 
00360 template<class T>
00361 ArrayView<T> arrayView( T* p, typename ArrayView<T>::Ordinal size );
00362 
00363 
00368 template<class T>
00369 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00370 
00371 
00376 template<class T>
00377 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00378 
00379 
00380 #ifndef __sun
00381 
00382 
00383 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00384 // a call to this function below is ambiguous.  However, if you just comment
00385 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00386 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00387 
00388 
00396 template<class T>
00397 std::vector<T> createVector( const ArrayView<T> &ptr );
00398 
00399 
00400 #endif // __sun
00401 
00402 
00410 template<class T>
00411 std::vector<T> createVector( const ArrayView<const T> &ptr );
00412 
00413 
00421 template<class T>
00422 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& p );
00423 
00424 
00425 } // end namespace Teuchos
00426 
00427 
00428 //
00429 // Inline members
00430 //
00431 
00432 
00433 // ToDo: Fill in!
00434 
00435 
00436 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP

Generated on Wed May 12 21:40:31 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7