CheckedMatrixT.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libbasicvector - A simple vector library
  4. * See file License for license information.
  5. */
  6. #ifndef _CHECKEDEMATRIX_BASICMATRIX_H
  7. #define _CHECKEDEMATRIX_BASICMATRIX_H
  8. #include "core/vector/MatrixT.h"
  9. namespace NICE {
  10. /**
  11. * @brief This subclass of \c MatrixT performs additional checks at runtime,
  12. * especially range checks for element access.
  13. *
  14. * Theses checks are implemented via "non-virtual overriding".
  15. * This means that checked versions of methods
  16. * (in this class as opposed to un-checked versions in \c MatrixT)
  17. * are only called if the object is CheckedMatrixT in the <b>static</b>
  18. * context. Example:<br>
  19. * @verbatim
  20. * CheckedMatrixT<float> v(10, 4.5);
  21. * MatrixT<float>& w = v;
  22. * try {
  23. * v[10]; // will throw range_exception
  24. * } catch (std::range_exception) {
  25. * }
  26. * w[10]; // will NOT throw an exception
  27. * // and (probably) cause a segmentation fault
  28. * @endverbatim
  29. *
  30. * See base class for further documentation.
  31. */
  32. template<class ElementType>
  33. class CheckedMatrixT : public MatrixT<ElementType> {
  34. public:
  35. inline CheckedMatrixT() : MatrixT<ElementType>() {}
  36. explicit CheckedMatrixT(const size_t rows, const size_t cols)
  37. : MatrixT<ElementType>(rows, cols) {}
  38. /**
  39. * Create a \c CheckedMatrixT
  40. * @param rows number of rows
  41. * @param cols number of cols
  42. * @param element initialize the elements of \c CheckedMatrixT with the value \c element
  43. */
  44. CheckedMatrixT(const size_t rows, const size_t cols,
  45. const ElementType& element)
  46. : MatrixT<ElementType>(rows, cols, element) {}
  47. /**
  48. * Create a \c CheckedMatrixT
  49. * @param _data pointer to the data of the \c CheckedMatrixT
  50. * @param rows number of rows of the data
  51. * @param cols number of cols of the data
  52. */
  53. CheckedMatrixT(const ElementType* _data, const size_t rows, const size_t cols)
  54. : MatrixT<ElementType>(_data, rows, cols) {}
  55. /**
  56. * Create a \c CheckedMatrixT
  57. * @param _data pointer to the data of the \c CheckedMatrixT
  58. * @param rows number of rows of the data
  59. * @param cols number of cols of the data
  60. * @param mode copy mode
  61. */
  62. CheckedMatrixT(ElementType* _data, const size_t rows, const size_t cols,
  63. const typename MatrixT<ElementType>::Mode mode
  64. = MatrixBase::copy)
  65. : MatrixT<ElementType>(_data, rows, cols, mode) {}
  66. explicit CheckedMatrixT(std::istream& input)
  67. : MatrixT<ElementType>(input) {}
  68. CheckedMatrixT(const CheckedMatrixT<ElementType>& v)
  69. : MatrixT<ElementType>(v) {}
  70. CheckedMatrixT(const MatrixT<ElementType>& v)
  71. : MatrixT<ElementType>(v) {}
  72. /**
  73. * Retrieve a reference to Element (i,j)
  74. * @param i row
  75. * @param j col
  76. */
  77. inline typename MatrixT<ElementType>::reference
  78. operator()(const ptrdiff_t i, const ptrdiff_t j) {
  79. if (i < 0 || static_cast<unsigned int>(i) >= this->rows()
  80. || j < 0 || static_cast<unsigned int>(j) >= this->cols()) {
  81. throw std::out_of_range("MatrixT () access out of range");
  82. }
  83. return MatrixT<ElementType>::operator()(i, j);
  84. }
  85. virtual ~CheckedMatrixT();
  86. };
  87. template<class ElementType>
  88. CheckedMatrixT<ElementType>::~CheckedMatrixT() {
  89. }
  90. }
  91. #endif // _CHECKEDEMATRIX_BASICMATRIX_H