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 
00055 template<class T> class ArrayRCP;
00056 
00057 
00076 template<class T>
00077 class ArrayView {
00078 public:
00079 
00082 
00084   typedef Teuchos_Ordinal Ordinal;
00085 
00087   typedef Ordinal size_type;
00089   typedef Ordinal difference_type;
00091   typedef T value_type;
00093   typedef T* pointer;
00095   typedef const T* const_pointer;
00097   typedef T& reference;
00099   typedef const T& const_reference;
00100 
00101 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00102 
00103   typedef ArrayRCP<T> iterator;
00105   typedef ArrayRCP<const T> const_iterator;
00106 #else
00107 
00108   typedef pointer iterator;
00110   typedef const_pointer const_iterator;
00111 #endif
00112 
00114 
00116 
00117 
00119   ArrayView( ENull null_arg = null );
00120 
00140   ArrayView( T* p, size_type size,
00141     const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
00142 
00157   ArrayView(const ArrayView<T>& array);
00158 
00160   ArrayView(
00161     std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00162     );
00163 
00165   ArrayView(
00166     const std::vector<typename ConstTypeTraits<T>::NonConstType>& vec
00167     );
00168   
00170   ArrayView<T>& operator=(const ArrayView<T>& array);
00171 
00174   ~ArrayView();
00175 
00177 
00179 
00180 
00182   bool is_null() const;
00183 
00185   size_type size() const;
00186 
00188   std::string toString() const;
00189 
00191 
00193 
00194 
00196   inline T* getRawPtr() const;
00197 
00205   T& operator[](size_type i) const;
00206 
00208   T& front() const;
00209 
00211   T& back() const;
00212 
00214 
00216 
00217 
00232   ArrayView<T> view( size_type offset, size_type size ) const;
00233 
00237   ArrayView<T> operator()( size_type offset, size_type size ) const;
00238 
00241   const ArrayView<T>& operator()() const;
00242 
00248   ArrayView<const T> getConst() const;
00249 
00250 // 2009/06/30: rabartl: Disable Intel compiler warning #597 about the function
00251 // below not ever being called.  This is a bogus warning and if you comment
00252 // out this function, the Teuchos unit tests for this class will not compile
00253 // (see Trilinos bug 4457).
00254 #ifdef __INTEL_COMPILER
00255 #  pragma warning(disable : 597)
00256 #endif
00257 
00263   operator ArrayView<const T>() const;
00264 
00266 
00269 
00286   void assign(const ArrayView<const T>& array) const;
00287 
00289 
00291 
00292 
00305   iterator begin() const;
00306 
00319   iterator end() const;
00320 
00322 
00324 
00325 
00329   const ArrayView<T>& assert_not_null() const;
00330 
00335   const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
00336 
00338 
00339 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00340 
00341   // I should make these private but templated friends are not very portable.
00342   // Besides, if a client directly calls this it will not compile in an
00343   // optimized build.
00344 
00345   explicit ArrayView( const ArrayRCP<T> &arcp );
00346 
00347   explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
00348 
00349 #endif
00350 
00351 private:
00352 
00353   // ///////////////////////
00354   // Private data members
00355 
00356   T *ptr_;
00357   int size_;
00358 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00359   ArrayRCP<T> arcp_;
00360 #endif
00361 
00362   void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
00363 
00364   // ///////////////////////
00365   // Private member functions
00366 
00367   void debug_assert_not_null() const
00368     {
00369 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00370       assert_not_null();
00371 #endif
00372     }
00373 
00374   void debug_assert_in_range( size_type offset, size_type size_in ) const
00375     {
00376       (void)offset; (void)size_in;
00377 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00378       assert_in_range(offset, size_in);
00379 #endif
00380     }
00381 
00382   void debug_assert_valid_ptr() const
00383     {
00384 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00385       arcp_.access_private_node().assert_valid_ptr(*this);
00386 #endif
00387     }
00388 
00389 public: // Bad bad bad
00390 
00391   // This is a very bad breach of encapsulation but it exists to avoid
00392   // problems with portability of tempalted friends
00393   T* access_private_ptr() const
00394     { return ptr_; }
00395 
00396 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00397   ArrayRCP<T> access_private_arcp() const
00398     { return arcp_; }
00399 #endif
00400 
00401 };
00402 
00403 
00408 template<class T>
00409 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
00410 
00411 
00416 template<class T>
00417 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00418 
00419 
00424 template<class T>
00425 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00426 
00427 
00428 #ifndef __sun
00429 
00430 
00431 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00432 // a call to this function below is ambiguous.  However, if you just comment
00433 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00434 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00435 
00436 
00444 template<class T>
00445 std::vector<T> createVector( const ArrayView<T> &av );
00446 
00447 
00448 #endif // __sun
00449 
00450 
00458 template<class T>
00459 std::vector<T> createVector( const ArrayView<const T> &av );
00460 
00461 
00466 template<class T>
00467 bool is_null( const ArrayView<T> &av );
00468 
00469 
00474 template<class T>
00475 bool nonnull( const ArrayView<T> &av );
00476 
00477 
00485 template<class T>
00486 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
00487 
00488 
00497 template<class T2, class T1>
00498 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
00499 
00500 
00513 template<class T2, class T1>
00514 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
00515 
00516 
00517 } // end namespace Teuchos
00518 
00519 
00520 //
00521 // Inline members
00522 //
00523 
00524 
00525 // ToDo: Fill in!
00526 
00527 
00528 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines