Lagrange
Loading...
Searching...
No Matches
Scalar.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <cstdint>
15
16namespace lagrange {
17namespace experimental {
18
19enum class ScalarEnum : uint8_t {
20 INT8 = 0,
21 INT16 = 1,
22 INT32 = 2,
23 INT64 = 3,
24 UINT8 = 4,
25 UINT16 = 5,
26 UINT32 = 6,
27 UINT64 = 7,
28 FLOAT = 8,
29 DOUBLE = 9,
30};
31
32template <typename T, typename T2 = void>
34{
35};
36
37template <>
38struct ScalarToEnum<int8_t, void>
39{
40 static constexpr ScalarEnum value = ScalarEnum::INT8;
41 static constexpr const char* name = "int8_t";
42};
43
44template <>
45struct ScalarToEnum<int16_t, void>
46{
47 static constexpr ScalarEnum value = ScalarEnum::INT16;
48 static constexpr const char* name = "int16_t";
49};
50
51template <>
52struct ScalarToEnum<int32_t, void>
53{
54 static constexpr ScalarEnum value = ScalarEnum::INT32;
55 static constexpr const char* name = "int32_t";
56};
57
58template <>
59struct ScalarToEnum<int64_t, void>
60{
61 static constexpr ScalarEnum value = ScalarEnum::INT64;
62 static constexpr const char* name = "int64_t";
63};
64
65template <>
66struct ScalarToEnum<uint8_t, void>
67{
68 static constexpr ScalarEnum value = ScalarEnum::UINT8;
69 static constexpr const char* name = "uint8_t";
70};
71
72template <>
73struct ScalarToEnum<uint16_t, void>
74{
75 static constexpr ScalarEnum value = ScalarEnum::UINT16;
76 static constexpr const char* name = "uint16_t";
77};
78
79template <>
80struct ScalarToEnum<uint32_t, void>
81{
82 static constexpr ScalarEnum value = ScalarEnum::UINT32;
83 static constexpr const char* name = "uint32_t";
84};
85
86template <>
87struct ScalarToEnum<uint64_t, void>
88{
89 static constexpr ScalarEnum value = ScalarEnum::UINT64;
90 static constexpr const char* name = "uint64_t";
91};
92
93template <>
94struct ScalarToEnum<float, void>
95{
96 static_assert(sizeof(float) == 4, "sizeof(float) == 4");
97 static constexpr ScalarEnum value = ScalarEnum::FLOAT;
98 static constexpr const char* name = "float";
99};
100
101template <>
102struct ScalarToEnum<double, void>
103{
104 static_assert(sizeof(double) == 8, "sizeof(double) == 8");
105 static constexpr ScalarEnum value = ScalarEnum::DOUBLE;
106 static constexpr const char* name = "double";
107};
108
109template <typename T>
110constexpr ScalarEnum ScalarToEnum_v = ScalarToEnum<T>::value;
111
112
113template <ScalarEnum type>
115{
116};
117
118template <>
119struct EnumToScalar<ScalarEnum::INT8>
120{
121 using type = int8_t;
122};
123
124template <>
125struct EnumToScalar<ScalarEnum::INT16>
126{
127 using type = int16_t;
128};
129
130template <>
131struct EnumToScalar<ScalarEnum::INT32>
132{
133 using type = int32_t;
134};
135
136template <>
137struct EnumToScalar<ScalarEnum::INT64>
138{
139 using type = int64_t;
140};
141
142template <>
143struct EnumToScalar<ScalarEnum::UINT8>
144{
145 using type = uint8_t;
146};
147
148template <>
149struct EnumToScalar<ScalarEnum::UINT16>
150{
151 using type = uint16_t;
152};
153
154template <>
155struct EnumToScalar<ScalarEnum::UINT32>
156{
157 using type = uint32_t;
158};
159
160template <>
161struct EnumToScalar<ScalarEnum::UINT64>
162{
163 using type = uint64_t;
164};
165
166template <>
167struct EnumToScalar<ScalarEnum::FLOAT>
168{
169 using type = float;
170};
171
172template <>
173struct EnumToScalar<ScalarEnum::DOUBLE>
174{
175 using type = double;
176};
177
178template <ScalarEnum T>
179using EnumToScalar_t = typename EnumToScalar<T>::type;
180
184inline std::string enum_to_name(ScalarEnum t)
185{
186 switch (t) {
187 case ScalarEnum::INT8: return ScalarToEnum<int8_t>::name;
188 case ScalarEnum::INT16: return ScalarToEnum<int16_t>::name;
189 case ScalarEnum::INT32: return ScalarToEnum<int32_t>::name;
190 case ScalarEnum::INT64: return ScalarToEnum<int64_t>::name;
191 case ScalarEnum::UINT8: return ScalarToEnum<uint8_t>::name;
192 case ScalarEnum::UINT16: return ScalarToEnum<uint16_t>::name;
193 case ScalarEnum::UINT32: return ScalarToEnum<uint32_t>::name;
194 case ScalarEnum::UINT64: return ScalarToEnum<uint64_t>::name;
195 case ScalarEnum::FLOAT: return ScalarToEnum<float>::name;
196 case ScalarEnum::DOUBLE: return ScalarToEnum<double>::name;
197 default: return "unknown";
198 }
199}
200
201} // namespace experimental
202} // namespace lagrange
Main namespace for Lagrange.