diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 6a67abf2d..b664c6e3d 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -30,6 +30,7 @@ if(WIN32) RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} api/*.cpp config/*.cpp + core/*.cpp domain_model/*.cpp expression/*.cpp logging/*.cpp diff --git a/src/lib/algorithm/PlusX.h b/src/lib/algorithm/PlusX.h new file mode 100644 index 000000000..314ff7f51 --- /dev/null +++ b/src/lib/algorithm/PlusX.h @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace bts::algorithm { + +template +struct PlusX { + /// Returns the sum + _Tp operator()(const _Tp &__x) const { return __x + _Addend; } +}; + +} // namespace bts::algorithm diff --git a/src/lib/algorithm/StringCaseConverter.h b/src/lib/algorithm/StringCaseConverter.h new file mode 100644 index 000000000..43ca4e238 --- /dev/null +++ b/src/lib/algorithm/StringCaseConverter.h @@ -0,0 +1,49 @@ +#pragma once +#include + +namespace bts::algorithm { + +class StringCaseConverter { + public: + static void to_upper(const std::string &str, std::string &dest) noexcept; + static std::string to_upper(const std::string &str) noexcept; + + static void to_lower(const std::string &str, std::string &dest) noexcept; + static std::string to_lower(const std::string &str) noexcept; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline std::string StringCaseConverter::to_upper(const std::string &str) noexcept { + std::string rv{str}; + for (auto &c : rv) { + c = std::toupper(c); + } + return rv; +} + +inline void StringCaseConverter::to_upper(const std::string &str, std::string &dest) noexcept { + dest = str; + for (auto &c : dest) { + c = std::toupper(c); + } +} + +inline std::string StringCaseConverter::to_lower(const std::string &str) noexcept { + std::string rv{str}; + for (auto &c : rv) { + c = std::tolower(c); + } + return rv; +} + +inline void StringCaseConverter::to_lower(const std::string &str, std::string &dest) noexcept { + dest = str; + for (auto &c : dest) { + c = std::tolower(c); + } +} + +} // namespace bts::algorithm diff --git a/src/lib/algorithm/TryCatch.h b/src/lib/algorithm/TryCatch.h new file mode 100644 index 000000000..8605965a3 --- /dev/null +++ b/src/lib/algorithm/TryCatch.h @@ -0,0 +1,28 @@ +#pragma once + +#define CATCH_AND_LOG(subsys, level, label) \ + catch (const std::exception &ex) { \ + LOG(subsys, level, label ": {}", std::string{ex.what()}); \ + } \ + catch (...) { \ + LOG(subsys, level, label ": Unknown exception"); \ + } + +#define CATCH_AND_HANDLE(err_var_name, impl) \ + catch (const std::exception &err_var_name##_ex) { \ + const auto &err_var_name = err_var_name##_ex.what(); \ + impl; \ + } \ + catch (...) { \ + const std::string err_var_name{"Unknown exception"}; \ + impl; \ + } + +// Normalizes 'std::exception' and '...' handlers into single callback +#define CATCH_AND_HANDLE_MSG(impl) \ + catch (const std::exception &ex) { \ + impl(std::string{ex.what()}); \ + } \ + catch (...) { \ + impl(std::string{"Unknown exception"}); \ + } diff --git a/src/lib/core/EnumBitwiseOperators.h b/src/lib/core/EnumBitwiseOperators.h deleted file mode 100644 index b8e8dbb69..000000000 --- a/src/lib/core/EnumBitwiseOperators.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#define BITWISE_OP(enum_type, numeric_type, op) \ - enum_type operator op(enum_type lhs, enum_type rhs) noexcept { \ - return static_cast(static_cast(lhs) \ - op static_cast(rhs)); \ - } - -#define BITWISE_OPS(enum_type, numeric_type) \ - BITWISE_OP(enum_type, numeric_type, |) \ - BITWISE_OP(enum_type, numeric_type, &) \ - BITWISE_OP(enum_type, numeric_type, ^) - -#define HAS_FLAG(enum_value, flag) (enum_value & flag) == flag -#define STRIP_FLAG(enum_value, flag) (enum_value | flag) ^ flag diff --git a/src/lib/core/EnumMacros.h b/src/lib/core/EnumMacros.h new file mode 100644 index 000000000..4fcac8ffe --- /dev/null +++ b/src/lib/core/EnumMacros.h @@ -0,0 +1,154 @@ +#pragma once + +#define ENUM_BASIC_MEMBERS(type) \ + Value value; \ + \ + type() = default; \ + /*NOLINTNEXTLINE*/ \ + constexpr type(Value value) noexcept : value(value) {} \ + \ + type(const type &) noexcept = default; \ + type(type &&) noexcept = default; \ + type &operator=(const type &) noexcept = default; \ + type &operator=(type &&) noexcept = default; \ + \ + ~type() = default; \ + \ + static constexpr bool parse(const char *name, type *value) noexcept; \ + static bool parse(const std::string &name, type *value) noexcept; \ + \ + [[nodiscard]] constexpr const char *name() const noexcept; + +#define ENUM_VALUE_TO_NAME_CASE(type, v) \ + case type::v: \ + return #v; + +#define ENUM_NAME_TO_VALUE_CLAUSE(type, v) \ + if (name == #v) { \ + *value = type::v; \ + return true; \ + } + +#define ENUM_NAMEC_TO_VALUE_CLAUSE(type, v) \ + if (std::string_view(name) == #v) { \ + *value = type::v; \ + return true; \ + } + +// clang-format off +#define ENUM_NAME_DEF(type, for_all) \ + constexpr const char *type::name() const noexcept { \ + switch (value) { \ + for_all(ENUM_VALUE_TO_NAME_CASE) \ + default: return "ENUM_UNKNOWN"; \ + } \ + } +// clang-format on + +#define ENUM_PARSE_DEF(type, for_all) \ + constexpr bool type::parse(const char *name, type *value) noexcept { \ + for_all(ENUM_NAMEC_TO_VALUE_CLAUSE) return false; \ + } \ + inline bool type::parse(const std::string &name, type *value) noexcept { \ + for_all(ENUM_NAME_TO_VALUE_CLAUSE) return false; \ + } + +#define ENUM_EQUALITY_OPS(type) \ + constexpr bool operator==(type lhs, type rhs) noexcept { \ + return lhs.value == rhs.value; \ + } \ + constexpr bool operator!=(type lhs, type rhs) noexcept { \ + return lhs.value != rhs.value; \ + } \ + constexpr bool operator==(type lhs, type::Value rhs) noexcept { \ + return lhs.value == rhs; \ + } \ + constexpr bool operator!=(type lhs, type::Value rhs) noexcept { \ + return lhs.value != rhs; \ + } + +#define ENUM_OUTPUT_OPS(type) \ + inline std::ostream &operator<<(std::ostream &out, type value) noexcept { \ + out << value.name(); \ + return out; \ + } \ + \ + inline std::ostream &operator<<( \ + std::ostream &out, type::Value value) noexcept { \ + out << type{value}.name(); \ + return out; \ + } + +#define ENUM_FROM_JSON(type) \ + inline void from_json(const nlohmann::json &j, type &value) { \ + std::string value_str; \ + j.get_to(value_str); \ + type::parse(value_str, &value); \ + } + +#define ENUM_TO_JSON(type) \ + inline void to_json(nlohmann::json &j, const type &value) { \ + j = value.name(); \ + } + +#define ENUM_DECLARE_VALUE(t, x) x, + +#define ENUM_CLASS_DEF(type, for_all) \ + class type { \ + public: \ + enum Value { for_all(ENUM_DECLARE_VALUE) }; \ + \ + ENUM_BASIC_MEMBERS(type) \ + }; \ + \ + ENUM_OUTPUT_OPS(type) \ + ENUM_NAME_DEF(type, for_all) \ + ENUM_PARSE_DEF(type, for_all) \ + ENUM_FROM_JSON(type) \ + ENUM_TO_JSON(type) \ + ENUM_EQUALITY_OPS(type) + +#define ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, op) \ + constexpr enum_type operator op(enum_type lhs, enum_type rhs) noexcept { \ + return static_cast(static_cast(lhs) \ + op static_cast(rhs)); \ + } + +#define ENUM_BITWISE_COMPARISON_OPS(enum_type, numeric_type) \ + ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, |) \ + ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, &) \ + ENUM_BITWISE_COMPARISON_OP(enum_type, numeric_type, ^) + +#define ENUM_BITWISE_ASSIGNMENT_OP(enum_type, op) \ + inline enum_type &operator op##=(enum_type &lhs, enum_type rhs) noexcept { \ + lhs = lhs op rhs; \ + return lhs; \ + } + +#define ENUM_BITWISE_ASSIGNMENT_OPS(enum_type) \ + ENUM_BITWISE_ASSIGNMENT_OP(enum_type, |) \ + ENUM_BITWISE_ASSIGNMENT_OP(enum_type, &) \ + ENUM_BITWISE_ASSIGNMENT_OP(enum_type, ^) + +#define ENUM_BITWISE_OPS(enum_type, numeric_type) \ + ENUM_BITWISE_COMPARISON_OPS(enum_type, numeric_type) \ + ENUM_BITWISE_ASSIGNMENT_OPS(enum_type) + +#define ENUM_HAS_FLAG(enum_value, flag) (enum_value & flag) == flag +#define ENUM_STRIP_FLAG(enum_value, flag) (enum_value | flag) ^ flag + +#define PB_ENUM_FROM_JSON(enum_type) \ + inline void from_json(const nlohmann::json &j, enum_type &value) { \ + io::JsonReader jr{&j}; \ + std::string name; \ + bool success = jr.read(name) && enum_type##_Parse(name, &value); \ + if (!success) { \ + const auto j_str = j.dump(2); \ + LOG(Gen, Diag, "Failed to parse json to " #enum_type ":\n{}", j_str); \ + } \ + } + +#define PB_ENUM_TO_JSON(enum_type) \ + inline void to_json(nlohmann::json &j, const enum_type &value) { \ + j = enum_type##_Name(value); \ + } diff --git a/src/lib/core/HashPhaser.h b/src/lib/core/HashPhaser.h index 7d0fd56ef..8b50c4572 100644 --- a/src/lib/core/HashPhaser.h +++ b/src/lib/core/HashPhaser.h @@ -132,7 +132,7 @@ std::size_t HashPhaser::auto_phase_range(Iter first, Iter last) { std::uint16_t el_count = num_vals > (std::numeric_limits::max)() ? max_els - : std::max(static_cast(num_vals), max_els); + : (static_cast(num_vals) > max_els ? static_cast(num_vals) : max_els); auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / el_count; std::size_t phase = 1U << (bits_per_hash - 1); std::size_t composite_hash = 0; @@ -157,7 +157,7 @@ std::size_t HashPhaser::auto_phase_range(Iter first, Iter last, Hash hash) { std::uint16_t el_count = num_vals > (std::numeric_limits::max)() ? max_els - : std::max(static_cast(num_vals), max_els); + : (static_cast(num_vals) > max_els ? static_cast(num_vals) : max_els); auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / el_count; std::size_t phase = 1U << (bits_per_hash - 1); std::size_t composite_hash = 0; diff --git a/src/lib/core/TimeSpan.cpp b/src/lib/core/TimeSpan.cpp new file mode 100644 index 000000000..eb8210b07 --- /dev/null +++ b/src/lib/core/TimeSpan.cpp @@ -0,0 +1,26 @@ +#include "TimeSpan.h" +#include +#include + +namespace bts::core { + +TimeSpan TimeSpan::from_iso_string(const std::string &iso_string) { + const auto pdur = boost::posix_time::duration_from_string(iso_string); + return from_posix_duration(pdur); +} + +std::string TimeSpan::to_iso_string() const { + const auto pdur = to_posix_duration(); + return boost::posix_time::to_iso_string(pdur); +} + +std::ostream &operator<<(std::ostream &out, const TimeSpan &ts) noexcept { + out << ts.to_iso_string(); + return out; +} + +TimeSpan TimeSpan::from_posix_duration(boost::posix_time::time_duration d) { + return TimeSpan{d.total_nanoseconds()}; +} + +} // namespace bts::core diff --git a/src/lib/core/TimeSpan.h b/src/lib/core/TimeSpan.h new file mode 100644 index 000000000..23970e616 --- /dev/null +++ b/src/lib/core/TimeSpan.h @@ -0,0 +1,125 @@ +#pragma once +#include "lib/core/FMT_IMPL.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/datetime.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace bts::core { + +/** + * @brief Represents a duration of time + */ +class TimeSpan { + public: + constexpr TimeSpan() = default; + explicit constexpr TimeSpan(std::int64_t ticks); + + static constexpr TimeSpan zero(); + + static constexpr TimeSpan from_nanos(std::int64_t value); + static constexpr TimeSpan from_micros(std::int64_t value); + static constexpr TimeSpan from_millis(std::int64_t value); + static constexpr TimeSpan from_seconds(std::int64_t value); + static TimeSpan from_posix_duration(boost::posix_time::time_duration t); + static TimeSpan from_iso_string(const std::string &iso_string); + + constexpr std::int64_t nanos() const; + constexpr std::int64_t micros() const; + constexpr std::int64_t millis() const; + constexpr std::int64_t seconds() const; + + constexpr double total_millis() const; + constexpr double total_seconds() const; + + constexpr boost::posix_time::time_duration to_posix_duration() const; + std::string to_iso_string() const; + + private: + // nanos + std::int64_t ticks_{}; +}; + +constexpr bool operator==(TimeSpan lhs, TimeSpan rhs) noexcept; +constexpr bool operator!=(TimeSpan lhs, TimeSpan rhs) noexcept; +constexpr bool operator<(TimeSpan lhs, TimeSpan rhs) noexcept; +constexpr bool operator>(TimeSpan lhs, TimeSpan rhs) noexcept; + +std::ostream &operator<<(std::ostream &out, const TimeSpan &ts) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +constexpr TimeSpan::TimeSpan(std::int64_t ticks) : ticks_(ticks) {} + +constexpr TimeSpan TimeSpan::zero() { return TimeSpan{0}; } + +constexpr TimeSpan TimeSpan::from_nanos(std::int64_t value) { + return TimeSpan{value}; +} + +constexpr TimeSpan TimeSpan::from_micros(std::int64_t value) { + constexpr const auto nanos_per_micro = 1'000; + return TimeSpan{value * nanos_per_micro}; +} + +constexpr TimeSpan TimeSpan::from_millis(std::int64_t value) { + constexpr const auto nanos_per_milli = 1'000'000; + return TimeSpan{value * nanos_per_milli}; +} + +constexpr TimeSpan TimeSpan::from_seconds(std::int64_t value) { + constexpr const auto nanos_per_second = 1'000'000'000; + return TimeSpan{value * nanos_per_second}; +} + +constexpr std::int64_t TimeSpan::nanos() const { return ticks_; } + +constexpr std::int64_t TimeSpan::micros() const { return ticks_ / 1'000; } + +constexpr std::int64_t TimeSpan::millis() const { return ticks_ / 1'000'000; } + +constexpr std::int64_t TimeSpan::seconds() const { + return ticks_ / 1'000'000'000; +} + +constexpr double TimeSpan::total_millis() const { + return static_cast(ticks_) / 1000'000; +} + +constexpr double TimeSpan::total_seconds() const { + return static_cast(ticks_) / 1000'000'000; +} + +constexpr boost::posix_time::time_duration TimeSpan::to_posix_duration() const { + return boost::date_time::subsecond_duration< + boost::posix_time::time_duration, 1000000000>{nanos()}; +} + +constexpr bool operator==(TimeSpan lhs, TimeSpan rhs) noexcept { + return lhs.nanos() == rhs.nanos(); +} + +constexpr bool operator!=(TimeSpan lhs, TimeSpan rhs) noexcept { + return lhs.nanos() != rhs.nanos(); +} + +constexpr bool operator<(TimeSpan lhs, TimeSpan rhs) noexcept { + return lhs.nanos() < rhs.nanos(); +} + +constexpr bool operator>(TimeSpan lhs, TimeSpan rhs) noexcept { + return lhs.nanos() > rhs.nanos(); +} + +} // namespace bts::core + +FMT_IMPL(bts::core::TimeSpan, t, "{}", t.to_iso_string()); diff --git a/src/lib/core/TimeStamp.cpp b/src/lib/core/TimeStamp.cpp new file mode 100644 index 000000000..4e51f86cb --- /dev/null +++ b/src/lib/core/TimeStamp.cpp @@ -0,0 +1,58 @@ +#include "TimeStamp.h" +#include "lib/utils/datetime.h" +#include + +namespace bts::core { + +using std::uint32_t; + +static bool is_valid_ymd(uint32_t year, uint32_t month, uint32_t day) { + return year >= 1970 && month >= 1 && month <= 12 && day >= 1 && day <= 31; +} + +TimeStamp::TimeStamp(uint32_t year, uint32_t month, uint32_t day) { + bts_assert_msgf( + is_valid_ymd(year, month, day), "Invalid timestamp {}/{}/{} (y/m/d)", + year, month, day); + std::tm date = {}; + date.tm_year = year - 1900; + date.tm_mon = month - 1; + date.tm_mday = day; + auto time = std::chrono::system_clock::from_time_t(mktime(&date)); + auto duration = time.time_since_epoch(); + value_ = + std::chrono::duration_cast(duration).count(); +} + +bool TimeStamp::from_ymd( + std::uint32_t year, std::uint32_t month, std::uint32_t day, TimeStamp *result) { + if (!is_valid_ymd(year, month, day)) { + return false; + } + *result = TimeStamp{year, month, day}; + return true; +} + +TimeStamp TimeStamp::from_iso_string(const std::string &iso_string) { + auto ptime = datetime::parse_iso_date_time_string(iso_string); + return from_ptime(ptime); +} + +std::string TimeStamp::to_iso_string() const { + return boost::posix_time::to_iso_string(to_ptime()); +} + +std::ostream &operator<<(std::ostream &out, const TimeStamp &ts) noexcept { + out << ts.to_iso_string(); + return out; +} + +TimeStamp TimeStamp::from_ptime(boost::posix_time::ptime t) { + // time_t is Unix time: number of seconds since epoch Jan 1, 1970 + auto epoch_pt = + t - boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1)); + auto epoch_nanos = epoch_pt.total_nanoseconds(); + return TimeStamp{static_cast(epoch_nanos)}; +} + +} // namespace bts::core diff --git a/src/lib/core/TimeStamp.h b/src/lib/core/TimeStamp.h index c139c1646..7123db305 100644 --- a/src/lib/core/TimeStamp.h +++ b/src/lib/core/TimeStamp.h @@ -1,27 +1,211 @@ #pragma once +#include "lib/core/FMT_IMPL.h" +#include "lib/core/TimeSpan.h" +#include "lib/utils/bts_assert.h" +#include "lib/utils/datetime.h" + +#include +#include +#include +#include +#include #include +#include +#include +#include namespace bts::core { -// I can send this whole class later but its a nice wrapper around the linux -// posix time +/** + * @brief Represents time as nano-seconds since the unix epoch (Jan 1, 1970) + */ struct TimeStamp { public: constexpr TimeStamp() = default; explicit constexpr TimeStamp(std::uint64_t value); + // constructs a TimeStamp from a UTC date + explicit TimeStamp(uint32_t year, uint32_t month, uint32_t day); + + static constexpr TimeStamp min(); + static constexpr TimeStamp max(); + + static constexpr TimeStamp from_int64(std::int64_t value); + static constexpr TimeStamp from_nanos(std::int64_t value); + static constexpr TimeStamp from_micros(std::int64_t value); + static constexpr TimeStamp from_millis(std::int64_t value); + static constexpr TimeStamp from_seconds(std::int64_t value); + static TimeStamp from_ptime(boost::posix_time::ptime t); + static TimeStamp from_iso_string(const std::string &iso_string); + static bool from_ymd( + std::uint32_t year, + std::uint32_t month, + std::uint32_t day, + TimeStamp *result); + constexpr std::uint64_t nanos() const; + constexpr std::uint64_t micros() const; + constexpr std::uint64_t millis() const; + constexpr std::uint64_t seconds() const; + + constexpr double total_millis() const; + constexpr double total_seconds() const; + + constexpr std::int64_t to_int64(); + inline void to_date(uint32_t &year, uint32_t &month, uint32_t &day); + constexpr boost::posix_time::ptime to_ptime() const; + std::string to_iso_string() const; + // current epoch ns + static uint64_t now_ns() { +#ifndef _WIN32 + return datetime::unix_nano_now(); +#else + auto now = std::chrono::system_clock::now(); + auto ns = std::chrono::duration_cast( + now.time_since_epoch()) + .count(); + return ns; +#endif + } + static TimeStamp now() { return from_nanos(now_ns()); } private: // posix time std::uint64_t value_{}; }; +constexpr bool operator==(TimeStamp lhs, TimeStamp rhs) noexcept; +constexpr bool operator!=(TimeStamp lhs, TimeStamp rhs) noexcept; +constexpr bool operator<(TimeStamp lhs, TimeStamp rhs) noexcept; +constexpr bool operator<=(TimeStamp lhs, TimeStamp rhs) noexcept; +constexpr bool operator>(TimeStamp lhs, TimeStamp rhs) noexcept; +constexpr bool operator>=(TimeStamp lhs, TimeStamp rhs) noexcept; + +constexpr TimeSpan operator-(TimeStamp lhs, TimeStamp rhs) noexcept; +constexpr TimeStamp operator-(TimeStamp lhs, TimeSpan rhs) noexcept; +constexpr TimeStamp operator+(TimeStamp lhs, TimeSpan rhs) noexcept; + +std::ostream &operator<<(std::ostream &out, const TimeStamp &ts) noexcept; + ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// constexpr TimeStamp::TimeStamp(std::uint64_t value) : value_(value) {} +constexpr TimeStamp TimeStamp::min() { return TimeStamp{0}; } + +constexpr TimeStamp TimeStamp::max() { + return TimeStamp{std::numeric_limits::max()}; +} + +constexpr TimeStamp TimeStamp::from_int64(std::int64_t value) { + bts_assert(value >= 0); + return TimeStamp{static_cast(value)}; +} + +constexpr TimeStamp TimeStamp::from_nanos(std::int64_t value) { + bts_assert(value >= 0); + return TimeStamp{static_cast(value)}; +} + +constexpr TimeStamp TimeStamp::from_micros(std::int64_t value) { + bts_assert(value >= 0); + constexpr const auto nanos_per_micro = 1'000; + return TimeStamp{static_cast(value * nanos_per_micro)}; +} + +constexpr TimeStamp TimeStamp::from_millis(std::int64_t value) { + bts_assert(value >= 0); + constexpr const auto nanos_per_milli = 1'000'000; + return TimeStamp{static_cast(value * nanos_per_milli)}; +} + +constexpr TimeStamp TimeStamp::from_seconds(std::int64_t value) { + bts_assert(value >= 0); + constexpr const auto nanos_per_second = 1'000'000'000; + return TimeStamp{static_cast(value * nanos_per_second)}; +} + constexpr std::uint64_t TimeStamp::nanos() const { return value_; } +constexpr std::uint64_t TimeStamp::micros() const { return value_ / 1'000; } + +constexpr std::uint64_t TimeStamp::millis() const { return value_ / 1'000'000; } + +constexpr std::uint64_t TimeStamp::seconds() const { + return value_ / 1'000'000'000; +} + +constexpr double TimeStamp::total_millis() const { + return static_cast(value_) / 1000'000; +} + +constexpr double TimeStamp::total_seconds() const { + return static_cast(value_) / 1000'000'000; +} + +constexpr std::int64_t TimeStamp::to_int64() { + bts_assert( + value_ <= + static_cast(std::numeric_limits::max())); + return static_cast(value_); +} +void TimeStamp::to_date(uint32_t &year, uint32_t &month, uint32_t &day) { + std::uint64_t epoch_secs = value_ / (1000 * 1000 * 1000); + time_t ts_tt(epoch_secs); + struct tm *ptm = gmtime(&ts_tt); + year = ptm->tm_year + 1900; + month = ptm->tm_mon + 1; + day = ptm->tm_mday; +} + +constexpr boost::posix_time::ptime TimeStamp::to_ptime() const { + constexpr const auto nanos_per_sec = 1'000'000'000; + constexpr const auto nanos_per_micro = 1'000; + // time_t is Unix time: number of seconds since epoch Jan 1, 1970 + std::time_t ts = value_ / nanos_per_sec; + const auto nanos_rem = value_ % nanos_per_sec; + return boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1)) + + boost::posix_time::seconds(static_cast(ts)) + + boost::posix_time::microseconds(nanos_rem / nanos_per_micro); +} + +constexpr bool operator==(TimeStamp lhs, TimeStamp rhs) noexcept { + return lhs.nanos() == rhs.nanos(); +} + +constexpr bool operator!=(TimeStamp lhs, TimeStamp rhs) noexcept { + return lhs.nanos() != rhs.nanos(); +} + +constexpr bool operator<(TimeStamp lhs, TimeStamp rhs) noexcept { + return lhs.nanos() < rhs.nanos(); +} + +constexpr bool operator<=(TimeStamp lhs, TimeStamp rhs) noexcept { + return lhs.nanos() <= rhs.nanos(); +} + +constexpr bool operator>(TimeStamp lhs, TimeStamp rhs) noexcept { + return lhs.nanos() > rhs.nanos(); +} + +constexpr bool operator>=(TimeStamp lhs, TimeStamp rhs) noexcept { + return lhs.nanos() >= rhs.nanos(); +} + +constexpr TimeSpan operator-(TimeStamp lhs, TimeStamp rhs) noexcept { + return TimeSpan::from_nanos(lhs.nanos() - rhs.nanos()); +} + +constexpr TimeStamp operator-(TimeStamp lhs, TimeSpan rhs) noexcept { + return TimeStamp{lhs.nanos() - rhs.nanos()}; +} + +constexpr TimeStamp operator+(TimeStamp lhs, TimeSpan rhs) noexcept { + return TimeStamp{lhs.nanos() + rhs.nanos()}; +} + } // namespace bts::core + +FMT_IMPL(bts::core::TimeStamp, t, "{}", t.to_iso_string()); diff --git a/src/lib/io/JsonDumper.h b/src/lib/io/JsonDumper.h new file mode 100644 index 000000000..d1703fc31 --- /dev/null +++ b/src/lib/io/JsonDumper.h @@ -0,0 +1,48 @@ +#pragma once +#include +#include +#include + +namespace bts::io { + +class JsonDumper { + public: + template + static std::string dump(const T &value, int indentation = 2); + + template + static std::string dump( + Iter first, + Iter last, + int indentation = 2, + const char *separator = ",\n"); +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +std::string JsonDumper::dump(const T &value, int indentation) { + nlohmann::json j; + to_json(j, value); + return j.dump(indentation); +} + +template +std::string JsonDumper::dump( + Iter first, Iter last, int indentation, const char *separator) { + if (first == last) { + return ""; + } + std::stringstream str; + auto it = first; + str << "[" << dump(*it, indentation); + while (++it != last) { + str << separator << dump(*it, indentation); + } + str << "]"; + return str.str(); +} + +} // namespace bts::io diff --git a/src/lib/io/JsonMacros.h b/src/lib/io/JsonMacros.h new file mode 100644 index 000000000..c32ea6727 --- /dev/null +++ b/src/lib/io/JsonMacros.h @@ -0,0 +1,22 @@ +#pragma once + +#define _TO_JSON_FIELD(field) j[#field] = t.field; +#define _FROM_JSON_FIELD(field) bts::io::JsonReader{&j, #field}.read(t.field); + +#define SPARK_TO_JSON_IMPL(type, for_each) \ + inline void to_json(nlohmann::json &j, const type &t) { \ + FOR_EACH(_TO_JSON_FIELD) \ + } + +#define SPARK_FROM_JSON_IMPL(type, for_each) \ + inline void from_json(const nlohmann::json &j, type &t) { \ + FOR_EACH(_FROM_JSON_FIELD) \ + } + +#define OSTREAM_JSON_IMPL(type) \ + inline std::ostream &operator<<(std::ostream &out, const type &t) { \ + nlohmann::json j; \ + to_json(j, t); \ + out << j.dump(2); \ + return out; \ + } diff --git a/src/lib/io/JsonReader.cpp b/src/lib/io/JsonReader.cpp new file mode 100644 index 000000000..6e603b316 --- /dev/null +++ b/src/lib/io/JsonReader.cpp @@ -0,0 +1,520 @@ +#include "JsonReader.h" +#include "lib/algorithm/TryCatch.h" +#include "lib/core/TimeStamp.h" +#include "lib/logging/logging.h" +#include "lib/utils/TimeStamp.h" +#include "lib/utils/datetime.h" +#include +#include +#include +#include + +namespace bts::io { + +using core::TimeStamp; +using nlohmann::json; +using std::int32_t; +using std::int64_t; +using std::numeric_limits; +using std::stoull; +using std::string; +using std::uint32_t; +using std::uint64_t; + +// 'read' fn bodies should be wrapped between BEGIN/END_READ_JSON tags. +// These macros abstract out repetative exception handling code (and a nullptr check). + +#define BEGIN_READ_JSON \ + if (json_ == nullptr) { \ + return false; \ + } \ + try + +#define END_READ_JSON(type_name) \ + catch (const std::exception &e) { \ + LOG(Gen, Diag, "Failed to parse json to '{}'. Exception: {}", \ + std::string{type_name}, std::string{e.what()}); \ + } \ + catch (...) { \ + bts_assert_msgf( \ + false, "Failed to parse json to '{}'. Exception: unknown", \ + std::string{type_name}); \ + } \ + return false; + +// The idea here is we parse numbers to wide types (either int64, uint64, or double), +// then cast that value to the (potentially narrower) output type. This macro evaluates +// to true if 'wide_val' is within the bounds of the result type (otherwise false). +#define NUM_WITHIN_BOUNDS(wide_val, result_t) \ + (wide_val >= \ + static_cast(numeric_limits::min()) && \ + wide_val <= \ + static_cast(numeric_limits::max())) + +static const nlohmann::json *get_field_json( + const nlohmann::json *json, const std::string &name) { + if (!json || !json->is_object()) { + return nullptr; + } + auto it = json->find(name); + if (it == json->end()) { + return nullptr; + } + return &it.value(); +} + +JsonReader::JsonReader(const nlohmann::json *json) : json_(json) {} + +JsonReader::JsonReader(const nlohmann::json *json, const std::string &name) + : json_(get_field_json(json, name)) {} + +bool JsonReader::try_parse( + const std::string &str, nlohmann::json &result, std::string *err) { + try { + result = nlohmann::json::parse(str); + return true; + } + CATCH_AND_HANDLE_MSG([err](const auto &msg) { + if (err != nullptr) { + *err = msg; + } + }) + return false; +} + +bool JsonReader::read(const string &name, string &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, int32_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, uint32_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, int64_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, uint64_t &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, float &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, double &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(const string &name, TimeStamp &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read_epoch_nanos(const string &name, TimeStamp &value) const { + return JsonReader{json_, name}.read_epoch_nanos(value); +} + +bool JsonReader::read_epoch_micros(const string &name, TimeStamp &value) const { + return JsonReader{json_, name}.read_epoch_micros(value); +} + +bool JsonReader::read_epoch_millis(const string &name, TimeStamp &value) const { + return JsonReader{json_, name}.read_epoch_millis(value); +} + +bool JsonReader::read(const string &name, nlohmann::json &result) const { + return JsonReader{json_, name}.read(result); +} + +bool JsonReader::read(int32_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + result = static_cast(d); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to int32 ({})", + d, result); + } + return true; + } + case nlohmann::json::value_t::number_integer: { + const auto il = json_->get(); + if (!NUM_WITHIN_BOUNDS(il, int32_t)) { + LOG(Gen, Error, + "Failed to convert field of type 'number_integer' ({}) to " + "uint32: value is negative.", + il); + } + result = static_cast(il); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + const auto ul = json_->get(); + if (ul > static_cast(numeric_limits::max())) { + LOG(Gen, Error, + "Failed to convert field of type 'number_unsigned' ({}) to " + "int32: value is too large.", + ul); + return false; + } + result = static_cast(ul); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stoi(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("int32") +} + +bool JsonReader::read(uint32_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + result = static_cast(d); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to uint32 ({})", + d, result); + } + return true; + } + case nlohmann::json::value_t::number_integer: { + const auto i = json_->get(); + if (!NUM_WITHIN_BOUNDS(i, uint32_t)) { + LOG(Gen, Error, + "Failed to convert field of type 'number_integer' ({}) to " + "uint32: value is negative.", + i); + return false; + } + result = static_cast(i); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + const auto ul = json_->get(); + if (ul > static_cast(numeric_limits::max())) { + LOG(Gen, Error, + "Failed to convert field of type 'number_unsigned' ({}) to " + "int32: value is too large.", + ul); + return false; + } + result = static_cast(ul); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stoul(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("uint32") +} + +bool JsonReader::read(int64_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to int64 ({})", + d, result); + } + result = static_cast(d); + return true; + } + case nlohmann::json::value_t::number_integer: { + result = json_->get(); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + const auto ul = json_->get(); + if (NUM_WITHIN_BOUNDS(ul, int64_t)) { + LOG(Gen, Error, + "Potential loss of data converting field of type " + "'number_unsigned' ({}) to int64 ({})", + ul, result); + return false; + } + result = static_cast(ul); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stol(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("int64") +} + +bool JsonReader::read(uint64_t &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: { + const auto d = json_->get(); + if (result != d) { + LOG(Gen, Warn, + "Potential loss of data converting field of type " + "'number_float' ({}) to uint64 ({})", + d, result); + } + result = static_cast(d); + return true; + } + case nlohmann::json::value_t::number_integer: { + const auto il = json_->get(); + if (NUM_WITHIN_BOUNDS(il, uint64_t)) { + LOG(Gen, Error, + "Potential loss of data converting field of type " + "'number_integer' ({}) to uint64 ({})", + il, result); + return false; + } + result = static_cast(il); + return true; + } + case nlohmann::json::value_t::number_unsigned: { + result = json_->get(); + return true; + } + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stoul(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("uint64") +} + +bool JsonReader::read(float &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: + case nlohmann::json::value_t::number_integer: + case nlohmann::json::value_t::number_unsigned: + json_->get_to(result); + return true; + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stof(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("float") +} + +bool JsonReader::read(double &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::number_float: + case nlohmann::json::value_t::number_integer: + case nlohmann::json::value_t::number_unsigned: + json_->get_to(result); + return true; + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + if (!str.empty()) { + result = std::stod(str); + return true; + } + break; + } + default: + return false; + } + } + END_READ_JSON("double") +} + +bool JsonReader::read(string &result) const { + BEGIN_READ_JSON { + json_->get_to(result); + return true; + } + END_READ_JSON("string") +} + +bool JsonReader::read(nlohmann::json &result) const { + BEGIN_READ_JSON { + json_->get_to(result); + return true; + } + END_READ_JSON("json-object") +} + +bool JsonReader::read_flat(std::vector &result) const { + if (json_ == nullptr) { + return false; + } + switch (json_->type()) { + case json::value_t::array: + return std::all_of(json_->begin(), json_->end(), [&](const auto &j) { + return JsonReader{&j}.read_flat(result); + }); + case json::value_t::null: + return false; + default: + read(result.emplace_back()); + return true; + } +} + +bool JsonReader::parse_time_stamp_(const string &str, TimeStamp &value) const { + return parse_iso_time_stamp_(str, value) || + parse_epoch_nanos_time_stamp_(str, value); +} + +bool JsonReader::parse_iso_time_stamp_( + const string &str, TimeStamp &value) const { + value = TimeStamp::from_iso_string(str); + return true; +} + +bool JsonReader::parse_epoch_nanos_time_stamp_( + const string &str, TimeStamp &value) const { + uint64_t nanos = stoull(str); + value = TimeStamp{nanos}; + return true; +} + +bool JsonReader::read(TimeStamp &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::null: + case nlohmann::json::value_t::object: + case nlohmann::json::value_t::array: + case nlohmann::json::value_t::boolean: + case nlohmann::json::value_t::binary: + case nlohmann::json::value_t::discarded: + return false; + case nlohmann::json::value_t::string: { + const auto str = json_->get(); + return parse_iso_time_stamp_(str, result) || + parse_epoch_nanos_time_stamp_(str, result); + } + case nlohmann::json::value_t::number_integer: + case nlohmann::json::value_t::number_unsigned: + case nlohmann::json::value_t::number_float: + // Maybe we could assume seconds here, idk though...when in doubt, prob + // better to be cautious than cavalier + return false; + } + } + END_READ_JSON("TimeStamp") +} + +bool JsonReader::read_iso( + const std::string &name, core::TimeStamp &result) const { + return JsonReader{json_, name}.read_iso(result); +} + +bool JsonReader::read_iso(core::TimeStamp &result) const { + BEGIN_READ_JSON { + switch (json_->type()) { + case nlohmann::json::value_t::string: { + return parse_iso_time_stamp_(json_->get(), result); + } + default: + return false; + } + } + END_READ_JSON("TimeStamp") +} + +bool JsonReader::read_epoch_nanos(TimeStamp &value) const { + int64_t nanos; + if (!read(nanos)) { + return false; + } + value = TimeStamp::from_nanos(nanos); + return true; +} + +bool JsonReader::read_epoch_micros(TimeStamp &value) const { + int64_t micros; + if (!read(micros)) { + return false; + } + value = TimeStamp::from_micros(micros); + return true; +} + +bool JsonReader::read_epoch_millis(TimeStamp &value) const { + int64_t millis; + if (!read(millis)) { + return false; + } + value = TimeStamp::from_millis(millis); + return true; +} + +bool JsonReader::read_optional_epoch_millis( + std::optional &result) const { + if (json_ == nullptr) { + return false; + } + if (json_->is_null()) { + result = std::nullopt; + return true; + } + return read_epoch_millis(result.emplace()); +} + +bool JsonReader::read_optional_epoch_millis( + const std::string &name, std::optional &result) const { + if (!json_ || !json_->is_object()) { + return false; + } + auto it = json_->find(name); + if (it == json_->end()) { + return false; + } + return JsonReader{&*it}.read_epoch_millis(result.emplace()); +} + +} // namespace bts::io diff --git a/src/lib/io/JsonReader.h b/src/lib/io/JsonReader.h new file mode 100644 index 000000000..ef687d3ab --- /dev/null +++ b/src/lib/io/JsonReader.h @@ -0,0 +1,291 @@ +#pragma once +#include "lib/core/TimeStamp.h" +#include "lib/utils/bts_assert.h" + +#include +#include +#include +#include + +namespace bts::io { + +class JsonReader { + public: + explicit JsonReader(const nlohmann::json *json); + + JsonReader(const nlohmann::json *json, const std::string &name); + + static bool try_parse( + const std::string &str, + nlohmann::json &result, + std::string *err = nullptr); + + bool read(const std::string &name, std::string &result) const; + bool read(const std::string &name, std::int32_t &result) const; + bool read(const std::string &name, std::uint32_t &result) const; + bool read(const std::string &name, std::int64_t &result) const; + bool read(const std::string &name, std::uint64_t &result) const; + bool read(const std::string &name, float &result) const; + bool read(const std::string &name, double &result) const; + bool read(const std::string &name, nlohmann::json &result) const; + bool read(const std::string &name, core::TimeStamp &result) const; + bool read_iso(const std::string &name, core::TimeStamp &result) const; + bool read_epoch_nanos( + const std::string &name, core::TimeStamp &result) const; + bool read_epoch_micros( + const std::string &name, core::TimeStamp &result) const; + bool read_epoch_millis( + const std::string &name, core::TimeStamp &result) const; + + template + bool read(const std::string &name, T &result) const; + + template + bool read_one_or_more( + const std::string &name, + std::vector &result, + bool append = true) const; + + template + T read(const std::string &name) const; + + bool read(std::string &result) const; + bool read(std::int32_t &result) const; + bool read(std::uint32_t &result) const; + bool read(std::int64_t &result) const; + bool read(std::uint64_t &result) const; + bool read(float &result) const; + bool read(double &result) const; + bool read(nlohmann::json &result) const; + bool read(core::TimeStamp &result) const; + bool read_iso(core::TimeStamp &result) const; + bool read_epoch_nanos(core::TimeStamp &result) const; + bool read_epoch_micros(core::TimeStamp &result) const; + bool read_epoch_millis(core::TimeStamp &result) const; + + template + bool read_one_or_more(std::vector &result, bool append = true) const; + + template + static bool read_flat( + const std::vector &items, std::vector &result); + + bool read_flat(std::vector &result) const; + + template + bool read_optional(std::optional &result) const; + + template + bool read_optional(const std::string &name, std::optional &result) const; + + bool read_optional_epoch_millis( + std::optional &result) const; + bool read_optional_epoch_millis( + const std::string &name, std::optional &result) const; + + template + bool read(T &result) const; + + template + T read() const; + + /** + * @brief Reads a json field into \p result + * + * @details If the read fails, assigns \p default_value to \p result + * @return True if the read succeeds, otherwise false + */ + template + bool read_or(const std::string &name, T &result, T default_value) const; + + private: + const nlohmann::json *json_; + + bool parse_time_stamp_( + const std::string &str, core::TimeStamp &result) const; + bool parse_iso_time_stamp_( + const std::string &str, core::TimeStamp &result) const; + bool parse_epoch_nanos_time_stamp_( + const std::string &str, core::TimeStamp &result) const; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +#define CATCH_PARSE_ERRORS(type_name) \ + catch (const std::exception &e) { \ + LOG(Gen, Diag, "Failed to parse json to '{}'. Exception: {}", \ + std::string{type_name}, std::string{e.what()}); \ + } \ + catch (...) { \ + bts_assert_msgf( \ + false, "Failed to parse json to '{}'. Exception: unknown", \ + std::string{type_name}); \ + } + +template +inline bool JsonReader::read(const std::string &name, T &result) const { + if (!json_ || !json_->is_object()) { + return false; + } + auto it = json_->find(name); + if (it == json_->end()) { + return false; + } + it->get_to(result); + return true; +} + +template +inline bool JsonReader::read_one_or_more( + const std::string &name, std::vector &result, bool append) const { + if (!json_ || !json_->is_object()) { + return false; + } + auto it = json_->find(name); + if (it == json_->end()) { + return false; + } + try { + switch (it->type()) { + case nlohmann::json::value_t::array: + if (append) { + for (const auto &item : it.value()) { + item.get_to(result.emplace_back()); + } + } else { + it.value().get_to(result); + } + return true; + default: + if (append) { + it.value().get_to(result.emplace_back()); + } else { + if (result.empty()) { + result.emplace_back(); + } + it.value().get_to(result.at(0)); + result.erase(result.begin() + 1, result.end()); + } + return true; + } + } + CATCH_PARSE_ERRORS(typeid(T).name()) + return false; +} + +template +inline bool JsonReader::read_one_or_more( + std::vector &result, bool append) const { + if (!json_) { + return false; + } + try { + switch (json_->type()) { + case nlohmann::json::value_t::array: + if (append) { + for (const auto &item : json_->array()) { + item.get_to(result.emplace_back()); + } + } else { + json_->get_to(result); + } + return true; + default: + if (append) { + json_->get_to(result.emplace_back()); + } else { + if (result.empty()) { + result.emplace_back(); + } + json_->get_to(result.at(0)); + result.erase(result.begin() + 1, result.end()); + } + return true; + } + } + CATCH_PARSE_ERRORS(typeid(T).name()); + return false; +} + +template +inline bool JsonReader::read(T &result) const { + try { + json_->get_to(result); + return true; + } catch (...) { + const auto j_str = json_->dump(2); + LOG(md, Error, "Exception parsing json:\n{}", j_str); + return false; + } +} + +template +inline T JsonReader::read() const { + T result; + read(result); + return result; +} + +template +inline bool JsonReader::read_optional(std::optional &result) const { + if (json_ == nullptr) { + return false; + } + if (json_->is_null()) { + result = std::nullopt; + return true; + } + return read(result.emplace()); +} + +template +inline bool JsonReader::read_optional( + const std::string &name, std::optional &result) const { + if (!json_ || !json_->is_object()) { + return false; + } + auto it = json_->find(name); + if (it == json_->end()) { + return false; + } + return JsonReader{&it.value()}.read_optional(result); +} + +template +inline T JsonReader::read(const std::string &name) const { + bts_assert(json_ && json_->is_object()); + auto it = json_->find(name); + bts_assert_msgf(it != json_->end(), "json doesn't contain field '{}'", name); + return JsonReader{&it.value()}.read(); +} + +template +inline bool JsonReader::read_or( + const std::string &name, T &result, T default_value) const { + if (read(name, result)) { + return true; + } + result = default_value; + return false; +} + +template +bool JsonReader::read_flat( + const std::vector &items, std::vector &result) { + std::vector flat_js; + for (const auto &item_j : items) { + if (!JsonReader{&item_j}.read_flat(flat_js)) { + return false; + } + } + for (const auto &el_j : flat_js) { + JsonReader{&el_j}.read(result.emplace_back()); + } + return true; +} + +#undef CATCH_PARSE_ERRORS + +} // namespace bts::io