MessageSorters.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_MESSAGESORTERS_H
00023 #define FIX_MESSAGESORTERS_H
00024 
00025 #ifdef _MSC_VER
00026 #pragma warning( disable : 4503 4355 4786 4290 )
00027 #endif
00028 
00029 #include "FieldNumbers.h"
00030 #include "SharedArray.h"
00031 #include <stdarg.h>
00032 #include <functional>
00033 #include <map>
00034 
00035 namespace FIX
00036 {
00038 struct header_order
00039 {
00040   static bool compare( const int x, const int y )
00041   {
00042     int orderedX = getOrderedPosition( x );
00043     int orderedY = getOrderedPosition( y );
00044 
00045     if ( orderedX && orderedY )
00046       return orderedX < orderedY;
00047     else
00048       if ( orderedX )
00049         return true;
00050       else
00051         if ( orderedY )
00052           return false;
00053         else
00054           return x < y;
00055   }
00056 
00057   static int getOrderedPosition( const int field )
00058   {
00059     switch ( field )
00060     {
00061       case FIELD::BeginString: return 1;
00062       case FIELD::BodyLength: return 2;
00063       case FIELD::MsgType: return 3;
00064       default: return 0;
00065     };
00066   }
00067 };
00068 
00070 struct trailer_order
00071 {
00072   static bool compare( const int x, const int y )
00073   {
00074     if ( x == FIELD::CheckSum ) return false;
00075     else
00076       if ( y == FIELD::CheckSum ) return true;
00077       else return x < y;
00078   }
00079 };
00080 
00082 struct group_order
00083 {
00084   static bool compare( const int x, const int y, int* order, int largest )
00085   {
00086     if ( x <= largest && y <= largest )
00087     {
00088       int iX = order[ x ];
00089       int iY = order[ y ];
00090       if ( iX == 0 && iY == 0 )
00091         return x < y;
00092       else if ( iX == 0 )
00093         return false;
00094       else if ( iY == 0 )
00095         return true;
00096       else
00097         return iX < iY;
00098     }
00099     else if ( x <= largest ) return true;
00100     else if ( y <= largest ) return false;
00101     else return x < y;
00102   }
00103 };
00104 
00105 typedef std::less < int > normal_order;
00106 
00113 struct message_order
00114 {
00115 public:
00116   enum cmp_mode { header, trailer, normal, group };
00117 
00118   message_order( cmp_mode mode = normal ) 
00119     : m_mode( mode ), m_delim( 0 ), m_largest( 0 ) {}
00120   message_order( int first, ... );
00121   message_order( const int order[] );
00122   message_order( const message_order& copy ) 
00123   { *this = copy; }
00124 
00125   bool operator() ( const int x, const int y ) const
00126   {
00127     switch ( m_mode )
00128     {
00129       case header:
00130       return header_order::compare( x, y );
00131       case trailer:
00132       return trailer_order::compare( x, y );
00133       case group:
00134       return group_order::compare( x, y, m_groupOrder, m_largest );
00135       case normal: default:
00136       return x < y;
00137     }
00138   }
00139 
00140   message_order& operator=( const message_order& rhs );
00141 
00142   operator bool() const
00143   { return !m_groupOrder.empty(); }
00144 
00145 private:
00146   void setOrder( int size, const int order[] );
00147 
00148   cmp_mode m_mode;
00149   int m_delim;
00150   shared_array<int> m_groupOrder;
00151   int m_largest;
00152 };
00153 }
00154 
00155 #endif //FIX_MESSAGESORTERS_H
00156 

Generated on Mon Jun 23 2014 23:49:38 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001