fb-cpp 0.0.1
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
types.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_TYPES_H
26#define FBCPP_TYPES_H
27
28#include "fb-api.h"
29#include "config.h"
30#include <chrono>
31#include <cstdint>
32#include <format>
33#include <iostream>
34#include <string>
35
36#if FB_CPP_USE_BOOST_MULTIPRECISION != 0
37#include <boost/multiprecision/cpp_int.hpp>
38#include <boost/multiprecision/cpp_dec_float.hpp>
39#endif
40
41
45namespace fbcpp
46{
50 template <typename T>
51 struct ScaledNumber final
52 {
53 bool operator==(const ScaledNumber&) const noexcept = default;
54
58 T value{};
59
63 int scale = 0;
64 };
65
70
75
80
81#if FB_CPP_USE_BOOST_MULTIPRECISION != 0
85 using BoostInt128 = boost::multiprecision::int128_t;
86
91#endif
92
93#if FB_CPP_USE_BOOST_MULTIPRECISION != 0
97 using BoostDecFloat16 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<16>>;
98
102 using BoostDecFloat34 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<34>>;
103#endif
104
108 using Date = std::chrono::year_month_day;
109
113 using Time = std::chrono::hh_mm_ss<std::chrono::microseconds>;
114
118 struct Timestamp final
119 {
120 bool operator==(const Timestamp& o) const noexcept
121 {
122 return date == o.date && time.to_duration() == o.time.to_duration();
123 }
124
130 std::chrono::local_time<std::chrono::microseconds> toLocalTime() const noexcept
131 {
132 return std::chrono::local_days{date} + time.to_duration();
133 }
134
138 static Timestamp fromLocalTime(std::chrono::local_time<std::chrono::microseconds> value) noexcept
139 {
140 const auto days = std::chrono::floor<std::chrono::days>(value);
141 const Date dateValue{days};
142 const auto timeOfDay = std::chrono::duration_cast<std::chrono::microseconds>(value - days);
143
144 return Timestamp{dateValue, Time{timeOfDay}};
145 }
146
151
155 Time time{std::chrono::microseconds::zero()};
156 };
157
161 struct TimeTz final
162 {
163 bool operator==(const TimeTz& o) const noexcept
164 {
165 return utcTime.to_duration() == o.utcTime.to_duration() && zone == o.zone;
166 }
167
172
176 std::string zone;
177 };
178
182 struct TimestampTz final
183 {
184 bool operator==(const TimestampTz&) const noexcept = default;
185
190
194 std::string zone;
195 };
196
200 using OpaqueInt128 = FB_I128;
201
205 using OpaqueDecFloat16 = FB_DEC16;
206
210 using OpaqueDecFloat34 = FB_DEC34;
211
216
220 struct alignas(alignof(ISC_DATE)) OpaqueDate final
221 {
222 bool operator==(const OpaqueDate&) const noexcept = default;
223
227 ISC_DATE value;
228 };
229
233 struct alignas(alignof(ISC_TIME)) OpaqueTime final
234 {
235 bool operator==(const OpaqueTime&) const noexcept = default;
236
240 ISC_TIME value;
241 };
242
246 struct alignas(alignof(ISC_TIMESTAMP)) OpaqueTimestamp final
247 {
248 bool operator==(const OpaqueTimestamp& o) const noexcept
249 {
250 return value.timestamp_date == o.value.timestamp_date && value.timestamp_time == o.value.timestamp_time;
251 }
252
256 ISC_TIMESTAMP value;
257 };
258
262 struct alignas(alignof(ISC_TIME_TZ)) OpaqueTimeTz final
263 {
264 bool operator==(const OpaqueTimeTz& o) const noexcept
265 {
266 return value.utc_time == o.value.utc_time && value.time_zone == o.value.time_zone;
267 }
268
272 ISC_TIME_TZ value;
273 };
274
278 struct alignas(alignof(ISC_TIMESTAMP_TZ)) OpaqueTimestampTz final
279 {
280 bool operator==(const OpaqueTimestampTz& o) const noexcept
281 {
282 return value.utc_timestamp.timestamp_date == o.value.utc_timestamp.timestamp_date &&
283 value.utc_timestamp.timestamp_time == o.value.utc_timestamp.timestamp_time &&
284 value.time_zone == o.value.time_zone;
285 }
286
290 ISC_TIMESTAMP_TZ value;
291 };
292
293 // FIXME: test
297 template <typename T>
298 std::ostream& operator<<(std::ostream& os, const fbcpp::ScaledNumber<T>& scaledNumber)
299 {
300 os << scaledNumber.value << "e" << scaledNumber.scale;
301 return os;
302 }
303} // namespace fbcpp
304
305
306// FIXME: test
310template <typename T>
311struct std::formatter<fbcpp::ScaledNumber<T>> : std::formatter<T>
312{
316 constexpr auto parse(format_parse_context& ctx)
317 {
318 return std::formatter<T>::parse(ctx);
319 }
320
324 template <typename FormatContext>
325 auto format(const fbcpp::ScaledNumber<T>& scaledNumber, FormatContext& ctx) const
326 {
327 return std::format_to(
328 ctx.out(), "{}e{}", std::formatter<T>::format(scaledNumber.value, ctx), scaledNumber.scale);
329 }
330};
331
332
333#endif // FBCPP_TYPES_H
fb-cpp namespace.
Definition Attachment.h:42
std::ostream & operator<<(std::ostream &os, const fbcpp::ScaledNumber< T > &scaledNumber)
Stream insertion helper that renders the scaled number as value followed by e and the scale.
Definition types.h:298
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 34 > > BoostDecFloat34
34-digit decimal floating point using Boost.Multiprecision.
Definition types.h:102
FB_DEC16 OpaqueDecFloat16
Opaque 16-digit decimal floating point exposed by the Firebird API.
Definition types.h:205
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 16 > > BoostDecFloat16
16-digit decimal floating point using Boost.Multiprecision.
Definition types.h:97
FB_I128 OpaqueInt128
Opaque 128-bit integer exposed by the Firebird API.
Definition types.h:200
std::chrono::year_month_day Date
Firebird SQL calendar date.
Definition types.h:108
FB_DEC34 OpaqueDecFloat34
Opaque 34-digit decimal floating point exposed by the Firebird API.
Definition types.h:210
boost::multiprecision::int128_t BoostInt128
128-bit integer using Boost.Multiprecision.
Definition types.h:85
std::chrono::hh_mm_ss< std::chrono::microseconds > Time
Firebird SQL time-of-day with microsecond resolution.
Definition types.h:113
Wrapper for Firebird date values.
Definition types.h:221
ISC_DATE value
Raw Firebird date representation.
Definition types.h:227
Wrapper for Firebird time-with-time-zone values.
Definition types.h:263
ISC_TIME_TZ value
Raw Firebird time-with-time-zone representation.
Definition types.h:272
Wrapper for Firebird time values.
Definition types.h:234
ISC_TIME value
Raw Firebird time representation.
Definition types.h:240
Wrapper for Firebird timestamp-with-time-zone values.
Definition types.h:279
ISC_TIMESTAMP_TZ value
Raw Firebird timestamp-with-time-zone representation.
Definition types.h:290
Wrapper for Firebird timestamp values.
Definition types.h:247
ISC_TIMESTAMP value
Raw Firebird timestamp representation.
Definition types.h:256
Represents a numeric value with an explicit decimal scale.
Definition types.h:52
int scale
Decimal scale applied to value.
Definition types.h:63
T value
Unscaled numeric value.
Definition types.h:58
Local time bound to a time zone.
Definition types.h:162
Time utcTime
UTC-normalised time-of-day.
Definition types.h:171
std::string zone
Time zone identifier.
Definition types.h:176
Timestamp bound to a time zone.
Definition types.h:183
Timestamp utcTimestamp
UTC-normalised timestamp.
Definition types.h:189
std::string zone
Time zone identifier.
Definition types.h:194
Combined date and time with microsecond precision.
Definition types.h:119
std::chrono::local_time< std::chrono::microseconds > toLocalTime() const noexcept
Converts to std::chrono::local_time<std::chrono::microseconds>.
Definition types.h:130
Time time
Time-of-day component.
Definition types.h:155
Date date
Calendar date component.
Definition types.h:150
static Timestamp fromLocalTime(std::chrono::local_time< std::chrono::microseconds > value) noexcept
Builds a timestamp from a local-time value.
Definition types.h:138
auto format(const fbcpp::ScaledNumber< T > &scaledNumber, FormatContext &ctx) const
Emits the scaled number using the native formatter for the value followed by the scale suffix.
Definition types.h:325
constexpr auto parse(format_parse_context &ctx)
Parses the format specifier forwarded to the underlying formatter.
Definition types.h:316