row_reference.hpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #pragma once
  2. #include <cstdlib>
  3. #include <stdexcept>
  4. #include <expect/expect.hpp>
  5. #include <math/vector/vector.hpp>
  6. #include "math/matrix/macro.h"
  7. namespace math::matrix {
  8. template <typename T, size_t C> class row_reference {
  9. private:
  10. row_reference(T (&h)[C]) : _handle(h) {}
  11. public:
  12. row_reference(row_reference const &) = delete;
  13. row_reference(row_reference &&) = default;
  14. template <typename S>
  15. row_reference & operator=(row_reference<S, C> const & other) {
  16. VECTOR_FOR_EACH_RANGE(i, C) { _handle[i] = other[i]; }
  17. return *this;
  18. }
  19. T const & operator[](size_t col) const { return _handle[col]; }
  20. T & operator[](size_t col) { return _handle[col]; }
  21. T const & at(size_t col) const {
  22. expects(col < C, std::out_of_range, "column index out of range");
  23. return operator[](col);
  24. }
  25. T & at(size_t col) {
  26. expects(col < C, std::out_of_range, "column index out of range");
  27. return operator[](col);
  28. }
  29. private:
  30. template <typename _T, size_t R, size_t _C> friend class matrix;
  31. T (&_handle)[C];
  32. };
  33. }
  34. #include "math/matrix/undef.h"