247 lines
6.4 KiB
C++
247 lines
6.4 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <string_view>
|
|
|
|
#include "lib/jetfix/codec/ReaderCursor.h"
|
|
#include "lib/utils/meta_programming.h"
|
|
#include "lib/utils/datetime.h"
|
|
#include "lib/jetfix/codec/sum_bytes.h"
|
|
#include "lib/jetfix/codec/i32_toa.h"
|
|
#include "lib/jetfix/codec/f_toa.h"
|
|
#include "lib/jetfix/codec/IFieldReader.h"
|
|
|
|
namespace bts {
|
|
namespace jetfix {
|
|
|
|
template <uint16_t t_tag, typename ValueType>
|
|
class BaseField : public IFieldReader
|
|
{
|
|
// compile time calculation of tag checksum
|
|
template <bool b, uint32_t N> struct calc_checksum;
|
|
|
|
template <uint32_t N>
|
|
struct calc_checksum<true,N>
|
|
{static const uint32_t value = N + '0';};
|
|
|
|
template <uint32_t N>
|
|
struct calc_checksum<false,N>
|
|
{static const uint32_t value = (N%10 + '0') + calc_checksum<N/10<10,N/10>::value;};
|
|
|
|
|
|
public:
|
|
static uint16_t const tag;
|
|
static const char* tag_s;
|
|
static size_t const tag_sz;
|
|
static uint32_t const tag_checksum ;
|
|
|
|
typedef ValueType Value;
|
|
|
|
BaseField () : _value () {}
|
|
const ValueType & value () const {return _value;}
|
|
|
|
virtual uint32_t get_tag ()
|
|
{return tag;}
|
|
protected:
|
|
ValueType _value;
|
|
};
|
|
|
|
template <uint16_t t_tag, typename ValueType>
|
|
uint16_t const BaseField<t_tag,ValueType>::tag = t_tag;
|
|
|
|
template <uint16_t t_tag, typename ValueType>
|
|
const char* BaseField<t_tag,ValueType>::tag_s = meta::int_to_string<t_tag>::type::value;
|
|
|
|
template <uint16_t t_tag, typename ValueType>
|
|
size_t const BaseField<t_tag,ValueType>::tag_sz = meta::int_to_string<t_tag>::size;
|
|
|
|
template <uint16_t t_tag, typename ValueType>
|
|
uint32_t const BaseField<t_tag,ValueType>::tag_checksum = calc_checksum<t_tag<10,t_tag>::value;
|
|
|
|
template <uint16_t tag>
|
|
class CharField : public BaseField<tag,char>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,char> Parent;
|
|
|
|
void read (ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
bts_assert (loc.second == 1);
|
|
Parent::_value = cursor.data ()[loc.first];
|
|
}
|
|
|
|
constexpr static uint32_t max_value_size (char ) {return 1;}
|
|
static std::pair<uint32_t,uint32_t> write (char* buffer,char c)
|
|
{
|
|
*buffer = c;
|
|
return std::make_pair (1,c);
|
|
}
|
|
};
|
|
|
|
template <uint16_t tag>
|
|
class BoolField : public BaseField<tag,bool>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,bool> Parent;
|
|
|
|
void read(ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
bts_assert (loc.second == 1);
|
|
Parent::_value = cursor.data ()[loc.first] == 'Y';
|
|
}
|
|
|
|
constexpr static uint32_t max_value_size ( bool) {return 1;}
|
|
static std::pair<uint32_t,uint32_t> write (char *buffer,bool b)
|
|
{
|
|
if (b)
|
|
*buffer = 'Y';
|
|
else
|
|
*buffer = 'N';
|
|
return std::make_pair (1,*buffer);
|
|
}
|
|
};
|
|
|
|
template <uint16_t tag>
|
|
class FloatField : public BaseField<tag,double>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,double> Parent;
|
|
|
|
void read (ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
Parent::_value = atof (&(cursor.data ()[loc.first]));
|
|
}
|
|
|
|
static uint32_t max_value_size ( double,int, int pad)
|
|
{
|
|
if (pad == -1)
|
|
return 17; // 15 from FIX spec, 1 for decimal, 1 for sign
|
|
else
|
|
return pad;
|
|
}
|
|
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,double d,int decimals, int pad )
|
|
{
|
|
auto written = d_toa (d,buffer,max_value_size (d,decimals, pad)+1,decimals,pad);
|
|
return std::make_pair (written,sum_bytes (buffer,written));
|
|
}
|
|
};
|
|
|
|
template <uint16_t tag>
|
|
class IntField : public BaseField<tag,int32_t>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,int32_t> Parent;
|
|
|
|
void read (ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
Parent::_value = atoi (&(cursor.data ()[loc.first]));
|
|
}
|
|
|
|
static uint32_t max_value_size (int32_t,int pad = 0)
|
|
{
|
|
if (pad == 0)
|
|
return 11;
|
|
else
|
|
return pad;
|
|
}
|
|
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,int32_t i,int pad = 0)
|
|
{
|
|
auto written = i32_toa (i,buffer,pad);
|
|
return std::make_pair (written,sum_bytes (buffer,written));
|
|
}
|
|
};
|
|
|
|
template <uint16_t tag>
|
|
class BigIntField : public BaseField<tag,int64_t>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,int64_t> Parent;
|
|
|
|
void read (ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
Parent::_value = atoll (&(cursor.data ()[loc.first]));
|
|
}
|
|
|
|
static uint32_t max_value_size (int64_t,int pad = 0)
|
|
{
|
|
if (pad == 0)
|
|
return 20;
|
|
else
|
|
return pad;
|
|
}
|
|
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,int64_t i,int pad = 0)
|
|
{
|
|
auto written = i64_toa (i,buffer,pad);
|
|
return std::make_pair (written,sum_bytes (buffer,written));
|
|
}
|
|
};
|
|
|
|
template <uint16_t tag>
|
|
class StringField : public BaseField<tag,std::string>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,std::string> Parent;
|
|
|
|
void read (ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
BaseField<tag,std::string>::_value =
|
|
std::string (cursor.data ()+loc.first,loc.second);
|
|
}
|
|
|
|
|
|
static uint32_t max_value_size (std::string const&str) {return str.size ();}
|
|
static uint32_t max_value_size (const char*,uint32_t sz) {return sz;}
|
|
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,std::string const &str)
|
|
{return write (buffer,str.data (),str.size ());}
|
|
|
|
|
|
#if __cplusplus >= 201703L
|
|
static uint32_t max_value_size (std::string_view const&str) {return str.size ();}
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,std::string_view const &str)
|
|
{return write (buffer,str.data (),str.size ());}
|
|
#endif
|
|
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,const char*str,uint32_t sz)
|
|
{
|
|
memcpy (buffer,str,sz);
|
|
return std::make_pair (sz,sum_bytes (buffer,sz));
|
|
}
|
|
};
|
|
|
|
template <uint16_t tag>
|
|
class TimeField : public BaseField<tag,boost::posix_time::ptime>
|
|
{
|
|
public:
|
|
typedef BaseField<tag,boost::posix_time::ptime> Parent;
|
|
|
|
void read (ReaderCursor &cursor)
|
|
{
|
|
auto loc = cursor.read_field ();
|
|
BaseField<tag,boost::posix_time::ptime>::_value =
|
|
datetime::parse_FIX_timestamp (std::string (cursor.data ()+loc.first,loc.second));
|
|
}
|
|
|
|
constexpr static uint32_t max_value_size (boost::posix_time::ptime const&)
|
|
{return sizeof("20121204-14:44:02.000")-1;}
|
|
|
|
static std::pair<uint32_t,uint32_t> write (char*buffer,boost::posix_time::ptime const &t)
|
|
{
|
|
memcpy (buffer,datetime::FIX_timestamp (t).data (),max_value_size (t));
|
|
return std::make_pair (max_value_size (t),sum_bytes (buffer,max_value_size (t)));
|
|
}
|
|
};
|
|
|
|
}}
|
|
|