FieldMap.h
Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 
00003 /****************************************************************************
00004 ** Copyright (c) 2001-2014
00005 **
00006 ** This file is part of the QuickFIX FIX Engine
00007 **
00008 ** This file may be distributed under the terms of the quickfixengine.org
00009 ** license as defined by quickfixengine.org and appearing in the file
00010 ** LICENSE included in the packaging of this file.
00011 **
00012 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 **
00015 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00016 **
00017 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00018 ** not clear to you.
00019 **
00020 ****************************************************************************/
00021 
00022 #ifndef FIX_FIELDMAP
00023 #define FIX_FIELDMAP
00024 
00025 #ifdef _MSC_VER
00026 #pragma warning( disable: 4786 )
00027 #endif
00028 
00029 #include "Field.h"
00030 #include "MessageSorters.h"
00031 #include "Exceptions.h"
00032 #include "Utility.h"
00033 #include <map>
00034 #include <vector>
00035 #include <sstream>
00036 #include <algorithm>
00037 
00038 namespace FIX
00039 {
00046 class FieldMap
00047 {
00048 public:
00049 #if defined(_MSC_VER) && _MSC_VER < 1300
00050   typedef std::multimap < int, FieldBase, message_order > Fields;
00051   typedef std::map < int, std::vector < FieldMap* >, std::less<int> > Groups;
00052 #else
00053   typedef std::multimap < int, FieldBase, message_order, 
00054                           ALLOCATOR<std::pair<const int,FieldBase> > > Fields;
00055   typedef std::map < int, std::vector < FieldMap* >, std::less<int>, 
00056                      ALLOCATOR<std::pair<const int, std::vector< FieldMap* > > > > Groups;
00057 #endif
00058 
00059   typedef Fields::const_iterator iterator;
00060   typedef iterator const_iterator;
00061   typedef Groups::const_iterator g_iterator;
00062   typedef Groups::const_iterator g_const_iterator;
00063 
00064   FieldMap( const message_order& order =
00065             message_order( message_order::normal ) )
00066   : m_fields( order ) {}
00067 
00068   FieldMap( const int order[] )
00069   : m_fields( message_order(order) ) {}
00070 
00071   FieldMap( const FieldMap& copy )
00072   { *this = copy; }
00073 
00074   virtual ~FieldMap();
00075 
00076   FieldMap& operator=( const FieldMap& rhs );
00077 
00079   void setField( const FieldBase& field, bool overwrite = true )
00080   throw( RepeatedTag )
00081   {
00082       if(!overwrite)
00083           m_fields.insert( Fields::value_type( field.getField(), field ) );
00084       else
00085       {
00086           Fields::iterator i = m_fields.find( field.getField() );
00087           if( i == m_fields.end() )
00088               m_fields.insert( Fields::value_type( field.getField(), field ) );
00089           else
00090               i->second = field;
00091       }
00092   }
00094   void setField( int field, const std::string& value )
00095   throw( RepeatedTag, NoTagValue )
00096   {
00097     FieldBase fieldBase( field, value );
00098     setField( fieldBase );
00099   }
00100 
00102   bool getFieldIfSet( FieldBase& field ) const
00103   {
00104     Fields::const_iterator iter = m_fields.find( field.getField() );
00105     if ( iter == m_fields.end() )
00106       return false;
00107     field = iter->second;
00108     return true;
00109   }
00110 
00112   FieldBase& getField( FieldBase& field )
00113   const throw( FieldNotFound )
00114   {
00115     field = getFieldRef( field.getField() );
00116     return field;
00117   }
00118 
00120   const std::string& getField( int field )
00121   const throw( FieldNotFound )
00122   {
00123     return getFieldRef( field ).getString();
00124   }
00125 
00127   const FieldBase& getFieldRef( int field )
00128   const throw( FieldNotFound )
00129   {
00130     Fields::const_iterator iter = m_fields.find( field );
00131     if ( iter == m_fields.end() )
00132       throw FieldNotFound( field );
00133     return iter->second;
00134   }
00135 
00137   const FieldBase* const getFieldPtr( int field )
00138   const throw( FieldNotFound )
00139   {
00140     return &getFieldRef( field );
00141   }
00142 
00144   bool isSetField( const FieldBase& field ) const
00145   { return isSetField( field.getField() ); }
00147   bool isSetField( int field ) const
00148   { return m_fields.find( field ) != m_fields.end(); }
00149 
00151   void removeField( int field );
00152 
00154   void addGroup( int field, const FieldMap& group, bool setCount = true );
00155 
00157   void addGroupPtr( int field, FieldMap * group, bool setCount = true );
00158 
00160   void replaceGroup( int num, int field, const FieldMap& group );
00161 
00163   FieldMap& getGroup( int num, int field, FieldMap& group ) const
00164   throw( FieldNotFound )
00165   {
00166     return group = getGroupRef( num, field );
00167   }
00168 
00170   FieldMap& getGroupRef( int num, int field ) const
00171   throw( FieldNotFound )
00172   {
00173     Groups::const_iterator i = m_groups.find( field );
00174     if( i == m_groups.end() ) throw FieldNotFound( field );
00175     if( num <= 0 ) throw FieldNotFound( field );
00176     if( i->second.size() < (unsigned)num ) throw FieldNotFound( field );
00177     return *( *(i->second.begin() + (num-1) ) );
00178   }
00179 
00181   FieldMap* getGroupPtr( int num, int field ) const
00182   throw( FieldNotFound )
00183   {
00184     return &getGroupRef( num, field );
00185   }
00186 
00188   void removeGroup( int num, int field );
00190   void removeGroup( int field );
00191 
00193   bool hasGroup( int field ) const;
00195   bool hasGroup( int num, int field ) const;
00197   size_t groupCount( int field ) const;
00198 
00200   void clear();
00202   bool isEmpty();
00203 
00204   size_t totalFields() const;
00205 
00206   std::string& calculateString( std::string& ) const;
00207 
00208   int calculateLength( int beginStringField = FIELD::BeginString,
00209                        int bodyLengthField = FIELD::BodyLength,
00210                        int checkSumField = FIELD::CheckSum ) const;
00211 
00212   int calculateTotal( int checkSumField = FIELD::CheckSum ) const;
00213 
00214   iterator begin() const { return m_fields.begin(); }
00215   iterator end() const { return m_fields.end(); }
00216   g_iterator g_begin() const { return m_groups.begin(); }
00217   g_iterator g_end() const { return m_groups.end(); }
00218 
00219 private:
00220   Fields m_fields;
00221   Groups m_groups;
00222 };
00224 }
00225 
00226 #define FIELD_SET( MAP, FIELD )           \
00227 bool isSet( const FIELD& field ) const    \
00228 { return (MAP).isSetField(field); }       \
00229 void set( const FIELD& field )            \
00230 { (MAP).setField(field); }                \
00231 FIELD& get( FIELD& field ) const          \
00232 { return (FIELD&)(MAP).getField(field); } \
00233 bool getIfSet( FIELD& field ) const       \
00234 { return (MAP).getFieldIfSet(field); }
00235 
00236 #define FIELD_GET_PTR( MAP, FLD ) \
00237 (const FIX::FLD*)MAP.getFieldPtr( FIX::FIELD::FLD )
00238 #define FIELD_GET_REF( MAP, FLD ) \
00239 (const FIX::FLD&)MAP.getFieldRef( FIX::FIELD::FLD )
00240 #define FIELD_THROW_IF_NOT_FOUND( MAP, FLD ) \
00241 if( !(MAP).isSetField( FIX::FIELD::FLD) ) \
00242   throw FieldNotFound( FIX::FIELD::FLD )
00243 #endif //FIX_FIELDMAP
00244 

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