-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlimit.hh
77 lines (60 loc) · 1.5 KB
/
limit.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Limit
#pragma once
#include "snn-core/time/unit.hh"
namespace snn::time
{
// ## Classes
// ### limit
template <any_unit Unit, i64 Min, i64 Max>
requires(Min >= 0 && Min <= Max)
class limit final
{
public:
// #### Converting constructors
constexpr limit(const Unit u) noexcept
: value_{u.value_or(0)}
{
if (value_ < Min)
{
value_ = Min;
}
else if (value_ > Max)
{
value_ = Max;
}
}
template <i64 N, i64 D>
constexpr limit(const unit<N, D> u) noexcept
: limit{u.template to<Unit>()}
{
}
template <typename U, i64 Mn, i64 Mx>
constexpr limit(const limit<U, Mn, Mx> other) noexcept
: limit{other.unit()}
{
}
// #### Get
[[nodiscard]] constexpr i64 get() const noexcept
{
return value_;
}
// #### Min/Max
[[nodiscard]] static constexpr i64 max() noexcept
{
return Max;
}
[[nodiscard]] static constexpr i64 min() noexcept
{
return Min;
}
// #### Conversion
[[nodiscard]] constexpr Unit unit() const noexcept
{
return Unit{value_};
}
private:
i64 value_;
};
}