fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
VariantTypeTraits.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef FBCPP_VARIANT_TYPE_TRAITS_H
26#define FBCPP_VARIANT_TYPE_TRAITS_H
27
28#include "config.h"
29#include "types.h"
30#include "Array.h"
31#include "StructBinding.h"
32#include <cstddef>
33#include <vector>
34
35#if FB_CPP_USE_BOOST_MULTIPRECISION != 0
36#include <boost/multiprecision/cpp_int.hpp>
37#include <boost/multiprecision/cpp_dec_float.hpp>
38#endif
39
40#if FB_CPP_USE_BOOST_DECIMAL != 0
41#include <boost/decimal.hpp>
42#endif
43
44
45namespace fbcpp::impl::reflection
46{
50 template <typename T>
51 struct IsSupportedVariantType : std::false_type
52 {
53 };
54
55 // Arithmetic types
56 template <>
57 struct IsSupportedVariantType<bool> : std::true_type
58 {
59 };
60 template <>
61 struct IsSupportedVariantType<std::int16_t> : std::true_type
62 {
63 };
64 template <>
65 struct IsSupportedVariantType<std::int32_t> : std::true_type
66 {
67 };
68 template <>
69 struct IsSupportedVariantType<std::int64_t> : std::true_type
70 {
71 };
72 template <>
73 struct IsSupportedVariantType<float> : std::true_type
74 {
75 };
76 template <>
77 struct IsSupportedVariantType<double> : std::true_type
78 {
79 };
80
81 // Scaled numbers
82 template <>
83 struct IsSupportedVariantType<ScaledInt16> : std::true_type
84 {
85 };
86 template <>
87 struct IsSupportedVariantType<ScaledInt32> : std::true_type
88 {
89 };
90 template <>
91 struct IsSupportedVariantType<ScaledInt64> : std::true_type
92 {
93 };
94
95 // String
96 template <>
97 struct IsSupportedVariantType<std::string> : std::true_type
98 {
99 };
100
101 // Bytes
102 template <>
103 struct IsSupportedVariantType<std::vector<std::byte>> : std::true_type
104 {
105 };
106
107 // Date/Time types
108 template <>
109 struct IsSupportedVariantType<Date> : std::true_type
110 {
111 };
112 template <>
113 struct IsSupportedVariantType<Time> : std::true_type
114 {
115 };
116 template <>
117 struct IsSupportedVariantType<Timestamp> : std::true_type
118 {
119 };
120 template <>
121 struct IsSupportedVariantType<TimeTz> : std::true_type
122 {
123 };
124 template <>
125 struct IsSupportedVariantType<TimestampTz> : std::true_type
126 {
127 };
128
129 // Opaque Date/Time types
130 template <>
131 struct IsSupportedVariantType<OpaqueDate> : std::true_type
132 {
133 };
134 template <>
135 struct IsSupportedVariantType<OpaqueTime> : std::true_type
136 {
137 };
138 template <>
139 struct IsSupportedVariantType<OpaqueTimestamp> : std::true_type
140 {
141 };
142 template <>
143 struct IsSupportedVariantType<OpaqueTimeTz> : std::true_type
144 {
145 };
146 template <>
147 struct IsSupportedVariantType<OpaqueTimestampTz> : std::true_type
148 {
149 };
150
151 // Blob
152 template <>
153 struct IsSupportedVariantType<BlobId> : std::true_type
154 {
155 };
156
157 // Array
158 template <>
159 struct IsSupportedVariantType<ArrayId> : std::true_type
160 {
161 };
162
163#if FB_CPP_USE_BOOST_MULTIPRECISION != 0
164 // Boost multiprecision types
165 template <>
166 struct IsSupportedVariantType<BoostInt128> : std::true_type
167 {
168 };
169 template <>
170 struct IsSupportedVariantType<ScaledBoostInt128> : std::true_type
171 {
172 };
173 template <>
174 struct IsSupportedVariantType<BoostDecFloat16> : std::true_type
175 {
176 };
177 template <>
178 struct IsSupportedVariantType<BoostDecFloat34> : std::true_type
179 {
180 };
181#endif
182
183#if FB_CPP_USE_BOOST_DECIMAL != 0
184 // Boost.Decimal types
185 template <>
186 struct IsSupportedVariantType<BoostDecimal32> : std::true_type
187 {
188 };
189 template <>
190 struct IsSupportedVariantType<BoostDecimal64> : std::true_type
191 {
192 };
193 template <>
194 struct IsSupportedVariantType<BoostDecimal128> : std::true_type
195 {
196 };
197#endif
198
199 // Opaque multiprecision types
200 template <>
201 struct IsSupportedVariantType<ScaledOpaqueInt128> : std::true_type
202 {
203 };
204 template <>
205 struct IsSupportedVariantType<OpaqueDecFloat16> : std::true_type
206 {
207 };
208 template <>
209 struct IsSupportedVariantType<OpaqueDecFloat34> : std::true_type
210 {
211 };
212
213 // std::monostate is always allowed for NULL representation
214 template <>
215 struct IsSupportedVariantType<std::monostate> : std::true_type
216 {
217 };
218
219 template <typename T>
220 inline constexpr bool isSupportedVariantTypeV = IsSupportedVariantType<T>::value;
221
225 template <typename V, std::size_t I = 0>
226 struct VariantAlternativesSupported : std::true_type
227 {
228 };
229
230 template <typename V, std::size_t I>
231 requires(I < std::variant_size_v<V>)
232 struct VariantAlternativesSupported<V, I>
233 {
234 using Alt = std::variant_alternative_t<I, V>;
235 static constexpr bool value = isSupportedVariantTypeV<Alt> && VariantAlternativesSupported<V, I + 1>::value;
236 };
237
238 template <typename V>
239 inline constexpr bool variantAlternativesSupportedV = VariantAlternativesSupported<V>::value;
240} // namespace fbcpp::impl::reflection
241
242
243// Specializations for opaque type detection
244namespace fbcpp::impl::reflection
245{
246 template <>
247 struct IsOpaqueType<ScaledOpaqueInt128> : std::true_type
248 {
249 };
250
251 template <>
252 struct IsOpaqueType<OpaqueDecFloat16> : std::true_type
253 {
254 };
255
256 template <>
257 struct IsOpaqueType<OpaqueDecFloat34> : std::true_type
258 {
259 };
260
261 template <>
262 struct IsOpaqueType<OpaqueDate> : std::true_type
263 {
264 };
265
266 template <>
267 struct IsOpaqueType<OpaqueTime> : std::true_type
268 {
269 };
270
271 template <>
272 struct IsOpaqueType<OpaqueTimestamp> : std::true_type
273 {
274 };
275
276 template <>
277 struct IsOpaqueType<OpaqueTimeTz> : std::true_type
278 {
279 };
280
281 template <>
282 struct IsOpaqueType<OpaqueTimestampTz> : std::true_type
283 {
284 };
285
286 template <>
287 struct IsOpaqueType<std::vector<std::byte>> : std::true_type
288 {
289 };
290} // namespace fbcpp::impl::reflection
291
292
293#endif // FBCPP_VARIANT_TYPE_TRAITS_H
ScaledNumber< OpaqueInt128 > ScaledOpaqueInt128
Scaled Firebird opaque 128-bit integer.
Definition types.h:246
ScaledNumber< std::int64_t > ScaledInt64
Signed 64-bit scaled number.
Definition types.h:93
ScaledNumber< std::int32_t > ScaledInt32
Signed 32-bit scaled number.
Definition types.h:88
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 34 > > BoostDecFloat34
34-digit decimal floating point using Boost.Multiprecision.
Definition types.h:116
FB_DEC16 OpaqueDecFloat16
Opaque 16-digit decimal floating point exposed by the Firebird API.
Definition types.h:236
ScaledNumber< std::int16_t > ScaledInt16
Signed 16-bit scaled number.
Definition types.h:83
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 16 > > BoostDecFloat16
16-digit decimal floating point using Boost.Multiprecision.
Definition types.h:111
boost::decimal::decimal128_t BoostDecimal128
34-digit decimal floating point using Boost.Decimal.
Definition types.h:133
boost::decimal::decimal32_t BoostDecimal32
7-digit decimal floating point using Boost.Decimal.
Definition types.h:123
std::chrono::year_month_day Date
Firebird SQL calendar date.
Definition types.h:139
FB_DEC34 OpaqueDecFloat34
Opaque 34-digit decimal floating point exposed by the Firebird API.
Definition types.h:241
boost::multiprecision::int128_t BoostInt128
128-bit integer using Boost.Multiprecision.
Definition types.h:99
ScaledNumber< BoostInt128 > ScaledBoostInt128
Scaled 128-bit integer backed by Boost.Multiprecision.
Definition types.h:104
boost::decimal::decimal64_t BoostDecimal64
16-digit decimal floating point using Boost.Decimal.
Definition types.h:128
std::chrono::hh_mm_ss< std::chrono::microseconds > Time
Firebird SQL time-of-day with microsecond resolution.
Definition types.h:144