Teuchos - Trilinos Tools Package Version of the Day
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 // 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 
00230   ArrayView<T> view( size_type offset, size_type size ) const;
00231 
00235   ArrayView<T> operator()( size_type offset, size_type size ) const;
00236 
00239   const ArrayView<T>& operator()() const;
00240 
00246   ArrayView<const T> getConst() const;
00247 
00248 // 2009/06/30: rabartl: Disable Intel compiler warning #597 about the function
00249 // below not ever being called.  This is a bogus warning and if you comment
00250 // out this function, the Teuchos unit tests for this class will not compile
00251 // (see Trilinos bug 4457).
00252 #ifdef __INTEL_COMPILER
00253 #  pragma warning(disable : 597)
00254 #endif
00255 
00261   operator ArrayView<const T>() const;
00262 
00264 
00267 
00284   void assign(const ArrayView<const T>& array) const;
00285 
00287 
00289 
00290 
00303   iterator begin() const;
00304 
00317   iterator end() const;
00318 
00320 
00322 
00323 
00327   const ArrayView<T>& assert_not_null() const;
00328 
00333   const ArrayView<T>& assert_in_range( size_type offset, size_type size ) const;
00334 
00336 
00337 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00338 
00339   // I should make these private but templated friends are not very portable.
00340   // Besides, if a client directly calls this it will not compile in an
00341   // optimized build.
00342 
00343   explicit ArrayView( const ArrayRCP<T> &arcp );
00344 
00345   explicit ArrayView( T* p, size_type size, const ArrayRCP<T> &arcp );
00346 
00347 #endif
00348 
00349 private:
00350 
00351   // ///////////////////////
00352   // Private data members
00353 
00354   T *ptr_;
00355   int size_;
00356 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00357   ArrayRCP<T> arcp_;
00358 #endif
00359 
00360   void setUpIterators(const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP);
00361 
00362   // ///////////////////////
00363   // Private member functions
00364 
00365   void debug_assert_not_null() const
00366     {
00367 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00368       assert_not_null();
00369 #endif
00370     }
00371 
00372   void debug_assert_in_range( size_type offset, size_type size_in ) const
00373     {
00374       (void)offset; (void)size_in;
00375 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00376       assert_in_range(offset, size_in);
00377 #endif
00378     }
00379 
00380   void debug_assert_valid_ptr() const
00381     {
00382 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00383       arcp_.access_private_node().assert_valid_ptr(*this);
00384 #endif
00385     }
00386 
00387 public: // Bad bad bad
00388 
00389   // This is a very bad breach of encapsulation but it exists to avoid
00390   // problems with portability of tempalted friends
00391   T* access_private_ptr() const
00392     { return ptr_; }
00393 
00394 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00395   ArrayRCP<T> access_private_arcp() const
00396     { return arcp_; }
00397 #endif
00398 
00399 };
00400 
00401 
00406 template<class T>
00407 ArrayView<T> arrayView( T* p, typename ArrayView<T>::size_type size );
00408 
00409 
00414 template<class T>
00415 ArrayView<T> arrayViewFromVector( std::vector<T>& vec );
00416 
00417 
00422 template<class T>
00423 ArrayView<const T> arrayViewFromVector( const std::vector<T>& vec );
00424 
00425 
00426 #ifndef __sun
00427 
00428 
00429 // 2007/11/30: From some reason, the Sun C++ compile on sass9000 compains that
00430 // a call to this function below is ambiguous.  However, if you just comment
00431 // the function out, then the code on g++ (3.4.6 at least) will not compile.
00432 // Therefore, I have no choice but to put in a hacked ifdef for the sun.
00433 
00434 
00442 template<class T>
00443 std::vector<T> createVector( const ArrayView<T> &av );
00444 
00445 
00446 #endif // __sun
00447 
00448 
00456 template<class T>
00457 std::vector<T> createVector( const ArrayView<const T> &av );
00458 
00459 
00464 template<class T>
00465 bool is_null( const ArrayView<T> &av );
00466 
00467 
00472 template<class T>
00473 bool nonnull( const ArrayView<T> &av );
00474 
00475 
00483 template<class T>
00484 std::ostream& operator<<( std::ostream& out, const ArrayView<T>& av );
00485 
00486 
00495 template<class T2, class T1>
00496 ArrayView<T2> av_const_cast(const ArrayView<T1>& p1);
00497 
00498 
00511 template<class T2, class T1>
00512 ArrayView<T2> av_reinterpret_cast(const ArrayView<T1>& p1);
00513 
00514 
00515 } // end namespace Teuchos
00516 
00517 
00518 //
00519 // Inline members
00520 //
00521 
00522 
00523 // ToDo: Fill in!
00524 
00525 
00526 #endif  // TEUCHOS_ARRAY_VIEW_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines