FieldMap.h
Go to the documentation of this file.
1 /* -*- C++ -*- */
2 
3 /****************************************************************************
4 ** Copyright (c) 2001-2014
5 **
6 ** This file is part of the QuickFIX FIX Engine
7 **
8 ** This file may be distributed under the terms of the quickfixengine.org
9 ** license as defined by quickfixengine.org and appearing in the file
10 ** LICENSE included in the packaging of this file.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** See http://www.quickfixengine.org/LICENSE for licensing information.
16 **
17 ** Contact ask@quickfixengine.org if any conditions of this licensing are
18 ** not clear to you.
19 **
20 ****************************************************************************/
21 
22 #ifndef FIX_FIELDMAP
23 #define FIX_FIELDMAP
24 
25 #ifdef _MSC_VER
26 #pragma warning( disable: 4786 )
27 #endif
28 
29 #include "Field.h"
30 #include "MessageSorters.h"
31 #include "Exceptions.h"
32 #include "Utility.h"
33 #include <map>
34 #include <vector>
35 #include <sstream>
36 #include <algorithm>
37 
38 namespace FIX
39 {
46 class FieldMap
47 {
48 public:
49 #if defined(_MSC_VER) && _MSC_VER < 1300
50  typedef std::multimap < int, FieldBase, message_order > Fields;
51  typedef std::map < int, std::vector < FieldMap* >, std::less<int> > Groups;
52 #else
53  typedef std::multimap < int, FieldBase, message_order,
54  ALLOCATOR<std::pair<const int,FieldBase> > > Fields;
55  typedef std::map < int, std::vector < FieldMap* >, std::less<int>,
56  ALLOCATOR<std::pair<const int, std::vector< FieldMap* > > > > Groups;
57 #endif
58 
59  typedef Fields::const_iterator iterator;
60  typedef iterator const_iterator;
61  typedef Groups::const_iterator g_iterator;
62  typedef Groups::const_iterator g_const_iterator;
63 
64  FieldMap( const message_order& order =
65  message_order( message_order::normal ) )
66  : m_fields( order ) {}
67 
68  FieldMap( const int order[] )
69  : m_fields( message_order(order) ) {}
70 
71  FieldMap( const FieldMap& copy )
72  { *this = copy; }
73 
74  virtual ~FieldMap();
75 
76  FieldMap& operator=( const FieldMap& rhs );
77 
79  void setField( const FieldBase& field, bool overwrite = true )
80  throw( RepeatedTag )
81  {
82  if(!overwrite)
83  m_fields.insert( Fields::value_type( field.getField(), field ) );
84  else
85  {
86  Fields::iterator i = m_fields.find( field.getField() );
87  if( i == m_fields.end() )
88  m_fields.insert( Fields::value_type( field.getField(), field ) );
89  else
90  i->second = field;
91  }
92  }
94  void setField( int field, const std::string& value )
95  throw( RepeatedTag, NoTagValue )
96  {
97  FieldBase fieldBase( field, value );
98  setField( fieldBase );
99  }
100 
102  bool getFieldIfSet( FieldBase& field ) const
103  {
104  Fields::const_iterator iter = m_fields.find( field.getField() );
105  if ( iter == m_fields.end() )
106  return false;
107  field = iter->second;
108  return true;
109  }
110 
112  FieldBase& getField( FieldBase& field )
113  const throw( FieldNotFound )
114  {
115  field = getFieldRef( field.getField() );
116  return field;
117  }
118 
120  const std::string& getField( int field )
121  const throw( FieldNotFound )
122  {
123  return getFieldRef( field ).getString();
124  }
125 
127  const FieldBase& getFieldRef( int field )
128  const throw( FieldNotFound )
129  {
130  Fields::const_iterator iter = m_fields.find( field );
131  if ( iter == m_fields.end() )
132  throw FieldNotFound( field );
133  return iter->second;
134  }
135 
137  const FieldBase* const getFieldPtr( int field )
138  const throw( FieldNotFound )
139  {
140  return &getFieldRef( field );
141  }
142 
144  bool isSetField( const FieldBase& field ) const
145  { return isSetField( field.getField() ); }
147  bool isSetField( int field ) const
148  { return m_fields.find( field ) != m_fields.end(); }
149 
151  void removeField( int field );
152 
154  void addGroup( int field, const FieldMap& group, bool setCount = true );
155 
157  void addGroupPtr( int field, FieldMap * group, bool setCount = true );
158 
160  void replaceGroup( int num, int field, const FieldMap& group );
161 
163  FieldMap& getGroup( int num, int field, FieldMap& group ) const
164  throw( FieldNotFound )
165  {
166  return group = getGroupRef( num, field );
167  }
168 
170  FieldMap& getGroupRef( int num, int field ) const
171  throw( FieldNotFound )
172  {
173  Groups::const_iterator i = m_groups.find( field );
174  if( i == m_groups.end() ) throw FieldNotFound( field );
175  if( num <= 0 ) throw FieldNotFound( field );
176  if( i->second.size() < (unsigned)num ) throw FieldNotFound( field );
177  return *( *(i->second.begin() + (num-1) ) );
178  }
179 
181  FieldMap* getGroupPtr( int num, int field ) const
182  throw( FieldNotFound )
183  {
184  return &getGroupRef( num, field );
185  }
186 
188  void removeGroup( int num, int field );
190  void removeGroup( int field );
191 
193  bool hasGroup( int field ) const;
195  bool hasGroup( int num, int field ) const;
197  size_t groupCount( int field ) const;
198 
200  void clear();
202  bool isEmpty();
203 
204  size_t totalFields() const;
205 
206  std::string& calculateString( std::string& ) const;
207 
208  int calculateLength( int beginStringField = FIELD::BeginString,
209  int bodyLengthField = FIELD::BodyLength,
210  int checkSumField = FIELD::CheckSum ) const;
211 
212  int calculateTotal( int checkSumField = FIELD::CheckSum ) const;
213 
214  iterator begin() const { return m_fields.begin(); }
215  iterator end() const { return m_fields.end(); }
216  g_iterator g_begin() const { return m_groups.begin(); }
217  g_iterator g_end() const { return m_groups.end(); }
218 
219 private:
220  Fields m_fields;
221  Groups m_groups;
222 };
224 }
225 
226 #define FIELD_SET( MAP, FIELD ) \
227 bool isSet( const FIELD& field ) const \
228 { return (MAP).isSetField(field); } \
229 void set( const FIELD& field ) \
230 { (MAP).setField(field); } \
231 FIELD& get( FIELD& field ) const \
232 { return (FIELD&)(MAP).getField(field); } \
233 bool getIfSet( FIELD& field ) const \
234 { return (MAP).getFieldIfSet(field); }
235 
236 #define FIELD_GET_PTR( MAP, FLD ) \
237 (const FIX::FLD*)MAP.getFieldPtr( FIX::FIELD::FLD )
238 #define FIELD_GET_REF( MAP, FLD ) \
239 (const FIX::FLD&)MAP.getFieldRef( FIX::FIELD::FLD )
240 #define FIELD_THROW_IF_NOT_FOUND( MAP, FLD ) \
241 if( !(MAP).isSetField( FIX::FIELD::FLD) ) \
242  throw FieldNotFound( FIX::FIELD::FLD )
243 #endif //FIX_FIELDMAP
244 
bool isSetField(const FieldBase &field) const
Check to see if a field is set.
Definition: FieldMap.h:144
int calculateTotal(int checkSumField=FIELD::CheckSum) const
Definition: FieldMap.cpp:218
iterator begin() const
Definition: FieldMap.h:214
bool hasGroup(int field) const
Check to see any instance of a group exists.
Definition: FieldMap.cpp:129
Fields::const_iterator iterator
Definition: FieldMap.h:59
bool getFieldIfSet(FieldBase &field) const
Get a field if set.
Definition: FieldMap.h:102
const std::string & getString() const
Get the string representation of the fields value.
Definition: Field.h:112
Groups::const_iterator g_iterator
Definition: FieldMap.h:61
Repeated tag not part of repeating group.
Definition: Exceptions.h:199
FieldMap & operator=(const FieldMap &rhs)
Definition: FieldMap.cpp:38
iterator end() const
Definition: FieldMap.h:215
void removeGroup(int num, int field)
Remove a specific instance of a group.
Definition: FieldMap.cpp:86
FieldMap(const FieldMap &copy)
Definition: FieldMap.h:71
Definition: Acceptor.cpp:34
FieldMap * getGroupPtr(int num, int field) const
Get direct access to a field through a pointer.
Definition: FieldMap.h:181
FieldBase & getField(FieldBase &field) const
Get a field without type checking.
Definition: FieldMap.h:112
Stores and organizes a collection of Fields.
Definition: FieldMap.h:46
const FieldBase & getFieldRef(int field) const
Get direct access to a field through a reference.
Definition: FieldMap.h:127
Fields m_fields
Definition: FieldMap.h:220
void setField(int field, const std::string &value)
Set a field without a field class.
Definition: FieldMap.h:94
virtual ~FieldMap()
Definition: FieldMap.cpp:33
const FieldBase *const getFieldPtr(int field) const
Get direct access to a field through a pointer.
Definition: FieldMap.h:137
bool isEmpty()
Check if map contains any fields.
Definition: FieldMap.cpp:157
Groups::const_iterator g_const_iterator
Definition: FieldMap.h:62
FieldMap(const int order[])
Definition: FieldMap.h:68
size_t groupCount(int field) const
Count the number of instance of a group.
Definition: FieldMap.cpp:135
const std::string & getField(int field) const
Get a field without a field class.
Definition: FieldMap.h:120
Field not found inside a message.
Definition: Exceptions.h:57
Groups m_groups
Definition: FieldMap.h:221
void setField(const FieldBase &field, bool overwrite=true)
Set a field without type checking.
Definition: FieldMap.h:79
int calculateLength(int beginStringField=FIELD::BeginString, int bodyLengthField=FIELD::BodyLength, int checkSumField=FIELD::CheckSum) const
Definition: FieldMap.cpp:194
size_t totalFields() const
Definition: FieldMap.cpp:162
std::string & calculateString(std::string &) const
Definition: FieldMap.cpp:176
int getField() const
Get the fields integer tag.
Definition: Field.h:108
void removeField(int field)
Remove a field. If field is not present, this is a no-op.
Definition: FieldMap.cpp:117
void addGroup(int field, const FieldMap &group, bool setCount=true)
Add a group.
Definition: FieldMap.cpp:58
g_iterator g_end() const
Definition: FieldMap.h:217
iterator const_iterator
Definition: FieldMap.h:60
g_iterator g_begin() const
Definition: FieldMap.h:216
Base representation of all Field classes.
Definition: Field.h:45
void addGroupPtr(int field, FieldMap *group, bool setCount=true)
Acquire ownership of Group object.
Definition: FieldMap.cpp:65
std::multimap< int, FieldBase, message_order, ALLOCATOR< std::pair< const int, FieldBase > > > Fields
Definition: FieldMap.h:54
Sorts fields in header, normal, or trailer order.
FieldMap(const message_order &order=message_order(message_order::normal))
Definition: FieldMap.h:64
const int BeginString
Field exists in message without a value.
Definition: Exceptions.h:128
std::map< int, std::vector< FieldMap *>, std::less< int >, ALLOCATOR< std::pair< const int, std::vector< FieldMap *> > > > Groups
Definition: FieldMap.h:56
const int BodyLength
void replaceGroup(int num, int field, const FieldMap &group)
Replace a specific instance of a group.
Definition: FieldMap.cpp:77
const int CheckSum
FieldMap & getGroupRef(int num, int field) const
Get direct access to a field through a reference.
Definition: FieldMap.h:170
void clear()
Clear all fields from the map.
Definition: FieldMap.cpp:143
bool isSetField(int field) const
Check to see if a field is set by referencing its number.
Definition: FieldMap.h:147
FieldMap & getGroup(int num, int field, FieldMap &group) const
Get a specific instance of a group.
Definition: FieldMap.h:163

Generated on Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001