Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy 

FieldMap.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) quickfixengine.org  All rights reserved.
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 #include "CallStack.h"
00026 
00027 #include "FieldMap.h"
00028 #include <algorithm>
00029 #include <iterator>
00030 
00031 namespace FIX
00032 {
00033 FieldMap::~FieldMap()
00034 { QF_STACK_IGNORE_BEGIN
00035   clear();
00036   QF_STACK_IGNORE_END
00037 }
00038 
00039 FieldMap& FieldMap::operator=( const FieldMap& rhs )
00040 { QF_STACK_PUSH(FieldMap::operator=)
00041 
00042   clear();
00043 
00044   std::copy( rhs.m_fields.begin (), rhs.m_fields.end(),
00045              std::inserter(m_fields, m_fields.begin()) );
00046 
00047   Groups::const_iterator i;
00048   for ( i = rhs.m_groups.begin(); i != rhs.m_groups.end(); ++i )
00049   {
00050     std::vector < FieldMap* > ::const_iterator j;
00051     for ( j = i->second.begin(); j != i->second.end(); ++j )
00052       addGroup( i->first, **j );
00053   }
00054 
00055   return *this;
00056 
00057   QF_STACK_POP
00058 }
00059 
00060 void FieldMap::addGroup( int field, const FieldMap& group, bool setCount )
00061 { QF_STACK_PUSH(FieldMap::addGroup)
00062 
00063   FieldMap * pGroup = new FieldMap( group.m_fields.key_comp() );
00064   *pGroup = group;
00065   m_groups[ field ].push_back( pGroup );
00066   Groups::iterator i = m_groups.find( field );
00067   if( setCount )
00068     setField( IntField( field, i->second.size() ) );
00069 
00070   QF_STACK_POP
00071 }
00072 
00073 void FieldMap::replaceGroup( int num, int field, FieldMap& group )
00074 { QF_STACK_PUSH(FieldMap::replaceGroup)
00075 
00076   Groups::const_iterator i = m_groups.find( field );
00077   if ( i == m_groups.end() ) return;
00078   if ( num <= 0 ) return;
00079   if ( i->second.size() < ( unsigned ) num ) return;
00080   *( *( i->second.begin() + ( num - 1 ) ) ) = group;
00081 
00082   QF_STACK_POP
00083 }
00084 
00085 void FieldMap::removeGroup( int num, int field )
00086 {
00087   Groups::iterator i = m_groups.find( field );
00088   if ( i == m_groups.end() ) return;
00089   if ( num <= 0 ) return;
00090   std::vector< FieldMap* >& vector = i->second;
00091   if ( vector.size() < ( unsigned ) num ) return;
00092 
00093   std::deque< FieldMap* > queue;
00094   while( vector.size() > (unsigned)num )
00095   {
00096     queue.push_back( vector.back() );
00097     vector.pop_back();
00098   }
00099   delete vector.back();
00100   vector.pop_back();
00101   while( queue.size() )
00102   {
00103     vector.push_back( queue.front() );
00104     queue.pop_front();
00105   }
00106 
00107   if( vector.size() == 0 )
00108   {
00109     m_groups.erase( field );
00110   }
00111   else
00112   {
00113     IntField groupCount( field, vector.size() );
00114     setField( groupCount, true );
00115   }
00116 }
00117 
00118 void FieldMap::removeGroup( int field )
00119 { QF_STACK_PUSH(FieldMap::removeGroup)
00120   removeGroup( groupCount(field), field );
00121   QF_STACK_POP
00122 }
00123 
00124 void FieldMap::removeField( int field )
00125 { QF_STACK_PUSH(FieldMap::removeField)
00126 
00127   Fields::iterator i = m_fields.find( field );
00128   if ( i != m_fields.end() )
00129     m_fields.erase( i );
00130 
00131   QF_STACK_POP
00132 }
00133 
00134 bool FieldMap::hasGroup( int num, int field ) const
00135 { QF_STACK_PUSH(FieldMap::hasGroup)
00136 
00137   return groupCount(field) >= num;
00138 
00139   QF_STACK_POP
00140 }
00141 
00142 bool FieldMap::hasGroup( int field ) const
00143 { QF_STACK_PUSH(FieldMap::hasGroup)
00144 
00145   Groups::const_iterator i = m_groups.find( field );
00146   return i != m_groups.end();
00147 
00148   QF_STACK_POP
00149 }
00150 
00151 int FieldMap::groupCount( int field ) const
00152 { QF_STACK_PUSH(FieldMap::groupCount)
00153 
00154   Groups::const_iterator i = m_groups.find( field );
00155   if( i == m_groups.end() )
00156     return 0;
00157   return i->second.size();
00158 
00159   QF_STACK_POP
00160 }
00161 
00162 void FieldMap::clear()
00163 { QF_STACK_PUSH(FieldMap::clear)
00164 
00165   m_fields.clear();
00166 
00167   Groups::iterator i;
00168   for ( i = m_groups.begin(); i != m_groups.end(); ++i )
00169   {
00170     std::vector < FieldMap* > ::iterator j;
00171     for ( j = i->second.begin(); j != i->second.end(); ++j )
00172       delete *j;
00173   }
00174   m_groups.clear();
00175 
00176   QF_STACK_POP
00177 }
00178 
00179 bool FieldMap::isEmpty()
00180 { QF_STACK_PUSH(FieldMap::isEmpty)
00181   return m_fields.size() == 0;
00182   QF_STACK_POP
00183 }
00184 
00185 int FieldMap::totalFields() const
00186 {
00187   int result = m_fields.size();
00188     
00189   Groups::const_iterator i;
00190   for ( i = m_groups.begin(); i != m_groups.end(); ++i )
00191   {
00192     std::vector < FieldMap* > ::const_iterator j;
00193     for ( j = i->second.begin(); j != i->second.end(); ++j )
00194       result += ( *j ) ->totalFields();
00195   }
00196   return result;
00197 }
00198 
00199 std::string& FieldMap::calculateString( std::string& result, bool clear ) const
00200 { QF_STACK_PUSH(FieldMap::calculateString)
00201 
00202 #if defined(_MSC_VER) && _MSC_VER < 1300
00203   if( clear ) result = "";
00204 #else
00205   if( clear ) result.clear();
00206 #endif
00207 
00208   if( !result.size() )
00209     result.reserve( totalFields() * 32 );
00210     
00211   Fields::const_iterator i;
00212   for ( i = m_fields.begin(); i != m_fields.end(); ++i )
00213   {
00214     result += i->second.getValue();
00215 
00216     // add groups if they exist
00217     if( !m_groups.size() ) continue;
00218     Groups::const_iterator j = m_groups.find( i->first );
00219     if ( j == m_groups.end() ) continue;
00220     std::vector < FieldMap* > ::const_iterator k;
00221     for ( k = j->second.begin(); k != j->second.end(); ++k )
00222       ( *k ) ->calculateString( result, false );
00223   }
00224   return result;
00225 
00226   QF_STACK_POP
00227 }
00228 
00229 int FieldMap::calculateLength( int beginStringField,
00230                                int bodyLengthField,
00231                                int checkSumField ) const
00232 {
00233   int result = 0;
00234   Fields::const_iterator i;
00235   for ( i = m_fields.begin(); i != m_fields.end(); ++i )
00236   {
00237     if ( i->first != beginStringField
00238          && i->first != bodyLengthField
00239          && i->first != checkSumField )
00240     { result += i->second.getLength(); }
00241   }
00242 
00243   Groups::const_iterator j;
00244   for ( j = m_groups.begin(); j != m_groups.end(); ++j )
00245   {
00246     std::vector < FieldMap* > ::const_iterator k;
00247     for ( k = j->second.begin(); k != j->second.end(); ++k )
00248       result += ( *k ) ->calculateLength();
00249   }
00250   return result;
00251 }
00252 
00253 int FieldMap::calculateTotal( int checkSumField ) const
00254 { QF_STACK_PUSH(FieldMap::calculateTotal)
00255 
00256   int result = 0;
00257   Fields::const_iterator i;
00258   for ( i = m_fields.begin(); i != m_fields.end(); ++i )
00259   {
00260     if ( i->first != checkSumField )
00261       result += i->second.getTotal();
00262   }
00263 
00264   Groups::const_iterator j;
00265   for ( j = m_groups.begin(); j != m_groups.end(); ++j )
00266   {
00267     std::vector < FieldMap* > ::const_iterator k;
00268     for ( k = j->second.begin(); k != j->second.end(); ++k )
00269       result += ( *k ) ->calculateTotal();
00270   }
00271   return result;
00272 
00273   QF_STACK_POP
00274 }
00275 }

Generated on Mon Mar 1 13:41:38 2010 for QuickFIX by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2001