Project
Loading...
Searching...
No Matches
BitPtr.h
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef RANS_INTERNAL_CONTAINERS_BITPTR_H_
17#define RANS_INTERNAL_CONTAINERS_BITPTR_H_
18
19#include <cassert>
20#include <cstdint>
21#include <cstring>
22
23#include <fmt/format.h>
24
26
27namespace o2::rans
28{
29
30class BitPtr
31{
32 public:
34
35 inline constexpr BitPtr() noexcept {};
36
37 inline constexpr BitPtr(intptr_t bitAdr) noexcept : mBitAdr{bitAdr} {};
38
39 template <typename T>
40 inline constexpr BitPtr(const T* ptr, intptr_t offset = 0) noexcept : mBitAdr{static_cast<intptr_t>(internal::adr2Bits(ptr)) + offset}
41 {
42 assert(reinterpret_cast<intptr_t>(ptr) % sizeof(T) == 0); // ensure pointer alignment
43 };
44
45 inline constexpr const bitAddress_type& getBitAddress() const noexcept { return mBitAdr; };
46
47 inline constexpr bitAddress_type& getBitAddress() noexcept { return const_cast<bitAddress_type&>(const_cast<const BitPtr&>(*this).getBitAddress()); };
48
49 template <typename T>
50 inline constexpr intptr_t getOffset() const noexcept
51 {
52 assert(mBitAdr >= 0);
53 // bit offset from next T
54 return mBitAdr % utils::toBits<T>();
55 };
56
57 template <typename T>
58 inline constexpr T* toPtr() const noexcept
59 {
60 assert(mBitAdr >= 0);
61 // convert from bits to bytes, cutting off offset
62 intptr_t byteAdress = getBitAddress();
63 if constexpr (sizeof(T) > 1) {
64 byteAdress -= getOffset<T>();
65 }
66 return reinterpret_cast<T*>(byteAdress >> ToBytesShift);
67 };
68
69 inline constexpr explicit operator intptr_t() const noexcept
70 {
71 return getBitAddress();
72 };
73
74 template <typename T>
75 inline constexpr explicit operator T*() const noexcept
76 {
77 return toPtr<T>();
78 };
79
80 // pointer arithmetics
81 inline constexpr BitPtr& operator++() noexcept
82 {
83 ++mBitAdr;
84 return *this;
85 };
86
87 inline constexpr BitPtr operator++(int) noexcept
88 {
89 auto res = *this;
90 ++(*this);
91 return res;
92 };
93
94 inline constexpr BitPtr& operator--() noexcept
95 {
96 --mBitAdr;
97 return *this;
98 };
99
100 inline constexpr BitPtr operator--(int) noexcept
101 {
102 auto res = *this;
103 --(*this);
104 return res;
105 };
106
107 inline constexpr BitPtr& operator+=(intptr_t bitOffset) noexcept
108 {
109 mBitAdr += bitOffset;
110 return *this;
111 };
112
113 inline constexpr BitPtr operator+(intptr_t bitOffset) const noexcept
114 {
115 auto tmp = *const_cast<BitPtr*>(this);
116 return tmp += bitOffset;
117 }
118
119 inline constexpr BitPtr& operator-=(intptr_t bitOffset) noexcept
120 {
121 mBitAdr -= bitOffset;
122 return *this;
123 };
124
125 inline constexpr BitPtr operator-(intptr_t bitOffset) const noexcept
126 {
127 auto tmp = *const_cast<BitPtr*>(this);
128 return tmp -= bitOffset;
129 };
130
131 inline constexpr intptr_t operator-(const BitPtr& other) const noexcept
132 {
133 return this->mBitAdr - other.mBitAdr;
134 };
135
136 inline friend BitPtr operator+(intptr_t bitOffset, const BitPtr& bitPtr)
137 {
138 return bitPtr + bitOffset;
139 }
140
141 // comparison
142 inline constexpr bool operator==(const BitPtr& other) const noexcept { return this->mBitAdr == other.mBitAdr; };
143 inline constexpr bool operator!=(const BitPtr& other) const noexcept { return !(*this == other); };
144 inline constexpr bool operator<(const BitPtr& other) const noexcept { return this->mBitAdr < other.mBitAdr; };
145 inline constexpr bool operator>(const BitPtr& other) const noexcept { return other < *this; };
146 inline constexpr bool operator>=(const BitPtr& other) const noexcept { return !(*this < other); };
147 inline constexpr bool operator<=(const BitPtr& other) const noexcept { return !(other < *this); };
148
149 inline friend void swap(BitPtr& first, BitPtr& second)
150 {
151 using std::swap;
152 swap(first.mBitAdr, second.mBitAdr);
153 }
154
155 private:
156 intptr_t mBitAdr{};
157 inline static constexpr intptr_t ToBytesShift = 3;
158};
159
160inline std::ostream& operator<<(std::ostream& os, const BitPtr bitPtr)
161{
162 return os << fmt::format("{:0x}", bitPtr.getBitAddress());
163}
164
165} // namespace o2::rans
166
167#endif /* RANS_INTERNAL_CONTAINERS_BITPTR_H_ */
uint32_t res
Definition RawData.h:0
TBranch * ptr
common helper classes and functions
constexpr BitPtr() noexcept
Definition BitPtr.h:35
constexpr intptr_t getOffset() const noexcept
Definition BitPtr.h:50
constexpr BitPtr(intptr_t bitAdr) noexcept
Definition BitPtr.h:37
constexpr BitPtr operator-(intptr_t bitOffset) const noexcept
Definition BitPtr.h:125
friend BitPtr operator+(intptr_t bitOffset, const BitPtr &bitPtr)
Definition BitPtr.h:136
constexpr const bitAddress_type & getBitAddress() const noexcept
Definition BitPtr.h:45
constexpr BitPtr & operator--() noexcept
Definition BitPtr.h:94
constexpr BitPtr operator+(intptr_t bitOffset) const noexcept
Definition BitPtr.h:113
constexpr BitPtr & operator+=(intptr_t bitOffset) noexcept
Definition BitPtr.h:107
constexpr BitPtr operator--(int) noexcept
Definition BitPtr.h:100
constexpr bool operator>(const BitPtr &other) const noexcept
Definition BitPtr.h:145
constexpr bitAddress_type & getBitAddress() noexcept
Definition BitPtr.h:47
constexpr T * toPtr() const noexcept
Definition BitPtr.h:58
constexpr BitPtr(const T *ptr, intptr_t offset=0) noexcept
Definition BitPtr.h:40
constexpr bool operator!=(const BitPtr &other) const noexcept
Definition BitPtr.h:143
constexpr bool operator>=(const BitPtr &other) const noexcept
Definition BitPtr.h:146
constexpr BitPtr & operator-=(intptr_t bitOffset) noexcept
Definition BitPtr.h:119
constexpr intptr_t operator-(const BitPtr &other) const noexcept
Definition BitPtr.h:131
constexpr bool operator<(const BitPtr &other) const noexcept
Definition BitPtr.h:144
constexpr bool operator==(const BitPtr &other) const noexcept
Definition BitPtr.h:142
constexpr bool operator<=(const BitPtr &other) const noexcept
Definition BitPtr.h:147
constexpr BitPtr & operator++() noexcept
Definition BitPtr.h:81
friend void swap(BitPtr &first, BitPtr &second)
Definition BitPtr.h:149
constexpr BitPtr operator++(int) noexcept
Definition BitPtr.h:87
GLintptr offset
Definition glcorearb.h:660
constexpr uintptr_t adr2Bits(T *address) noexcept
Definition utils.h:84
std::ostream & operator<<(std::ostream &os, const BitPtr bitPtr)
Definition BitPtr.h:160
VectorOfTObjectPtrs other