Skip to content

Commit 113c947

Browse files
committed
DPL: move byteswapping helpers to Endian.h
1 parent 0a2bcda commit 113c947

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

Framework/Core/src/TableTreeHelpers.cxx

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -128,54 +128,6 @@ BranchToColumn::BranchToColumn(TBranch* branch, bool VLA, std::string name, EDat
128128
}
129129
}
130130

131-
template <typename T>
132-
inline T doSwap(T)
133-
{
134-
static_assert(always_static_assert_v<T>, "Unsupported type");
135-
}
136-
137-
template <>
138-
inline uint16_t doSwap(uint16_t x)
139-
{
140-
return swap16_(x);
141-
}
142-
143-
template <>
144-
inline uint32_t doSwap(uint32_t x)
145-
{
146-
return swap32_(x);
147-
}
148-
149-
template <>
150-
inline uint64_t doSwap(uint64_t x)
151-
{
152-
return swap64_(x);
153-
}
154-
155-
template <typename T>
156-
void doSwapCopy_(void* dest, void* source, int size) noexcept
157-
{
158-
auto tdest = static_cast<T*>(dest);
159-
auto tsrc = static_cast<T*>(source);
160-
for (auto i = 0; i < size; ++i) {
161-
tdest[i] = doSwap<T>(tsrc[i]);
162-
}
163-
}
164-
165-
void swapCopy(unsigned char* dest, char* source, int size, int typeSize) noexcept
166-
{
167-
switch (typeSize) {
168-
case 1:
169-
return (void)std::memcpy(dest, source, size);
170-
case 2:
171-
return doSwapCopy_<uint16_t>(dest, source, size);
172-
case 4:
173-
return doSwapCopy_<uint32_t>(dest, source, size);
174-
case 8:
175-
return doSwapCopy_<uint64_t>(dest, source, size);
176-
}
177-
}
178-
179131
std::pair<std::shared_ptr<arrow::ChunkedArray>, std::shared_ptr<arrow::Field>> BranchToColumn::read(TBuffer* buffer)
180132
{
181133
auto totalEntries = mBranch->GetEntries();

Framework/Foundation/include/Framework/Endian.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#ifndef O2_FRAMEWORK_ENDIAN_H_
1313
#define O2_FRAMEWORK_ENDIAN_H_
1414

15+
#include <cstdint>
16+
#include <concepts>
17+
#include <cstring>
1518
// Lookup file for __BYTE_ORDER
1619
#ifdef __APPLE__
1720
#include <machine/endian.h>
@@ -29,4 +32,50 @@
2932
#define O2_HOST_BYTE_ORDER __BYTE_ORDER
3033
#define O2_BIG_ENDIAN __BIG_ENDIAN
3134
#define O2_LITTLE_ENDIAN __LITTLE_ENDIAN
35+
36+
37+
template <typename T>
38+
requires std::same_as<T, uint16_t>
39+
inline uint16_t doSwap(uint16_t x)
40+
{
41+
return swap16_(x);
42+
}
43+
44+
template <typename T>
45+
requires std::same_as<T, uint32_t>
46+
inline uint32_t doSwap(uint32_t x)
47+
{
48+
return swap32_(x);
49+
}
50+
51+
template <typename T>
52+
requires std::same_as<T, uint64_t>
53+
inline uint64_t doSwap(uint64_t x)
54+
{
55+
return swap64_(x);
56+
}
57+
58+
template <typename T>
59+
inline void doSwapCopy_(void* dest, void* source, int size) noexcept
60+
{
61+
auto tdest = static_cast<T*>(dest);
62+
auto tsrc = static_cast<T*>(source);
63+
for (auto i = 0; i < size; ++i) {
64+
tdest[i] = doSwap<T>(tsrc[i]);
65+
}
66+
}
67+
68+
inline void swapCopy(unsigned char* dest, char* source, int size, int typeSize) noexcept
69+
{
70+
switch (typeSize) {
71+
case 1:
72+
return (void)std::memcpy(dest, source, size);
73+
case 2:
74+
return doSwapCopy_<uint16_t>(dest, source, size);
75+
case 4:
76+
return doSwapCopy_<uint32_t>(dest, source, size);
77+
case 8:
78+
return doSwapCopy_<uint64_t>(dest, source, size);
79+
}
80+
}
3281
#endif // O2_FRAMEWORK_ENDIAN_H_

0 commit comments

Comments
 (0)