arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

arrayOwn1.h

Go to the documentation of this file.
00001 /*
00002  * $Id: arrayOwn1.h,v 1.2 2003/06/27 04:58:37 jmk Exp $
00003  */
00004 ///////////////////////////////////////////////////////////////////////////
00005 //              _____________  ______________________    __   __  _____
00006 //             /  ________  |  |   ___   ________   /   |  \ /  \   |
00007 //            |  |       |  |_ |  |_ |  |       /  /    \__  |      |
00008 //            |  |  ___  |  || |  || |  |      /  /        | |      |
00009 //            |  | |   \ |  || |  || |  |     /  /      \__/ \__/ __|__
00010 //            |  | |_@  ||  || |  || |  |    /  /          Institute
00011 //            |  |___/  ||  ||_|  || |  |   /  /_____________________
00012 //             \_______/  \______/ | |__|  /___________________________
00013 //                        |  |__|  |
00014 //                         \______/
00015 //                    University of Utah       
00016 //                           2002
00017 ///////////////////////////////////////////////////////////////////////////
00018 ////////////////////////////////////////////////////////////////////////
00019 // 9/7/02       Aaron Lefohn    Scientific Computing and Imaging Institute
00020 //                                                      School of Computing
00021 //                                                      University of Utah
00022 ////////////////////////////////////////////////////////////////////////
00023 //
00024 // arrayOwn1 - An array that OWNS its data
00025 //                       - The constructors copy the data in
00026 //                       - The destructor deletes the data
00027 //                       
00028 ////////////////////////////////////////////////////////////////////////
00029 
00030 #ifndef __ARRAY_OWN_1_H__
00031 #define __ARRAY_OWN_1_H__
00032 
00033 #include "arrayWrap1.h"
00034 #include <assert.h>
00035 
00036 namespace gutz {
00037 
00038 ////////////////////////////////////
00039 // ArrayOwn1
00040 ////////////////////////////////////
00041 
00042 template <class T>
00043 class arrayOwn1 : public arrayWrap1<T>
00044 {
00045 public:
00046         // Constructors - Deep Copies of data
00047     arrayOwn1();
00048     arrayOwn1( int s, T v ); 
00049         arrayOwn1( int s, const T* v);
00050         arrayOwn1( const arrayWrap1<T>& a );
00051         
00052         arrayOwn1( const arrayOwn1<T>& a );
00053     arrayOwn1<T>& operator=( const arrayOwn1<T>& a );
00054     
00055     ~arrayOwn1() {killData();}
00056 
00057         // Transfer ownership of data ('v') to this object.
00058         // - Destroys previous contents of 'this' before doing shallow copy of new data
00059         // - 'killWithDelete' is true if 'v' is allocated with 'new', otherwise false ('malloc')
00060         void transfer( int s, T* v, bool killWithDelete );
00061         
00062 private:
00063         void initVal (T v);
00064         void initData (const T* v);
00065 };
00066 
00067 
00068 ////////////////////////////////////
00069 // Implementation
00070 ////////////////////////////////////
00071 
00072 // Constructors
00073 template <class T>
00074 arrayOwn1<T>::arrayOwn1 ()
00075                                 : arrayWrap1<T>()
00076 {}
00077 
00078 template <class T>
00079 arrayOwn1<T>::arrayOwn1 (int s, T v)
00080                                 : arrayWrap1<T>(s,NULL)
00081 {
00082     arrayBase<T>::initValOwn( v );
00083 }
00084 
00085 template <class T>
00086 arrayOwn1<T>::arrayOwn1 (int s, const T* v)
00087                                 : arrayWrap1<T>(s,NULL)
00088 {
00089         arrayBase<T>::initDataOwn( v );
00090 }
00091 
00092 template <class T>
00093 arrayOwn1<T>::arrayOwn1 ( const arrayWrap1<T>& a)
00094                                 : arrayWrap1<T>(a.size(),NULL)
00095 {
00096         arrayBase<T>::initDataOwn(a.data());
00097 }
00098 
00099 // Copy Constructor: "this" does NOT exist yet
00100 template <class T>
00101 arrayOwn1<T>::arrayOwn1( const arrayOwn1<T>& a ) : arrayWrap1<T>(a) // SHALLOW copy 'a' 
00102 {
00103         arrayBase<T>::initDataOwn(a.mData);                                                             // DEEP copy of a
00104 }
00105 
00106 // Assignment operator: "this" DOES exist
00107 template <class T>
00108 arrayOwn1<T>& arrayOwn1<T>::operator=( const arrayOwn1<T>& a )
00109 {
00110         if( &a != this ) {
00111                 // Kill data and allocate new memory only if sizes don't match.
00112                 if( (mSize != a.size() ) || 
00113                         (dim(0) != a.dim(0)) ) {
00114                         killData();
00115                         arrayWrap1<T>::operator=(a);    // SHALLOW copy of a
00116                         arrayBase<T>::allocDataOwn();
00117                 }
00118                 arrayBase<T>::copyDataOwn(a.mData);     // Deep copy of a.mData
00119         }
00120         return *this; 
00121 }
00122 
00123 // Transfer ownership of data ('v') to this object.
00124 // - Destroys previous contents of 'this' before doing shallow copy of new data
00125 template <class T>
00126 void arrayOwn1<T>::transfer( int s, T* v, bool killWithDelete )
00127 {
00128         killData();
00129         
00130         int sizes[1];
00131         sizes[0] = s;
00132         set(1, sizes, v);
00133 
00134         mKillWithDelete = killWithDelete;
00135 }
00136 
00137 
00138 ////////////////////////////////////
00139 // Typedefs
00140 ////////////////////////////////////
00141 
00142 typedef arrayOwn1<char>                 arrayo1c;
00143 typedef arrayOwn1<uchar>                arrayo1uc;
00144 typedef arrayOwn1<char>                 arrayo1b;
00145 typedef arrayOwn1<uchar>                arrayo1ub;
00146 typedef arrayOwn1<short>                arrayo1s;
00147 typedef arrayOwn1<ushort>               arrayo1us;
00148 typedef arrayOwn1<int>                  arrayo1i;
00149 typedef arrayOwn1<uint>                 arrayo1ui;
00150 typedef arrayOwn1<long>                 arrayo1l;
00151 typedef arrayOwn1<ulong>                arrayo1ul;
00152 typedef arrayOwn1<llong>                arrayo1ll;
00153 typedef arrayOwn1<ullong>               arrayo1ull;
00154 typedef arrayOwn1<float>                arrayo1f;
00155 typedef arrayOwn1<double>               arrayo1d;
00156 
00157 #ifdef USING_MATHGUTZ
00158         typedef arrayOwn1<vec2ub>       arrayo1v2ub;
00159         typedef arrayOwn1<vec2i>        arrayo1v2i;
00160         typedef arrayOwn1<vec2ui>       arrayo1v2ui;
00161         typedef arrayOwn1<vec2f>        arrayo1v2f;
00162 
00163         typedef arrayOwn1<vec3ub>       arrayo1v3ub;
00164         typedef arrayOwn1<vec3i>        arrayo1v3i;
00165         typedef arrayOwn1<vec3ui>       arrayo1v3ui;
00166         typedef arrayOwn1<vec3f>        arrayo1v3f;
00167 
00168         typedef arrayOwn1<vec4ub>       arrayo1v4ub;
00169         typedef arrayOwn1<vec4i>        arrayo1v4i;
00170         typedef arrayOwn1<vec4ui>       arrayo1v4ui;
00171         typedef arrayOwn1<vec4f>        arrayo1v4f;
00172 
00173         typedef arrayOwn1<mat2ub>       arrayo1m2ub;
00174         typedef arrayOwn1<mat2i>        arrayo1m2i;
00175         typedef arrayOwn1<mat2ui>       arrayo1m2ui;
00176         typedef arrayOwn1<mat2f>        arrayo1m2f;
00177 
00178         typedef arrayOwn1<mat3ub>       arrayo1m3ub;
00179         typedef arrayOwn1<mat3i>        arrayo1m3i;
00180         typedef arrayOwn1<mat3ui>       arrayo1m3ui;
00181         typedef arrayOwn1<mat3f>        arrayo1m3f;
00182 
00183         typedef arrayOwn1<mat4ub>       arrayo1m4ub;
00184         typedef arrayOwn1<mat4i>        arrayo1m4i;
00185         typedef arrayOwn1<mat4ui>       arrayo1m4ui;
00186         typedef arrayOwn1<mat4f>        arrayo1m4f;
00187 #endif
00188 
00189 } // End of namespace gutz
00190 
00191 #endif // arrayOwn1

Send questions, comments, and bug reports to:
jmk