vnl_matrix_ref.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_matrix_ref.h
2 #ifndef vnl_matrix_ref_h_
3 #define vnl_matrix_ref_h_
4 //:
5 // \file
6 // \brief vnl_matrix reference to user-supplied storage.
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 04 Aug 96
9 //
10 // \verbatim
11 // Modifications
12 // Documentation updated by Ian Scott 12 Mar 2000
13 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
14 // \endverbatim
15 //
16 //-----------------------------------------------------------------------------
17 
18 #include <vnl/vnl_matrix.h>
19 #include "vnl/vnl_export.h"
20 
21 //: vnl_matrix reference to user-supplied storage
22 // vnl_matrix_ref is a vnl_matrix for which the data space has been
23 // supplied externally. This is useful for two main tasks:
24 // (a) Treating some row-based "C" matrix as a vnl_matrix in order to
25 // perform vnl_matrix operations on it.
26 //
27 // This is a dangerous class. I believe that I've covered all the bases, but
28 // it's really only intended for interfacing with the Fortran routines.
29 //
30 // The big warning is that returning a vnl_matrix_ref pointer will free non-heap
31 // memory if deleted through a vnl_matrix pointer. This should be
32 // very difficult though, as vnl_matrix_ref objects may not be constructed using
33 // operator new, and are therefore unlikely to be the unwitting subject
34 // of an operator delete.
35 template <class T>
36 class VNL_EXPORT vnl_matrix_ref : public vnl_matrix<T>
37 {
38  typedef vnl_matrix<T> Base;
39 
40  public:
41  // Constructors/Destructors--------------------------------------------------
42  vnl_matrix_ref(unsigned int m, unsigned int n, T *datablck) {
43  Base::data = vnl_c_vector<T>::allocate_Tptr(m);
44  for (unsigned int i = 0; i < m; ++i)
45  Base::data[i] = datablck + i * n;
46  Base::num_rows = m;
47  Base::num_cols = n;
48  }
49 
50  vnl_matrix_ref(vnl_matrix_ref<T> const & other) : vnl_matrix<T>() {
51  Base::data = vnl_c_vector<T>::allocate_Tptr(other.rows());
52  for (unsigned int i = 0; i < other.rows(); ++i)
53  Base::data[i] = const_cast<T*>(other.data_block()) + i * other.cols();
54  Base::num_rows = other.rows();
55  Base::num_cols = other.cols();
56  }
57 
58  ~vnl_matrix_ref() {
59  Base::data[0] = nullptr; // Prevent base dtor from releasing our memory
60  }
61 
62  //: Reference to self to make non-const temporaries.
63  // This is intended for passing vnl_matrix_fixed objects to
64  // functions that expect non-const vnl_matrix references:
65  // \code
66  // void mutator( vnl_matrix<double>& );
67  // ...
68  // vnl_matrix_fixed<double,5,3> my_m;
69  // mutator( m ); // Both these fail because the temporary vnl_matrix_ref
70  // mutator( m.as_ref() ); // cannot be bound to the non-const reference
71  // mutator( m.as_ref().non_const() ); // works
72  // \endcode
73  // \attention Use this only to pass the reference to a
74  // function. Otherwise, the underlying object will be destructed and
75  // you'll be left with undefined behaviour.
76  vnl_matrix_ref& non_const() { return *this; }
77 
78  private:
79  //: Resizing is disallowed
80  bool resize (unsigned int, unsigned int) { return false; }
81  //: Resizing is disallowed
82  bool make_size (unsigned int, unsigned int) { return false; }
83  //: Resizing is disallowed
84  bool set_size (unsigned int, unsigned int) { return false; }
85 
86  //: Copy constructor from vnl_matrix<T> is disallowed
87  // (because it would create a non-const alias to the matrix)
88  vnl_matrix_ref(vnl_matrix<T> const &) {}
89 };
90 
91 #endif // vnl_matrix_ref_h_
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
An ordinary mathematical matrix.
#define m
Definition: vnl_vector.h:43
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
static T ** allocate_Tptr(const std::size_t n)
Memory allocation.
T const * data_block() const
Access the contiguous block storing the elements in the matrix row-wise. O(1).
Definition: vnl_matrix.h:601
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179
bool set_size(unsigned r, unsigned c)
Resize to r rows by c columns. Old data lost.
Definition: vnl_matrix.hxx:390
vnl_matrix reference to user-supplied storage.
Definition: vnl_fwd.h:20