From 6cf4200bad648def00db075dec0c3083caeb226b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sun, 29 Oct 2023 15:15:09 -0400 Subject: [PATCH] feat: Add cme_websocket connector code --- CMakeLists.txt | 9 +- CMakePresets.json | 5 - src/lib/md/cme_websocket/CmeOauthConfig.h | 44 ++ src/lib/md/cme_websocket/CmeOauthResponse.h | 26 + .../CmeWebsocketOAuthRequestor.h | 35 ++ .../CmeWebsocketSubscription.cpp | 60 ++ .../cme_websocket/CmeWebsocketSubscription.h | 17 + src/lib_bts-http-client/CMakeLists.txt | 26 + .../bts-http-client/curl_requestor.h | 81 +++ .../bts-http-client/http_client.cpp | 28 + .../bts-http-client/http_client.h | 14 + src/lib_bts-http/CMakeLists.txt | 10 + src/lib_bts-http/bts-http/QueryParams.cpp | 18 + src/lib_bts-http/bts-http/QueryParams.h | 18 + src/lib_bts-http/bts-http/URL.cpp | 184 ++++++ src/lib_bts-http/bts-http/URL.h | 103 ++++ src/lib_bts-http/bts-http/http_headers.h | 61 ++ src/lib_bts-http/bts-http/http_request.h | 91 +++ src/lib_bts-http/bts-http/http_response.h | 107 ++++ src/lib_bts-http/bts-http/oauth_response.h | 26 + src/lib_bts-json/CMakeLists.txt | 31 + src/lib_bts-json/bts-json/JsonDumper.h | 48 ++ src/lib_bts-json/bts-json/JsonReader.cpp | 519 +++++++++++++++++ src/lib_bts-json/bts-json/JsonReader.h | 293 ++++++++++ src/lib_bts-system/CMakeLists.txt | 12 + src/lib_bts-system/bts-system/ALWAYS_INLINE.h | 3 + src/lib_bts-system/bts-system/EnumMacros.h | 124 ++++ src/lib_bts-system/bts-system/FMT_IMPL.h | 65 +++ .../bts-system/FlatUnorderedMap.h | 157 +++++ src/lib_bts-system/bts-system/HashPhaser.h | 224 +++++++ .../bts-system/StringSplitter.h | 81 +++ src/lib_bts-system/bts-system/TimeSpan.cpp | 26 + src/lib_bts-system/bts-system/TimeSpan.h | 134 +++++ src/lib_bts-system/bts-system/TimeStamp.cpp | 58 ++ src/lib_bts-system/bts-system/TimeStamp.h | 212 +++++++ src/lib_bts-system/bts-system/TryCatch.h | 28 + src/lib_bts-system/bts-system/UNUSED.h | 4 + src/lib_bts-system/bts-system/bts_assert.h | 46 ++ src/lib_bts-system/bts-system/bts_debugger.h | 43 ++ src/lib_bts-system/bts-system/datetime.cpp | 546 ++++++++++++++++++ src/lib_bts-system/bts-system/datetime.h | 168 ++++++ src/lib_bts-system/bts-system/logging.h | 10 + .../bts-system/table_formatter.h | 176 ++++++ 43 files changed, 3964 insertions(+), 7 deletions(-) create mode 100644 src/lib/md/cme_websocket/CmeOauthConfig.h create mode 100644 src/lib/md/cme_websocket/CmeOauthResponse.h create mode 100644 src/lib/md/cme_websocket/CmeWebsocketOAuthRequestor.h create mode 100644 src/lib/md/cme_websocket/CmeWebsocketSubscription.cpp create mode 100644 src/lib/md/cme_websocket/CmeWebsocketSubscription.h create mode 100644 src/lib_bts-http-client/CMakeLists.txt create mode 100644 src/lib_bts-http-client/bts-http-client/curl_requestor.h create mode 100644 src/lib_bts-http-client/bts-http-client/http_client.cpp create mode 100644 src/lib_bts-http-client/bts-http-client/http_client.h create mode 100644 src/lib_bts-http/CMakeLists.txt create mode 100644 src/lib_bts-http/bts-http/QueryParams.cpp create mode 100644 src/lib_bts-http/bts-http/QueryParams.h create mode 100644 src/lib_bts-http/bts-http/URL.cpp create mode 100644 src/lib_bts-http/bts-http/URL.h create mode 100644 src/lib_bts-http/bts-http/http_headers.h create mode 100644 src/lib_bts-http/bts-http/http_request.h create mode 100644 src/lib_bts-http/bts-http/http_response.h create mode 100644 src/lib_bts-http/bts-http/oauth_response.h create mode 100644 src/lib_bts-json/CMakeLists.txt create mode 100644 src/lib_bts-json/bts-json/JsonDumper.h create mode 100644 src/lib_bts-json/bts-json/JsonReader.cpp create mode 100644 src/lib_bts-json/bts-json/JsonReader.h create mode 100644 src/lib_bts-system/CMakeLists.txt create mode 100644 src/lib_bts-system/bts-system/ALWAYS_INLINE.h create mode 100644 src/lib_bts-system/bts-system/EnumMacros.h create mode 100644 src/lib_bts-system/bts-system/FMT_IMPL.h create mode 100644 src/lib_bts-system/bts-system/FlatUnorderedMap.h create mode 100644 src/lib_bts-system/bts-system/HashPhaser.h create mode 100644 src/lib_bts-system/bts-system/StringSplitter.h create mode 100644 src/lib_bts-system/bts-system/TimeSpan.cpp create mode 100644 src/lib_bts-system/bts-system/TimeSpan.h create mode 100644 src/lib_bts-system/bts-system/TimeStamp.cpp create mode 100644 src/lib_bts-system/bts-system/TimeStamp.h create mode 100644 src/lib_bts-system/bts-system/TryCatch.h create mode 100644 src/lib_bts-system/bts-system/UNUSED.h create mode 100644 src/lib_bts-system/bts-system/bts_assert.h create mode 100644 src/lib_bts-system/bts-system/bts_debugger.h create mode 100644 src/lib_bts-system/bts-system/datetime.cpp create mode 100644 src/lib_bts-system/bts-system/datetime.h create mode 100644 src/lib_bts-system/bts-system/logging.h create mode 100644 src/lib_bts-system/bts-system/table_formatter.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bde1584f..b89f046c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,8 +42,8 @@ if(WIN32) add_subdirectory(src/lib) else() # make sparkd look for libspark.so in its directory - set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) - set(CMAKE_INSTALL_RPATH ".") + # set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) + # set(CMAKE_INSTALL_RPATH ".") set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/install") set(SPARK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/$") @@ -112,6 +112,11 @@ else() # subdirs if (BUILD_SPARKD) + add_subdirectory(src/lib_bts-system) + add_subdirectory(src/lib_bts-json) + add_subdirectory(src/lib_bts-http) + add_subdirectory(src/lib_bts-http-client) + add_subdirectory(src/app_bts-websocket-listener) add_subdirectory(src/server/sparkd) add_subdirectory(src/data_files) endif() diff --git a/CMakePresets.json b/CMakePresets.json index b21a171e4..769faec7d 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -45,12 +45,7 @@ "binaryDir": "${sourceDir}/build/${presetName}", "cacheVariables": { "platform": "", - "CMAKE_BUILD_WITH_INSTALL_RPATH": { - "type": "BOOL", - "value": "ON" - }, "CMAKE_CXX_STANDARD": "20", - "CMAKE_INSTALL_RPATH": ".", "CMAKE_INSTALL_PREFIX": "${sourceDir}/install/${presetName}/sparkd", "CMAKE_EXE_LINKER_FLAGS": "-Wno-undef", "EXTERNALS_ROOT": "${sourceDir}/externals/linux", diff --git a/src/lib/md/cme_websocket/CmeOauthConfig.h b/src/lib/md/cme_websocket/CmeOauthConfig.h new file mode 100644 index 000000000..a313acb8c --- /dev/null +++ b/src/lib/md/cme_websocket/CmeOauthConfig.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include + +#include "lib/io/URL.h" + +namespace bts::md::cme_websocket { + +class CmeOauthConfig { + public: + std::string protocol{"https"}; + std::string host; + std::uint16_t port{443}; + std::string endpoint; + std::string client_id; + std::string client_secret; + + io::URL get_url() const; + void get_url(io::URL &dest) const; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + CmeOauthConfig, protocol, host, port, endpoint, client_id, client_secret); + +inline io::URL CmeOauthConfig::get_url() const { + io::URL url; + get_url(url); + return url; +} + +inline void CmeOauthConfig::get_url(io::URL &dest) const { + dest.fill_host(host); + dest.fill_path(endpoint); + dest.fill_port(port); + dest.fill_protocol("https"); +} + +} // namespace bts::websocket diff --git a/src/lib/md/cme_websocket/CmeOauthResponse.h b/src/lib/md/cme_websocket/CmeOauthResponse.h new file mode 100644 index 000000000..86357dc90 --- /dev/null +++ b/src/lib/md/cme_websocket/CmeOauthResponse.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +// { +// "access_token": +// "eyJhbGciOiJSUzI1NiIsImtpZCI6IjF0al9MTVFrT2pGVzJWQTZvdkZzOEZHUWViMCIsInBpLmF0bSI6IjEifQ.eyJzY29wZSI6W10sImNsaWVudF9pZCI6ImFwaV90aGlua2FscGhhMzIxIiwiYXVkIjoiTnIiLCJqdGkiOiJ5bzlWZUdOMWxudzBXNHhCIiwic3ViIjoiYXBpX3RoaW5rYWxwaGEzMjEiLCJleHAiOjE2OTc1Nzg2NjJ9.HO-UzBvYcdHH6EP0WbmKNbwpZWUF3UbScUQ6bEF4iZqzPyZ1eFLS20VSDULbUpTc_Kl0g9IltJL4ZhnhRqoAX_cWGpqDCDI4wk-04NrcU0-I7YZuNXpDaTwJLChTASQX0PXmmGYDascdem-8FAEp3EgRMrQPXug4FOfp1aKXlgmB5U0Q5lKbZsy_52n56N928iuieYH63JhWRFG0wvleXeyeDDKJNmizKLpGWuBPRwyUYhZWFNQCxbla2TwDTXBWLlO_eZyPq16SS_NKVssTarmY8ohAwuD7N_D7mQbWcbeggks1B9r2w66DuKotRzIrSuhK4VlbHj94FLyqE2oVYw", +// "token_type": "Bearer", +// "expires_in": 1799 +// } + +namespace bts::md::cme_websocket { + +class CmeOauthResponse { + public: + std::string access_token; + std::string token_type; + std::int64_t expires_in; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + CmeOauthResponse, access_token, token_type, expires_in); + +} // namespace bts::md::cme_websocket diff --git a/src/lib/md/cme_websocket/CmeWebsocketOAuthRequestor.h b/src/lib/md/cme_websocket/CmeWebsocketOAuthRequestor.h new file mode 100644 index 000000000..b2936c2ce --- /dev/null +++ b/src/lib/md/cme_websocket/CmeWebsocketOAuthRequestor.h @@ -0,0 +1,35 @@ +#include "CmeOauthConfig.h" +#include "CmeOauthResponse.h" +#include "lib/io/JsonDumper.h" +#include "lib/io/http/HTTPRequest.h" +#include "lib/io/http/HTTPClient.h" + +namespace bts::md::cme_websocket { + +class CmeWebsocketOauthRequestor { + public: + using HTTPRequest = io::http::HTTPRequest; + using HTTPClient = io::http::HTTPClient; + + static HTTPRequest build_oauth_request(const CmeOauthConfig &cfg) { + HTTPRequest http_req; + http_req.url() = cfg.get_url(); + io::QueryParams qp; + qp["grant_type"] = "client_credentials"; + qp["client_id"] = cfg.client_id; + qp["client_secret"] = cfg.client_secret; + http_req.body() = qp.to_string(); + return http_req; + } + + static CmeOauthResponse request_oauth_token(const CmeOauthConfig &cfg) { + HTTPRequest req = build_oauth_request(cfg); + LOG(Gen, Info, "Requesting oauth:\n{}", io::JsonDumper::dump(req)); + HTTPClient client; + HTTPResponse res = client.post(req); + LOG(Gen, Info, "Received oauth response:\n{}", io::JsonDumper::dump(res)); + const auto j = nlohmann::json::parse(res.body()); + return io::JsonReader{&j}.read(); + } +}; +} // namespace bts::md::cme_websocket diff --git a/src/lib/md/cme_websocket/CmeWebsocketSubscription.cpp b/src/lib/md/cme_websocket/CmeWebsocketSubscription.cpp new file mode 100644 index 000000000..cd46ae340 --- /dev/null +++ b/src/lib/md/cme_websocket/CmeWebsocketSubscription.cpp @@ -0,0 +1,60 @@ +#include "CmeWebsocketSubscription.h" + +#include +#include +#include +#include +#include +#include + +namespace bts::md::cme_websocket { + +struct CmeWebsocketSubscription::PImpl { + // TODO + void connect() {} + + static void on_open(websocketpp::connection_hdl hdl) { + (void)(hdl); + // Connection opened, you can send WebSocket messages here + } + + int run() { + websocketpp::client client; + + // Set up connection settings + std::string server_uri = "wss://example.com/websocket_endpoint"; + + client.init_asio(); + client.set_open_handler(&on_open); + + // Define the headers, including the Authorization header + websocketpp::lib::error_code ec; + websocketpp::client::connection_ptr + con = client.get_connection(server_uri, ec); + + if (!ec) { + con->append_header("Authorization", "Bearer your_token_here"); + + // Connect to the server + client.connect(con); + + // Start the WebSocket++ event loop + client.run(); + } else { + std::cerr << "Error creating the connection: " << ec.message() + << std::endl; + } + + return 0; + } +}; + +CmeWebsocketSubscription::CmeWebsocketSubscription() + : me_(std::make_unique()) { + // TODO +} + +CmeWebsocketSubscription::~CmeWebsocketSubscription() = default; + +int CmeWebsocketSubscription::run() { return me_->run(); } +} // namespace bts::md::cme_websocket diff --git a/src/lib/md/cme_websocket/CmeWebsocketSubscription.h b/src/lib/md/cme_websocket/CmeWebsocketSubscription.h new file mode 100644 index 000000000..d9782e59a --- /dev/null +++ b/src/lib/md/cme_websocket/CmeWebsocketSubscription.h @@ -0,0 +1,17 @@ +#pragma once +#include + +namespace bts::md::cme_websocket { + +class CmeWebsocketSubscription { + public: + CmeWebsocketSubscription(); + ~CmeWebsocketSubscription(); + + int run(); + private: + struct PImpl; + std::unique_ptr me_; +}; + +} diff --git a/src/lib_bts-http-client/CMakeLists.txt b/src/lib_bts-http-client/CMakeLists.txt new file mode 100644 index 000000000..6571a8c4f --- /dev/null +++ b/src/lib_bts-http-client/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.23) +project(bts-http-client) +include(FetchContent) +include (GNUInstallDirs) + +file( + GLOB_RECURSE ${PROJECT_NAME}_Sources + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_NAME}/*.cpp) +add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${PROJECT_NAME} PUBLIC bts-http bts-json bts-system) + +# curlpp +FetchContent_Declare( + curlpp + GIT_REPOSITORY git@github.com:jpbarrette/curlpp.git + GIT_TAG v0.8.1 + # CMAKE_ARGS + SYSTEM + OVERRIDE_FIND_PACKAGE +) +FetchContent_MakeAvailable(curlpp) +target_include_directories(${PROJECT_NAME} PUBLIC ${curlpp_SOURCE_DIR}/include) +target_link_directories(${PROJECT_NAME} PUBLIC ${curlpp_BINARY_DIR}) +# target_link_libraries(${PROJECT_NAME} PUBLIC curlpp) diff --git a/src/lib_bts-http-client/bts-http-client/curl_requestor.h b/src/lib_bts-http-client/bts-http-client/curl_requestor.h new file mode 100644 index 000000000..3c558d761 --- /dev/null +++ b/src/lib_bts-http-client/bts-http-client/curl_requestor.h @@ -0,0 +1,81 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "bts-http/http_request.h" +#include "bts-http/http_response.h" +#include "bts-system/UNUSED.h" +#include "bts-system/logging.h" + +namespace bts::http { + +/** + * @brief Sets boilerplate curl opts and callbacks for http requests. + */ +class CurlRequestor { + public: + explicit CurlRequestor(const HTTPRequest &request) + : request_(std::make_unique()) { + request_->setOpt(request.url); + std::list req_hdrs; + for (const auto &[k, v] : request.headers) { + req_hdrs.push_back(fmt::format("{}:{}", k, v)); + } + request_->setOpt(req_hdrs); + request_->setOpt(true); + } + + CurlRequestor(const CurlRequestor &) = delete; + CurlRequestor(CurlRequestor &&) noexcept = default; + CurlRequestor &operator=(const CurlRequestor &) = delete; + CurlRequestor &operator=(CurlRequestor &&) noexcept = default; + + curlpp::Easy &request(); + const curlpp::Easy &request() const; + + void perform(HTTPResponse &response); + + private: + std::unique_ptr request_; + std::string errors_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline curlpp::Easy &CurlRequestor::request() { return *request_; } +inline const curlpp::Easy &CurlRequestor::request() const { return *request_; } + +inline void CurlRequestor::perform(HTTPResponse &response) { + request_->setOpt( + [&response](char *cstr, size_t sz, size_t n) { + std::string str{cstr, sz * n}; + const auto colon_idx = str.find(':'); + if (colon_idx >= str.size()) { + response.headers()[str] = {}; + } else { + response.headers()[str.substr(0, colon_idx)] = + str.substr(colon_idx + 1, str.size() - colon_idx - 1); + } + return sz * n; + }); + request_->setOpt( + [&response](char *cstr, size_t sz, size_t n) { + response.body().append(cstr, sz * n); + return sz * n; + }); + try { + request_->perform(); + } catch (const std::exception &ex) { + LOG(Gen, Error, "CurlRequestor::perform threw an exception: {}", + std::string{ex.what()}); + throw; + } +} + +} // namespace bts::http diff --git a/src/lib_bts-http-client/bts-http-client/http_client.cpp b/src/lib_bts-http-client/bts-http-client/http_client.cpp new file mode 100644 index 000000000..1cfa00a01 --- /dev/null +++ b/src/lib_bts-http-client/bts-http-client/http_client.cpp @@ -0,0 +1,28 @@ +#include "http_client.h" + +#include + +#include "bts-http-client/curl_requestor.h" +#include "bts-http/http_request.h" +#include "bts-http/http_response.h" + +namespace bts::http { + +HTTPResponse HTTPClient::get(const HTTPRequest &request) { + CurlRequestor requestor{request}; + requestor.request().setOpt(true); + HTTPResponse response; + requestor.perform(response); + return response; +} + +HTTPResponse HTTPClient::post(const HTTPRequest &request) { + CurlRequestor requestor{request}; + requestor.request().setOpt(true); + requestor.request().setOpt(request.body); + HTTPResponse response; + requestor.perform(response); + return response; +} + +} // namespace bts::http diff --git a/src/lib_bts-http-client/bts-http-client/http_client.h b/src/lib_bts-http-client/bts-http-client/http_client.h new file mode 100644 index 000000000..cf0192578 --- /dev/null +++ b/src/lib_bts-http-client/bts-http-client/http_client.h @@ -0,0 +1,14 @@ +#pragma once + +namespace bts::http { + +class HTTPRequest; +class HTTPResponse; + +class HTTPClient { + public: + HTTPResponse get(const HTTPRequest &request); + HTTPResponse post(const HTTPRequest &request); +}; + +} // namespace bts::http diff --git a/src/lib_bts-http/CMakeLists.txt b/src/lib_bts-http/CMakeLists.txt new file mode 100644 index 000000000..2483d0d31 --- /dev/null +++ b/src/lib_bts-http/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.23) +project(bts-http) + +file( + GLOB_RECURSE ${PROJECT_NAME}_Sources + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_NAME}/*.cpp) +add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(${PROJECT_NAME} PUBLIC bts-json bts-system) diff --git a/src/lib_bts-http/bts-http/QueryParams.cpp b/src/lib_bts-http/bts-http/QueryParams.cpp new file mode 100644 index 000000000..e079b8056 --- /dev/null +++ b/src/lib_bts-http/bts-http/QueryParams.cpp @@ -0,0 +1,18 @@ +#include "QueryParams.h" + +namespace bts::io { + +std::string QueryParams::to_string() const { + std::string rv; + auto it = begin(); + if (it == end()) { + return ""; + } + rv.append(it->first + "=" + it->second); + while (++it != end()) { + rv.append("&" + it->first + "=" + it->second); + } + return rv; +} + +} // namespace bts::io diff --git a/src/lib_bts-http/bts-http/QueryParams.h b/src/lib_bts-http/bts-http/QueryParams.h new file mode 100644 index 000000000..276f494be --- /dev/null +++ b/src/lib_bts-http/bts-http/QueryParams.h @@ -0,0 +1,18 @@ +#pragma once +#include + +#include "bts-system/FMT_IMPL.h" +#include "bts-system/FlatUnorderedMap.h" + + +namespace bts::io { + +class QueryParams + : public collections::FlatUnorderedMap { + public: + std::string to_string() const; +}; + +} // namespace bts::io + +FMT_IMPL(bts::io::QueryParams, p, "{}", p.to_string()); diff --git a/src/lib_bts-http/bts-http/URL.cpp b/src/lib_bts-http/bts-http/URL.cpp new file mode 100644 index 000000000..22664d982 --- /dev/null +++ b/src/lib_bts-http/bts-http/URL.cpp @@ -0,0 +1,184 @@ +#include "URL.h" +// #include "lib/api/io/URL.pb.h" + +#include +#include + +namespace bts::io { + +// URL::URL(const bts::api::io::URL &url) +// : protocol_(url.protocol()), host_(url.host()), port_(url.port()), +// path_(url.path()) { +// for (const auto &qp : url.query_params()) { +// query_params_[qp.key()] = qp.value(); +// } +// } +URL URL::from_string(const std::string &url_str) { + // Protocol + URL url; + const char *protocol_separator{"://"}; + std::size_t pos = 0; + std::size_t pos_end = url_str.find(protocol_separator); + if (pos_end != std::string::npos) { + url.fill_protocol(url_str.substr(pos, pos_end)); + } + pos = pos_end + strlen(protocol_separator) + 1; + pos_end = url_str.find(':', pos); + if (pos_end != std::string::npos) { + // This chunk is : + url.fill_host(url_str.substr(pos, pos_end)); + pos = pos_end + 1; + pos_end = url_str.find('/', pos); + if (pos_end == std::string::npos) { + pos_end = url_str.size(); + } + std::int32_t port = std::stoi(url_str.substr(pos, pos_end - pos)); + url.fill_port(static_cast(port)); + } else { + // This chunk is + pos_end = url_str.find('/', pos); + if (pos_end == std::string::npos) { + pos_end = url_str.size(); + } + url.fill_host(url_str.substr(pos, pos_end - pos)); + } + // Path + if (pos_end != std::string::npos && pos_end < url_str.size()) { + url.fill_host(url_str.substr(pos, pos_end - pos)); + pos = pos_end + 1; + if (pos < url_str.size()) { + url.fill_path(url_str.substr(pos, url_str.size() - pos)); + } + } else { + url.fill_host(url_str.substr(pos, url_str.size() - pos)); + } + return url; +} + +std::vector> URL::well_known_ports_() { + return std::vector>{ + {"http", 80}, {"https", 443}, {"ws", 80}, + {"wss", 443}, {"ssh", 22}, {"sftp", 22}, + }; +} + +std::uint16_t URL::get_known_port_for_protocol(const std::string &protocol) { + auto wkps = well_known_ports_(); + for (const auto &p : wkps) { + if (p.first == protocol) { + return p.second; + } + } + return 0; +} + +std::string URL::get_known_protocol_for_port(std::uint16_t port) { + auto wkps = well_known_ports_(); + for (const auto &p : wkps) { + if (p.second == port) { + return p.first; + } + } + return ""; +} + +std::string &URL::protocol() { return protocol_; } +const std::string &URL::protocol() const { return protocol_; } + +std::string &URL::host() { return host_; } +const std::string &URL::host() const { return host_; } + +std::uint16_t &URL::port() { return port_; } +std::uint16_t URL::port() const { return port_; } + +std::string &URL::path() { return path_; } +const std::string &URL::path() const { return path_; } + +QueryParams &URL::query_params() { return query_params_; } + +const QueryParams &URL::query_params() const { return query_params_; } + +std::string URL::to_string() const { + auto rv = protocol() + "://"; + if (host().empty()) { + rv.append("localhost"); + } else { + rv.append(host()); + } + if (port_ != 0) { + rv.append(":" + std::to_string(port_)); + } + if (!path_.empty()) { + rv.append("/" + path_); + } + if (!query_params_.empty()) { + rv.append("?"); + rv.append(query_params_.to_string()); + } + return rv; +} + +std::ostream &operator<<(std::ostream &out, const URL &url) noexcept { + out << url.to_string(); + return out; +} + +void URL::fill_protocol(const std::string &protocol) { + if (protocol_.empty()) { + protocol_ = protocol; + } +} + +void URL::fill_host(const std::string &host) { + if (host_.empty()) { + host_ = host; + } +} + +void URL::fill_port(std::uint16_t port) { + if (port_ == 0) { + port_ = port; + } +} + +void URL::fill_path(const std::string &path) { + if (path_.empty()) { + path_ = path; + } +} + +void URL::fill_query_params( + const QueryParams &query_params, bool add_missing_params) { + if (query_params_.empty()) { + query_params_ = query_params; + } else if (add_missing_params) { + for (const auto &[k, v] : query_params) { + query_params_.try_emplace(k, v); + } + } +} + +void URL::fill_with(const URL &url) { + fill_path(url.path()); + fill_host(url.host()); + fill_protocol(url.protocol()); + fill_port(url.port()); + fill_query_params(url.query_params()); +} + +bool operator==(const URL &lhs, const URL &rhs) noexcept { + bool rv = lhs.protocol() == rhs.protocol() && lhs.host() == rhs.host() && + lhs.path() == rhs.path() && lhs.port() == rhs.port(); + if (rv) { + rv = std::equal( + lhs.query_params().begin(), lhs.query_params().end(), + rhs.query_params().begin(), rhs.query_params().end()); + } + return rv; +} + +bool operator!=(const URL &lhs, const URL &rhs) noexcept { + return !(lhs == rhs); +} + +} // namespace bts::io diff --git a/src/lib_bts-http/bts-http/URL.h b/src/lib_bts-http/bts-http/URL.h new file mode 100644 index 000000000..304b9e0d2 --- /dev/null +++ b/src/lib_bts-http/bts-http/URL.h @@ -0,0 +1,103 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bts-http/QueryParams.h" +#include "bts-system/FMT_IMPL.h" +#include "bts-system/HashPhaser.h" + +namespace bts::api::io { +// class URL; +} + +namespace bts::io { + +class URL { + public: + URL() = default; + // explicit URL(const bts::api::io::URL &url); + + URL(const URL &) = default; + URL(URL &&) noexcept = default; + URL &operator=(const URL &) = default; + URL &operator=(URL &&) noexcept = default; + + std::string &protocol(); + const std::string &protocol() const; + + std::string &host(); + const std::string &host() const; + + std::uint16_t &port(); + std::uint16_t port() const; + + std::string &path(); + const std::string &path() const; + + QueryParams &query_params(); + const QueryParams &query_params() const; + + std::string to_string() const; + + void fill_protocol(const std::string &protocol); + void fill_host(const std::string &host); + void fill_path(const std::string &path); + void fill_port(std::uint16_t port); + void fill_query_params( + const QueryParams &query_params, bool add_missing_params = false); + void fill_with(const URL &url); + + static std::uint16_t get_known_port_for_protocol( + const std::string &protocol); + static std::string get_known_protocol_for_port(std::uint16_t port); + static URL from_string(const std::string &str); + + private: + std::string protocol_; + std::string host_; + std::uint16_t port_{0}; + std::string path_; + QueryParams query_params_; + + static std::vector> + well_known_ports_(); +}; + +bool operator==(const URL &lhs, const URL &rhs) noexcept; +bool operator!=(const URL &lhs, const URL &rhs) noexcept; + +std::ostream &operator<<(std::ostream &out, const URL &url) noexcept; + +} // namespace bts::io + +namespace std { + +template <> +struct hash { + std::size_t operator()(const bts::io::URL &url) const; +}; + +} // namespace std + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +namespace std { + +inline std::size_t hash::operator()( + const bts::io::URL &url) const { + return bts::core::HashPhaser::auto_phase( + url.protocol(), url.host(), url.port(), url.path(), + url.query_params().size()); +} + +} // namespace std + +FMT_IMPL(bts::io::URL, u, "{}", u.to_string()); diff --git a/src/lib_bts-http/bts-http/http_headers.h b/src/lib_bts-http/bts-http/http_headers.h new file mode 100644 index 000000000..94c0f37ab --- /dev/null +++ b/src/lib_bts-http/bts-http/http_headers.h @@ -0,0 +1,61 @@ +#pragma once +#include "bts-system/FMT_IMPL.h" +#include "bts-system/FlatUnorderedMap.h" +#include "bts-system/StringSplitter.h" + + +namespace bts::http { + +class HTTPHeaders + : public collections::FlatUnorderedMap { + public: + HTTPHeaders() = default; + explicit HTTPHeaders(const std::string &header_str); + + HTTPHeaders(const HTTPHeaders &) noexcept = default; + HTTPHeaders(HTTPHeaders &&) noexcept = default; + HTTPHeaders &operator=(const HTTPHeaders &) noexcept = default; + HTTPHeaders &operator=(HTTPHeaders &&) noexcept = default; + + std::string to_string() const; +}; + +std::ostream &operator<<( + std::ostream &out, const HTTPHeaders &headers) noexcept; + +inline HTTPHeaders::HTTPHeaders(const std::string &str) { + auto item_strs = system::StringSplitter::split(str, "\n"); + for (const auto &item_str : item_strs) { + auto kv = system::StringSplitter::split(item_str, ":"); + if (kv.empty()) { + try_emplace("", ""); + continue; + } + if (kv.size() == 1) { + try_emplace(kv[0], ""); + continue; + } + try_emplace(kv[0], kv[1]); + } +} + +inline std::string HTTPHeaders::to_string() const { + auto it = begin(); + if (it == end()) { + return ""; + } + std::string rv{it->first + ": " + it->second}; + while (++it != end()) { + rv.append("\n" + it->first + ": " + it->second); + } + return rv; +} + +inline std::ostream &operator<<( + std::ostream &out, const HTTPHeaders &headers) noexcept { + out << headers.to_string(); + return out; +} +} // namespace bts::http + +FMT_IMPL(bts::http::HTTPHeaders, h, "{}", h.to_string()); diff --git a/src/lib_bts-http/bts-http/http_request.h b/src/lib_bts-http/bts-http/http_request.h new file mode 100644 index 000000000..e36342989 --- /dev/null +++ b/src/lib_bts-http/bts-http/http_request.h @@ -0,0 +1,91 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +#include "bts-http/http_response.h" +#include "bts-system/FMT_IMPL.h" +#include "bts-system/StringSplitter.h" + +namespace bts::http { + +class HTTPRequest { + public: + std::string url; + std::string body; + std::map headers; + + std::string get_headers_string() const; + void set_headers_string(const std::string &hdrs_str); + + HTTPResponse response; +}; + +void to_json(nlohmann::json &j, const HTTPRequest &req); +void from_json(const nlohmann::json &j, HTTPRequest &req); + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline void HTTPRequest::set_headers_string(const std::string &hdrs_str) { + headers.clear(); + auto item_strs = system::StringSplitter::split(hdrs_str, "\n"); + for (const auto &item_str : item_strs) { + auto kv = system::StringSplitter::split(item_str, ":"); + switch (kv.size()) { + case 1: + headers.try_emplace(kv[0], ""); + break; + case 2: + headers.try_emplace(kv[0], kv[1]); + break; + default: + throw std::runtime_error{ + fmt::format("Invalid http header '{}'", hdrs_str)}; + } + } +} + +inline std::string HTTPRequest::get_headers_string() const { + std::string rv; + for (const auto &[k, v] : headers) { + if (!rv.empty()) { + rv.append("\n"); + } + if (v.empty()) { + rv.append(k); + } else { + // Don't add space after colon. The spec says to ignore + // leading/trailing whitespace around header values. + // https://www.rfc-editor.org/rfc/rfc7230#section-3.2 + rv.append(k + ":" + v); + } + } + return rv; +} + +inline void to_json(nlohmann::json &j, const HTTPRequest &req) { + j["url"] = req.url; + j["headers"] = req.headers; + j["body"] = req.body; +} + +inline void from_json(const nlohmann::json &j, HTTPRequest &req) { + j.at("url").get_to(req.url); + j.at("headers").get_to(req.headers); + j.at("body").get_to(req.body); +} + +} // namespace bts::http + +FMT_IMPL( + bts::http::HTTPRequest, + r, + "url: {} | headers: {} | body: {}", + r.url, + r.get_headers_string(), + r.body); diff --git a/src/lib_bts-http/bts-http/http_response.h b/src/lib_bts-http/bts-http/http_response.h new file mode 100644 index 000000000..ecd1cdcaf --- /dev/null +++ b/src/lib_bts-http/bts-http/http_response.h @@ -0,0 +1,107 @@ +#pragma once + +#include +#include +#include + +#include "bts-system/FMT_IMPL.h" +#include "http_headers.h" +#include "nlohmann/detail/macro_scope.hpp" + +namespace bts::http { + +class HTTPResponse { + public: + std::string &url(); + const std::string &url() const; + + HTTPHeaders &headers(); + const HTTPHeaders &headers() const; + + std::string &body(); + const std::string &body() const; + + std::string &errors(); + const std::string &errors() const; + + std::int64_t &code(); + std::int64_t code() const; + + double &elapsed(); + double elapsed() const; + + private: + std::string url_; + HTTPHeaders headers_; + std::string body_; + std::string errors_; + std::int64_t code_; + double elapsed_; +}; + +void to_json(nlohmann::json &j, const HTTPResponse &res); +void from_json(const nlohmann::json &j, HTTPResponse &res); + +std::ostream &operator<<( + std::ostream &out, const HTTPResponse &response) noexcept; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline std::string &HTTPResponse::url() { return url_; } +inline const std::string &HTTPResponse::url() const { return url_; } + +inline HTTPHeaders &HTTPResponse::headers() { return headers_; } +inline const HTTPHeaders &HTTPResponse::headers() const { return headers_; } + +inline std::string &HTTPResponse::body() { return body_; } +inline const std::string &HTTPResponse::body() const { return body_; } + +inline std::string &HTTPResponse::errors() { return errors_; } +inline const std::string &HTTPResponse::errors() const { return errors_; } + +inline std::int64_t &HTTPResponse::code() { return code_; } +inline std::int64_t HTTPResponse::code() const { return code_; } + +inline double &HTTPResponse::elapsed() { return elapsed_; } +inline double HTTPResponse::elapsed() const { return elapsed_; } + +inline void to_json(nlohmann::json &j, const HTTPResponse &res) { + j["url"] = res.url(); + j["body"] = res.body(); + j["code"] = res.code(); + j["elapsed"] = res.elapsed(); + j["errors"] = res.errors(); +} + +inline void from_json(const nlohmann::json &j, HTTPResponse &res) { + j.at("url").get_to(res.url()); + j.at("body").get_to(res.body()); + j.at("code").get_to(res.code()); + j.at("elapsed").get_to(res.elapsed()); + j.at("errors").get_to(res.errors()); +} + +inline std::ostream &operator<<( + std::ostream &out, const HTTPResponse &res) noexcept { + out << "**Url**\n" + << res.url() << "\n**Headers**\n" + << res.headers() << "\n**Body**\n" + << res.body() << "\n**Code**\n" + << res.code() << "\n**Elapsed**\n" + << res.elapsed(); + return out; +} + +} // namespace bts::http + +FMT_IMPL( + bts::http::HTTPResponse, + r, + "url: {} | headers: {} | body: {} | code: {} | elapsed: {}", + r.url(), + r.headers(), + r.body(), + r.code(), + r.elapsed()); diff --git a/src/lib_bts-http/bts-http/oauth_response.h b/src/lib_bts-http/bts-http/oauth_response.h new file mode 100644 index 000000000..c8ff91c8f --- /dev/null +++ b/src/lib_bts-http/bts-http/oauth_response.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +// { +// "access_token": +// "eyJhbGciOiJSUzI1NiIsImtpZCI6IjF0al9MTVFrT2pGVzJWQTZvdkZzOEZHUWViMCIsInBpLmF0bSI6IjEifQ.eyJzY29wZSI6W10sImNsaWVudF9pZCI6ImFwaV90aGlua2FscGhhMzIxIiwiYXVkIjoiTnIiLCJqdGkiOiJ5bzlWZUdOMWxudzBXNHhCIiwic3ViIjoiYXBpX3RoaW5rYWxwaGEzMjEiLCJleHAiOjE2OTc1Nzg2NjJ9.HO-UzBvYcdHH6EP0WbmKNbwpZWUF3UbScUQ6bEF4iZqzPyZ1eFLS20VSDULbUpTc_Kl0g9IltJL4ZhnhRqoAX_cWGpqDCDI4wk-04NrcU0-I7YZuNXpDaTwJLChTASQX0PXmmGYDascdem-8FAEp3EgRMrQPXug4FOfp1aKXlgmB5U0Q5lKbZsy_52n56N928iuieYH63JhWRFG0wvleXeyeDDKJNmizKLpGWuBPRwyUYhZWFNQCxbla2TwDTXBWLlO_eZyPq16SS_NKVssTarmY8ohAwuD7N_D7mQbWcbeggks1B9r2w66DuKotRzIrSuhK4VlbHj94FLyqE2oVYw", +// "token_type": "Bearer", +// "expires_in": 1799 +// } + +namespace bts::websocket { + +class OauthResponse { + public: + std::string access_token; + std::string token_type; + std::int64_t expires_in; +}; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE( + OauthResponse, access_token, token_type, expires_in); + +} // namespace bts::websocket diff --git a/src/lib_bts-json/CMakeLists.txt b/src/lib_bts-json/CMakeLists.txt new file mode 100644 index 000000000..e56d71b82 --- /dev/null +++ b/src/lib_bts-json/CMakeLists.txt @@ -0,0 +1,31 @@ +cmake_minimum_required(VERSION 3.23) +project(bts-json) +include(FetchContent) + +file( + GLOB_RECURSE ${PROJECT_NAME}_Sources + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_NAME}/*.cpp) +add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) +target_link_libraries(${PROJECT_NAME} PUBLIC + bts-system + debug fmtd + optimized fmt) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# nlohmann json +FetchContent_Declare( + nlohmann_json + GIT_REPOSITORY git@github.com:nlohmann/json.git + GIT_TAG v3.11.1 + CMAKE_ARGS + -DJSON_BuildTests=OFF + -DJSON_SystemInclude=ON + SYSTEM + OVERRIDE_FIND_PACKAGE +) + +FetchContent_MakeAvailable(nlohmann_json) + +find_package(nlohmann_json REQUIRED) +target_link_libraries(${PROJECT_NAME} PUBLIC nlohmann_json) diff --git a/src/lib_bts-json/bts-json/JsonDumper.h b/src/lib_bts-json/bts-json/JsonDumper.h new file mode 100644 index 000000000..390d7d425 --- /dev/null +++ b/src/lib_bts-json/bts-json/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_bts-json/bts-json/JsonReader.cpp b/src/lib_bts-json/bts-json/JsonReader.cpp new file mode 100644 index 000000000..bb54efc7b --- /dev/null +++ b/src/lib_bts-json/bts-json/JsonReader.cpp @@ -0,0 +1,519 @@ +#include "JsonReader.h" + +#include +#include +#include +#include + +#include "bts-system/TryCatch.h" +#include "bts-system/datetime.h" + +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_bts-json/bts-json/JsonReader.h b/src/lib_bts-json/bts-json/JsonReader.h new file mode 100644 index 000000000..b6e247db5 --- /dev/null +++ b/src/lib_bts-json/bts-json/JsonReader.h @@ -0,0 +1,293 @@ +#pragma once + +#include +#include +#include +#include + +#include "bts-system/TimeStamp.h" +#include "bts-system/bts_assert.h" +#include "bts-system/logging.h" + +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 diff --git a/src/lib_bts-system/CMakeLists.txt b/src/lib_bts-system/CMakeLists.txt new file mode 100644 index 000000000..db5dfee4a --- /dev/null +++ b/src/lib_bts-system/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.23) +project(bts-system) + +file( + GLOB_RECURSE ${PROJECT_NAME}_Sources + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + ${PROJECT_NAME}/*.cpp) +add_library(${PROJECT_NAME} ${${PROJECT_NAME}_Sources}) +target_link_libraries(${PROJECT_NAME} PUBLIC + debug fmtd + optimized fmt) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/src/lib_bts-system/bts-system/ALWAYS_INLINE.h b/src/lib_bts-system/bts-system/ALWAYS_INLINE.h new file mode 100644 index 000000000..a46c0fbc8 --- /dev/null +++ b/src/lib_bts-system/bts-system/ALWAYS_INLINE.h @@ -0,0 +1,3 @@ +#pragma once + +#define ALWAYS_INLINE inline __attribute__((always_inline)) diff --git a/src/lib_bts-system/bts-system/EnumMacros.h b/src/lib_bts-system/bts-system/EnumMacros.h new file mode 100644 index 000000000..a7ee0f629 --- /dev/null +++ b/src/lib_bts-system/bts-system/EnumMacros.h @@ -0,0 +1,124 @@ +#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_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_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 diff --git a/src/lib_bts-system/bts-system/FMT_IMPL.h b/src/lib_bts-system/bts-system/FMT_IMPL.h new file mode 100644 index 000000000..f82a06095 --- /dev/null +++ b/src/lib_bts-system/bts-system/FMT_IMPL.h @@ -0,0 +1,65 @@ +#pragma once + +#include + +#define FMT_IMPL(Type, var_name, ...) \ + template <> \ + struct fmt::formatter { \ + constexpr auto parse(fmt::format_parse_context &ctx) \ + -> decltype(ctx.end()) { \ + return ctx.end(); \ + } \ + \ + template \ + auto format(const Type &var_name, FormatContext &ctx) const \ + -> decltype(ctx.out()) { \ + return fmt::format_to(ctx.out(), __VA_ARGS__); \ + } \ + } + +#define FMT_IMPL_T(Type, var_name, ...) \ + struct fmt::formatter { \ + constexpr auto parse(fmt::format_parse_context &ctx) \ + -> decltype(ctx.end()) { \ + return ctx.end(); \ + } \ + \ + template \ + auto format(const Type &var_name, FormatContext &ctx) const \ + -> decltype(ctx.out()) { \ + return fmt::format_to(ctx.out(), __VA_ARGS__); \ + } \ + } + +#define FMT_IMPL_OSTREAM(T) \ + template <> \ + struct fmt::formatter { \ + constexpr auto parse(fmt::format_parse_context &ctx) \ + -> decltype(ctx.end()) { \ + return ctx.end(); \ + } \ + \ + template \ + auto format(const T &t, FormatContext &ctx) const \ + -> decltype(ctx.out()) { \ + std::stringstream ss; \ + ss << t; \ + return fmt::format_to(ctx.out(), "{}", ss.str()); \ + } \ + } + +#define FMT_IMPL_OSTREAM_T(T, ...) \ + struct fmt::formatter> { \ + constexpr auto parse(fmt::format_parse_context &ctx) \ + -> decltype(ctx.end()) { \ + return ctx.end(); \ + } \ + \ + template \ + auto format(const T<__VA_ARGS__> &t, FormatContext &ctx) const \ + -> decltype(ctx.out()) { \ + std::stringstream ss; \ + ss << t; \ + return fmt::format_to(ctx.out(), "{}", ss.str()); \ + } \ + } diff --git a/src/lib_bts-system/bts-system/FlatUnorderedMap.h b/src/lib_bts-system/bts-system/FlatUnorderedMap.h new file mode 100644 index 000000000..d1227cbba --- /dev/null +++ b/src/lib_bts-system/bts-system/FlatUnorderedMap.h @@ -0,0 +1,157 @@ +#pragma once +#include +#include +#include + +namespace bts::collections { + +template +class FlatUnorderedMap { + public: + using Container = std::vector>; + using Iterator = Container::iterator; + using ConstIterator = Container::const_iterator; + using ReverseIterator = Container::reverse_iterator; + using ConstReverseIterator = Container::const_reverse_iterator; + + Value *get(const Key &key); + const Value *get(const Key &key) const; + + template + std::pair try_emplace(const Key &key, Args... args); + + Iterator begin(); + ConstIterator begin() const; + + ReverseIterator rbegin(); + ConstReverseIterator rbegin() const; + + Iterator end(); + ConstIterator end() const; + + ReverseIterator rend(); + ConstReverseIterator rend() const; + + bool empty() const; + + std::size_t size() const; + + Value &operator[](const Key &key); + + private: + std::vector> items_; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +Value *FlatUnorderedMap::get(const Key &key) { + for (auto &qp : items_) { + if (qp.first == key) { + return &qp.second; + } + } + return nullptr; +} + +template +const Value *FlatUnorderedMap::get(const Key &key) const { + for (auto &qp : items_) { + if (qp.first == key) { + return &qp.second; + } + } + return nullptr; +} + +// template +// std::pair::Iterator, bool> +// FlatUnorderedMap::try_emplace(const Key &key) { +// for (auto it = items_.begin(); it != items_.end(); it++) { +// if (it->first == key) { +// return {it, false}; +// } +// } +// return items_.emplace_back(key); +// } + +template +template +std::pair::Iterator, bool> +FlatUnorderedMap::try_emplace(const Key &key, Args... args) { + for (auto it = items_.begin(); it != items_.end(); it++) { + if (it->first == key) { + return {it, false}; + } + } + Value v(args...); + items_.emplace_back(key, std::move(v)); + return {items_.end() - 1, true}; +} + +template +typename FlatUnorderedMap::Iterator +FlatUnorderedMap::begin() { + return items_.begin(); +} + +template +typename FlatUnorderedMap::ConstIterator +FlatUnorderedMap::begin() const { + return items_.begin(); +} + +template +typename FlatUnorderedMap::ReverseIterator +FlatUnorderedMap::rbegin() { + return items_.rbegin(); +} + +template +typename FlatUnorderedMap::ConstReverseIterator +FlatUnorderedMap::rbegin() const { + return items_.rbegin(); +} + +template +typename FlatUnorderedMap::Iterator +FlatUnorderedMap::end() { + return items_.end(); +} + +template +typename FlatUnorderedMap::ConstIterator +FlatUnorderedMap::end() const { + return items_.end(); +} + +template +typename FlatUnorderedMap::ReverseIterator +FlatUnorderedMap::rend() { + return items_.rend(); +} + +template +typename FlatUnorderedMap::ConstReverseIterator +FlatUnorderedMap::rend() const { + return items_.rend(); +} + +template +bool FlatUnorderedMap::empty() const { + return items_.empty(); +} + +template +std::size_t FlatUnorderedMap::size() const { + return items_.size(); +} + +template +Value &FlatUnorderedMap::operator[](const Key &key) { + return try_emplace(key).first->second; +} + +} // namespace bts::collections diff --git a/src/lib_bts-system/bts-system/HashPhaser.h b/src/lib_bts-system/bts-system/HashPhaser.h new file mode 100644 index 000000000..4e06dd37e --- /dev/null +++ b/src/lib_bts-system/bts-system/HashPhaser.h @@ -0,0 +1,224 @@ +#pragma once +#include +#include +#include +#include + +namespace bts::core { + +#ifndef __CHAR_BIT__ +#define __CHAR_BIT__ 8 +#endif + +/** + * Provides facilities to combine multiple hash codes into a single hash code. + * + * @remarks Employs techniques, such as modular arithmetic and bitwise shifts, + * to minimize and evenly distribute information loss to the [multiple] input + * hash codes in the [singular] output hash code. + */ +class HashPhaser { + public: + /** + * Combines the hash codes of several parameters into a single hash code. + * + * @param value One of the parameters whose hash to store + * @param next The other parameters whose hash to store + * + * @remarks A well-chosen modulus of each parameter's hash is stored in + * equally sized blocks of bits. + */ + template + static std::size_t auto_phase(T value, NextT... next); + + /** + * Combines the hash codes of a limited number of elements into a single hash + * code. + * + * @param first Iterator pointing to the first element whose hash to store + * @param last Iterator pointing just after the last element whose hash to + * store + * + * @remarks A well-chosen modulus of each value's hash is stored in equally + * sized blocks of bits + */ + template + static std::size_t auto_phase_range(Iter first, Iter last); + + /** + * Combines the hash codes of a limited number of elements into a single hash + * code. + * + * @param first Iterator pointing to the first element whose hash to store + * @param last Iterator pointing just after the last element whose hash to + * store + * @param hash The hash op to use + * + * @overload + */ + template + static std::size_t auto_phase_range(Iter first, Iter last, Hash hash); + + /** + * Combines the hash codes of several parameters into a single composite hash + * code. + * + * @param value One of the parameters whose hash to store + * @param hash_op The hash code operator to use for the 'value' parameter + * @param next The other parameters whose hash to store + * + * @remarks A well-chosen modulus of each parameter's hash is stored in + * equally sized blocks of bits. + */ + template + static std::size_t custom_phase(T value, HashOp hash_op, NextT... next); + + private: + template + static void _auto_phase( + std::size_t &hash, std::uint16_t &depth, T value, NextT... next); + + template + static void _auto_phase(std::size_t &hash, std::uint16_t &depth, T value); + + template + static void _custom_phase( + std::size_t &hash, std::uint16_t &depth, T value, HashOp, NextT... next); + + template + static void _custom_phase( + std::size_t &hash, std::uint16_t &depth, T value, HashOp); + + /** + * Get the result of std::hash()(value) without having to declare the + * template parameter T explicitly (helpful when using 'auto' variables). + */ + template + static std::size_t _default_hash(T value); +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +std::size_t HashPhaser::custom_phase(T value, HashOp hash_op, NextT... next) { + std::size_t res = 0; + std::uint16_t depth = 0; + HashPhaser::_custom_phase(res, depth, value, hash_op, next...); + return res; +} + +template +std::size_t HashPhaser::auto_phase(T value, NextT... next) { + std::size_t res = 0; + std::uint16_t depth = 0; + HashPhaser::_auto_phase(res, depth, value, next...); + return res; +} + +template +inline std::size_t HashPhaser::_default_hash(T value) { + return std::hash()(value); +} + +template +std::size_t HashPhaser::auto_phase_range(Iter first, Iter last) { + if (first == last) { + return 0; + } + const std::uint16_t max_els = 8; + auto num_vals = std::distance(first, last); + std::uint16_t el_count = + num_vals > (std::numeric_limits::max)() + ? max_els + : std::max(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; + auto el_rem = el_count; + while (first != last) { + std::size_t val_hash = _default_hash(*first); + composite_hash |= (val_hash % phase) << ((el_count - 1) * bits_per_hash); + if (--el_rem == 0) { + break; + } + } + return composite_hash; +} + +template +std::size_t HashPhaser::auto_phase_range(Iter first, Iter last, Hash hash) { + if (first == last) { + return 0; + } + const std::uint16_t max_els = 8; + auto num_vals = std::distance(first, last); + std::uint16_t el_count = + num_vals > (std::numeric_limits::max)() + ? max_els + : std::max(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; + auto el_rem = el_count; + while (first != last) { + std::size_t val_hash = hash(*first); + composite_hash |= (val_hash % phase) << ((el_count - 1) * bits_per_hash); + if (--el_rem == 0) { + break; + } + } + return composite_hash; +} + +template +void HashPhaser::_auto_phase( + std::size_t &composite_hash, std::uint16_t &depth, T value, NextT... next) { + auto m_depth = depth++; + _auto_phase(composite_hash, depth, next...); + auto val_hash = std::hash()(value); + std::size_t bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; + std::size_t phase = std::size_t(1) << static_cast(bits_per_hash - 1); + composite_hash |= (val_hash % phase) << (m_depth * bits_per_hash); +} + +template +void HashPhaser::_auto_phase( + std::size_t &composite_hash, std::uint16_t &depth, T value) { + depth++; + std::size_t val_hash = std::hash()(value); + std::size_t bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; + std::size_t phase = std::size_t(1) << static_cast(bits_per_hash - 1); + composite_hash |= (val_hash % phase) << ((depth - 1) * bits_per_hash); +} + +template +void HashPhaser::_custom_phase( + std::size_t &composite_hash, + std::uint16_t &depth, + T value, + HashOp hash_op, + NextT... next) { + auto m_depth = depth++; + _custom_phase(composite_hash, depth, next...); + auto val_hash = hash_op(value); + auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; + std::size_t phase = 1U << (bits_per_hash - 1); + composite_hash |= (val_hash % phase) << (m_depth * bits_per_hash); +} + +template +void HashPhaser::_custom_phase( + std::size_t &composite_hash, + std::uint16_t &depth, + T value, + HashOp hash_op) { + depth++; + std::size_t val_hash = hash_op(value); + auto bits_per_hash = (sizeof(std::size_t) * __CHAR_BIT__) / depth; + std::size_t phase = 1U << (bits_per_hash - 1); + composite_hash |= (val_hash % phase) << ((depth - 1) * bits_per_hash); +} + +} // namespace bts::core diff --git a/src/lib_bts-system/bts-system/StringSplitter.h b/src/lib_bts-system/bts-system/StringSplitter.h new file mode 100644 index 000000000..27d5326f9 --- /dev/null +++ b/src/lib_bts-system/bts-system/StringSplitter.h @@ -0,0 +1,81 @@ +#pragma once +#include +#include +#include +#include + +namespace bts::system { + +class StringSplitter { + public: + template + static std::vector split( + Range range, const std::string &delimiter); + + template + static std::vector split( + Iterator first, Iterator last, const std::string &delimiter); + + template + static void split( + Iterator first, + Iterator last, + const std::string &delimiter, + std::vector &parts); +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +std::vector StringSplitter::split( + Range range, const std::string &delimiter) { + std::vector parts; + split(range.begin(), range.end(), delimiter, parts); + return parts; +} + +template +std::vector StringSplitter::split( + Iterator first, Iterator last, const std::string &delimiter) { + std::vector parts; + split(first, last, delimiter, parts); + return parts; +} + +template +void StringSplitter::split( + Iterator first, + Iterator last, + const std::string &delimiter, + std::vector &parts) { + + auto cursor = first; + auto curr_end = first; + auto curr_start = first; + + auto find_delim = [&cursor, &delimiter, &last]() { + for (auto it = cursor; it != last; it++) { + if (*it == delimiter[0]) { + if (std::equal( + it + 1, it + delimiter.size(), delimiter.begin() + 1, + delimiter.end())) { + return it; + } + } + } + return last; + }; + + while ((curr_end = find_delim()) != last) { + parts.push_back(std::string(curr_start, curr_end)); + curr_start = curr_end + delimiter.size(); + cursor = curr_end + delimiter.size(); + } + if (cursor != last) { + parts.push_back(std::string(cursor, last)); + } +} + +} // namespace bts::utils diff --git a/src/lib_bts-system/bts-system/TimeSpan.cpp b/src/lib_bts-system/bts-system/TimeSpan.cpp new file mode 100644 index 000000000..c10b3d47b --- /dev/null +++ b/src/lib_bts-system/bts-system/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_bts-system/bts-system/TimeSpan.h b/src/lib_bts-system/bts-system/TimeSpan.h new file mode 100644 index 000000000..627ca9414 --- /dev/null +++ b/src/lib_bts-system/bts-system/TimeSpan.h @@ -0,0 +1,134 @@ +#pragma once +#include "bts-system/FMT_IMPL.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{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_bts-system/bts-system/TimeStamp.cpp b/src/lib_bts-system/bts-system/TimeStamp.cpp new file mode 100644 index 000000000..c5c201d1d --- /dev/null +++ b/src/lib_bts-system/bts-system/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_bts-system/bts-system/TimeStamp.h b/src/lib_bts-system/bts-system/TimeStamp.h new file mode 100644 index 000000000..9f8f609b3 --- /dev/null +++ b/src/lib_bts-system/bts-system/TimeStamp.h @@ -0,0 +1,212 @@ +#pragma once +#include "bts-system/FMT_IMPL.h" +#include "bts-system/TimeSpan.h" +#include "bts-system/datetime.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 time as nano-seconds since the unix epoch (Jan 1, 1970) + */ +class 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_bts-system/bts-system/TryCatch.h b/src/lib_bts-system/bts-system/TryCatch.h new file mode 100644 index 000000000..a8441c2a9 --- /dev/null +++ b/src/lib_bts-system/bts-system/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_bts-system/bts-system/UNUSED.h b/src/lib_bts-system/bts-system/UNUSED.h new file mode 100644 index 000000000..11e2b6506 --- /dev/null +++ b/src/lib_bts-system/bts-system/UNUSED.h @@ -0,0 +1,4 @@ +#pragma once + +#define UNUSED(x) (void)(x) +#define UNUSED_PARAM __attribute__((unused)) diff --git a/src/lib_bts-system/bts-system/bts_assert.h b/src/lib_bts-system/bts-system/bts_assert.h new file mode 100644 index 000000000..ab13485cf --- /dev/null +++ b/src/lib_bts-system/bts-system/bts_assert.h @@ -0,0 +1,46 @@ +#pragma once +#include +#include +#include +#include + +// #include "lib/logging/logging.h" +#include "bts_debugger.h" + +#ifdef _WIN32 +#define TERMINATE ::_exit(-1); +#else +#define TERMINATE abort(); +#endif + +#define bts_assert_msg(EXP, msg) \ + do { \ + if (!(EXP)) { \ + std::cerr << fmt::format("Assert failure: {} {}", std::string(#EXP), \ + std::string(msg)) << std::endl; \ + std::this_thread::sleep_for(std::chrono::seconds(1)); \ + if (is_debugger_present()) \ + debug_break(); \ + TERMINATE; \ + } \ + } while (0) + +#define bts_assert(EXP) bts_assert_msg(EXP, "") + +#define bts_assert_msgf(EXP, msg_template, ...) \ + do { \ + if (!(EXP)) { \ + std::cerr << fmt::format("Assert failure: {} {}", std::string(#EXP), \ + fmt::format(msg_template, __VA_ARGS__)) << std::endl; \ + std::this_thread::sleep_for(std::chrono::seconds(1)); \ + if (is_debugger_present()) \ + debug_break(); \ + TERMINATE; \ + } \ + } while (0) + +#ifndef NDEBUG +#define bts_assert_d bts_assert +#else +#define bts_assert_d(EXP) +#endif diff --git a/src/lib_bts-system/bts-system/bts_debugger.h b/src/lib_bts-system/bts-system/bts_debugger.h new file mode 100644 index 000000000..28f40f624 --- /dev/null +++ b/src/lib_bts-system/bts-system/bts_debugger.h @@ -0,0 +1,43 @@ +#pragma once + +// #include "windows_wrapper.h" +#include +#include +#include +#include +#include + +inline bool is_debugger_present() { +#ifdef _WIN32 + return IsDebuggerPresent(); +#else + char buf[1024]; + int debugger_present = 0; + + int status_fd = open("/proc/self/status", O_RDONLY); + if (status_fd == -1) + return 0; + + ssize_t num_read = read(status_fd, buf, sizeof(buf) - 1); + + if (num_read > 0) { + static const char TracerPid[] = "TracerPid:"; + char *tracer_pid; + + buf[num_read] = 0; + tracer_pid = strstr(buf, TracerPid); + if (tracer_pid) + debugger_present = !!atoi(tracer_pid + sizeof(TracerPid) - 1); + } + + return debugger_present; +#endif +} + +inline void debug_break() { +#ifdef _WIN32 + __debugbreak(); +#else + std::raise(SIGINT); +#endif +} diff --git a/src/lib_bts-system/bts-system/datetime.cpp b/src/lib_bts-system/bts-system/datetime.cpp new file mode 100644 index 000000000..a2e7a7c27 --- /dev/null +++ b/src/lib_bts-system/bts-system/datetime.cpp @@ -0,0 +1,546 @@ +#include "datetime.h" + +#include "TimeStamp.h" +#include "bts-system/TimeSpan.h" +#include "bts_assert.h" + +#include +#include +#include +#include +#include + +namespace bts { +namespace datetime { + +const boost::gregorian::date anchor(1970, boost::gregorian::Jan, 1); +const uint64_t anchor_ticks = 621355968000000000; +const uint64_t micros_per_day = 86400000000; +const uint64_t ticks_per_micro = 10; +const uint64_t ticks_per_day = micros_per_day * ticks_per_micro; +const uint64_t ticks_mask = 0x3FFFFFFFFFFFFFFF; +// DateTimeKinds +const uint64_t KindUnspecified = 0x0000000000000000; +const uint64_t KindUtc = 0x4000000000000000; +const uint64_t KindLocal = 0x8000000000000000; + +unsigned long long ptime_to_ll(PTime const &t, uint64_t const &kind) { + uint64_t days = (t.date() - anchor).days(); + uint64_t usecs = t.time_of_day().total_microseconds(); + uint64_t ticks = + anchor_ticks + usecs * ticks_per_micro + days * ticks_per_day; + return (ticks & ticks_mask) | kind; +} + +unsigned long long ptime_to_ll(PTime const &t) { + return ptime_to_ll(t, KindUnspecified); +} + +LLDateTime ll_now() { + return ptime_to_ll( + boost::posix_time::microsec_clock::universal_time(), KindUtc); +} + +LLDateTime ll_local_now() { + return ptime_to_ll( + boost::posix_time::microsec_clock::local_time(), KindLocal); +} + +long unix_now() { + return (boost::posix_time::microsec_clock::universal_time() - PTime(anchor)) + .total_microseconds() / + (long)1E6; +} + +std::chrono::system_clock::time_point utc_ll_to_timepoint( + unsigned long long ll) { + auto us_since_epoch = ((ll & ticks_mask) - anchor_ticks) / ticks_per_micro; + return std::chrono::system_clock::time_point( + std::chrono::microseconds(us_since_epoch)); +} + +long long ll_diff_in_us(unsigned long long ll1, unsigned long long ll2) { +#ifndef NO_CPP0X + auto p1 = ll_to_ptime(ll1); + auto p2 = ll_to_ptime(ll2); // NOLINT + return (p2 - p1).total_microseconds(); +#else + return 0; +#endif +} + +long long ll_diff_in_seconds(unsigned long long ll1, unsigned long long ll2) { +#ifndef NO_CPP0X + auto p1 = ll_to_ptime(ll1); + auto p2 = ll_to_ptime(ll2); // NOLINT + return (p2 - p1).total_seconds(); +#else + return 0; +#endif +} + +long long ptime_diff_in_us(PTime const &t1, PTime const &t2) { + return (t1 - t2).total_microseconds(); +} + +long long ptime_diff_in_ms(PTime const &t1, PTime const &t2) { + return (t1 - t2).total_milliseconds(); +} + +long long ptime_diff_in_secs(PTime const &t1, PTime const &t2) { + return (t1 - t2).total_seconds(); +} + +PTime timestamp_to_ptime(core::TimeStamp const &t) { + auto now_ts = core::TimeStamp::now(); + auto now = datetime::local_now(); + + auto delta = (now_ts - t).micros(); + return now - boost::posix_time::microseconds(delta); +} + +core::TimeStamp ptime_to_timestamp(PTime const &t) { + auto now_ts = core::TimeStamp::now(); + auto now = datetime::now(); + + auto delta = (now - t).total_milliseconds(); + auto delta_tspan = core::TimeSpan::from_millis(delta); + return now_ts - delta_tspan; +} + +long long usecs_since(PTime const &t1) { + return (boost::posix_time::microsec_clock::universal_time() - t1) + .total_microseconds(); +} + +long long msecs_since(PTime const &t1) { + return (boost::posix_time::microsec_clock::universal_time() - t1) + .total_milliseconds(); +} + +PTime now() { return boost::posix_time::microsec_clock::universal_time(); } + +PTime local_now() { return boost::posix_time::microsec_clock::local_time(); } + +PTime make_ptime(int year, int month, int day) { + // January=1 + bts_assert_d(month > 0); + boost::gregorian::date dt(year, boost::gregorian::greg_month(month), day); + return PTime(dt); +} + +PTime make_ptime( + int year, + int month, + int day, + int hour, + int minute, + int second, + int millisecond) { + boost::gregorian::date dt(year, boost::gregorian::greg_month(month), day); + return PTime( + dt, boost::posix_time::time_duration(hour, minute, second) + + boost::posix_time::milliseconds(millisecond)); +} + +PTime ll_to_ptime(LLDateTime ll) { + ll &= ticks_mask; + uint64_t ticks_part = ll % ticks_per_day; + uint64_t days_part = (ll - anchor_ticks) / ticks_per_day; + return PTime( + anchor + boost::gregorian::date_duration(days_part), + boost::posix_time::time_duration( + boost::posix_time::microseconds(ticks_part / ticks_per_micro))); +} + +extern "C" BTS_EXPORT int ll_to_cstr( + LLDateTime ll, char *buffer, int buffer_sz) { + const int str_sz = sizeof("20121204-14:44:02.000"); + bts_assert(buffer_sz >= str_sz); + auto str = FIX_timestamp(ll_to_ptime(ll)); + bts_assert(str.size() == str_sz - 1); + memcpy(buffer, &str[0], str.size()); + buffer[str.size()] = '\0'; + return str.size(); +} + +std::string time_string(PTime const &t) { + int hours = t.time_of_day().hours(); + int minutes = t.time_of_day().minutes(); + int seconds = t.time_of_day().seconds(); + int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60; + int frac_ms = t.time_of_day().total_milliseconds() - whole_ms; + std::ostringstream out; + out << std::setw(2) << std::setfill('0') << hours << ":" << std::setw(2) + << std::setfill('0') << minutes << ":" << std::setw(2) + << std::setfill('0') << seconds << "." << std::setw(3) + << std::setfill('0') << frac_ms; + return out.str(); +} + +std::string date_string(PTime const &t) { + std::string result(10, ' '); + sprintf( + &result[0], "%02d/%02d/%04d", t.date().month().as_number(), + t.date().day().as_number(), (unsigned short)t.date().year()); + return result; +} + +std::string iso_date_time_string(PTime const &t) { + return boost::posix_time::to_iso_extended_string(t); +} + +PTime parse_iso_date_time_tz(const std::string &date) { + // example 1) 2022-10-27T09:30-5:00 + // example 2) 2022-10-27T09:30+5:00 + +#ifdef _WIN32 + struct tm tt = {0, 0, 0, 0, 0, 0, 0, 0, 0}; +#else + struct tm tt = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +#endif + + int tz_hours; + int tz_minutes; + int num_scanned = sscanf( + date.c_str(), "%4d-%2d-%2dT%2d:%2d:%2d%3d:%2d", &tt.tm_year, &tt.tm_mon, + &tt.tm_mday, &tt.tm_hour, &tt.tm_min, &tt.tm_sec, &tz_hours, + &tz_minutes); + bts_assert_msgf( + num_scanned == 8, + "Failed to parse iso8601 date from string '{}' (format must be " + "YYYY-MM-DDThh:mm:ss<+/->hh:ss)", + date); + tt.tm_mon -= 1; + tt.tm_year -= 1900; + tt.tm_isdst = -1; + return boost::posix_time::ptime_from_tm(tt) + + boost::posix_time::time_duration{tz_hours, tz_minutes, 0, 0}; +} + +PTime parse_iso_date_time_string(std::string const &t) { + constexpr const std::size_t time_idx = sizeof("YYYY-MM-DD") - 1; + auto actual_time_idx = t.find('T', time_idx); + if (actual_time_idx != std::string::npos) { + constexpr const std::size_t time_zone_idx = + time_idx + sizeof("HH:MM") - 1; + // time zone must be after time + bts_assert(actual_time_idx < time_zone_idx); + if (t.find('+', time_zone_idx) != std::string::npos || + t.find('-', time_zone_idx) != std::string::npos) { + // date, time, and time zone + return parse_iso_date_time_tz(t); + } + // date and time (no time zone) + return boost::posix_time::from_iso_extended_string(t); + } + // date (no time or time zone) + return PTime{boost::gregorian::from_simple_string(t)}; +} + +std::string simple_date_time_string(PTime const &t) { + return boost::posix_time::to_simple_string(t); +} + +PTime parse_simple_date_time_string(std::string const &t) { + if (t.find(' ') == t.npos) + return boost::posix_time::time_from_string(t + " 00:00:00"); + else + return boost::posix_time::time_from_string(t); +} + +PDuration parse_duration(std::string const &time_str) { + try { + return boost::posix_time::duration_from_string(time_str); + } catch (boost::bad_lexical_cast &) { + bts_assert(false); + return boost::posix_time::time_duration(); + } +} + +std::string FIX_date_time_string(PTime const &t) { + std::string result(strlen("20121204-14:44:02"), ' '); + sprintf( + &result[0], "%d%02d%02d-%02d:%02d:%02d", (unsigned short)t.date().year(), + t.date().month().as_number(), t.date().day().as_number(), + (int)t.time_of_day().hours(), (int)t.time_of_day().minutes(), + (int)t.time_of_day().seconds()); + return result; +} + +void FIX_timestamp(PTime const &t, std::string *out) { + int hours = t.time_of_day().hours(); + int minutes = t.time_of_day().minutes(); + int seconds = t.time_of_day().seconds(); + int whole_ms = seconds * 1000 + minutes * 1000 * 60 + hours * 1000 * 60 * 60; + int frac_ms = t.time_of_day().total_milliseconds() - whole_ms; + out->resize(sizeof("20121204-14:44:02.000")); + +#ifdef _WIN32 + _snprintf( + &(*out)[0], out->size(), "%04d%02d%02d-%02d:%02d:%02d.%03d", + (int)t.date().year(), t.date().month().as_number(), + t.date().day().as_number(), hours, minutes, seconds, frac_ms); +#else + snprintf( + &(*out)[0], out->size(), "%04d%02d%02d-%02d:%02d:%02d.%03d", + (int)t.date().year(), t.date().month().as_number(), + t.date().day().as_number(), hours, minutes, seconds, frac_ms); +#endif + out->resize(out->size() - 1); +} + +std::string FIX_timestamp(PTime const &t) { + std::string result; + FIX_timestamp(t, &result); + return result; +} + +bool is_from_today(PTime const &time) { + return ( + time.date() == + boost::posix_time::microsec_clock::universal_time().date()); +} + +bool is_at_least_msecs_old(PTime const &time, uint64_t ms) { + PTime now = boost::posix_time::microsec_clock::universal_time(); + return (now - time).total_milliseconds() >= (int64_t)ms; +} + +bool is_at_least_secs_old(PTime const &time, uint64_t sec) { + PTime now = boost::posix_time::microsec_clock::universal_time(); + return (now - time).total_seconds() >= (int64_t)sec; +} + +bool is_at_least_mins_old(PTime const &time, uint64_t mins) { + PTime now = boost::posix_time::microsec_clock::universal_time(); + return ((now - time).total_seconds() / 60) >= (int64_t)mins; +} + +bool is_at_least_hours_old(PTime const &time, uint64_t hours) { + PTime now = boost::posix_time::microsec_clock::universal_time(); + int64_t secs = (int64_t)((now - time).total_seconds()); + return (secs / 60 / 60) >= (int64_t)hours; +} + +bool is_at_least_days_old(PTime const &time, uint64_t days) { + return is_at_least_hours_old(time, days * 24); +} + +LLDateTime ll_from_unix_epoch(long seconds) { + PTime result(anchor, boost::posix_time::seconds(seconds)); + return ptime_to_ll(result, KindUnspecified); +} + +LLDateTime ll_from_unix_epoch_ms(long long milliseconds) { + PTime result(anchor, boost::posix_time::millisec(milliseconds)); + return ptime_to_ll(result, KindUnspecified); +} + +void ll_to_parts( + LLDateTime ll_time, + int &year, + int &month, + int &day, + int &hour, + int &minute, + int &second, + int &millisecond) { + PTime t = ll_to_ptime(ll_time); + year = t.date().year(); + month = t.date().month(); + day = t.date().day(); + hour = t.time_of_day().hours(); + minute = t.time_of_day().minutes(); + second = t.time_of_day().seconds(); + int whole_ms = second * 1000 + minute * 1000 * 60 + hour * 1000 * 60 * 60; + millisecond = t.time_of_day().total_milliseconds() - whole_ms; +} + +LLDateTime ll_from_parts( + int year, + int month, + int day, + int hour, + int minute, + int second, + int millisecond) { + return ptime_to_ll(make_ptime( + year, month, day, hour, minute, second, millisecond)); // NOLINT +} + +LLDateTime ll_from_date(const Date &date) { + if (date.year == 0 && date.day == 0 && date.month == 0) { + return 0; + } + return ll_from_parts(date.year, date.month, date.day, 0, 0, 0, 0); +} + +Date ll_to_date(LLDateTime time) { + Date date; + int year, month, day, unused; + ll_to_parts(time, year, month, day, unused, unused, unused, unused); + date.year = year; + date.month = month; + date.day = day; + return date; +} + +bool is_local_time_sunday() { + auto now = local_now(); + return now.date().day_of_week() == boost::date_time::Sunday; +} +bool is_local_time_between(int hh_start, int mm_start, int hh_end, int mm_end) { + using namespace boost::posix_time; + auto now = local_now(); + auto start = ptime(now.date(), hours(hh_start) + minutes(mm_start)); + auto end = ptime(now.date(), hours(hh_end) + minutes(mm_end)); + return (now >= start && now <= end); +} + +struct UtcTimeStamp { + int hour; + int min; + int sec; + int msec; + int day; + int month; + int year; +}; + +struct UtcTimeStampConvertor { + static UtcTimeStamp convert(const std::string &value) { + bool haveMilliseconds = false; + + switch (value.size()) { + case 21: + haveMilliseconds = true; + case 17: + break; + default: + bts_assert(false); + } + + int i = 0; + int c = 0; + for (c = 0; c < 8; ++c) + if (!isdigit(value[i++])) + bts_assert(false); + if (value[i++] != '-') + bts_assert(false); + for (c = 0; c < 2; ++c) + if (!isdigit(value[i++])) + bts_assert(false); + if (value[i++] != ':') + bts_assert(false); + for (c = 0; c < 2; ++c) + if (!isdigit(value[i++])) + bts_assert(false); + if (value[i++] != ':') + bts_assert(false); + for (c = 0; c < 2; ++c) + if (!isdigit(value[i++])) + bts_assert(false); + + if (haveMilliseconds) { + if (value[i++] != '.') + bts_assert(false); + for (c = 0; c < 3; ++c) + if (!isdigit(value[i++])) + bts_assert(false); + } + + int year, mon, mday, hour, min, sec, millis; + + i = 0; + + year = value[i++] - '0'; + year = 10 * year + value[i++] - '0'; + year = 10 * year + value[i++] - '0'; + year = 10 * year + value[i++] - '0'; + + mon = value[i++] - '0'; + mon = 10 * mon + value[i++] - '0'; + if (mon < 1 || 12 < mon) + bts_assert(false); + + mday = value[i++] - '0'; + mday = 10 * mday + value[i++] - '0'; + if (mday < 1 || 31 < mday) + bts_assert(false); + + ++i; // skip '-' + + hour = value[i++] - '0'; + hour = 10 * hour + value[i++] - '0'; + // No check for >= 0 as no '-' are converted here + if (23 < hour) + bts_assert(false); + + ++i; // skip ':' + + min = value[i++] - '0'; + min = 10 * min + value[i++] - '0'; + // No check for >= 0 as no '-' are converted here + if (59 < min) + bts_assert(false); + + ++i; // skip ':' + + sec = value[i++] - '0'; + sec = 10 * sec + value[i++] - '0'; + + // No check for >= 0 as no '-' are converted here + if (60 < sec) + bts_assert(false); + + if (haveMilliseconds) { + millis = + (100 * (value[i + 1] - '0') + 10 * (value[i + 2] - '0') + + (value[i + 3] - '0')); + } else + millis = 0; + + return UtcTimeStamp{hour, min, sec, millis, mday, mon, year}; + } +}; + +PTime parse_FIX_timestamp(const std::string &ts_str) { + auto ts = UtcTimeStampConvertor::convert(ts_str); + boost::gregorian::date dt(ts.year, ts.month, ts.day); + // fucking boost. unbelieavable. + auto ticks_per_second = + boost::posix_time::time_duration::rep_type::res_adjust(); + boost::posix_time::time_duration td( + ts.hour, ts.min, ts.sec, ts.msec * (ticks_per_second / 1000)); + return PTime(dt, td); +} + +#ifndef NO_CPP0X +extern "C" BTS_EXPORT LLDateTime ll_today() { + return datetime::ll_from_date(today()); +} + +BTS_EXPORT Date today() { + auto now = datetime::now(); + Date date; + date.year = now.date().year(); + date.month = now.date().month(); + date.day = now.date().day(); + return date; +} +BTS_EXPORT Date add_days(Date indate, int days) { + boost::gregorian::date dt(indate.year, indate.month, indate.day); + auto adjusted_dt = dt + boost::gregorian::days(days); + Date date(adjusted_dt.year(), adjusted_dt.month(), adjusted_dt.day()); + return date; +} + +extern "C" BTS_EXPORT LLDateTime ll_yesterday() { + auto now = datetime::now() - boost::gregorian::days(1); + return datetime::ll_from_parts( + now.date().year(), now.date().month(), now.date().day(), 0, 0, 0, 0); +} +#endif +} // namespace datetime +} // namespace bts diff --git a/src/lib_bts-system/bts-system/datetime.h b/src/lib_bts-system/bts-system/datetime.h new file mode 100644 index 000000000..d14ce658d --- /dev/null +++ b/src/lib_bts-system/bts-system/datetime.h @@ -0,0 +1,168 @@ +#pragma once + +// #include "bts_export.h" +#include +#include + +// #include "windows_wrapper.h" // tick_count.h includes windows.h + +namespace boost { +namespace posix_time { +class ptime; +} +} // namespace boost +namespace boost:: posix_time { +class time_duration; +} // namespace boost + +namespace bts::core { + class TimeStamp; +} + +namespace bts { +namespace datetime { +typedef boost::posix_time::ptime PTime; +typedef boost::posix_time::time_duration PDuration; + +#ifndef BTS_EXPORT +#define BTS_EXPORT +#endif + +typedef unsigned long long LLDateTime; +// Simple Date structure +struct Date { + Date() : year(0), month(0), day(0) {} + Date(uint16_t y, uint8_t m, uint8_t d) : year(y), month(m), day(d) {} + Date(uint16_t date) + : year((date >> 9) + 1970), month(date >> 5 & 15), day(date & 31) {} + uint16_t year; + uint8_t month; + uint8_t day; + bool operator<(const Date &other) const { + if (year < other.year) + return true; + if (year == other.year && month < other.month) + return true; + if (year == other.year && month == other.month && day < other.day) + return true; + return false; + } + bool operator>(const Date &other) const { + return !(*this == other) && !(*this < other); + } + bool operator==(const Date &other) const { + return day == other.day && month == other.month && year == other.year; + } + bool operator!=(const Date &other) const { return !(*this == other); } + uint16_t to_short() { + return ((year - 1970) << 9) + (month << 5) + day; + } // NOLINT +}; + +// extern "C" BTS_EXPORT LLDateTime ll_now(); +extern "C" LLDateTime ll_local_now(); +extern "C" double OA_date_from_ll(LLDateTime ll); +extern "C" LLDateTime ll_from_OA_date(double oa_date); +extern "C" long unix_now(); + +#ifndef _WIN32 +inline uint64_t unix_nano_now() { + struct timespec ts; + // CLOCK_REALTIME is already anchored at the unix epoch + clock_gettime(CLOCK_REALTIME, &ts); + return static_cast(1000000000UL) * static_cast(ts.tv_sec) + + static_cast(ts.tv_nsec); +} +#endif + +// BTS_EXPORT std::chrono::system_clock::time_point +// utc_ll_to_timepoint(unsigned long long ll); +BTS_EXPORT PTime now(); +BTS_EXPORT PTime local_now(); +// BTS_EXPORT PTime make_ptime(int year, int month, int day); +// BTS_EXPORT PTime make_ptime( +// int year, +// int month, +// int day, +// int hour, +// int minute, +// int second, +// int millisecond); +LLDateTime ptime_to_ll(PTime const &t); +PTime ll_to_ptime(LLDateTime ll); +// extern "C" BTS_EXPORT int +// ll_to_cstr(LLDateTime ll, char *buffer, int buffer_sz); +// BTS_EXPORT long long +// ll_diff_in_us(unsigned long long start, unsigned long long stop); +// BTS_EXPORT long long +// ll_diff_in_seconds(unsigned long long start, unsigned long long stop); +long long ptime_diff_in_us(PTime const &t1, PTime const &t2); +long long ptime_diff_in_ms(PTime const &t1, PTime const &t2); +long long ptime_diff_in_secs(PTime const &t1, PTime const &t2); +PTime timestamp_to_ptime(core::TimeStamp const &t); +core::TimeStamp ptime_to_timestamp(PTime const &t); +long long usecs_since(PTime const &t1); +long long msecs_since(PTime const &t1); +std::string time_string(PTime const &t); +std::string date_string(PTime const &t); +std::string iso_date_time_string(PTime const &t); +PTime parse_iso_date_time_string(std::string const &t); +PTime parse_iso_date_time_tz(const std::string &date); +std::string simple_date_time_string(PTime const &t); +PTime parse_simple_date_time_string(std::string const &t); +PDuration parse_duration(std::string const &time_str); +std::string FIX_date_time_string(PTime const &t); +PTime parse_FIX_timestamp(const std::string &ts); +std::string FIX_timestamp(PTime const &t); +void FIX_timestamp(PTime const &t, std::string *out); +bool is_from_today(PTime const &time); +bool is_at_least_msecs_old(PTime const &time, uint64_t ms); +bool is_at_least_secs_old(PTime const &time, uint64_t sec); +bool is_at_least_mins_old(PTime const &time, uint64_t min); +bool is_at_least_hours_old(PTime const &time, uint64_t hour); +bool is_at_least_hours_old(PTime const &time, uint64_t hour); +bool is_at_least_days_old(PTime const &time, uint64_t days); +LLDateTime ll_from_unix_epoch(long); +LLDateTime ll_from_unix_epoch_ms(long long); + +// extern "C" BTS_EXPORT void ll_to_parts( +// LLDateTime, +// int &year, +// int &month, +// int &day, +// int &hour, +// int &minute, +// int &second, +// int &millisecond); +// BTS_EXPORT Date ll_to_date(LLDateTime); +// extern "C" BTS_EXPORT LLDateTime ll_from_parts( +// int year, +// int month, +// int day, +// int hour, +// int minute, +// int second, +// int millisecond); +// BTS_EXPORT LLDateTime ll_from_date(const Date &date); + +bool is_local_time_sunday(); +bool is_local_time_between(int hh_start, int mm_start, int hh_end, int mm_end); + +#ifndef NO_CPP0X +// extern "C" BTS_EXPORT LLDateTime ll_today(); +BTS_EXPORT Date today(); +// BTS_EXPORT Date add_days(Date date, int days); +// extern "C" BTS_EXPORT LLDateTime ll_yesterday(); +#endif + +} // namespace datetime +} // namespace bts + +// namespace std { +// template <> +// struct numeric_limits { +// static bts::datetime::Date min() { return +// bts::datetime::Date(1970,1,1); } static bts::datetime::Date max() { +// return bts::datetime::Date(2097,12,31); } +// }; +// } diff --git a/src/lib_bts-system/bts-system/logging.h b/src/lib_bts-system/bts-system/logging.h new file mode 100644 index 000000000..99cf76236 --- /dev/null +++ b/src/lib_bts-system/bts-system/logging.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +#include +#include + +#define LOG(subsys, level, msg_template, ...) \ + std::cerr << std::setw(8) << #subsys << " | " << std::setw(8) << #level \ + << " | " << fmt::format(msg_template, __VA_ARGS__) << std::endl diff --git a/src/lib_bts-system/bts-system/table_formatter.h b/src/lib_bts-system/bts-system/table_formatter.h new file mode 100644 index 000000000..f779f9465 --- /dev/null +++ b/src/lib_bts-system/bts-system/table_formatter.h @@ -0,0 +1,176 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace bts::system { + +class TableFormatter { + public: + template + using Schema = + std::vector>>; + + template + static std::ostream &format( + std::ostream &out, const Range &range, const Schema &schema); + + template + static std::ostream &format( + std::ostream &out, TIter first, TIter last, const Schema &schema); + + template + static std::ostream &format_record( + std::ostream &out, + const T &record, + const Schema &schema, + bool headers = false); + + template + class SchemaBuilder { + public: + Schema result; + + template + void add(std::string name, GetCell get_cell, int width = 0); + }; +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +template +template +inline void TableFormatter::SchemaBuilder::add( + std::string name, GetCell get_cell, int width) { + std::function get_cell_str = [&get_cell](const TRow &r) { + std::stringstream ss; + ss << get_cell(r); + return ss.str(); + }; + result.emplace_back(std::move(name), width, get_cell_str); +} + +template +std::ostream &TableFormatter::format( + std::ostream &out, const Range &range, const Schema &schema) { + return format(out, range.begin(), range.end(), schema); +} + +template +inline std::ostream &TableFormatter::format( + std::ostream &out, + TIter first, + TIter last, + const typename TableFormatter::Schema &schema) { + if (first == last || schema.size() == 0) return out; + int total_width = 0; + for (auto col : schema) { + total_width += std::get<1>(col); + } + // top border of table + for (int i = 0; i < total_width + static_cast(schema.size()); i++) { + out << "-"; + } + out << std::endl; + // column headers themselves + out << "|"; + for (auto col : schema) { + out << std::setw(std::get<1>(col)) << std::get<0>(col) << "|"; + } + // line between column headers and rows + out << std::endl; + for (int i = 0; i < total_width + static_cast(schema.size()); i++) { + out << "-"; + } + // rows + while (first != last) { + out << std::endl; + out << "|"; + for (uint32_t i = 0; i < schema.size(); i++) { + out << std::setw(std::get<1>(schema[i])) + << std::get<2>(schema[i])(*first) << "|"; + } + first++; + } + // bottom border of table + out << std::endl; + for (int i = 0; i < total_width + static_cast(schema.size()); i++) + out << "-"; + return out; +} + +template +inline std::ostream &TableFormatter::format_record( + std::ostream &out, + const T &record, + const typename TableFormatter::Schema &schema, + bool headers) { + constexpr const char *const field_header = "Field"; + constexpr const char *const value_header = "Value"; + + int ulabel_width = headers ? strlen(field_header) : 0; + for (const auto &field : schema) { + auto label_width = std::get<1>(field); + if (label_width == 0) { + label_width = static_cast(std::get<0>(field).size()); + } + if (label_width > ulabel_width) { + ulabel_width = label_width; + } + } + + std::vector field_vals; + for (const auto &field : schema) { + field_vals.push_back(std::get<2>(field)(record)); + } + + int uvalue_width = headers ? strlen(value_header) : 0; + for (const auto &v : field_vals) { + auto value_width = static_cast(v.size()); + if (value_width > uvalue_width) { + uvalue_width = value_width; + } + } + + int table_width = ulabel_width + uvalue_width + 4; + if (headers) { + // top border of table + for (int i = 0; i < table_width + 3; i++) { + out << "-"; + } + out << std::endl; + // column headers + out << "| " << std::setw(ulabel_width) << field_header << " | " + << std::setw(uvalue_width) << value_header << " |\n"; + } + + // border between col headers and rows (or top border of table, if 'headers' + // is false) + for (int i = 0; i < table_width + 3; i++) { + out << "-"; + } + + // rows + for (std::size_t i = 0; i < field_vals.size(); i++) { + out << "\n"; + const auto &label = std::get<0>(schema.at(i)); + const auto &val = field_vals.at(i); + out << "| " << std::setw(ulabel_width) << label << " | " + << std::setw(uvalue_width) << val << " |"; + } + out << "\n"; + + // bottom border of table + for (int i = 0; i < table_width + 3; i++) { + out << "-"; + } + return out; +} + +} // namespace bts::core