High Frequency FIX Parser
C++ library for high frequency messaging with the Financial Information Exchange (FIX) protocol.
Public Member Functions | List of all members
hffix::message_writer Class Reference

One FIX message for writing. More...

#include <hffix.hpp>

Public Member Functions

 message_writer (char *buffer, size_t size)
 Construct by buffer size.
 
 message_writer (char *begin, char *end)
 Construct by buffer begin and end.
 
template<size_t N>
 message_writer (char(&buffer)[N])
 Construct on an array reference to a buffer.
 
 ~message_writer ()
 Owns no resources, so destruction is no-op.
 
Buffer Access
size_t message_size () const
 Size of the message in bytes.
 
char * message_begin () const
 Pointer to beginning of the message.
 
char * message_end () const
 Pointer to past-the-end of the message.
 
size_t buffer_size () const
 Total available buffer size, including buffer already written to by this message.
 
size_t buffer_size_remaining () const
 Remaining available buffer size. Excludes buffer already written to by this message.
 
Transport Fields
void push_back_header (char const *begin_string_version)
 Write the BeginString and BodyLength fields to the buffer.
 
void push_back_header (std::string_view begin_string_version)
 Write the BeginString and BodyLength fields to the buffer.
 
void push_back_trailer (bool calculate_checksum=true)
 Write the CheckSum field to the buffer.
 
String Fields
void push_back_string (int tag, char const *begin, char const *end)
 Append a string field to the message.
 
void push_back_string (int tag, char const *cstring)
 Append a string field to the message.
 
void push_back_string (int tag, std::string const &s)
 Append a string field to the message.
 
void push_back_string (int tag, std::string_view s)
 Append a string field to the message.
 
void push_back_char (int tag, char character)
 Append a char field to the message.
 
Integer Fields
template<typename Int_type >
void push_back_int (int tag, Int_type number)
 Append an integer field to the message.
 
Decimal Float Fields
template<typename Int_type >
void push_back_decimal (int tag, Int_type mantissa, Int_type exponent)
 Append a decimal float field to the message.
 
Date and Time Fields
void push_back_date (int tag, int year, int month, int day)
 Append a LocalMktDate or UTCDate field to the message.
 
void push_back_monthyear (int tag, int year, int month)
 Append a month-year field to the message.
 
void push_back_timeonly (int tag, int hour, int minute, int second)
 Append a UTCTimeOnly field to the message.
 
void push_back_timeonly (int tag, int hour, int minute, int second, int millisecond)
 Append a UTCTimeOnly field to the message with millisecond precision.
 
void push_back_timeonly_nano (int tag, int hour, int minute, int second, int nanosecond)
 Append a UTCTimeOnly field to the message with up to nanosecond precision.
 
void push_back_timestamp (int tag, int year, int month, int day, int hour, int minute, int second)
 Append a UTCTimestamp field to the message.
 
void push_back_timestamp (int tag, int year, int month, int day, int hour, int minute, int second, int millisecond)
 Append a UTCTimestamp field to the message with millisecond precision.
 
void push_back_timestamp_nano (int tag, int year, int month, int day, int hour, int minute, int second, int nanosecond)
 Append a UTCTimestamp field to the message with nanosecond precision.
 
Boost Date and Time Fields
void push_back_date (int tag, boost::gregorian::date date)
 Append a LocalMktDate or UTCDate field to the message.
 
void push_back_timeonly (int tag, boost::posix_time::time_duration timeonly)
 Append a UTCTimeOnly field to the message with millisecond precision.
 
void push_back_timeonly_nano (int tag, boost::posix_time::time_duration timeonly)
 Append a UTCTimeOnly field to the message with nanosecond precision.
 
void push_back_timestamp (int tag, boost::posix_time::ptime timestamp)
 Append a UTCTimestamp field to the message with millisecond precision.
 
void push_back_timestamp_nano (int tag, boost::posix_time::ptime timestamp)
 Append a UTCTimestamp field to the message with nanosecond precision.
 
std::chrono Date and Time Fields
template<typename Clock , typename Duration >
void push_back_timestamp (int tag, std::chrono::time_point< Clock, Duration > tp)
 Append a std::chrono::time_point field to the message with millisecond precision.
 
template<typename Clock , typename Duration >
void push_back_timestamp_nano (int tag, std::chrono::time_point< Clock, Duration > tp)
 Append a std::chrono::time_point field to the message with nanosecond precision.
 
template<typename Rep , typename Period >
void push_back_timeonly (int tag, std::chrono::duration< Rep, Period > timeonly)
 Append a UTCTimeOnly field to the message with millisecond precision.
 
template<typename Rep , typename Period >
void push_back_timeonly_nano (int tag, std::chrono::duration< Rep, Period > timeonly)
 Append a UTCTimeOnly field to the message with nanosecond precision.
 

Data Fields

void push_back_data (int tag_data_length, int tag_data, char const *begin, char const *end)
 Append a data length field and a data field to the message.
 

Detailed Description

One FIX message for writing.

Given a buffer, the message_writer will write a FIX message to the buffer. message_writer does not take ownership of the buffer.

Usage

The message_writer interface is patterned after Back Insertion Sequence Containers, with overloads of push_back for different FIX field data types.

The push_back_header() method will write the BeginString and BodyLength fields to the message, and the FIX Standard Message Header requires also MsgType, SenderCompID, TargetCompID, MsgSeqNum and SendingTime. You must write those fields yourself, starting with MsgType.

After calling all other push_back methods and before sending the message, you must call push_back_trailer(), which will write the CheckSum field for you.

Extension

Keep in mind that if you don't like the way any of these push_back methods perform serialization, then you can always do your own serialization for any data type, and then append it to the message by push_back_string().

For example, these two statements are equivalent for hffix::message_writer w.

w.push_back_int (hffix::tag::HeartBtInt, 12);
w.push_back_string(hffix::tag::HeartBtInt, "12");

For another example, if a trading peer has extended FIX for femtosecond timestamp precision, then a custom timestamp can be serialized to a string and then written to a field.

w.push_back_string(hffix::tag::SendingTime, "20180309-13:46:01.0123456789123456");

Definition at line 622 of file hffix.hpp.

Constructor & Destructor Documentation

◆ message_writer() [1/3]

hffix::message_writer::message_writer ( char *  buffer,
size_t  size 
)
inline

Construct by buffer size.

Parameters
bufferPointer to the buffer to be written to.
sizeSize of the buffer in bytes.

Definition at line 630 of file hffix.hpp.

◆ message_writer() [2/3]

hffix::message_writer::message_writer ( char *  begin,
char *  end 
)
inline

Construct by buffer begin and end.

Parameters
beginPointer to the buffer to be written to.
endPointer to past-the-end of the buffer to be written to.

Definition at line 642 of file hffix.hpp.

◆ message_writer() [3/3]

template<size_t N>
hffix::message_writer::message_writer ( char(&)  buffer[N])
inline

Construct on an array reference to a buffer.

Template Parameters
NThe size of the array.
Parameters
bufferAn array reference. The writer will write into the entire array of length N.

Definition at line 655 of file hffix.hpp.

◆ ~message_writer()

hffix::message_writer::~message_writer ( )
inline

Owns no resources, so destruction is no-op.

Definition at line 666 of file hffix.hpp.

Member Function Documentation

◆ buffer_size()

size_t hffix::message_writer::buffer_size ( ) const
inline

Total available buffer size, including buffer already written to by this message.

Definition at line 700 of file hffix.hpp.

◆ buffer_size_remaining()

size_t hffix::message_writer::buffer_size_remaining ( ) const
inline

Remaining available buffer size. Excludes buffer already written to by this message.

Definition at line 707 of file hffix.hpp.

◆ message_begin()

char * hffix::message_writer::message_begin ( ) const
inline

Pointer to beginning of the message.

Precondition
None.

Definition at line 685 of file hffix.hpp.

◆ message_end()

char * hffix::message_writer::message_end ( ) const
inline

Pointer to past-the-end of the message.

Precondition
push_back_trailer() has been called.

Definition at line 693 of file hffix.hpp.

◆ message_size()

size_t hffix::message_writer::message_size ( ) const
inline

Size of the message in bytes.

Precondition
push_back_trailer() has been called.

Definition at line 676 of file hffix.hpp.

◆ push_back_char()

void hffix::message_writer::push_back_char ( int  tag,
char  character 
)
inline

Append a char field to the message.

Parameters
tagFIX tag.
characterAn ascii character.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 915 of file hffix.hpp.

◆ push_back_data()

void hffix::message_writer::push_back_data ( int  tag_data_length,
int  tag_data,
char const *  begin,
char const *  end 
)
inline

Append a data length field and a data field to the message.

Note that this method will append two fields to the message. The first field is an integer equal to the content length of the second field. FIX does this so that the content of the second field may contain Ascii NULL or SOH or other control characters.

High Frequency FIX Parser calculates the content length for you and writes out both fields, you just have to provide both tags and pointers to the data. For most of the data fields in FIX, it is true that tag_data = tag_data_length + 1, but we daren't assume that.

Example:

std::string data("Some data.");
One FIX message for writing.
Definition hffix.hpp:622
void push_back_data(int tag_data_length, int tag_data, char const *begin, char const *end)
Append a data length field and a data field to the message.
Definition hffix.hpp:1513
Parameters
tag_data_lengthFIX tag for the data length field.
tag_dataFIX tag for the data field.
beginPointer to the beginning of the data.
endPointer to after-the-end of the data.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1513 of file hffix.hpp.

◆ push_back_date() [1/2]

void hffix::message_writer::push_back_date ( int  tag,
boost::gregorian::date  date 
)
inline

Append a LocalMktDate or UTCDate field to the message.

Parameters
tagFIX tag.
dateDate.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
See also
HFFIX_NO_BOOST_DATETIME

Definition at line 1267 of file hffix.hpp.

◆ push_back_date() [2/2]

void hffix::message_writer::push_back_date ( int  tag,
int  year,
int  month,
int  day 
)
inline

Append a LocalMktDate or UTCDate field to the message.

Parameters
tagFIX tag.
yearYear.
monthMonth.
dayDay.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 990 of file hffix.hpp.

◆ push_back_decimal()

template<typename Int_type >
void hffix::message_writer::push_back_decimal ( int  tag,
Int_type  mantissa,
Int_type  exponent 
)
inline

Append a decimal float field to the message.

The decimal float is of the form mantissa×10exponent.

Non-normalized. The exponent parameter must be less than or equal to zero. If the exponent parameter is zero, no decimal point will be written to the ascii field.

Template Parameters
Int_typeInteger type for the mantissa and exponent.
Parameters
tagFIX tag.
mantissaThe mantissa of the decimal float.
exponentThe exponent of the decimal float. Must be less than or equal to zero.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 966 of file hffix.hpp.

◆ push_back_header() [1/2]

void hffix::message_writer::push_back_header ( char const *  begin_string_version)
inline

Write the BeginString and BodyLength fields to the buffer.

This method must be called before any other push_back method. It may only be called once for each message_writer.

Precondition
No other push_back method has yet been called.
Parameters
begin_string_versionThe value for the BeginString FIX field. Should probably be "FIX.4.2" or "FIX.4.3" or "FIX.4.4" or "FIXT.1.1" (for FIX 5.0).
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
std::logic_errorWhen called more than once for a single message.

Definition at line 727 of file hffix.hpp.

◆ push_back_header() [2/2]

void hffix::message_writer::push_back_header ( std::string_view  begin_string_version)
inline

Write the BeginString and BodyLength fields to the buffer.

This method must be called before any other push_back method. It may only be called once for each message_writer.

Precondition
No other push_back method has yet been called.
Parameters
begin_string_versionThe value for the BeginString FIX field. Should probably be "FIX.4.2" or "FIX.4.3" or "FIX.4.4" or "FIXT.1.1" (for FIX 5.0).
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
std::logic_errorWhen called more than once for a single message.

Definition at line 756 of file hffix.hpp.

◆ push_back_int()

template<typename Int_type >
void hffix::message_writer::push_back_int ( int  tag,
Int_type  number 
)
inline

Append an integer field to the message.

Template Parameters
Int_typeType of integer.
Parameters
tagFIX tag.
numberInteger value.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 937 of file hffix.hpp.

◆ push_back_monthyear()

void hffix::message_writer::push_back_monthyear ( int  tag,
int  year,
int  month 
)
inline

Append a month-year field to the message.

Parameters
tagFIX tag.
yearYear.
monthMonth.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1013 of file hffix.hpp.

◆ push_back_string() [1/4]

void hffix::message_writer::push_back_string ( int  tag,
char const *  begin,
char const *  end 
)
inline

Append a string field to the message.

Parameters
tagFIX tag.
beginPointer to the beginning of the string.
endPointer to past-the-end of the string.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 848 of file hffix.hpp.

◆ push_back_string() [2/4]

void hffix::message_writer::push_back_string ( int  tag,
char const *  cstring 
)
inline

Append a string field to the message.

Parameters
tagFIX tag.
cstringPointer to the beginning of a C-style null-terminated string.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 867 of file hffix.hpp.

◆ push_back_string() [3/4]

void hffix::message_writer::push_back_string ( int  tag,
std::string const &  s 
)
inline

Append a string field to the message.

The entire, literal contents of s will be copied to the output buffer, so if you are using std::wstring you may need to first convert from UTF-32 to UTF-8, or do some other encoding transformation.

Parameters
tagFIX tag.
sString.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 886 of file hffix.hpp.

◆ push_back_string() [4/4]

void hffix::message_writer::push_back_string ( int  tag,
std::string_view  s 
)
inline

Append a string field to the message.

The range of s will be copied to the output buffer.

Parameters
tagFIX tag.
sString.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 902 of file hffix.hpp.

◆ push_back_timeonly() [1/4]

void hffix::message_writer::push_back_timeonly ( int  tag,
boost::posix_time::time_duration  timeonly 
)
inline

Append a UTCTimeOnly field to the message with millisecond precision.

No time zone or daylight savings time transformations are done to the time.

Fractional seconds will be written to the field, rounded to the millisecond.

Parameters
tagFIX tag.
timeonlyTime.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
See also
HFFIX_NO_BOOST_DATETIME

Definition at line 1286 of file hffix.hpp.

◆ push_back_timeonly() [2/4]

void hffix::message_writer::push_back_timeonly ( int  tag,
int  hour,
int  minute,
int  second 
)
inline

Append a UTCTimeOnly field to the message.

No time zone or daylight savings time transformations are done to the time.

No fractional seconds are written to the field.

Parameters
tagFIX tag.
hourHour.
minuteMinute.
secondSecond.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1040 of file hffix.hpp.

◆ push_back_timeonly() [3/4]

void hffix::message_writer::push_back_timeonly ( int  tag,
int  hour,
int  minute,
int  second,
int  millisecond 
)
inline

Append a UTCTimeOnly field to the message with millisecond precision.

No time zone or daylight savings time transformations are done to the time.

Parameters
tagFIX tag.
hourHour.
minuteMinute.
secondSecond.
millisecondMillisecond.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1070 of file hffix.hpp.

◆ push_back_timeonly() [4/4]

template<typename Rep , typename Period >
void hffix::message_writer::push_back_timeonly ( int  tag,
std::chrono::duration< Rep, Period >  timeonly 
)
inline

Append a UTCTimeOnly field to the message with millisecond precision.

No time zone or daylight savings time transformations are done to the time.

Fractional seconds will be written to the field, rounded to the millisecond.

Parameters
tagFIX tag.
timeonlyTime.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1445 of file hffix.hpp.

◆ push_back_timeonly_nano() [1/3]

void hffix::message_writer::push_back_timeonly_nano ( int  tag,
boost::posix_time::time_duration  timeonly 
)
inline

Append a UTCTimeOnly field to the message with nanosecond precision.

No time zone or daylight savings time transformations are done to the time.

Fractional seconds will be written to the field, rounded to the nanosecond.

Parameters
tagFIX tag.
timeonlyTime.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
See also
HFFIX_NO_BOOST_DATETIME

Definition at line 1311 of file hffix.hpp.

◆ push_back_timeonly_nano() [2/3]

void hffix::message_writer::push_back_timeonly_nano ( int  tag,
int  hour,
int  minute,
int  second,
int  nanosecond 
)
inline

Append a UTCTimeOnly field to the message with up to nanosecond precision.

No time zone or daylight savings time transformations are done to the time.

Parameters
tagFIX tag.
hourHour.
minuteMinute.
secondSecond.
nanosecondNanosecond.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1103 of file hffix.hpp.

◆ push_back_timeonly_nano() [3/3]

template<typename Rep , typename Period >
void hffix::message_writer::push_back_timeonly_nano ( int  tag,
std::chrono::duration< Rep, Period >  timeonly 
)
inline

Append a UTCTimeOnly field to the message with nanosecond precision.

No time zone or daylight savings time transformations are done to the time.

Fractional seconds will be written to the field, rounded to the nanosecond.

Parameters
tagFIX tag.
timeonlyTime.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1470 of file hffix.hpp.

◆ push_back_timestamp() [1/4]

void hffix::message_writer::push_back_timestamp ( int  tag,
boost::posix_time::ptime  timestamp 
)
inline

Append a UTCTimestamp field to the message with millisecond precision.

No time zone or daylight savings time transformations are done to the timestamp.

Fractional seconds will be written to the field, rounded to the millisecond.

Parameters
tagFIX tag.
timestampDate and time.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
See also
HFFIX_NO_BOOST_DATETIME

Definition at line 1336 of file hffix.hpp.

◆ push_back_timestamp() [2/4]

void hffix::message_writer::push_back_timestamp ( int  tag,
int  year,
int  month,
int  day,
int  hour,
int  minute,
int  second 
)
inline

Append a UTCTimestamp field to the message.

No time zone or daylight savings time transformations are done to the timestamp.

No fractional seconds are written to the field.

Parameters
tagFIX tag.
yearYear.
monthMonth.
dayDay.
hourHour.
minuteMinute.
secondSecond.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1140 of file hffix.hpp.

◆ push_back_timestamp() [3/4]

void hffix::message_writer::push_back_timestamp ( int  tag,
int  year,
int  month,
int  day,
int  hour,
int  minute,
int  second,
int  millisecond 
)
inline

Append a UTCTimestamp field to the message with millisecond precision.

No time zone or daylight savings time transformations are done to the timestamp.

Parameters
tagFIX tag.
yearYear.
monthMonth.
dayDay.
hourHour.
minuteMinute.
secondSecond.
millisecondMillisecond.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1181 of file hffix.hpp.

◆ push_back_timestamp() [4/4]

template<typename Clock , typename Duration >
void hffix::message_writer::push_back_timestamp ( int  tag,
std::chrono::time_point< Clock, Duration >  tp 
)
inline

Append a std::chrono::time_point field to the message with millisecond precision.

Fractional seconds will be written to the field, rounded to the millisecond.

Uses algorithms from http://howardhinnant.github.io/date_algorithms.html , which implement a proleptic Gregorian calendar. This will probably be superseded by C++20.

Parameters
tagFIX tag.
tpstd::chrono::time_point.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1403 of file hffix.hpp.

◆ push_back_timestamp_nano() [1/3]

void hffix::message_writer::push_back_timestamp_nano ( int  tag,
boost::posix_time::ptime  timestamp 
)
inline

Append a UTCTimestamp field to the message with nanosecond precision.

No time zone or daylight savings time transformations are done to the timestamp.

Fractional seconds will be written to the field, rounded to the nanosecond.

Parameters
tagFIX tag.
timestampDate and time.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
See also
HFFIX_NO_BOOST_DATETIME

Definition at line 1366 of file hffix.hpp.

◆ push_back_timestamp_nano() [2/3]

void hffix::message_writer::push_back_timestamp_nano ( int  tag,
int  year,
int  month,
int  day,
int  hour,
int  minute,
int  second,
int  nanosecond 
)
inline

Append a UTCTimestamp field to the message with nanosecond precision.

No time zone or daylight savings time transformations are done to the timestamp.

Parameters
tagFIX tag.
yearYear.
monthMonth.
dayDay.
hourHour.
minuteMinute.
secondSecond.
nanosecondNanosecond.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1224 of file hffix.hpp.

◆ push_back_timestamp_nano() [3/3]

template<typename Clock , typename Duration >
void hffix::message_writer::push_back_timestamp_nano ( int  tag,
std::chrono::time_point< Clock, Duration >  tp 
)
inline

Append a std::chrono::time_point field to the message with nanosecond precision.

Fractional seconds will be written to the field, rounded to the nanosecond.

Uses algorithms from http://howardhinnant.github.io/date_algorithms.html , which implement a proleptic Gregorian calendar. This will probably be superseded by C++20.

Parameters
tagFIX tag.
tpstd::chrono::time_point.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.

Definition at line 1425 of file hffix.hpp.

◆ push_back_trailer()

void hffix::message_writer::push_back_trailer ( bool  calculate_checksum = true)
inline

Write the CheckSum field to the buffer.

This function must be called after all other push_back functions. It may only be called once for each message_writer.

Precondition
push_back_header() method has been called.
Postcondition
There is a complete and valid FIX message in the buffer.
Parameters
calculate_checksumIf this flag is set to false, then instead of iterating over the entire message and calculating the CheckSum, the standard trailer will simply write CheckSum=000. This is fine if you're sending the message to a FIX parser that, like High Frequency FIX Parser, doesn't care about the CheckSum.
Exceptions
std::out_of_rangeWhen the remaining buffer size is too small.
std::logic_errorWhen called before message_writer::push_back_header()

Definition at line 790 of file hffix.hpp.


The documentation for this class was generated from the following file: