FieldMap.h
Go to the documentation of this file.
/* -*- C++ -*- */
/****************************************************************************
** Copyright (c) 2001-2014
**
** This file is part of the QuickFIX FIX Engine
**
** This file may be distributed under the terms of the quickfixengine.org
** license as defined by quickfixengine.org and appearing in the file
** LICENSE included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.quickfixengine.org/LICENSE for licensing information.
**
** Contact ask@quickfixengine.org if any conditions of this licensing are
** not clear to you.
**
****************************************************************************/
#ifndef FIX_FIELDMAP
#define FIX_FIELDMAP
#ifdef _MSC_VER
#pragma warning( disable: 4786 )
#endif
#include "Field.h"
#include "MessageSorters.h"
#include "Exceptions.h"
#include "Utility.h"
#include <map>
#include <vector>
#include <sstream>
#include <algorithm>
namespace FIX
{
class FieldMap
{
public:
#if defined(_MSC_VER) && _MSC_VER < 1300
typedef std::multimap < int, FieldBase, message_order > Fields;
typedef std::map < int, std::vector < FieldMap* >, std::less<int> > Groups;
#else
typedef std::multimap < int, FieldBase, message_order,
ALLOCATOR<std::pair<const int,FieldBase> > > Fields;
typedef std::map < int, std::vector < FieldMap* >, std::less<int>,
ALLOCATOR<std::pair<const int, std::vector< FieldMap* > > > > Groups;
#endif
typedef Fields::const_iterator iterator;
typedef iterator const_iterator;
typedef Groups::const_iterator g_iterator;
typedef Groups::const_iterator g_const_iterator;
FieldMap( const message_order& order =
message_order( message_order::normal ) )
: m_fields( order ) {}
FieldMap( const int order[] )
: m_fields( message_order(order) ) {}
FieldMap( const FieldMap& copy )
{ *this = copy; }
virtual ~FieldMap();
FieldMap& operator=( const FieldMap& rhs );
void setField( const FieldBase& field, bool overwrite = true )
throw( RepeatedTag )
{
if(!overwrite)
m_fields.insert( Fields::value_type( field.getField(), field ) );
else
{
Fields::iterator i = m_fields.find( field.getField() );
if( i == m_fields.end() )
m_fields.insert( Fields::value_type( field.getField(), field ) );
else
i->second = field;
}
}
void setField( int field, const std::string& value )
throw( RepeatedTag, NoTagValue )
{
FieldBase fieldBase( field, value );
setField( fieldBase );
}
bool getFieldIfSet( FieldBase& field ) const
{
Fields::const_iterator iter = m_fields.find( field.getField() );
if ( iter == m_fields.end() )
return false;
field = iter->second;
return true;
}
FieldBase& getField( FieldBase& field )
const throw( FieldNotFound )
{
field = getFieldRef( field.getField() );
return field;
}
const std::string& getField( int field )
const throw( FieldNotFound )
{
return getFieldRef( field ).getString();
}
const FieldBase& getFieldRef( int field )
const throw( FieldNotFound )
{
Fields::const_iterator iter = m_fields.find( field );
if ( iter == m_fields.end() )
throw FieldNotFound( field );
return iter->second;
}
const FieldBase* const getFieldPtr( int field )
const throw( FieldNotFound )
{
return &getFieldRef( field );
}
bool isSetField( const FieldBase& field ) const
{ return isSetField( field.getField() ); }
bool isSetField( int field ) const
{ return m_fields.find( field ) != m_fields.end(); }
void removeField( int field );
void addGroup( int field, const FieldMap& group, bool setCount = true );
void addGroupPtr( int field, FieldMap * group, bool setCount = true );
void replaceGroup( int num, int field, const FieldMap& group );
FieldMap& getGroup( int num, int field, FieldMap& group ) const
throw( FieldNotFound )
{
return group = getGroupRef( num, field );
}
FieldMap& getGroupRef( int num, int field ) const
throw( FieldNotFound )
{
Groups::const_iterator i = m_groups.find( field );
if( i == m_groups.end() ) throw FieldNotFound( field );
if( num <= 0 ) throw FieldNotFound( field );
if( i->second.size() < (unsigned)num ) throw FieldNotFound( field );
return *( *(i->second.begin() + (num-1) ) );
}
FieldMap* getGroupPtr( int num, int field ) const
throw( FieldNotFound )
{
return &getGroupRef( num, field );
}
void removeGroup( int num, int field );
void removeGroup( int field );
bool hasGroup( int field ) const;
bool hasGroup( int num, int field ) const;
size_t groupCount( int field ) const;
void clear();
bool isEmpty();
size_t totalFields() const;
std::string& calculateString( std::string& ) const;
int calculateLength( int beginStringField = FIELD::BeginString,
int bodyLengthField = FIELD::BodyLength,
int checkSumField = FIELD::CheckSum ) const;
int calculateTotal( int checkSumField = FIELD::CheckSum ) const;
iterator begin() const { return m_fields.begin(); }
iterator end() const { return m_fields.end(); }
g_iterator g_begin() const { return m_groups.begin(); }
g_iterator g_end() const { return m_groups.end(); }
private:
Fields m_fields;
Groups m_groups;
};
}
#define FIELD_SET( MAP, FIELD ) \
bool isSet( const FIELD& field ) const \
{ return (MAP).isSetField(field); } \
void set( const FIELD& field ) \
{ (MAP).setField(field); } \
FIELD& get( FIELD& field ) const \
{ return (FIELD&)(MAP).getField(field); } \
bool getIfSet( FIELD& field ) const \
{ return (MAP).getFieldIfSet(field); }
#define FIELD_GET_PTR( MAP, FLD ) \
(const FIX::FLD*)MAP.getFieldPtr( FIX::FIELD::FLD )
#define FIELD_GET_REF( MAP, FLD ) \
(const FIX::FLD&)MAP.getFieldRef( FIX::FIELD::FLD )
#define FIELD_THROW_IF_NOT_FOUND( MAP, FLD ) \
if( !(MAP).isSetField( FIX::FIELD::FLD) ) \
throw FieldNotFound( FIX::FIELD::FLD )
#endif //FIX_FIELDMAP
FIX::FieldMap::isSetField
bool isSetField(const FieldBase &field) const
Check to see if a field is set.
Definition: FieldMap.h:144
FIX::FieldMap::calculateTotal
int calculateTotal(int checkSumField=FIELD::CheckSum) const
Definition: FieldMap.cpp:218
FIX::FieldMap::begin
iterator begin() const
Definition: FieldMap.h:214
FIX::FieldMap::hasGroup
bool hasGroup(int field) const
Check to see any instance of a group exists.
Definition: FieldMap.cpp:129
FIX::FieldMap::iterator
Fields::const_iterator iterator
Definition: FieldMap.h:59
FIX::FieldMap::getFieldIfSet
bool getFieldIfSet(FieldBase &field) const
Get a field if set.
Definition: FieldMap.h:102
FIX::FieldBase::getString
const std::string & getString() const
Get the string representation of the fields value.
Definition: Field.h:112
FIX::FieldMap::g_iterator
Groups::const_iterator g_iterator
Definition: FieldMap.h:61
FIX::RepeatedTag
Repeated tag not part of repeating group.
Definition: Exceptions.h:199
Utility.h
FIX::message_order::normal
Definition: MessageSorters.h:116
FIX::FieldMap::operator=
FieldMap & operator=(const FieldMap &rhs)
Definition: FieldMap.cpp:38
FIX::FieldMap::end
iterator end() const
Definition: FieldMap.h:215
FIX::FieldMap::removeGroup
void removeGroup(int num, int field)
Remove a specific instance of a group.
Definition: FieldMap.cpp:86
MessageSorters.h
FIX::FieldMap::FieldMap
FieldMap(const FieldMap &copy)
Definition: FieldMap.h:71
FIX
Definition: Acceptor.cpp:34
FIX::FieldMap::getGroupPtr
FieldMap * getGroupPtr(int num, int field) const
Get direct access to a field through a pointer.
Definition: FieldMap.h:181
FIX::FieldMap::getField
FieldBase & getField(FieldBase &field) const
Get a field without type checking.
Definition: FieldMap.h:112
FIX::FieldMap
Stores and organizes a collection of Fields.
Definition: FieldMap.h:46
FIX::FieldMap::getFieldRef
const FieldBase & getFieldRef(int field) const
Get direct access to a field through a reference.
Definition: FieldMap.h:127
FIX::FieldMap::m_fields
Fields m_fields
Definition: FieldMap.h:220
FIX::FieldMap::setField
void setField(int field, const std::string &value)
Set a field without a field class.
Definition: FieldMap.h:94
FIX::FieldMap::~FieldMap
virtual ~FieldMap()
Definition: FieldMap.cpp:33
FIX::FieldMap::getFieldPtr
const FieldBase *const getFieldPtr(int field) const
Get direct access to a field through a pointer.
Definition: FieldMap.h:137
FIX::FieldMap::isEmpty
bool isEmpty()
Check if map contains any fields.
Definition: FieldMap.cpp:157
FIX::FieldMap::g_const_iterator
Groups::const_iterator g_const_iterator
Definition: FieldMap.h:62
Field.h
FIX::FieldMap::FieldMap
FieldMap(const int order[])
Definition: FieldMap.h:68
FIX::FieldMap::groupCount
size_t groupCount(int field) const
Count the number of instance of a group.
Definition: FieldMap.cpp:135
FIX::FieldMap::getField
const std::string & getField(int field) const
Get a field without a field class.
Definition: FieldMap.h:120
FIX::FieldNotFound
Field not found inside a message.
Definition: Exceptions.h:57
FIX::FieldMap::m_groups
Groups m_groups
Definition: FieldMap.h:221
FIX::FieldMap::setField
void setField(const FieldBase &field, bool overwrite=true)
Set a field without type checking.
Definition: FieldMap.h:79
FIX::FieldMap::calculateLength
int calculateLength(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Definition: FieldMap.cpp:194
FIX::FieldMap::totalFields
size_t totalFields() const
Definition: FieldMap.cpp:162
FIX::FieldMap::calculateString
std::string & calculateString(std::string &) const
Definition: FieldMap.cpp:176
FIX::FieldBase::getField
int getField() const
Get the fields integer tag.
Definition: Field.h:108
FIX::FieldMap::removeField
void removeField(int field)
Remove a field. If field is not present, this is a no-op.
Definition: FieldMap.cpp:117
FIX::FieldMap::addGroup
void addGroup(int field, const FieldMap &group, bool setCount=true)
Add a group.
Definition: FieldMap.cpp:58
FIX::FieldMap::g_end
g_iterator g_end() const
Definition: FieldMap.h:217
FIX::FieldMap::const_iterator
iterator const_iterator
Definition: FieldMap.h:60
FIX::FieldMap::g_begin
g_iterator g_begin() const
Definition: FieldMap.h:216
FIX::FieldBase
Base representation of all Field classes.
Definition: Field.h:45
FIX::FieldMap::addGroupPtr
void addGroupPtr(int field, FieldMap *group, bool setCount=true)
Acquire ownership of Group object.
Definition: FieldMap.cpp:65
FIX::FieldMap::Fields
std::multimap< int, FieldBase, message_order, ALLOCATOR< std::pair< const int, FieldBase > > > Fields
Definition: FieldMap.h:54
FIX::message_order
Sorts fields in header, normal, or trailer order.
Definition: MessageSorters.h:113
FIX::FieldMap::FieldMap
FieldMap(const message_order &order=message_order(message_order::normal))
Definition: FieldMap.h:64
FIX::FIELD::BeginString
const int BeginString
Definition: FixFieldNumbers.h:499
FIX::NoTagValue
Field exists in message without a value.
Definition: Exceptions.h:128
FIX::FieldMap::Groups
std::map< int, std::vector< FieldMap *>, std::less< int >, ALLOCATOR< std::pair< const int, std::vector< FieldMap *> > > > Groups
Definition: FieldMap.h:56
FIX::FIELD::BodyLength
const int BodyLength
Definition: FixFieldNumbers.h:1478
FIX::FieldMap::replaceGroup
void replaceGroup(int num, int field, const FieldMap &group)
Replace a specific instance of a group.
Definition: FieldMap.cpp:77
FIX::FIELD::CheckSum
const int CheckSum
Definition: FixFieldNumbers.h:1426
FIX::FieldMap::getGroupRef
FieldMap & getGroupRef(int num, int field) const
Get direct access to a field through a reference.
Definition: FieldMap.h:170
Exceptions.h
FIX::FieldMap::clear
void clear()
Clear all fields from the map.
Definition: FieldMap.cpp:143
FIX::FieldMap::isSetField
bool isSetField(int field) const
Check to see if a field is set by referencing its number.
Definition: FieldMap.h:147
FIX::FieldMap::getGroup
FieldMap & getGroup(int num, int field, FieldMap &group) const
Get a specific instance of a group.
Definition: FieldMap.h:163