Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __parsing_events_hpp
- #define __parsing_events_hpp
- #include <parsing/types.hpp>
- #include <chrono>
- namespace mira {
- template <typename Type>
- class basic_event {
- public:
- Type type;
- size_t orig_ns;
- const ticker_t* symbol = nullptr;
- void set_symbol(const ticker_t* new_symbol) {
- symbol = new_symbol;
- if (event_) {
- event_->set_symbol(symbol);
- }
- }
- template <typename Event>
- const Event& get() const {
- if (!event_) {
- return *static_cast<const Event*>(this);
- }
- return *static_cast<const Event*>(event_.get());
- }
- template <typename Event>
- void set(Event&& event) {
- type = event.type;
- event_ = std::make_shared<Event>(std::move(event));
- }
- protected:
- std::shared_ptr<basic_event> event_;
- };
- template <typename In>
- class basic_handler {
- public:
- virtual void handle(In event) = 0;
- };
- template <>
- class basic_handler<void> {
- public:
- virtual void handle() = 0;
- };
- template <typename Out>
- class callback_storage {
- public:
- void add_callback(basic_handler<Out>* callback_obj, const ticker_t* symbol = nullptr) {
- callbacks_.push_back(std::make_pair(callback_obj, symbol));
- }
- protected:
- using callback = std::pair<basic_handler<Out>*, const ticker_t*>;
- std::vector<callback> callbacks_;
- };
- template <typename In, typename Out>
- class advanced_handler : public basic_handler<In>, virtual public callback_storage<Out> {
- public:
- using callback_storage<Out>::callbacks_;
- void handle(In event) {
- if constexpr (std::is_same<Out, void>::value) {
- _process(event);
- } else {
- Out result = _process(event);
- if (result.type == decltype(result.type)::NONE) {
- return;
- }
- result.set_symbol(event.symbol);
- for (auto callback_obj : callbacks_) {
- if (callback_obj.second == nullptr || event.symbol == callback_obj.second) {
- callback_obj.first->handle(result);
- }
- }
- }
- }
- // void add_callback(basic_handler<Out>* callback_obj) {
- // callbacks_.push_back(std::make_pair(callback_obj, nullptr));
- // }
- virtual ~advanced_handler() {}
- protected:
- virtual Out _process(const In& event) = 0;
- };
- enum class e_stream : size_t {
- TRADE,
- PART_DEPTH,
- DIFF_DEPTH,
- BOOK,
- ACCOUNT_UPDATE,
- ACCOUNT_TRADE,
- ACCOUNT_ORDER,
- TICK,
- NONE
- };
- class ws_event : public basic_event<e_stream> {
- public:
- size_t event_time;
- };
- class ws_trade_event : public ws_event {
- public:
- std::vector<trade_t> trades;
- };
- class ws_part_depth_event : public ws_event {
- public:
- depth_t depth;
- };
- class ws_diff_depth_event : public ws_event {
- public:
- depth_t depth;
- size_t first_version;
- size_t last_version;
- };
- class ws_book_event : public ws_event {
- public:
- book_t book;
- };
- class ws_account_update_event : public ws_event {
- public:
- balance_t balance;
- };
- class ws_account_trade_event : public ws_event {
- public:
- trade_t trade;
- size_t client_order_id;
- commission_t commission;
- bool is_self_trade;
- bool is_maker;
- };
- class ws_account_order_event : public ws_event {
- public:
- order_t order;
- };
- struct stream_description_t {
- e_stream stream;
- std::unordered_map<std::string, std::string> args;
- };
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement