CheckedVectorT.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 _CHECKEDEVECTOR_H_
  7. #define _CHECKEDEVECTOR_H_
  8. #include "core/vector/VectorT.h"
  9. namespace NICE {
  10. /**
  11. * @brief This subclass of \c VectorT 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 VectorT)
  17. * are only called if the object is CheckedVectorT in the <b>static</b>
  18. * context. Example:<br>
  19. * @verbatim
  20. * CheckedVectorT<float> v(10, 4.5);
  21. * VectorT<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 CheckedVectorT : public VectorT<ElementType> {
  34. public:
  35. inline CheckedVectorT() : VectorT<ElementType>() {}
  36. explicit CheckedVectorT(const size_t size) : VectorT<ElementType>(size) {}
  37. CheckedVectorT(const size_t size, const ElementType& element)
  38. : VectorT<ElementType>(size, element) {}
  39. CheckedVectorT(const ElementType* _data, const size_t size)
  40. : VectorT<ElementType>(_data, size) {}
  41. CheckedVectorT(ElementType* _data, const size_t size,
  42. const typename VectorT<ElementType>::Mode mode = VectorT<ElementType>::copy)
  43. : VectorT<ElementType>(_data, size, mode) {}
  44. explicit CheckedVectorT(std::istream& input)
  45. : VectorT<ElementType>(input) {}
  46. CheckedVectorT(const CheckedVectorT<ElementType>& v)
  47. : VectorT<ElementType>(v) {}
  48. CheckedVectorT(const VectorT<ElementType>& v)
  49. : VectorT<ElementType>(v) {}
  50. inline typename VectorT<ElementType>::reference
  51. operator[](const ptrdiff_t i) {
  52. if (i < 0 || static_cast<unsigned int>(i) >= this->size()) {
  53. throw std::out_of_range("VectorT () access out of range");
  54. }
  55. return this->data[i];
  56. }
  57. virtual ~CheckedVectorT();
  58. };
  59. template<class ElementType>
  60. CheckedVectorT<ElementType>::~CheckedVectorT() {
  61. }
  62. }
  63. #endif //_CHECKEDEVECTOR_H_