CbmRoot
Loading...
Searching...
No Matches
CbmYamlBaseTypes.h
Go to the documentation of this file.
1/* Copyright (C) 2023-2025 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Felix Weiglhofer [committer] */
4#ifndef CBM_YAML_BASETYPES_H
5#define CBM_YAML_BASETYPES_H
6#pragma once
7
8#include <array>
9#include <cstdint>
10#include <map>
11#include <set>
12#include <string>
13#include <tuple>
14#include <unordered_map>
15#include <unordered_set>
16#include <vector>
17
18// FIXME: move some of the variables to CbmTypeTraits.h
19
20namespace cbm::util::yaml
21{
22 // typealias for Rust-like fixed size integer types
23 using i8 = int8_t;
24 using u8 = uint8_t;
25 using i16 = int16_t;
26 using u16 = uint16_t;
27 using i32 = int32_t;
28 using u32 = uint32_t;
29 using i64 = int64_t;
30 using u64 = uint64_t;
31 using f32 = float;
32 using f64 = double;
33
34 // clang-format off
35 using FundamentalTypes = std::tuple< bool
36 , u8, i8
37 , u16, i16
38 , u32, i32
39 , u64, i64
40 , f32, f64
41 , std::string
42// Clang on macOS somehow treats unsigned long as a different type than uint64_t
43#ifdef __APPLE__
44 , unsigned long, long
45#endif
46 >;
47 // clang-format on
48
49 template<typename T, typename Tuple>
50 struct has_type;
51
52 template<typename T, typename... Us>
53 struct has_type<T, std::tuple<Us...>> : std::disjunction<std::is_same<T, Us>...> {
54 };
55
56 template<typename T>
58
59 template<typename T>
60 constexpr bool IsEnum = std::is_enum_v<T>;
61
62 template<typename T>
63 constexpr bool IsScalar = IsFundamental<T> || IsEnum<T>;
64
65 template<typename T, typename = std::enable_if_t<IsFundamental<T>>>
66 constexpr std::string_view Typename()
67 {
68 if constexpr (std::is_same_v<bool, T>) {
69 return "bool";
70 }
71 else if constexpr (std::is_same_v<u8, T>) {
72 return "u8";
73 }
74 else if constexpr (std::is_same_v<i8, T>) {
75 return "i8";
76 }
77 else if constexpr (std::is_same_v<u16, T>) {
78 return "u16";
79 }
80 else if constexpr (std::is_same_v<i16, T>) {
81 return "i16";
82 }
83 else if constexpr (std::is_same_v<u32, T>) {
84 return "u32";
85 }
86 else if constexpr (std::is_same_v<i32, T>) {
87 return "i32";
88 }
89 else if constexpr (std::is_same_v<f32, T>) {
90 return "f32";
91 }
92 else if constexpr (std::is_same_v<f64, T>) {
93 return "f64";
94 }
95 else if constexpr (std::is_same_v<std::string, T>) {
96 return "string";
97 }
98 else {
99 return "unknown";
100 }
101 }
102
103 template<typename>
104 struct is_std_vector : std::false_type {
105 };
106
107 template<typename T, typename A>
108 struct is_std_vector<std::vector<T, A>> : std::true_type {
109 };
110
111 template<typename T>
113
114 template<typename>
115 struct is_std_array : std::false_type {
116 };
117
118 template<typename T, std::size_t N>
119 struct is_std_array<std::array<T, N>> : std::true_type {
120 };
121
122 template<typename T>
124
125 template<typename>
126 struct is_std_map : std::false_type {
127 };
128
129 template<typename K, typename V, typename C, typename A>
130 struct is_std_map<std::map<K, V, C, A>> : std::true_type {
131 };
132
133 template<typename>
134 struct is_std_unordered_map : std::false_type {
135 };
136
137 template<typename K, typename V, typename H, typename E, typename A>
138 struct is_std_unordered_map<std::unordered_map<K, V, H, E, A>> : std::true_type {
139 };
140
141 template<typename T>
143
144 template<typename>
145 struct is_std_set : std::false_type {
146 };
147
148 template<typename K, typename C, typename A>
149 struct is_std_set<std::set<K, C, A>> : std::true_type {
150 };
151
152 template<typename>
153 struct is_std_unordered_set : std::false_type {
154 };
155
156 template<typename K, typename H, typename E, typename A>
157 struct is_std_unordered_set<std::unordered_set<K, H, E, A>> : std::true_type {
158 };
159
160 template<typename T>
162
163 template<typename>
164 struct is_optional : std::false_type {
165 };
166
167 template<typename T>
168 struct is_optional<std::optional<T>> : std::true_type {
169 };
170
171 template<typename T>
173
174} // namespace cbm::util::yaml
175#endif // CBM_YAML_BASETYPES_H
constexpr bool IsArray
uint64_t u64
Definition CbmYaml.h:32
constexpr bool IsEnum
int64_t i64
Definition CbmYaml.h:31
constexpr bool IsFundamental
constexpr bool IsMap
uint16_t u16
Definition CbmYaml.h:28
constexpr bool IsSet
std::tuple< bool, u8, i8, u16, i16, u32, i32, u64, i64, f32, f64, std::string > FundamentalTypes
constexpr bool IsOptional
int32_t i32
Definition CbmYaml.h:29
int16_t i16
Definition CbmYaml.h:27
constexpr std::string_view Typename()
constexpr bool IsScalar
constexpr bool IsVector
uint32_t u32
Definition CbmYaml.h:30
uint8_t u8
Definition CbmYaml.h:26
Hash for CbmL1LinkKey.