ITCHCPP 1.6.0
High-Performance NASDAQ TotalView-ITCH 5.0 Parser & Market-Data Platform
Loading...
Searching...
No Matches
parser.hpp
Go to the documentation of this file.
1#pragma once
2
14
15#include <algorithm>
16#include <array>
17#include <bit>
18#include <concepts>
19#include <cstddef>
20#include <cstdint>
21#include <functional>
22#include <istream>
23#include <optional>
24#include <span>
25#include <vector>
26#include <version>
27
28#ifdef __cpp_lib_expected
29#include <expected>
30#endif
31
32#include "itch/messages.hpp"
33
34namespace itch {
35
40using MessageCallback = std::function<void(const Message&)>;
41
47enum class ParseError {
48 truncated,
51};
52
58using ErrorCallback = std::function<void(ParseError, char)>;
59
78class Parser {
79 public:
84 Parser() = default;
85
101 auto parse(const char* data, size_t size, const MessageCallback& callback) -> void;
102
115 auto parse(const char* data, size_t size) -> std::vector<Message>;
116
130 auto parse(const char* data, size_t size, const std::vector<char>& messages)
131 -> std::vector<Message>;
132
146 auto parse(std::istream& data, const MessageCallback& callback) -> void;
147
158 auto parse(std::istream& data) -> std::vector<Message>;
159
170 auto parse(std::istream& data, const std::vector<char>& messages) -> std::vector<Message>;
171
183 auto parse(std::span<const std::byte> data, const MessageCallback& callback) -> void;
184
190 auto parse(std::span<const std::byte> data) -> std::vector<Message>;
191
198 auto parse(std::span<const std::byte> data, const std::vector<char>& messages)
199 -> std::vector<Message>;
200
201#ifdef __cpp_lib_expected
217 [[nodiscard]] auto try_parse(std::span<const std::byte> data, const MessageCallback& callback)
218 -> std::expected<void, ParseError>;
219
224 [[nodiscard]] auto try_parse(std::span<const std::byte> data
225 ) -> std::expected<std::vector<Message>, ParseError>;
226#endif
227
236 auto set_error_callback(ErrorCallback callback) -> void;
237
241 [[nodiscard]] auto unknown_message_count() const noexcept -> std::uint64_t {
242 return m_unknown_message_count;
243 }
244
250 [[nodiscard]] auto malformed_message_count() const noexcept -> std::uint64_t {
251 return m_malformed_message_count;
252 }
253
255 auto reset_diagnostics() noexcept -> void {
256 m_unknown_message_count = 0;
257 m_malformed_message_count = 0;
258 }
259
260 private:
268 auto parse_impl(const char* data, std::size_t size, const MessageCallback& callback)
269 -> std::optional<ParseError>;
270
276 auto report_error(ParseError error, char message_type) -> void;
277
278 ErrorCallback m_error_callback {};
279 std::uint64_t m_unknown_message_count {0};
280 std::uint64_t m_malformed_message_count {0};
281};
282
283namespace utils {
284
295template <std::integral IntType>
296[[nodiscard]] constexpr auto swap_bytes(IntType value) noexcept -> IntType {
297#ifdef __cpp_lib_byteswap
298 return std::byteswap(value);
299#else
300 if constexpr (sizeof(IntType) == 1) {
301 return value;
302 } else {
303 auto bytes = std::bit_cast<std::array<std::byte, sizeof(IntType)>>(value);
304 std::ranges::reverse(bytes);
305 return std::bit_cast<IntType>(bytes);
306 }
307#endif
308}
309
318template <std::integral IntType>
319[[nodiscard]] constexpr auto from_big_endian(IntType value) noexcept -> IntType {
320 if constexpr (std::endian::native == std::endian::little) {
321 return swap_bytes(value);
322 } else {
323 return value;
324 }
325}
326
334template <typename ValueType>
335auto unpack(const char* buffer, std::size_t& offset) -> ValueType;
336
343inline auto unpack_string(const char* buffer, std::size_t& offset, char* dest, std::size_t size)
344 -> void;
345
351inline auto unpack_timestamp(const char* buffer, std::size_t& offset) -> std::uint64_t;
352} // namespace utils
353
354} // namespace itch
A high-performance parser for the NASDAQ TotalView-ITCH 5.0 protocol.
Definition parser.hpp:78
auto parse(const char *data, size_t size, const MessageCallback &callback) -> void
Parses messages from a memory buffer and invokes a callback for each.
Definition parser.cpp:343
auto unknown_message_count() const noexcept -> std::uint64_t
The number of frames skipped because their type byte was unknown.
Definition parser.hpp:241
auto reset_diagnostics() noexcept -> void
Resets the accumulating diagnostics counters to zero.
Definition parser.hpp:255
auto set_error_callback(ErrorCallback callback) -> void
Registers a callback invoked for each recoverable framing problem.
Definition parser.cpp:424
Parser()=default
Constructs a Parser instance.
auto malformed_message_count() const noexcept -> std::uint64_t
The number of frames skipped because their declared length was too small for the message type.
Definition parser.hpp:250
char message_type
Definition csv_sink.cpp:15
Defines every ITCH 5.0 message struct, the Message variant, and the stream-printing helpers used to f...
ParseError
Categories of recoverable problems encountered while framing a feed.
Definition parser.hpp:47
@ truncated
The buffer ended in the middle of a frame.
@ size_mismatch
The declared length is shorter than the message type requires.
@ unknown_type
The message type byte does not correspond to a known message.
std::function< void(ParseError, char)> ErrorCallback
The signature for the optional diagnostics callback.
Definition parser.hpp:58
std::variant< SystemEventMessage, StockDirectoryMessage, StockTradingActionMessage, RegSHOMessage, MarketParticipantPositionMessage, MWCBDeclineLevelMessage, MWCBStatusMessage, IPOQuotingPeriodUpdateMessage, LULDAuctionCollarMessage, OperationalHaltMessage, AddOrderMessage, AddOrderMPIDAttributionMessage, OrderExecutedMessage, OrderExecutedWithPriceMessage, OrderCancelMessage, OrderDeleteMessage, OrderReplaceMessage, NonCrossTradeMessage, CrossTradeMessage, BrokenTradeMessage, NOIIMessage, RetailPriceImprovementIndicatorMessage, DLCRMessage > Message
A variant able to hold any one of the ITCH 5.0 message structs.
Definition messages.hpp:483
std::function< void(const Message &)> MessageCallback
The signature for the callback function used in streaming parse methods.
Definition parser.hpp:40