00001 #ifndef vnl_matrix_ref_h_ 00002 #define vnl_matrix_ref_h_ 00003 00004 // This is vxl/vnl/vnl_matrix_ref.h 00005 00006 //: 00007 // \file 00008 // \brief vnl_matrix reference to user-supplied storage. 00009 // 00010 // \author Andrew W. Fitzgibbon, Oxford RRG, 04 Aug 96 00011 00012 00013 // 00014 //Modifications: 00015 // Documentation updated by Ian Scott 12 Mar 2000 00016 // 00017 //----------------------------------------------------------------------------- 00018 00019 #include <vcl_new.h> 00020 #include <vnl/vnl_matrix.h> 00021 00022 //: vnl_matrix reference to user-supplied storage 00023 // vnl_matrix_ref is a vnl_matrix for which the data space has been 00024 // supplied externally. This is useful for two main tasks: 00025 // (a) Treating some row-based "C" matrix as a vnl_matrix in order to 00026 // perform vnl_matrix operations on it. 00027 // (b) Declaring a vnl_matrix that uses stack-based storage for the 00028 // matrix (See MatrixFixed). Note however that the rows are still allocated 00029 // on the heap. See MatrixFixed for a fully stack-based solution. 00030 // 00031 // This is a dangerous class. I believe that I've covered all the bases, but 00032 // it's really only intended for interfacing with the Fortran routines. 00033 // 00034 // The big warning is that returning a vnl_matrix_ref pointer will free non-heap 00035 // memory if deleted through a vnl_matrix pointer. This should be 00036 // very difficult though, as vnl_matrix_ref objects may not be constructed using 00037 // operator new, and are therefore unlikely to be the unwitting subject 00038 // of an operator delete. 00039 template <class T> 00040 class vnl_matrix_ref : public vnl_matrix<T> { 00041 typedef vnl_matrix<T> Base; 00042 00043 public: 00044 // Constructors/Destructors-------------------------------------------------- 00045 vnl_matrix_ref(int m, int n, T *datablck) { 00046 Base::data = vnl_c_vector<T>::allocate_Tptr(m); 00047 for(int i = 0; i < m; ++i) 00048 Base::data[i] = datablck + i * n; 00049 Base::num_rows = m; 00050 Base::num_cols = n; 00051 } 00052 ~vnl_matrix_ref() { 00053 Base::data[0] = 0; // Prevent base dtor from releasing our memory 00054 } 00055 00056 private: 00057 // Private operator new because deleting a pointer to 00058 // one of these through a baseclass pointer will attempt 00059 // to free this in-class memory. 00060 // Therefore disallow newing of these -- if you're paying for 00061 // one malloc, you can afford three. 00062 // fsm: This was wrong for two reasons: 00063 // 1. operator new may not return a null pointer. 00064 // 2. it should be enabled for compilers that need it, 00065 // not disabled for compilers that don't need it. 00066 //#if !defined(VCL_GCC_295) 00067 // void* operator new(size_t) { return 0; } 00068 //#endif 00069 00070 00071 //: Resizing is disallowed 00072 bool resize (unsigned int, unsigned int) { return 0; } 00073 00074 00075 //: Copy constructor is disallowed 00076 // because it would create a non-const alias to the Matrix 00077 vnl_matrix_ref(vnl_matrix<T> const &) {} 00078 00079 00080 // You can't assign one of these from a matrix, cos' you don't have any space 00081 vnl_matrix_ref(vnl_matrix_ref<T> const &) {} 00082 vnl_matrix_ref<T>& operator=(vnl_matrix<T> const &) { return *this; } 00083 }; 00084 00085 #endif // vnl_matrix_ref_h_