Sen API
Sen Libraries
Toggle main menu visibility
Loading...
Searching...
No Matches
integer_compare.h
Go to the documentation of this file.
1
// === integer_compare.h ===============================================================================================
2
// Sen Infrastructure
3
// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4
// See the LICENSE.txt file for more information.
5
// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6
// =====================================================================================================================
7
8
#ifndef SEN_CORE_BASE_INTEGER_COMPARE_H
9
#define SEN_CORE_BASE_INTEGER_COMPARE_H
10
11
#if defined(__cpp_lib_integer_comparison_functions)
12
# include <utility>
13
#endif
14
15
// std
16
#include <type_traits>
17
18
#if defined(__cpp_lib_integer_comparison_functions)
19
namespace
sen::std_util
20
{
21
using
std::cmp_equal;
22
using
std::cmp_greater;
23
using
std::cmp_greater_equal;
24
using
std::cmp_less;
25
using
std::cmp_less_equal;
26
using
std::cmp_not_equal;
27
}
// namespace sen::std_util
28
#else
29
namespace
sen::std_util
30
{
31
32
// Forward implementation of std::cmp_* from C++20.
33
// Impl provided by: https://en.cppreference.com/w/cpp/utility/intcmp.html
34
template
<
class
T,
class
U>
35
// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
36
constexpr
bool
cmp_equal
(T t, U u)
noexcept
37
{
38
if
constexpr
(std::is_signed_v<T> == std::is_signed_v<U>)
39
{
40
return
t == u;
41
}
42
else
if
constexpr
(std::is_signed_v<T>)
43
{
44
// NOLINTNEXTLINE(google-readability-casting): not a c-style cast
45
return
t >= 0 && std::make_unsigned_t<T>(t) == u;
46
}
47
else
48
{
49
// NOLINTNEXTLINE(google-readability-casting): not a c-style cast
50
return
u >= 0 && std::make_unsigned_t<U>(u) == t;
51
}
52
}
53
54
template
<
class
T,
class
U>
55
// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
56
constexpr
bool
cmp_not_equal
(T t, U u)
noexcept
57
{
58
return
!
cmp_equal
(t, u);
59
}
60
61
template
<
class
T,
class
U>
62
// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
63
constexpr
bool
cmp_less
(T t, U u)
noexcept
64
{
65
if
constexpr
(std::is_signed_v<T> == std::is_signed_v<U>)
66
{
67
return
t < u;
68
}
69
else
if
constexpr
(std::is_signed_v<T>)
70
{
71
// NOLINTNEXTLINE(google-readability-casting): not a c-style cast
72
return
t < 0 || std::make_unsigned_t<T>(t) < u;
73
}
74
else
75
{
76
// NOLINTNEXTLINE(google-readability-casting): not a c-style cast
77
return
u >= 0 && t < std::make_unsigned_t<U>(u);
78
}
79
}
80
81
template
<
class
T,
class
U>
82
// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
83
constexpr
bool
cmp_greater
(T t, U u)
noexcept
84
{
85
return
cmp_less
(u, t);
86
}
87
88
template
<
class
T,
class
U>
89
// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
90
constexpr
bool
cmp_less_equal
(T t, U u)
noexcept
91
{
92
return
!
cmp_less
(u, t);
93
}
94
95
template
<
class
T,
class
U>
96
// NOLINTNEXTLINE(readability-identifier-naming): name defined by std
97
constexpr
bool
cmp_greater_equal
(T t, U u)
noexcept
98
{
99
return
!
cmp_less
(t, u);
100
}
101
102
}
// namespace sen::std_util
103
#endif
104
105
#endif
// SEN_CORE_BASE_INTEGER_COMPARE_H
sen::std_util
Definition
bits.h:26
sen::std_util::cmp_greater
constexpr bool cmp_greater(T t, U u) noexcept
Definition
integer_compare.h:83
sen::std_util::cmp_equal
constexpr bool cmp_equal(T t, U u) noexcept
Definition
integer_compare.h:36
sen::std_util::cmp_not_equal
constexpr bool cmp_not_equal(T t, U u) noexcept
Definition
integer_compare.h:56
sen::std_util::cmp_less_equal
constexpr bool cmp_less_equal(T t, U u) noexcept
Definition
integer_compare.h:90
sen::std_util::cmp_less
constexpr bool cmp_less(T t, U u) noexcept
Definition
integer_compare.h:63
sen::std_util::cmp_greater_equal
constexpr bool cmp_greater_equal(T t, U u) noexcept
Definition
integer_compare.h:97
libs
core
include
sen
core
base
integer_compare.h
Generated by
1.17.0