Lagrange
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
36template <>
37struct ScalarToEnum<int8_t, void>
38{
39 static constexpr ScalarEnum value = ScalarEnum::INT8;
40 static constexpr const char* name = "int8_t";
41};
42
43template <>
44struct ScalarToEnum<int16_t, void>
45{
46 static constexpr ScalarEnum value = ScalarEnum::INT16;
47 static constexpr const char* name = "int16_t";
48};
49
50template <>
51struct ScalarToEnum<int32_t, void>
52{
53 static constexpr ScalarEnum value = ScalarEnum::INT32;
54 static constexpr const char* name = "int32_t";
55};
56
57template <>
58struct ScalarToEnum<int64_t, void>
59{
60 static constexpr ScalarEnum value = ScalarEnum::INT64;
61 static constexpr const char* name = "int64_t";
62};
63
64template <>
65struct ScalarToEnum<uint8_t, void>
66{
67 static constexpr ScalarEnum value = ScalarEnum::UINT8;
68 static constexpr const char* name = "uint8_t";
69};
70
71template <>
72struct ScalarToEnum<uint16_t, void>
73{
74 static constexpr ScalarEnum value = ScalarEnum::UINT16;
75 static constexpr const char* name = "uint16_t";
76};
77
78template <>
79struct ScalarToEnum<uint32_t, void>
80{
81 static constexpr ScalarEnum value = ScalarEnum::UINT32;
82 static constexpr const char* name = "uint32_t";
83};
84
85template <>
86struct ScalarToEnum<uint64_t, void>
87{
88 static constexpr ScalarEnum value = ScalarEnum::UINT64;
89 static constexpr const char* name = "uint64_t";
90};
91
92template <>
93struct ScalarToEnum<float, void>
94{
95 static_assert(sizeof(float) == 4, "sizeof(float) == 4");
96 static constexpr ScalarEnum value = ScalarEnum::FLOAT;
97 static constexpr const char* name = "float";
98};
99
100template <>
101struct ScalarToEnum<double, void>
102{
103 static_assert(sizeof(double) == 8, "sizeof(double) == 8");
104 static constexpr ScalarEnum value = ScalarEnum::DOUBLE;
105 static constexpr const char* name = "double";
106};
107
108template <typename T>
109constexpr ScalarEnum ScalarToEnum_v = ScalarToEnum<T>::value;
110
111
112template <ScalarEnum type>
114{
115};
116
117template <>
118struct EnumToScalar<ScalarEnum::INT8>
119{
120 using type = int8_t;
121};
122
123template <>
124struct EnumToScalar<ScalarEnum::INT16>
125{
126 using type = int16_t;
127};
128
129template <>
130struct EnumToScalar<ScalarEnum::INT32>
131{
132 using type = int32_t;
133};
134
135template <>
136struct EnumToScalar<ScalarEnum::INT64>
137{
138 using type = int64_t;
139};
140
141template <>
142struct EnumToScalar<ScalarEnum::UINT8>
143{
144 using type = uint8_t;
145};
146
147template <>
148struct EnumToScalar<ScalarEnum::UINT16>
149{
150 using type = uint16_t;
151};
152
153template <>
154struct EnumToScalar<ScalarEnum::UINT32>
155{
156 using type = uint32_t;
157};
158
159template <>
160struct EnumToScalar<ScalarEnum::UINT64>
161{
162 using type = uint64_t;
163};
164
165template <>
166struct EnumToScalar<ScalarEnum::FLOAT>
167{
168 using type = float;
169};
170
171template <>
172struct EnumToScalar<ScalarEnum::DOUBLE>
173{
174 using type = double;
175};
176
177template <ScalarEnum T>
178using EnumToScalar_t = typename EnumToScalar<T>::type;
179
183inline std::string enum_to_name(ScalarEnum t)
184{
185 switch(t) {
186 case ScalarEnum::INT8:
188 case ScalarEnum::INT16:
190 case ScalarEnum::INT32:
192 case ScalarEnum::INT64:
194 case ScalarEnum::UINT8:
196 case ScalarEnum::UINT16:
198 case ScalarEnum::UINT32:
200 case ScalarEnum::UINT64:
202 case ScalarEnum::FLOAT:
204 case ScalarEnum::DOUBLE:
206 default:
207 return "unknown";
208 }
209}
210
211} // namespace experimental
212} // namespace lagrange
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Scalar.h:114