BoundChecks.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <stdexcept>
8 #include <string>
9 
10 #include "Utilities/PrettyType.hpp"
11 
12 namespace py_bindings {
13 /*!
14  * \ingroup PythonBindingsGroup
15  * \brief Check if a vector-like object access is in bounds. Throws
16  * std::runtime_error if it is not.
17  */
18 template <typename T>
19 void bounds_check(const T& t, const size_t i) {
20  if (i >= t.size()) {
21  throw std::runtime_error{"Out of bounds access (" + std::to_string(i) +
22  ") into " + pretty_type::short_name<T>() +
23  " of size " + std::to_string(t.size())};
24  }
25 }
26 /*!
27  * \ingroup PythonBindingsGroup
28  * \brief Check if a matrix-like object access is in bounds. Throws
29  * std::runtime_error if it is not.
30  */
31 template <typename T>
32 void matrix_bounds_check(const T& matrix, const size_t row,
33  const size_t column) {
34  if (row >= matrix.rows() or column >= matrix.columns()) {
35  throw std::runtime_error{"Out of bounds access (" + std::to_string(row) +
36  ", " + std::to_string(column) +
37  ") into Matrix of size (" +
38  std::to_string(matrix.rows()) + ", " +
39  std::to_string(matrix.columns()) + ")"};
40  }
41 }
42 } // namespace py_bindings
Definition: Bindings.cpp:6
void bounds_check(const T &t, const size_t i)
Check if a vector-like object access is in bounds. Throws std::runtime_error if it is not...
Definition: BoundChecks.hpp:19
Contains a pretty_type library to write types in a "pretty" format.
void matrix_bounds_check(const T &matrix, const size_t row, const size_t column)
Check if a matrix-like object access is in bounds. Throws std::runtime_error if it is not...
Definition: BoundChecks.hpp:32