game_launcher
1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
safe_enum_cast.h
Go to the documentation of this file.
1
// SPDX-License-Identifier: MIT
2
// ==============================================================================
3
// safe_enum_cast.h
4
// ==============================================================================
5
// Bounds-checked enum cast.
6
//
7
// Storage that returns plain `int` values (SQLite columns, JSON numbers,
8
// settings files, IPC blobs, …) cannot guarantee that the integer is a
9
// valid representation of the target enum. A future schema with new enum
10
// values, a corrupted DB row, or a deliberate downgrade will produce
11
// out-of-range integers; using `static_cast<E>(v)` on them is undefined
12
// behaviour by [basic.cast]/4 and surfaces as broken switch landing pads
13
// or implausible runtime values.
14
//
15
// Use `core::safeEnumCast<E>(v, default, kMax)` instead. If `v` is outside
16
// the closed range `[0, kMax]`, the call returns `default` and the caller
17
// can log / recover gracefully.
18
//
19
// References:
20
// https://wiki.sei.cmu.edu/confluence/display/cplusplus/INT50-CPP.+Do+not+cast+to+an+out-of-range+enumeration+value
21
// C++17 [expr.static.cast]/10 — narrowing-to-enum is well-defined only
22
// when the value is representable as the target enum's underlying type.
23
// ==============================================================================
24
25
#pragma once
26
27
#include <type_traits>
28
29
namespace
core
30
{
31
32
/// Bounds-checked cast from int to enum.
33
///
34
/// @tparam E Target enum type (scoped or unscoped).
35
/// @param v Source integer (typically from SQLite/JSON/IPC).
36
/// @param def Default value to return when `v` is out of range.
37
/// @param kMax Maximum valid integer value for `E` (inclusive).
38
/// Non-negative integers in `[0, kMax]` are accepted.
39
/// @return Either `static_cast<E>(v)` or `def`.
40
///
41
/// Most enums in this codebase start at 0 and run consecutively. If you
42
/// have a sparse / non-zero-based enum, this helper isn't appropriate —
43
/// fall back to a hand-written switch.
44
template
<
typename
E>
constexpr
E
safeEnumCast
(
int
v, E def,
int
kMax)
noexcept
45
{
46
static_assert
(std::is_enum_v<E>,
"safeEnumCast target must be an enum type"
);
47
// Guard against the caller-typo `kMax = -1`. With a negative kMax the
48
// condition `v <= kMax` is unsatisfiable (since v >= 0 is also
49
// required), so every input would silently return `def` and the column
50
// would always read as the default — masking the bug. Returning `def`
51
// here too is the same observable behaviour, but the if-branch makes
52
// it intentional rather than incidental, and a future refactor that
53
// tightens the contract (e.g. throwing in DEBUG) has a clear hook.
54
if
(kMax < 0)
55
return
def;
56
return
(v >= 0 && v <= kMax) ?
static_cast<
E
>
(v) : def;
57
}
58
59
}
// namespace core
core
Definition
app_controller.h:15
core::safeEnumCast
constexpr E safeEnumCast(int v, E def, int kMax) noexcept
Bounds-checked cast from int to enum.
Definition
safe_enum_cast.h:44
core
include
util
safe_enum_cast.h
Generated by
1.16.1