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 // 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     const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
00129 
00144   ArrayView(const ArrayView<T>& array);
00145 
00147   ArrayView(
00148     std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00149     );
00150 
00152   ArrayView(
00153     const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00154     );
00155   
00157   ArrayView<T>& operator=(const ArrayView<T>& array);
00158 
00161   ~ArrayView();
00162 
00164 
00166 
00167 
00169   bool is_null() const;
00170 
00172   size_type size() const;
00173 
00175   std::string toString() const;
00176 
00178 
00180 
00181 
00183   inline T* getRawPtr() const;
00184 
00192   T& operator[](size_type i) const;
00193 
00195   T& front() const;
00196 
00198   T& back() const;
00199 
00201 
00203 
00204 
00216   ArrayView<T> view( size_type offset, size_type size ) const;
00217 
00220   ArrayView<T> operator()( size_type offset, size_type size ) const;
00221 
00224   const ArrayView<T>& operator()() const;
00225 
00231   ArrayView<const T> getConst() const;
00232 
00233 // 2009/06/30: rabartl: Disable Intel compiler warning #597 about the function
00234 // below not ever being called.  This is a bogus warning and if you comment
00235 // out this function, the Teuchos unit tests for this class will not compile
00236 // (see Trilinos bug 4457).
00237 #ifdef __INTEL_COMPILER
00238 #  pragma warning(disable : 597)
00239 #endif
00240 
00246   operator ArrayView<const T>() const;
00247 
00249 
00252 
00269   void assign(const ArrayView<const T>& array) const;
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( size_type offset, size_type size ) const;
00319 
00321 
00322 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00323 
00324   // I should make these private but templated friends are not very portable.
00325   // Besides, if a client directly calls this it will not compile in an
00326   // optimized build.
00327 
00328   explicit ArrayView( const ArrayRCP<T> &arcp );
00329 
00330   explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
00331 
00332 #endif
00333 
00334 private:
00335 
00336   // ///////////////////////
00337   // Private data members
00338 
00339   T *ptr_;
00340   int size_;
00341 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00342   ArrayRCP<T> arcp_;
00343 #endif
00344 
00345   void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
00346 
00347   // ///////////////////////
00348   // Private member functions
00349 
00350   void debug_assert_not_null() const
00351     {
00352 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00353       assert_not_null();
00354 #endif
00355     }
00356 
00357   void debug_assert_in_range( size_type offset, size_type size_in ) const
00358     {
00359       (void)offset; (void)size_in;
00360 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00361       assert_in_range(offset, size_in);
00362 #endif
00363     }
00364 
00365   void debug_assert_valid_ptr() const
00366     {
00367 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00368       arcp_.access_private_node().assert_valid_ptr(*this);
00369 #endif
00370     }
00371 
00372 public: // Bad bad bad
00373 
00374   // This is a very bad breach of encapsulation but it exists to avoid
00375   // problems with portability of tempalted friends
00376   T* access_private_ptr() const
00377     { return ptr_; }
00378 
00379 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00380   ArrayRCP<T> access_private_arcp() const
00381     { return arcp_; }
00382 #endif
00383 
00384 };
00385 
00386 
00391 template<class T>
00392 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
00393 
00394 
00399 template<class T>
00400 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00401 
00402 
00407 template<class T>
00408 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00409 
00410 
00411 #ifndef __sun
00412 
00413 
00414 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00415 // a call to this function below is ambiguous.  However, if you just comment
00416 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00417 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00418 
00419 
00427 template<class T>
00428 std::vector<T> createVector( const ArrayView<T> &av );
00429 
00430 
00431 #endif // __sun
00432 
00433 
00441 template<class T>
00442 std::vector<T> createVector( const ArrayView<const T> &av );
00443 
00444 
00449 template<class T>
00450 bool is_null( const ArrayView<T> &av );
00451 
00452 
00457 template<class T>
00458 bool nonnull( const ArrayView<T> &av );
00459 
00460 
00468 template<class T>
00469 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
00470 
00471 
00480 template<class T2, class T1>
00481 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
00482 
00483 
00496 template<class T2, class T1>
00497 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
00498 
00499 
00500 } // end namespace Teuchos
00501 
00502 
00503 //
00504 // Inline members
00505 //
00506 
00507 
00508 // ToDo: Fill in!
00509 
00510 
00511 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines