#pragma once #include "python_interop/py_contract_specification.h" #include "python_interop/py_future.h" #include "python_interop/py_option.h" #include "python_interop/py_spot.h" #include "python_interop/py_spread.h" #include "spark/io/json_formatter.h" #include #include #include #include #include #include namespace bts::md::py_interop { class PySecurity { public: constexpr std::uint32_t id() const; void set_id(std::uint32_t value); const std::string &exchange_id() const; void set_exchange_id(std::string value); const std::string &name() const; void set_name(std::string value); const std::string &venue() const; void set_venue(std::string value); const std::string ¤cy() const; void set_currency(std::string value); const std::string &group() const; void set_group(std::string value); const std::string &asset() const; void set_asset(std::string value); const PySpot &spot() const; void set_spot(PySpot); constexpr bool is_spot() const; const PyFuture &future() const; void set_future(PyFuture); constexpr bool is_future() const; const PyOption &option() const; void set_option(PyOption); constexpr bool is_option() const; const PySpread &spread() const; void set_spread(PySpread); constexpr bool is_spread() const; const PyContractSpecification &contract_spec() const; void set_contract_spec(PyContractSpecification value); std::string to_string() const; std::string format(const std::string &format_spec) const; private: std::uint32_t id_; std::string exchange_id_; std::string venue_; std::string group_; std::string asset_; std::string name_; std::string currency_; PyContractSpecification contract_spec_; std::variant sec_; friend struct fmt::formatter; friend void to_json(nlohmann::json &j, const PySecurity &sec); friend void from_json(const nlohmann::json &j, PySecurity &sec); }; void to_json(nlohmann::json &j, const PySecurity &sec); void from_json(const nlohmann::json &j, PySecurity &sec); } // namespace bts::md::py_interop ////////////////////////////////////////////////// // Definition ////////////////////////////////////////////////// template <> struct fmt::formatter { // Parse format specifier: only support 'j' flag for JSON output bool json_format = false; constexpr auto parse(format_parse_context &ctx) { auto it = ctx.begin(); auto end = ctx.end(); if (it != end && *it == 'j') { json_format = true; ++it; } if (it != end && *it != '}') { throw format_error("invalid format"); } return it; } template auto format( const bts::md::py_interop::PySecurity &sec, FormatContext &ctx) const { if (json_format) { return fmt::format_to(ctx.out(), "{}", bts::io::json_formatter(sec)); } else { return fmt::format_to(ctx.out(), "{}", sec.name()); } } }; namespace bts::md::py_interop { constexpr std::uint32_t PySecurity::id() const { return id_; } inline void PySecurity::set_id(std::uint32_t value) { id_ = value; } inline const std::string &PySecurity::exchange_id() const { return exchange_id_; } inline void PySecurity::set_exchange_id(std::string value) { exchange_id_ = std::move(value); } inline const std::string &PySecurity::name() const { return name_; } inline void PySecurity::set_name(std::string value) { name_ = std::move(value); } inline const std::string &PySecurity::venue() const { return venue_; } inline void PySecurity::set_venue(std::string value) { venue_ = std::move(value); } inline const std::string &PySecurity::currency() const { return currency_; } inline void PySecurity::set_currency(std::string value) { currency_ = std::move(value); } inline const std::string &PySecurity::group() const { return group_; } inline void PySecurity::set_group(std::string value) { group_ = std::move(value); } inline const std::string &PySecurity::asset() const { return asset_; } inline void PySecurity::set_asset(std::string value) { asset_ = std::move(value); } inline const PySpot &PySecurity::spot() const { if (std::holds_alternative(sec_)) { return std::get(sec_); } throw std::runtime_error{"PySecurity is not a spot"}; } inline void PySecurity::set_spot(PySpot value) { sec_ = std::move(value); } constexpr bool PySecurity::is_spot() const { return std::holds_alternative(sec_); } inline const PyFuture &PySecurity::future() const { if (std::holds_alternative(sec_)) { return std::get(sec_); } throw std::runtime_error{"PySecurity is not a future"}; } inline void PySecurity::set_future(PyFuture value) { sec_ = std::move(value); } constexpr bool PySecurity::is_future() const { return std::holds_alternative(sec_); } inline const PyOption &PySecurity::option() const { if (std::holds_alternative(sec_)) { return std::get(sec_); } throw std::runtime_error{"PySecurity is not an option"}; } inline void PySecurity::set_option(PyOption value) { sec_ = std::move(value); } constexpr bool PySecurity::is_option() const { return std::holds_alternative(sec_); } inline const PySpread &PySecurity::spread() const { if (std::holds_alternative(sec_)) { return std::get(sec_); } throw std::runtime_error{"PySecurity is not a spread"}; } inline void PySecurity::set_spread(PySpread value) { sec_ = std::move(value); } constexpr bool PySecurity::is_spread() const { return std::holds_alternative(sec_); } inline const PyContractSpecification &PySecurity::contract_spec() const { return contract_spec_; } inline void PySecurity::set_contract_spec(PyContractSpecification value) { contract_spec_ = std::move(value); } inline std::string PySecurity::to_string() const { return name_; } inline std::string PySecurity::format(const std::string &format_spec) const { if (format_spec.empty()) { return fmt::format("{}", *this); } return fmt::format(fmt::runtime("{:" + format_spec + "}"), *this); } } // namespace bts::md::py_interop