styyx-util 1.0
Utility Header for SKSE Plugin development
Loading...
Searching...
No Matches
st-math.h
Go to the documentation of this file.
1//
2// Created by styyx on 12/03/2026.
3//
4
5#pragma once
6
7namespace StyyxUtil
8{
9 struct MathUtil {
10
22 template <typename T>
23 requires std::is_arithmetic_v<T>
24 [[deprecated("Use MathUtil::SafelyAddWithCap instead")]]
25 static T SafelyAdd(T base, T increment)
26 {
27 using ForMath = std::conditional_t<std::is_integral_v<T> && sizeof(T) < sizeof(int), int, T>;
28
29 const auto a = static_cast<ForMath>(base);
30 const auto b = static_cast<ForMath>(increment);
31 const auto max = static_cast<ForMath>(std::numeric_limits<T>::max());
32
33 return static_cast<T>(a + b > max ? max : a + b);
34 }
35
43 template <typename T>
44 requires std::is_arithmetic_v<T>
45 static T SafelyAddWithCap(T base, T increment, T cap = std::numeric_limits<T>::max()) {
46
47 using ForMath = std::conditional_t<std::is_integral_v<T> && sizeof(T) < sizeof(int), int, T>;
48
49 const auto a = static_cast<ForMath>(base);
50 const auto b = static_cast<ForMath>(increment);
51 const auto max = std::min(static_cast<ForMath>(cap), static_cast<ForMath>(std::numeric_limits<T>::max()));
52
53 return static_cast<T>(a + b > max ? max : a + b);
54 }
55};
56}
Definition st-actor.h:7
Definition st-math.h:9
static T SafelyAddWithCap(T base, T increment, T cap=std::numeric_limits< T >::max())
Add a value to another one and cap the result.
Definition st-math.h:45
static T SafelyAdd(T base, T increment)
Template function to add to a value without ever risking overflowing the maximum value that type can ...
Definition st-math.h:25