refactor: Add lib_system proj for common utils

Migrates the following:
* Delegate.h
* bts_array_size.h
* string_utils.h

Note: File names in the new lib are snake_case (this seems to be the
standard naming convention for c++).
This commit is contained in:
John Colvin committed 2024-10-30 19:31:40 +00:00
1 parent c660c52c3d
commit b020939c99
29 files changed
+215 -90

No files matched your search

+64 -67
View File
@@ -41,80 +41,77 @@ if(DEFINED APP_DATA_DIR)
add_compile_definitions(APP_DATA_DIR="${APP_DATA_DIR}")
endif()
add_compile_definitions(EXPORT_MD_CONSUMER="${EXPORT_MD_CONSUMER}")
if(WIN32)
# subdirs
add_subdirectory(src/lib_spark)
else()
if(BUILD_SPARKD)
# Note: All macros that begin w/ 'CCAPI' are ccapi's macros (defined and/or
# read by their code). We override some below.
add_compile_definitions(SPARK_ENABLE_CCAPI)
# This allows us to override hardcoded exchange-urls with ones read from
# config (i.e., resolved at runtime rather than compile-time)
add_compile_definitions(CCAPI_EXPOSE_INTERNAL)
if(BUILD_SPARKD)
# Note: All macros that begin w/ 'CCAPI' are ccapi's macros (defined and/or
# read by their code). We override some below.
add_compile_definitions(SPARK_ENABLE_CCAPI)
# Enable ccapi services
add_compile_definitions(CCAPI_ENABLE_SERVICE_MARKET_DATA)
add_compile_definitions(CCAPI_ENABLE_SERVICE_EXECUTION_MANAGEMENT)
# Enable ccapi exchanges
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE_COIN_FUTURES)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE_US)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE_USDS_FUTURES)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_COINBASE)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_DERIBIT)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_OKX)
# This allows us to override hardcoded exchange-urls with ones read from
# config (i.e., resolved at runtime rather than compile-time)
add_compile_definitions(CCAPI_EXPOSE_INTERNAL)
set(CCAPI_LOG_LEVEL
"Warn"
CACHE STRING "<Fatal|Error|Warn|Info|Debug|Trace> Log level for ccapi")
string(TOUPPER "${CCAPI_LOG_LEVEL}" _ccapi_log_level_upper)
add_compile_definitions("CCAPI_ENABLE_LOG_${_ccapi_log_level_upper}")
# Enable ccapi services
add_compile_definitions(CCAPI_ENABLE_SERVICE_MARKET_DATA)
add_compile_definitions(CCAPI_ENABLE_SERVICE_EXECUTION_MANAGEMENT)
# Enable ccapi exchanges
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE_COIN_FUTURES)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE_US)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_BINANCE_USDS_FUTURES)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_COINBASE)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_DERIBIT)
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_OKX)
# ** Binance **
add_compile_definitions(CCAPI_BINANCE_URL_REST_BASE="https://api.binance.com")
add_compile_definitions(BINANCE_INSTRUMENTS_PATH="/api/v3/exchangeInfo")
add_compile_definitions(BINANCE_PRICE_STATS_PATH="/api/v3/ticker/24hr")
set(CCAPI_LOG_LEVEL
"Warn"
CACHE STRING "<Fatal|Error|Warn|Info|Debug|Trace> Log level for ccapi")
string(TOUPPER "${CCAPI_LOG_LEVEL}" _ccapi_log_level_upper)
add_compile_definitions("CCAPI_ENABLE_LOG_${_ccapi_log_level_upper}")
# ** Binance US **
add_compile_definitions(BINANCE_US_INSTRUMENTS_PATH="/api/v3/exchangeInfo")
add_compile_definitions(BINANCE_US_PRICE_STATS_PATH="/api/v3/ticker/24hr")
# ** Binance **
add_compile_definitions(CCAPI_BINANCE_URL_REST_BASE="https://api.binance.com")
add_compile_definitions(BINANCE_INSTRUMENTS_PATH="/api/v3/exchangeInfo")
add_compile_definitions(BINANCE_PRICE_STATS_PATH="/api/v3/ticker/24hr")
# ** Binance USDS Futures **
add_compile_definitions(CCAPI_BINANCE_USDS_FUTURES_URL_REST_BASE="https://dapi.binance.com")
add_compile_definitions(BINANCE_USDS_FUTURES_INSTRUMENTS_PATH="/dapi/v1/exchangeInfo")
add_compile_definitions(BINANCE_USDS_FUTURES_PRICE_STATS_PATH="/dapi/v1/ticker/24hr")
# ** Binance US **
add_compile_definitions(BINANCE_US_INSTRUMENTS_PATH="/api/v3/exchangeInfo")
add_compile_definitions(BINANCE_US_PRICE_STATS_PATH="/api/v3/ticker/24hr")
# ** Deribit **
add_compile_definitions(DERIBIT_INSTRUMENT_PATH="/api/v2/public/get_instrument")
add_compile_definitions(DERIBIT_INSTRUMENTS_PATH="/api/v2/public/get_instruments")
add_compile_definitions(DERIBIT_COMBOS_PATH="/api/v2/public/get_combos")
add_compile_definitions(DERIBIT_TICKER_PATH="/api/v2/public/ticker")
add_compile_definitions(DERIBIT_CURRENCIES_METHOD="public/get_currencies")
add_compile_definitions(DERIBIT_INSTRUMENT_METHOD="public/get_instrument")
add_compile_definitions(DERIBIT_INSTRUMENTS_METHOD="public/get_instruments")
add_compile_definitions(DERIBIT_COMBO_DETAILS_METHOD="public/get_combo_details")
add_compile_definitions(DERIBIT_COMBOS_METHOD="public/get_combos")
# ** Binance USDS Futures **
add_compile_definitions(CCAPI_BINANCE_USDS_FUTURES_URL_REST_BASE="https://dapi.binance.com")
add_compile_definitions(BINANCE_USDS_FUTURES_INSTRUMENTS_PATH="/dapi/v1/exchangeInfo")
add_compile_definitions(BINANCE_USDS_FUTURES_PRICE_STATS_PATH="/dapi/v1/ticker/24hr")
# ** Okx **
add_compile_definitions(OKX_INSTRUMENTS_PATH="/api/v5/public/instruments")
add_compile_definitions(OKX_UNDERLYING_PATH="/api/v5/public/underlying")
endif()
# ** Deribit **
add_compile_definitions(DERIBIT_INSTRUMENT_PATH="/api/v2/public/get_instrument")
add_compile_definitions(DERIBIT_INSTRUMENTS_PATH="/api/v2/public/get_instruments")
add_compile_definitions(DERIBIT_COMBOS_PATH="/api/v2/public/get_combos")
add_compile_definitions(DERIBIT_TICKER_PATH="/api/v2/public/ticker")
add_compile_definitions(DERIBIT_CURRENCIES_METHOD="public/get_currencies")
add_compile_definitions(DERIBIT_INSTRUMENT_METHOD="public/get_instrument")
add_compile_definitions(DERIBIT_INSTRUMENTS_METHOD="public/get_instruments")
add_compile_definitions(DERIBIT_COMBO_DETAILS_METHOD="public/get_combo_details")
add_compile_definitions(DERIBIT_COMBOS_METHOD="public/get_combos")
# subdirs
if (BUILD_SPARKD)
add_subdirectory(src/app_sparkd)
add_subdirectory(src/data_files)
endif()
add_subdirectory(src/lib_spark)
if (BUILD_ADMIN_WEB_APP)
add_subdirectory(src/Spark.Admin.WebApp)
endif()
if(BUILD_DOCS)
add_subdirectory(doc/user)
endif()
if(BUILD_SDK)
add_subdirectory(src/sdk)
endif()
# ** Okx **
add_compile_definitions(OKX_INSTRUMENTS_PATH="/api/v5/public/instruments")
add_compile_definitions(OKX_UNDERLYING_PATH="/api/v5/public/underlying")
endif()
# subdirs
add_subdirectory(src/lib_system)
if (BUILD_SPARKD)
add_subdirectory(src/app_sparkd)
add_subdirectory(src/data_files)
endif()
add_subdirectory(src/lib_spark)
if (BUILD_ADMIN_WEB_APP)
add_subdirectory(src/Spark.Admin.WebApp)
endif()
if(BUILD_DOCS)
add_subdirectory(doc/user)
endif()
if(BUILD_SDK)
add_subdirectory(src/sdk)
endif()
@@ -1,6 +1,6 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <unordered_set>
@@ -1,6 +1,6 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <unordered_set>
@@ -1,6 +1,6 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <vector>
@@ -2,7 +2,7 @@
#include "sparkd/md/ccapi/CCApiCorrelationID.h"
#include "sparkd/md/ccapi/ICCApiMessageHandler.h"
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <ccapi_cpp/ccapi_event.h>
#include <ccapi_cpp/ccapi_message.h>
@@ -1,8 +1,8 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "sparkd/md/ccapi/CCApiCorrelationID.h"
#include "sparkd/md/ccapi/ICCApiMessageHandler.h"
#include "system/delegate.h"
#include <cstdint>
#include <map>
@@ -1,6 +1,6 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <unordered_set>
@@ -1,7 +1,7 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "sparkd/md/ccapi/ICCApiMessageHandler.h"
#include "system/delegate.h"
#include <nlohmann/json_fwd.hpp>
#include <string>
@@ -1,6 +1,6 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <vector>
@@ -1,6 +1,6 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <vector>
@@ -1,8 +1,8 @@
#pragma once
#include "spark/utils/Delegate.h"
#include "sparkd/md/ccapi/CCApiCorrelationID.h"
#include "sparkd/md/ccapi/ICCApiMessageHandler.h"
#include "system/delegate.h"
#include <nlohmann/json.hpp>
@@ -1,5 +1,6 @@
function(get_link_libraries dest)
set(${dest}
system
curl
tbb
boost_system
@@ -1,5 +1,6 @@
function(get_link_libraries dest)
set(${dest}
system
protobuf
double-conversion
boost_system
@@ -10,10 +10,10 @@
#include "spark/core/FMT.h"
#include "spark/io/ProtoPrinter.h"
#include "spark/os/os.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/defer.h"
#include "spark/utils/is_unittest_environment.h"
#include "system/bts_array_size.h"
#include "spark/config/protos/system_config.pb.h"
@@ -113,7 +113,7 @@ string ConfigurationState::settings_path() {
void ConfigurationState::initialize_defaults(ConfigLog &log) {
initialize_defaults(
log, {"lib/config/protos/", "lib/api/domain/", "lib/api/md/"});
log, {"spark/config/protos/", "spark/api/domain/", "spark/api/md/"});
}
void ConfigurationState::initialize_defaults(
@@ -226,8 +226,15 @@ void ConfigurationState::initialize_builtins(ConfigLog *log) {
//
// Add the System value to the defaults
const proto::Descriptor *defaults_desc = _defaults->GetDescriptor();
bts_assert_msg(
defaults_desc != nullptr,
"Failed to get descriptor for config defaults");
const proto::FieldDescriptor *field_desc =
_defaults->GetDescriptor()->FindFieldByName("SystemConfig");
defaults_desc->FindFieldByName("SystemConfig");
bts_assert_msg(
field_desc != nullptr,
"Failed to get descriptor for config defaults field SystemConfig");
const proto::Descriptor *message_desc = field_desc->message_type();
// Create a meta message
@@ -5,7 +5,7 @@
#include "spark/io/websocket/JsonRpcError.h"
#include "spark/io/websocket/JsonRpcRequest.h"
#include "spark/io/websocket/JsonRpcResponse.h"
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <nlohmann/json.hpp>
@@ -5,7 +5,7 @@
#include "spark/io/websocket/JsonRpcError.h"
#include "spark/io/websocket/JsonRpcRequest.h"
#include "spark/io/websocket/JsonRpcResponse.h"
#include "spark/utils/Delegate.h"
#include "system/delegate.h"
#include <memory>
#include <nlohmann/json.hpp>
+1 -1
View File
@@ -1,7 +1,7 @@
#pragma once
#include "spark/domain_model/Security.pb.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/bts_assert.h"
#include "system/bts_array_size.h"
namespace bts {
namespace md {
-1
View File
@@ -6,7 +6,6 @@
#include "spark/os/os.h"
#include "spark/utils/Optimistic.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_no_copy.h"
#include "spark/utils/warn_ctrl.h"
+1 -1
View File
@@ -6,12 +6,12 @@
#include "spark/io/JsonDumper.h"
#include "spark/io/JsonReader.h"
#include "spark/utils/ValueWrapper.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_export.h"
#include "spark/utils/bts_no_copy.h"
#include "spark/utils/warn_ctrl.h"
#include "spark/domain_model/SecurityKey.h"
#include "system/bts_array_size.h"
#include <nlohmann/json.hpp>
#include <sstream>
+1 -1
View File
@@ -2,10 +2,10 @@
#include "spark/config/config.h"
#include "spark/config/protos/system_config.pb.h"
#include "spark/os/app_data_dir.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_debugger.h"
#include "spark/utils/helper_macros.h"
#include "system/bts_array_size.h"
#include <boost/algorithm/string/replace.hpp>
#include <boost/asio.hpp>
+1 -1
View File
@@ -3,10 +3,10 @@
#include "spark/config/protos/perflog_cfg.pb.h"
#include "spark/config/protos/system_config.pb.h"
#include "spark/persistence/persistence_utils.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/datetime.h"
#include "spark/utils/stats.h"
#include "spark/utils/thread_manager.h"
#include "system/bts_array_size.h"
#include <SQLiteCpp/SQLiteCpp.h>
#include <boost/algorithm/string/replace.hpp>
+1 -1
View File
@@ -2,10 +2,10 @@
#include "spark/logging/logging.h"
#include "spark/utils/SharedMemory.h"
#include "spark/utils/bts_array_size.h"
#include "spark/utils/bts_assert.h"
#include "spark/utils/bts_no_copy.h"
#include "spark/utils/defer.h"
#include "system/bts_array_size.h"
#include "spark/config/config.h"
#include "spark/config/protos/system_config.pb.h"
+2 -1
View File
@@ -1,8 +1,9 @@
#include "Uuid.h"
#include "bts_array_size.h"
#include "bts_assert.h"
#include "spark/os/os.h"
#include "system/bts_array_size.h"
#include <algorithm>
#include <cstring>
#include <sstream>
+1 -1
View File
@@ -1,7 +1,7 @@
#pragma once
#include "bts_array_size.h"
#include "bts_export.h"
#include "system/bts_array_size.h"
#include <algorithm>
#include <cstring>
+6
View File
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.23)
project(system)
include(${CMAKE_SOURCE_DIR}/cmake/InstallBasic.cmake)
add_library(${PROJECT_NAME} INTERFACE)
install_basic()
+5
View File
@@ -0,0 +1,5 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
check_required_components(@PROJECT_NAME@)
File renamed without changes.
+108
View File
@@ -0,0 +1,108 @@
#pragma once
#include <cstddef>
#include <string>
#include <vector>
namespace bts::system::string_utils {
void snake_to_pascal_case(std::string &str);
template <class Range>
std::vector<std::string> split(Range range, const std::string &delimiter);
template <class Iterator>
std::vector<std::string> split(
Iterator first, Iterator last, const std::string &delimiter);
template <class Iterator>
void split(
Iterator first,
Iterator last,
const std::string &delimiter,
std::vector<std::string> &parts);
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline void snake_to_pascal_case(std::string &str) {
std::size_t n = 0;
// Remove leading '_'
while (!str.empty() && str[0] == '_') {
n++;
}
if (n > 0) {
str.erase(0, n);
}
std::size_t pos = 0;
// Remove middle '_' (word delimiter), capitalize first letter of words
for (n = 0; n < str.size();) {
str[n] = toupper(str[n]);
pos = ++n;
while (n < str.size() && str[n] == '_') {
n++;
}
if (n > pos) {
str.erase(pos, n - pos);
}
}
// Remove trailing '_'
while (!str.empty() && str.back() == '_') {
n++;
}
if (n > 0) {
str.erase(str.size() - n);
}
}
template <class Range>
std::vector<std::string> split(Range range, const std::string &delimiter) {
std::vector<std::string> parts;
split(range.begin(), range.end(), delimiter, parts);
return parts;
}
template <class Iterator>
std::vector<std::string> split(
Iterator first, Iterator last, const std::string &delimiter) {
std::vector<std::string> parts;
split(first, last, delimiter, parts);
return parts;
}
template <class Iterator>
void split(
Iterator first,
Iterator last,
const std::string &delimiter,
std::vector<std::string> &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::system::string_utils