Stratax
0.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
IndexValidation.hpp
1
#pragma once
2
3
#include <cstddef>
4
#include <limits>
5
6
#include <stratax/core/Exceptions.hpp>
7
8
namespace
stratax::core::validation {
9
20
inline
std::size_t nonnegative_index(std::ptrdiff_t value,
const
char
* message)
21
{
22
if
(value < 0)
23
{
24
throw
Exceptions::IndexError(message);
25
}
26
27
return
static_cast<
std::size_t
>
(value);
28
}
29
43
inline
std::size_t normalize_index(std::ptrdiff_t value, std::size_t size,
const
char
* message)
44
{
45
if
(size >
static_cast<
std::size_t
>
(std::numeric_limits<std::ptrdiff_t>::max()))
46
{
47
throw
Exceptions::IndexError(message);
48
}
49
50
const
std::ptrdiff_t limit =
static_cast<
std::ptrdiff_t
>
(size);
51
std::ptrdiff_t normalized = value;
52
53
if
(normalized < 0)
54
{
55
normalized += limit;
56
}
57
58
if
(normalized < 0 || normalized >= limit)
59
{
60
throw
Exceptions::IndexError(message);
61
}
62
63
return
static_cast<
std::size_t
>
(normalized);
64
}
65
75
inline
void
require_index(std::size_t index, std::size_t size,
const
char
* message)
76
{
77
if
(index >= size)
78
{
79
throw
Exceptions::IndexError(message);
80
}
81
}
82
94
inline
void
require_at_most(std::size_t value, std::size_t upper,
const
char
* message)
95
{
96
if
(value > upper)
97
{
98
throw
Exceptions::IndexError(message);
99
}
100
}
101
102
}
include
stratax
core
validation
IndexValidation.hpp
Generated by
1.17.0