alpaka
Abstraction Library for Parallel Kernel Acceleration
Loading...
Searching...
No Matches
UniqueId.hpp
Go to the documentation of this file.
1/* Copyright 2024 René Widera
2 * SPDX-License-Identifier: MPL-2.0
3 */
4
5#pragma once
6
7#include <cstdint>
8#include <source_location>
9#include <string_view>
10
11namespace alpaka
12{
14 {
15 public:
16 static constexpr size_t getId(std::source_location const location = std::source_location::current())
17 {
18 return generate(location);
19 }
20
21 private:
22 static constexpr size_t generate(std::source_location const& location)
23 {
24 size_t hash = 0xc6a4'a793'5bd1'e995;
25 hashCombine(hash, location.file_name());
26 hashCombine(hash, location.function_name());
27 hashCombine(hash, location.line());
28 hashCombine(hash, static_cast<size_t>(location.column()) << 32u);
29 return hash;
30 }
31
32 static constexpr void hashCombine(size_t& seed, std::string_view value)
33 {
34 for(char c : value)
35 {
36 seed ^= static_cast<size_t>(c) + 0x9e37'79b9 + (seed << 6) + (seed >> 2);
37 }
38 }
39
40 static constexpr void hashCombine(size_t& seed, size_t value)
41 {
42 seed ^= value + 0x9e37'79b9 + (seed << 6) + (seed >> 2);
43 }
44 };
45
46 /** creates a unique id on any call
47 *
48 * If a class is storing the compile time id and the file of the class is included within two compile units the
49 * id will be equal in both compile units.
50 * The id is derived from the file name, function name, line, and column from where this method is called.
51 * If this call is used to default set a template parameter of a class it will only generate once a unique number
52 * not each time the class will be used.
53 *
54 * @param location The location is the base for the unique id. For the same location the same id is generated.
55 * @return unique id
56 */
57 inline consteval size_t uniqueId(std::source_location const location = std::source_location::current())
58 {
59 return UniqueId::getId(location);
60 }
61} // namespace alpaka
static constexpr size_t getId(std::source_location const location=std::source_location::current())
Definition UniqueId.hpp:16
main alpaka namespace.
Definition alpaka.hpp:76
consteval size_t uniqueId(std::source_location const location=std::source_location::current())
creates a unique id on any call
Definition UniqueId.hpp:57