Stratax 0.2.0
Loading...
Searching...
No Matches
ShapeValidation.hpp
1#pragma once
2
3#include <cstddef>
4
5#include <stratax/core/Exceptions.hpp>
6
7namespace stratax::core::validation {
8
19inline std::size_t nonnegative_shape_dimension(std::ptrdiff_t value, const char* message)
20{
21 if (value < 0)
22 {
23 throw Exceptions::ShapeError(message);
24 }
25
26 return static_cast<std::size_t>(value);
27}
28
39inline std::size_t positive_shape_dimension(std::ptrdiff_t value, const char* message)
40{
41 if (value <= 0)
42 {
43 throw Exceptions::ShapeError(message);
44 }
45
46 return static_cast<std::size_t>(value);
47}
48
57inline void require_positive_shape_dimension(std::size_t value, const char* message)
58{
59 if (value == 0)
60 {
61 throw Exceptions::ShapeError(message);
62 }
63}
64
73template<typename Lhs, typename Rhs>
74[[nodiscard]] bool same_shape(const Lhs& lhs, const Rhs& rhs)
75{
76 return lhs.size() == rhs.size() && lhs.shape() == rhs.shape();
77}
78
88template<typename Lhs, typename Rhs>
89void require_same_shape(const Lhs& lhs, const Rhs& rhs, const char* message)
90{
91 if (!same_shape(lhs, rhs))
92 {
93 throw Exceptions::ShapeError(message);
94 }
95}
96
106inline void require_equal_size(std::size_t lhs, std::size_t rhs, const char* message)
107{
108 if (lhs != rhs)
109 {
110 throw Exceptions::ShapeError(message);
111 }
112}
113
114}