ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
CxxUtils
CxxUtils
UIntConv.h
Go to the documentation of this file.
1
// This file's extension implies that it's C, but it's really -*- C++ -*-.
2
/*
3
* Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
4
*/
18
19
20
#ifndef CXXUTILS_UINTCONV_H
21
#define CXXUTILS_UINTCONV_H
22
23
24
#include "
CxxUtils/SizedUInt.h
"
25
#include <cstdint>
26
#include <bit>
27
#include <type_traits>
28
29
30
namespace
CxxUtils
{
31
namespace
detail
{
32
33
37
template
<
class
T>
38
struct
UIntConv
39
{
40
// Unsigned type of the same size as T.
41
using
uint_t
=
typename
SizedUInt
<
sizeof
(T)>
::type
;
42
//
43
//sensible convertible types?
44
static_assert
(std::is_trivially_copyable_v<T>);
45
static_assert
(
sizeof
(
uint_t
) ==
sizeof
(T));
46
static_assert
(
sizeof
(T) <=
sizeof
(uintptr_t));
50
static
uintptr_t
valToUInt
(T
x
) {
51
const
uint_t
ui = std::bit_cast<uint_t>(
x
);
52
return
static_cast<
uintptr_t
>
(ui);
53
};
54
55
59
static
T
uintToVal
(uintptr_t ui) {
60
const
uint_t
narrowed =
static_cast<
uint_t
>
(ui);
61
return
std::bit_cast<T>(narrowed);
62
};
63
};
64
65
66
// Specialization for the case where no conversion is required.
67
template
<>
68
struct
UIntConv
<uintptr_t>
69
{
73
static
uintptr_t
valToUInt
(uintptr_t
x
) {
74
return
x
;
75
};
76
77
81
static
uintptr_t
uintToVal
(uintptr_t ui) {
82
return
ui;
83
};
84
};
85
86
87
}
// namespace detail
88
}
// namespace CxxUtils
89
90
91
#endif
// not CXXUTILS_UINTCONV_H
SizedUInt.h
Generate an unsigned integer type of a specified size.
x
#define x
CxxUtils::detail
Definition
concepts.h:25
CxxUtils
Definition
aligned_vector.h:29
CxxUtils::detail::SizedUInt
Definition
SizedUInt.h:28
CxxUtils::detail::UIntConv< uintptr_t >::uintToVal
static uintptr_t uintToVal(uintptr_t ui)
Convert a uintptr_t to a T.
Definition
UIntConv.h:81
CxxUtils::detail::UIntConv< uintptr_t >::valToUInt
static uintptr_t valToUInt(uintptr_t x)
Convert a T to a uintptr_t.
Definition
UIntConv.h:73
CxxUtils::detail::UIntConv
Helpers for converting between uintptr_t and a pointer or integer.
Definition
UIntConv.h:39
CxxUtils::detail::UIntConv::valToUInt
static uintptr_t valToUInt(T x)
Convert a T to a uintptr_t.
Definition
UIntConv.h:50
CxxUtils::detail::UIntConv::uint_t
typename SizedUInt< sizeof(T)>::type uint_t
Definition
UIntConv.h:41
CxxUtils::detail::UIntConv::uintToVal
static T uintToVal(uintptr_t ui)
Convert a uintptr_t to a T.
Definition
UIntConv.h:59
type
Generated on
for ATLAS Offline Software by
1.17.0