CheckedRowMatrixT.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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/RowMatrixT.h"
  9. namespace NICE {
  10. /**
  11. * @brief This subclass of \c RowMatrixT 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 RowMatrixT)
  17. * are only called if the object is CheckedRowMatrixT in the <b>static</b>
  18. * context. Example:<br>
  19. * @verbatim
  20. * CheckedRowMatrixT<float> v(10, 4.5);
  21. * RowMatrixT<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 CheckedRowMatrixT : public RowMatrixT<ElementType> {
  34. public:
  35. inline CheckedRowMatrixT() : RowMatrixT<ElementType>() {}
  36. explicit CheckedRowMatrixT(const size_t rows, const size_t cols)
  37. : RowMatrixT<ElementType>(rows, cols) {}
  38. CheckedRowMatrixT(const size_t rows, const size_t cols,
  39. const ElementType& element)
  40. : RowMatrixT<ElementType>(rows, cols, element) {}
  41. CheckedRowMatrixT(const ElementType* _data, const size_t rows, const size_t cols)
  42. : RowMatrixT<ElementType>(_data, rows, cols) {}
  43. CheckedRowMatrixT(ElementType* _data, const size_t rows, const size_t cols,
  44. const typename RowMatrixT<ElementType>::Mode mode
  45. = MatrixBase::copy)
  46. : RowMatrixT<ElementType>(_data, rows, cols, mode) {}
  47. explicit CheckedRowMatrixT(std::istream& input)
  48. : RowMatrixT<ElementType>(input) {}
  49. CheckedRowMatrixT(const CheckedRowMatrixT<ElementType>& v)
  50. : RowMatrixT<ElementType>(v) {}
  51. CheckedRowMatrixT(const RowMatrixT<ElementType>& v)
  52. : RowMatrixT<ElementType>(v) {}
  53. /**
  54. * Retrieve a reference to Element (i,j)
  55. * @param i row
  56. * @param j col
  57. */
  58. inline typename RowMatrixT<ElementType>::reference
  59. operator()(const ptrdiff_t i, const ptrdiff_t j) {
  60. if (i < 0 || static_cast<unsigned int>(i) >= this->rows()
  61. || j < 0 || static_cast<unsigned int>(j) >= this->cols()) {
  62. throw std::out_of_range("RowMatrixT () access out of range");
  63. }
  64. return RowMatrixT<ElementType>::operator()(i, j);
  65. }
  66. virtual ~CheckedRowMatrixT();
  67. };
  68. template<class ElementType> CheckedRowMatrixT<ElementType>::~CheckedRowMatrixT() {
  69. }
  70. }
  71. #endif // _CHECKEDEMATRIX_BASICMATRIX_H