refactor: Pass ostream to LiveLadderPrinter (don't hardcode cout)

This commit is contained in:
John Colvin committed 2023-01-04 13:35:38 -05:00
1 parent a5459a3aff
commit 88943f0857
2 files changed
+22 -4

No files matched your search

+5 -4
View File
@@ -46,12 +46,13 @@ static inline std::ostream &fmt_ladder(
return output;
}
LiveLadderPrinter::LiveLadderPrinter(std::ostream *out) : out_ptr_(out) {}
void LiveLadderPrinter::on_book(const domain::Book &book) {
auto bids = book[domain::Side::Buy];
auto asks = book[domain::Side::Sell];
if (bids.is_populated() || asks.is_populated()) {
std::cout << "Book Update (sid " << book.security_id()
<< "):" << std::endl;
out_() << "Book Update (sid " << book.security_id() << "):" << std::endl;
}
fmt_ladder(
bids.begin(), bids.end(), asks.rbegin(), asks.rend(),
@@ -59,9 +60,9 @@ void LiveLadderPrinter::on_book(const domain::Book &book) {
[](const domain::BookLevel &bl) { return bl.price; }),
std::function<domain::Quantity(const domain::BookLevel &)>(
[](const domain::BookLevel &bl) { return bl.total_quantity; }),
std::cout);
out_());
if (bids.is_populated() || asks.is_populated()) {
std::cout << std::endl;
out_() << std::endl;
}
}
+17
View File
@@ -1,12 +1,29 @@
#pragma once
#include "lib/md/MarketDataListener.h"
#include <ostream>
namespace bts::md::listeners {
class LiveLadderPrinter : public MarketDataListener {
public:
explicit LiveLadderPrinter(std::ostream *out = &std::cout);
void on_book(const domain::Book &book) override;
void on_error(const MarketDataError &error) override;
private:
std::ostream *out_ptr_;
std::ostream &out_();
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline std::ostream &LiveLadderPrinter::out_() {
bts_assert(out_ptr_ != nullptr);
return *out_ptr_;
}
} // namespace bts::md::listeners