FieldMap.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "FieldMap.h"
00027 #include <algorithm>
00028 #include <iterator>
00029 #include <deque>
00030 
00031 namespace FIX
00032 {
00033 FieldMap::~FieldMap()
00034 {
00035   clear();
00036 }
00037 
00038 FieldMap& FieldMap::operator=( const FieldMap& rhs )
00039 {
00040   clear();
00041 
00042   m_fields = rhs.m_fields;
00043 
00044   Groups::const_iterator i;
00045   for ( i = rhs.m_groups.begin(); i != rhs.m_groups.end(); ++i )
00046   {
00047     std::vector < FieldMap* > ::const_iterator j;
00048     for ( j = i->second.begin(); j != i->second.end(); ++j )
00049     {
00050         FieldMap * pGroup = new FieldMap( **j );
00051         m_groups[ i->first ].push_back( pGroup );
00052     }
00053   }
00054 
00055   return *this;
00056 }
00057 
00058 void FieldMap::addGroup( int field, const FieldMap& group, bool setCount )
00059 {
00060   FieldMap * pGroup = new FieldMap( group );
00061 
00062   addGroupPtr( field, pGroup, setCount );
00063 }
00064 
00065 void FieldMap::addGroupPtr( int field, FieldMap * group, bool setCount )
00066 {
00067     if( group == 0 )
00068         return;
00069 
00070     std::vector< FieldMap* >& vec = m_groups[ field ];
00071     vec.push_back( group );
00072 
00073     if( setCount )
00074         setField( IntField( field, (int)vec.size() ) );
00075 }
00076 
00077 void FieldMap::replaceGroup( int num, int field, const FieldMap& group )
00078 {
00079   Groups::const_iterator i = m_groups.find( field );
00080   if ( i == m_groups.end() ) return;
00081   if ( num <= 0 ) return;
00082   if ( i->second.size() < ( unsigned ) num ) return;
00083   *( *( i->second.begin() + ( num - 1 ) ) ) = group;
00084 }
00085 
00086 void FieldMap::removeGroup( int num, int field )
00087 {
00088   Groups::iterator i = m_groups.find( field );
00089   if ( i == m_groups.end() ) return;
00090   if ( num <= 0 ) return;
00091   std::vector< FieldMap* >& vector = i->second;
00092   if ( vector.size() < ( unsigned ) num ) return;
00093 
00094   std::vector< FieldMap* >::iterator iter = vector.begin();
00095   std::advance( iter, ( num - 1 ) );
00096 
00097   delete (*iter);
00098   vector.erase( iter );
00099 
00100   if( vector.size() == 0 )
00101   {
00102     m_groups.erase( field );
00103     removeField( field );
00104   }
00105   else
00106   {
00107     IntField groupCount( field, (int)vector.size() );
00108     setField( groupCount, true );
00109   }
00110 }
00111 
00112 void FieldMap::removeGroup( int field )
00113 {
00114   removeGroup( (int)groupCount(field), field );
00115 }
00116 
00117 void FieldMap::removeField( int field )
00118 {
00119   Fields::iterator i = m_fields.find( field );
00120   if ( i != m_fields.end() )
00121     m_fields.erase( i );
00122 }
00123 
00124 bool FieldMap::hasGroup( int num, int field ) const
00125 {
00126   return (int)groupCount(field) >= num;
00127 }
00128 
00129 bool FieldMap::hasGroup( int field ) const
00130 {
00131   Groups::const_iterator i = m_groups.find( field );
00132   return i != m_groups.end();
00133 }
00134 
00135 size_t FieldMap::groupCount( int field ) const
00136 {
00137   Groups::const_iterator i = m_groups.find( field );
00138   if( i == m_groups.end() )
00139     return 0;
00140   return i->second.size();
00141 }
00142 
00143 void FieldMap::clear()
00144 {
00145   m_fields.clear();
00146 
00147   Groups::iterator i;
00148   for ( i = m_groups.begin(); i != m_groups.end(); ++i )
00149   {
00150     std::vector < FieldMap* > ::iterator j;
00151     for ( j = i->second.begin(); j != i->second.end(); ++j )
00152       delete *j;
00153   }
00154   m_groups.clear();
00155 }
00156 
00157 bool FieldMap::isEmpty()
00158 {
00159   return m_fields.size() == 0;
00160 }
00161 
00162 size_t FieldMap::totalFields() const
00163 {
00164   size_t result = m_fields.size();
00165     
00166   Groups::const_iterator i;
00167   for ( i = m_groups.begin(); i != m_groups.end(); ++i )
00168   {
00169     std::vector < FieldMap* > ::const_iterator j;
00170     for ( j = i->second.begin(); j != i->second.end(); ++j )
00171       result += ( *j ) ->totalFields();
00172   }
00173   return result;
00174 }
00175 
00176 std::string& FieldMap::calculateString( std::string& result ) const
00177 {  
00178   Fields::const_iterator i;
00179   for ( i = m_fields.begin(); i != m_fields.end(); ++i )
00180   {
00181     result += i->second.getFixString();
00182 
00183     // add groups if they exist
00184     if( !m_groups.size() ) continue;
00185     Groups::const_iterator j = m_groups.find( i->first );
00186     if ( j == m_groups.end() ) continue;
00187     std::vector < FieldMap* > ::const_iterator k;
00188     for ( k = j->second.begin(); k != j->second.end(); ++k )
00189       ( *k ) ->calculateString( result );
00190   }
00191   return result;
00192 }
00193 
00194 int FieldMap::calculateLength( int beginStringField,
00195                                int bodyLengthField,
00196                                int checkSumField ) const
00197 {
00198   int result = 0;
00199   Fields::const_iterator i;
00200   for ( i = m_fields.begin(); i != m_fields.end(); ++i )
00201   {
00202     if ( i->first != beginStringField
00203          && i->first != bodyLengthField
00204          && i->first != checkSumField )
00205     { result += i->second.getLength(); }
00206   }
00207 
00208   Groups::const_iterator j;
00209   for ( j = m_groups.begin(); j != m_groups.end(); ++j )
00210   {
00211     std::vector < FieldMap* > ::const_iterator k;
00212     for ( k = j->second.begin(); k != j->second.end(); ++k )
00213       result += ( *k ) ->calculateLength();
00214   }
00215   return result;
00216 }
00217 
00218 int FieldMap::calculateTotal( int checkSumField ) const
00219 {
00220   int result = 0;
00221   Fields::const_iterator i;
00222   for ( i = m_fields.begin(); i != m_fields.end(); ++i )
00223   {
00224     if ( i->first != checkSumField )
00225       result += i->second.getTotal();
00226   }
00227 
00228   Groups::const_iterator j;
00229   for ( j = m_groups.begin(); j != m_groups.end(); ++j )
00230   {
00231     std::vector < FieldMap* > ::const_iterator k;
00232     for ( k = j->second.begin(); k != j->second.end(); ++k )
00233       result += ( *k ) ->calculateTotal();
00234   }
00235   return result;
00236 }
00237 
00238 }

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001