|
| 1 | +/*! |
| 2 | + \file zpp_bits.cpp |
| 3 | + \brief zpp::bits serialization example |
| 4 | + \author Ivan Shynkarenka |
| 5 | + \date 16.07.2025 |
| 6 | + \copyright MIT License |
| 7 | +*/ |
| 8 | + |
| 9 | +#include "../proto/trade.h" |
| 10 | + |
| 11 | +#include <iostream> |
| 12 | + |
| 13 | +int main(int argc, char** argv) |
| 14 | +{ |
| 15 | + // Create a new account with some orders |
| 16 | + TradeProto::Account account(1, "Test", "USD", 1000); |
| 17 | + account.Orders.emplace_back(TradeProto::Order(1, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::MARKET, 1.23456, 1000)); |
| 18 | + account.Orders.emplace_back(TradeProto::Order(2, "EURUSD", TradeProto::OrderSide::SELL, TradeProto::OrderType::LIMIT, 1.0, 100)); |
| 19 | + account.Orders.emplace_back(TradeProto::Order(3, "EURUSD", TradeProto::OrderSide::BUY, TradeProto::OrderType::STOP, 1.5, 10)); |
| 20 | + |
| 21 | + // Serialize the account to the zpp::bits buffer |
| 22 | + auto [buffer, out] = zpp::bits::data_out(); |
| 23 | + (void) out(account); |
| 24 | + |
| 25 | + // Show original and zpp::bits serialized sizes |
| 26 | + std::cout << "Original size: " << account.size() << std::endl; |
| 27 | + std::cout << "zpp::bits size: " << buffer.size() << std::endl; |
| 28 | + |
| 29 | + // Deserialize the account from the zpp::bits buffer |
| 30 | + TradeProto::Account deserialized; |
| 31 | + (void) zpp::bits::in{buffer}(deserialized); |
| 32 | + |
| 33 | + // Show account content |
| 34 | + std::cout << std::endl; |
| 35 | + std::cout << "Account.Id = " << deserialized.Id << std::endl; |
| 36 | + std::cout << "Account.Name = " << deserialized.Name << std::endl; |
| 37 | + std::cout << "Account.Wallet.Currency = " << deserialized.Wallet.Currency << std::endl; |
| 38 | + std::cout << "Account.Wallet.Amount = " << deserialized.Wallet.Amount << std::endl; |
| 39 | + for (const auto& order : deserialized.Orders) |
| 40 | + { |
| 41 | + std::cout << "Account.Order => Id: " << order.Id |
| 42 | + << ", Symbol: " << order.Symbol |
| 43 | + << ", Side: " << (int)order.Side |
| 44 | + << ", Type: " << (int)order.Type |
| 45 | + << ", Price: " << order.Price |
| 46 | + << ", Volume: " << order.Volume |
| 47 | + << std::endl; |
| 48 | + } |
| 49 | + |
| 50 | + return 0; |
| 51 | +} |
0 commit comments