Project
Loading...
Searching...
No Matches
TypeIdHelpers.h
Go to the documentation of this file.
1// Copyright 2019-2020 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
12#ifndef O2_FRAMEWORK_TYPEIDHELPERS_H_
13#define O2_FRAMEWORK_TYPEIDHELPERS_H_
14
15#include <string_view>
16#include <sstream>
17#if __cplusplus >= 202002L
18#include <source_location>
19#endif
21
22namespace o2::framework
23{
24#if defined(__CLING__) || __cplusplus < 202002L
25template <typename T>
27 static constexpr auto get() noexcept
28 {
29 constexpr std::string_view full_name{__PRETTY_FUNCTION__};
30 return full_name;
31 }
32
33 static constexpr std::string_view value{get()};
34};
35
36template <typename T>
38#endif
39
40#if !defined(__CLING__) && __cplusplus >= 202002L
42template <typename T>
43consteval static std::string_view type_name_impl(T*)
44{
45 return std::source_location::current().function_name();
46}
47#endif
48
51template <typename T>
52constexpr static std::string_view type_name()
53{
54#if defined(__CLING__) || __cplusplus < 202002L
55 constexpr std::string_view wrapped_name{unique_type_id_v<T>};
56#else
57 constexpr std::string_view wrapped_name = type_name_impl<T>(nullptr);
58#endif
59 const std::string_view left_marker{"T = "};
60#if !defined(__clang__) && __cplusplus >= 202002L
61 const std::string_view right_marker{";"};
62#else
63 const std::string_view right_marker{"]"};
64#endif
65
66 const auto left_marker_index = wrapped_name.find(left_marker);
67 const auto start_index = left_marker_index + left_marker.size();
68 const auto end_index = wrapped_name.find(right_marker, left_marker_index);
69 const auto length = end_index - start_index;
70 return wrapped_name.substr(start_index, length);
71}
72
74 template <typename T>
75 constexpr static uint32_t uniqueId()
76 {
77#ifdef __CLING__
78 constexpr uint32_t r = crc32(unique_type_id_v<T>.data(), unique_type_id_v<T>.size());
79 return r;
80#else
81 return compile_time_hash(type_name<T>().data());
82#endif
83 }
84};
85
87inline static std::string type_to_task_name(std::string_view& camelCase)
88{
89 std::ostringstream str;
90 str << static_cast<char>(std::tolower(camelCase[0]));
91
92 for (auto it = camelCase.begin() + 1; it != camelCase.end(); ++it) {
93 if (std::isupper(*it) && *(it - 1) != '-') {
94 str << "-";
95 }
96 str << static_cast<char>(std::tolower(*it));
97 }
98
99 return str.str();
100}
101
102} // namespace o2::framework
103
104#endif // O2_FRAMEWORK_TYPEIDHELPERS_H_
consteval uint32_t crc32(char const *str, int length)
consteval uint32_t compile_time_hash(char const *str)
GLsizeiptr size
Definition glcorearb.h:659
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean * data
Definition glcorearb.h:298
GLuint GLsizei GLsizei * length
Definition glcorearb.h:790
GLboolean r
Definition glcorearb.h:1233
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
constexpr auto unique_type_id_v
static constexpr uint32_t uniqueId()
static constexpr auto get() noexcept
const std::string str