Teuchos - Trilinos Tools Package Version of the Day
Teuchos_ArrayRCPDecl.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 
00043 #ifndef TEUCHOS_ARRAY_RCP_DECL_HPP
00044 #define TEUCHOS_ARRAY_RCP_DECL_HPP
00045 
00046 
00047 #include "Teuchos_RCP.hpp"
00048 #include "Teuchos_Exceptions.hpp"
00049 #include "Teuchos_ArrayViewDecl.hpp"
00050 
00051 
00052 namespace Teuchos {
00053 
00084 template<class T>
00085 class ArrayRCP {
00086 public:
00087 
00089 
00090 
00092   typedef Teuchos_Ordinal Ordinal;
00093 
00095   typedef Ordinal size_type;
00097   typedef Ordinal difference_type;
00099   typedef std::random_access_iterator_tag iterator_category;
00101   typedef  T* iterator_type;
00103   typedef  T value_type;
00105   typedef T& reference; 
00107   typedef const T& const_reference; 
00109   typedef T* pointer;
00111   typedef T* const_pointer;
00113   typedef T  element_type;
00114 
00115 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00116 
00117   typedef ArrayRCP<T> iterator;
00119   typedef ArrayRCP<const T> const_iterator;
00120 #else
00121   typedef T* iterator;
00122   typedef const T* const_iterator;
00123 #endif
00124 
00126 
00128 
00129 
00142   inline ArrayRCP( ENull null_arg = null );
00143 
00156   inline ArrayRCP( T* p, size_type lowerOffset, size_type size,
00157     bool has_ownership, const ERCPNodeLookup rcpNodeLookup = RCP_ENABLE_NODE_LOOKUP );
00158 
00171   template<class Dealloc_T>
00172   inline ArrayRCP( T* p, size_type lowerOffset, size_type size, Dealloc_T dealloc,
00173     bool has_ownership );
00174 
00183   inline explicit ArrayRCP( size_type size, const T& val = T() );
00184 
00202   inline ArrayRCP(const ArrayRCP<T>& r_ptr);
00203 
00216   inline ~ArrayRCP();
00217 
00235   inline ArrayRCP<T>& operator=(const ArrayRCP<T>& r_ptr);
00236 
00238 
00240 
00241 
00243   inline bool is_null() const;
00244 
00254   inline T* operator->() const;
00255 
00265   inline T& operator*() const;
00266 
00274   inline T* get() const;
00275 
00283   inline T* getRawPtr() const;
00284 
00292   inline T& operator[](size_type offset) const;
00293 
00295 
00297 
00298 
00309   inline ArrayRCP<T>& operator++();
00310 
00321   inline ArrayRCP<T> operator++(int);
00322 
00333   inline ArrayRCP<T>& operator--();
00334 
00345   inline ArrayRCP<T> operator--(int);
00346 
00357   inline ArrayRCP<T>& operator+=(size_type offset);
00358 
00369   inline ArrayRCP<T>& operator-=(size_type offset);
00370 
00385   inline ArrayRCP<T> operator+(size_type offset) const;
00386 
00401   inline ArrayRCP<T> operator-(size_type offset) const;
00402 
00404 
00406 
00407 
00420   inline iterator begin() const;
00421 
00434   inline iterator end() const;
00435 
00437 
00439 
00440 
00446   inline ArrayRCP<const T> getConst() const;
00447 
00465   inline ArrayRCP<T> persistingView( size_type lowerOffset, size_type size ) const;
00466 
00468 
00470 
00471 
00473   inline size_type lowerOffset() const;
00474 
00476   inline size_type upperOffset() const;
00477 
00481   inline size_type size() const;
00482 
00484 
00486 
00487 
00505   inline ArrayView<T> view( size_type lowerOffset, size_type size ) const;
00506 
00510   inline ArrayView<T> operator()( size_type lowerOffset, size_type size ) const;
00511 
00516   inline ArrayView<T> operator()() const;
00517 
00519 
00521 
00522 
00524   inline operator ArrayRCP<const T>() const;
00525 
00527 
00529 
00530 
00535   inline void assign(size_type n, const T &val);
00536 
00544   template<class Iter>
00545   inline void assign(Iter first, Iter last);
00546 
00552   inline void deepCopy(const ArrayView<const T>& av);
00553 
00557   inline void resize(const size_type n, const T &val = T());
00558 
00563   inline void clear();
00564 
00566 
00568 
00569 
00581   inline ERCPStrength strength() const;
00582 
00592   inline bool is_valid_ptr() const;
00593 
00599   inline int strong_count() const;
00600 
00606   inline int weak_count() const;
00607 
00609   inline int total_count() const;
00610 
00628   inline void set_has_ownership();
00629 
00641   inline bool has_ownership() const;
00642 
00663   inline T* release();
00664 
00681   inline ArrayRCP<T> create_weak() const;
00682 
00699   inline ArrayRCP<T> create_strong() const;
00700 
00707   template<class T2>
00708   inline bool shares_resource(const ArrayRCP<T2>& r_ptr) const;
00709 
00711 
00713 
00714 
00718   inline const ArrayRCP<T>& assert_not_null() const;
00719 
00725   inline const ArrayRCP<T>& assert_in_range( size_type lowerOffset, size_type size ) const;
00726 
00736   inline const ArrayRCP<T>& assert_valid_ptr() const;
00737 
00739 
00740 
00743 
00745   inline TEUCHOS_DEPRECATED int count() const;
00746 
00748 
00749 private:
00750 
00751   // //////////////////////////////////////////////////////////////
00752   // Private data members
00753 
00754   T *ptr_; // NULL if this pointer is null
00755   RCPNodeHandle node_; // NULL if this pointer is null
00756   size_type lowerOffset_; // 0 if this pointer is null
00757   size_type upperOffset_; // -1 if this pointer is null
00758 
00759   inline void debug_assert_not_null() const
00760     {
00761 #ifdef TEUCHOS_REFCOUNTPTR_ASSERT_NONNULL
00762       assert_not_null();
00763 #endif
00764     }
00765 
00766   inline void debug_assert_in_range( size_type lowerOffset_in,
00767     size_type size_in ) const
00768     {
00769       (void)lowerOffset_in; (void)size_in;
00770 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00771       assert_in_range(lowerOffset_in, size_in);
00772 #endif
00773     }
00774 
00775   inline void debug_assert_valid_ptr() const
00776     {
00777 #ifdef TEUCHOS_DEBUG
00778       assert_valid_ptr();
00779 #endif
00780     }
00781 
00782 public:
00783 
00784 
00785 #ifndef DOXYGEN_COMPILE
00786   // These constructors should be private but I have not had good luck making
00787   // this portable (i.e. using friendship etc.) in the past
00788   // This is a very bad breach of encapsulation that is needed since MS VC++
00789   // 5.0 will not allow me to declare template functions as friends.
00790   ArrayRCP( T* p, size_type lowerOffset, size_type size,
00791     const RCPNodeHandle& node );
00792   T* access_private_ptr() const;
00793   RCPNodeHandle& nonconst_access_private_node();
00794   const RCPNodeHandle& access_private_node() const;
00795 #endif
00796 
00797 };  // end class ArrayRCP<...>
00798 
00799 
00809 template<>
00810 class ArrayRCP<void> {
00811 public:
00812 
00814 
00815 
00817   typedef Teuchos_Ordinal Ordinal;
00818 
00820   typedef Ordinal size_type;
00822   typedef Ordinal difference_type;
00824   typedef std::random_access_iterator_tag iterator_category;
00826   typedef  void* iterator_type;
00828   typedef  void value_type;
00830   // typedef T& reference;              // these are not valid
00832   // typedef const T& const_reference;  // these are not valid
00834   typedef void* pointer;
00836   typedef void* const_pointer;
00838   typedef void  element_type;
00839 
00842   inline ArrayRCP( );
00843 
00845 
00846 };  // end class ArrayRCP<void>
00847 
00852 template<>
00853 class ArrayRCP<const void> {
00854 public:
00855 
00857 
00858 
00860   typedef Teuchos_Ordinal Ordinal;
00861 
00863   typedef Ordinal size_type;
00865   typedef Ordinal difference_type;
00867   typedef std::random_access_iterator_tag iterator_category;
00869   typedef  const void* iterator_type;
00871   typedef  const void value_type;
00873   // typedef T& reference;              // these are not valid
00875   // typedef const T& const_reference;  // these are not valid
00877   typedef const void* pointer;
00879   typedef const void* const_pointer;
00881   typedef const void  element_type;
00882 
00885   inline ArrayRCP( );
00886 
00888 
00889 };  // end class ArrayRCP<void>
00890 
00891 // 2008/09/22: rabartl: NOTE: I removed the TypeNameTraits<ArrayRCP<T> >
00892 // specialization since I want to be able to print the type name of an
00893 // ArrayRCP that does not have the type T fully defined!
00894 
00895 
00900 template<typename T>
00901 class NullIteratorTraits<ArrayRCP<T> > {
00902 public:
00903   static ArrayRCP<T> getNull() { return null; }
00904 };
00905 
00906 
00912 template<class T>
00913 ArrayRCP<T> arcp(
00914   T* p,
00915   typename ArrayRCP<T>::size_type lowerOffset,
00916   typename ArrayRCP<T>::size_type size,
00917   bool owns_mem = true
00918   );
00919 
00920 
00926 template<class T, class Dealloc_T>
00927 ArrayRCP<T> arcp(
00928   T* p,
00929   typename ArrayRCP<T>::size_type lowerOffset,
00930   typename ArrayRCP<T>::size_type size,
00931   Dealloc_T dealloc, bool owns_mem
00932   );
00933 
00934  
00945 template<class T>
00946 ArrayRCP<T> arcp( typename ArrayRCP<T>::size_type size );
00947 
00948  
00962 template<class T>
00963 ArrayRCP<T> arcpCloneNode( const ArrayRCP<T> &a );
00964 
00965  
00970 template<class T>
00971 ArrayRCP<T> arcpClone( const ArrayView<const T> &v );
00972 
00973 
00984 template<class T, class Embedded>
00985 ArrayRCP<T>
00986 arcpWithEmbeddedObjPreDestroy(
00987   T* p,
00988   typename ArrayRCP<T>::size_type lowerOffset,
00989   typename ArrayRCP<T>::size_type size,
00990   const Embedded &embedded,
00991   bool owns_mem = true
00992   );
00993 
00994 
01005 template<class T, class Embedded>
01006 ArrayRCP<T>
01007 arcpWithEmbeddedObjPostDestroy(
01008   T* p,
01009   typename ArrayRCP<T>::size_type lowerOffset,
01010   typename ArrayRCP<T>::size_type size,
01011   const Embedded &embedded,
01012   bool owns_mem = true
01013   );
01014 
01015 
01027 template<class T, class Embedded>
01028 ArrayRCP<T>
01029 arcpWithEmbeddedObj(
01030   T* p,
01031   typename ArrayRCP<T>::size_type lowerOffset,
01032   typename ArrayRCP<T>::size_type size,
01033   const Embedded &embedded,
01034   bool owns_mem = true
01035   );
01036 
01037 
01043 template<class T>
01044 ArrayRCP<T> arcp( const RCP<std::vector<T> > &v );
01045 
01046 
01052 template<class T>
01053 ArrayRCP<const T> arcp( const RCP<const std::vector<T> > &v );
01054 
01055 
01064 template<class T>
01065 ArrayRCP<T> arcpFromArrayView(const ArrayView<T> &av);
01066 
01067 
01074 template<class T>
01075 RCP<std::vector<T> > get_std_vector( const ArrayRCP<T> &ptr );
01076 
01077 
01084 template<class T>
01085 RCP<const std::vector<T> > get_std_vector( const ArrayRCP<const T> &ptr );
01086 
01087 
01092 template<class T>
01093 bool is_null( const ArrayRCP<T> &p );
01094 
01095 
01100 template<class T>
01101 bool nonnull( const ArrayRCP<T> &p );
01102 
01103 
01108 template<class T>
01109 bool operator==( const ArrayRCP<T> &p, ENull );
01110 
01111 
01116 template<class T>
01117 bool operator!=( const ArrayRCP<T> &p, ENull );
01118 
01119 
01124 template<class T1, class T2>
01125 bool operator==( const ArrayRCP<T1> &p1, const ArrayRCP<T2> &p2 );
01126 
01127 
01132 template<class T1, class T2>
01133 bool operator!=( const ArrayRCP<T1> &p1, const ArrayRCP<T2> &p2 );
01134 
01135 
01140 template<class T1, class T2>
01141 bool operator<( const ArrayRCP<T1> &p1, const ArrayRCP<T2> &p2 );
01142 
01143 
01148 template<class T1, class T2>
01149 bool operator<=( const ArrayRCP<T1> &p1, const ArrayRCP<T2> &p2 );
01150 
01151 
01156 template<class T1, class T2>
01157 bool operator>( const ArrayRCP<T1> &p1, const ArrayRCP<T2> &p2 );
01158 
01159 
01164 template<class T1, class T2>
01165 bool operator>=( const ArrayRCP<T1> &p1, const ArrayRCP<T2> &p2 );
01166 
01167 
01172 template<class T>
01173 typename ArrayRCP<T>::difference_type
01174 operator-( const ArrayRCP<T> &p1, const ArrayRCP<T> &p2 );
01175 
01176 
01185 template<class T2, class T1>
01186 inline
01187 ArrayRCP<T2> arcp_const_cast(const ArrayRCP<T1>& p1);
01188 
01189 
01202 template<class T2, class T1>
01203 ArrayRCP<T2> arcp_reinterpret_cast(const ArrayRCP<T1>& p1);
01204 
01205 
01224 template<class T2, class T1>
01225 ArrayRCP<T2> arcp_reinterpret_cast_nonpod(const ArrayRCP<T1>& p1, const T2& val=T2());
01226 
01227 
01253 template<class T2, class T1>
01254 inline
01255 ArrayRCP<T2> arcp_implicit_cast(const ArrayRCP<T1>& p1);
01256 
01257 
01316 template<class T1, class T2>
01317 void set_extra_data(
01318   const T1 &extra_data, const std::string& name,
01319   const Ptr<ArrayRCP<T2> > &p, EPrePostDestruction destroy_when = POST_DESTROY,
01320   bool force_unique = true );
01321 
01322 
01342 template<class T1, class T2>
01343 T1& get_extra_data( ArrayRCP<T2>& p, const std::string& name );
01344 
01345 
01371 template<class T1, class T2>
01372 const T1& get_extra_data( const ArrayRCP<T2>& p, const std::string& name );
01373 
01374 
01399 template<class T1, class T2>
01400 T1* get_optional_extra_data( ArrayRCP<T2>& p, const std::string& name );
01401 
01402 
01432 template<class T1, class T2>
01433 const T1* get_optional_extra_data( const ArrayRCP<T2>& p, const std::string& name );
01434 
01435 
01446 template<class Dealloc_T, class T>
01447 Dealloc_T& get_nonconst_dealloc( const ArrayRCP<T>& p );
01448 
01449 
01467 template<class Dealloc_T, class T>
01468 const Dealloc_T& get_dealloc( const ArrayRCP<T>& p );
01469 
01470 
01485 template<class Dealloc_T, class T>
01486 const Dealloc_T* get_optional_dealloc( const ArrayRCP<T>& p );
01487 
01488 
01510 template<class Dealloc_T, class T>
01511 Dealloc_T* get_optional_nonconst_dealloc( const ArrayRCP<T>& p );
01512 
01513 
01520 template<class TOrig, class Embedded, class T>
01521 const Embedded& getEmbeddedObj( const ArrayRCP<T>& p );
01522 
01523 
01530 template<class TOrig, class Embedded, class T>
01531 Embedded& getNonconstEmbeddedObj( const ArrayRCP<T>& p );
01532 
01533 
01541 template<class T>
01542 std::ostream& operator<<( std::ostream& out, const ArrayRCP<T>& p );
01543 
01544 
01545 } // end namespace Teuchos
01546 
01547 
01548 #endif  // TEUCHOS_ARRAY_RCP_DECL_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines