ATLAS Offline Software
Loading...
Searching...
No Matches
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
30namespace CxxUtils {
31namespace detail {
32
33
37template <class T>
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.
67template <>
68struct 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
Generate an unsigned integer type of a specified size.
#define x
static uintptr_t uintToVal(uintptr_t ui)
Convert a uintptr_t to a T.
Definition UIntConv.h:81
static uintptr_t valToUInt(uintptr_t x)
Convert a T to a uintptr_t.
Definition UIntConv.h:73
Helpers for converting between uintptr_t and a pointer or integer.
Definition UIntConv.h:39
static uintptr_t valToUInt(T x)
Convert a T to a uintptr_t.
Definition UIntConv.h:50
typename SizedUInt< sizeof(T)>::type uint_t
Definition UIntConv.h:41
static T uintToVal(uintptr_t ui)
Convert a uintptr_t to a T.
Definition UIntConv.h:59