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-2023 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
27
28namespace CxxUtils {
29namespace detail {
30
31
35template <class T>
37{
38 // Unsigned type of the same size as T.
39 using uint_t = typename SizedUInt<sizeof(T)>::type;
40
41 // Union members to convert between uint_t and T.
43 T x;
44
45
49 static uintptr_t valToUInt (T x) {
50 UIntConv u;
51 u.x = x;
52 return static_cast<uintptr_t> (u.ui);
53 };
54
55
59 static T uintToVal (uintptr_t ui) {
60 UIntConv u;
61 u.ui = static_cast<uint_t>(ui);
62 return u.x;
63 };
64};
65
66
67// Specialization for the case where no conversion is required.
68template <>
69union UIntConv<uintptr_t>
70{
74 static uintptr_t valToUInt (uintptr_t x) {
75 return x;
76 };
77
78
82 static uintptr_t uintToVal (uintptr_t ui) {
83 return ui;
84 };
85};
86
87
88} // namespace detail
89} // namespace CxxUtils
90
91
92#endif // not CXXUTILS_UINTCONV_H
Generate an unsigned integer type of a specified size.
static uintptr_t uintToVal(uintptr_t ui)
Convert a uintptr_t to a T.
Definition UIntConv.h:82
static uintptr_t valToUInt(uintptr_t x)
Convert a T to a uintptr_t.
Definition UIntConv.h:74
Helpers for converting between uintptr_t and a pointer or integer.
Definition UIntConv.h:37
static uintptr_t valToUInt(T x)
Convert a T to a uintptr_t.
Definition UIntConv.h:49
typename SizedUInt< sizeof(T)>::type uint_t
Definition UIntConv.h:39
static T uintToVal(uintptr_t ui)
Convert a uintptr_t to a T.
Definition UIntConv.h:59