Skip to content

Commit 2958190

Browse files
committed
GPU: add constexpr version of qStr2Tag
Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent ab5e32c commit 2958190

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

GPU/GPUTracking/utils/strtag.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,29 @@
1515
#ifndef STRTAG_H
1616
#define STRTAG_H
1717

18+
#include <string_view>
19+
#include <cstring>
1820
#include <stdexcept>
21+
#include <cstdint>
1922
#include <string>
23+
#include <type_traits>
24+
25+
template <class T = uint64_t, std::size_t N>
26+
constexpr T qStr2Tag(const char (&str)[N])
27+
{
28+
static_assert(std::is_trivially_copyable_v<T>);
29+
static_assert(N - 1 == sizeof(T), "Invalid tag length");
30+
T value{};
31+
for (std::size_t i = 0; i < sizeof(T); ++i) {
32+
value |= T(static_cast<unsigned char>(str[i])) << (i * 8);
33+
}
34+
return value;
35+
}
2036

2137
template <class T = uint64_t>
22-
constexpr T qStr2Tag(const char* str)
38+
constexpr T qStr2Tag(std::string_view str)
2339
{
24-
if (strlen(str) != sizeof(T)) {
40+
if (str.size() != sizeof(T)) {
2541
throw std::runtime_error("Invalid tag length");
2642
}
2743
T tmp;

0 commit comments

Comments
 (0)