fix8  version 1.4.0
Open Source C++ FIX Framework
f8c.hpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------------------
2 /*
3 
4 Fix8 is released under the GNU LESSER GENERAL PUBLIC LICENSE Version 3.
5 
6 Fix8 Open Source FIX Engine.
7 Copyright (C) 2010-16 David L. Dight <fix@fix8.org>
8 
9 Fix8 is free software: you can redistribute it and / or modify it under the terms of the
10 GNU Lesser General Public License as published by the Free Software Foundation, either
11 version 3 of the License, or (at your option) any later version.
12 
13 Fix8 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
14 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 
16 You should have received a copy of the GNU Lesser General Public License along with Fix8.
17 If not, see <http://www.gnu.org/licenses/>.
18 
19 BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO
20 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
21 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
22 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
24 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
25 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
26 
27 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
28 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
29 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
30 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
31 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
32 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
33 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 
35 */
36 //-------------------------------------------------------------------------------------------------
37 #ifndef FIX8_F8C_HPP_
38 #define FIX8_F8C_HPP_
39 
40 //-------------------------------------------------------------------------------------------------
41 namespace FIX8 {
42 
43 //-------------------------------------------------------------------------------------------------
45 struct Ctxt
46 {
49  using Output = std::pair<std::pair<std::string, std::string>, std::ostream *>;
51  static const std::string _exts[count], _exts_ver[2];
52  unsigned _version;
54 };
55 
56 //-------------------------------------------------------------------------------------------------
59 {
60  bool _isRange;
61 
62 protected:
63  virtual bool comp(const RealmObject *p1, const RealmObject *p2) = 0;
64  friend std::ostream& operator<<(std::ostream& os, RealmObject& what)
65  {
66  what.print(os);
67  return os;
68  }
69 
70  virtual void print(std::ostream& os) = 0;
71 
72 public:
73  RealmObject(bool isRange) : _isRange(isRange) {}
74  virtual ~RealmObject() {}
75 
76  bool is_range() const { return _isRange; }
77  static RealmObject *create(const std::string& from, FieldTrait::FieldType ftype, bool isRange=false);
78 
79  struct less
80  {
81  bool operator()(const RealmObject *p1, const RealmObject *p2) const
82  { return const_cast<RealmObject*>(p1)->comp(p1, p2); }
83  };
84 };
85 
87 
88 template <typename T>
89 class TypedRealm : public RealmObject
90 {
91 protected:
92  T _obj;
93 
94 public:
95  TypedRealm(const T& from, bool isRange) : RealmObject(isRange), _obj(from) {}
96  virtual void print(std::ostream& os) { os << _obj; }
97  bool comp(const RealmObject *p1, const RealmObject *p2)
98  { return (static_cast<const TypedRealm<T>*>(p1))->_obj < static_cast<const TypedRealm<T>*>(p2)->_obj; }
99 };
100 
102 struct StringRealm : public TypedRealm<std::string>
103 {
104  StringRealm(const std::string& from, bool isRange) : TypedRealm<std::string>(from, isRange) {}
105  void print(std::ostream& os) { os << '"' << _obj << '"'; }
106 };
107 
109 struct CharRealm : public TypedRealm<char>
110 {
111  CharRealm(const char& from, bool isRange) : TypedRealm<char>(from, isRange) {}
112  void print(std::ostream& os) { os << '\'' << _obj << '\''; }
113 };
114 
115 using RealmMap = std::map<RealmObject *, std::string, RealmObject::less>;
116 
117 //-------------------------------------------------------------------------------------------------
118 using FieldSpecMap = std::map<unsigned, struct FieldSpec>;
119 using FieldToNumMap = std::map<std::string, unsigned>;
120 using GroupMap = std::map<unsigned, struct MessageSpec>;
121 
122 //-------------------------------------------------------------------------------------------------
123 using BaseTypeMap = std::map<std::string, FieldTrait::FieldType>;
124 using TypeToCPP = std::map<FieldTrait::FieldType, std::pair<std::string, std::string>>;
125 
127 struct FieldSpec
128 {
129  static const BaseTypeMap _baseTypeMap;
130  static const TypeToCPP _typeToCPP;
131 
132  std::string _name, _description, _comment;
135  unsigned _doffset;
137 
138  mutable bool _used;
139 
141  : _name(name), _ftype(ftype), _dtype(RealmBase::dt_set), _doffset(), _dvals(), _used() {}
142 
143  virtual ~FieldSpec()
144  {
145  if (_dvals)
146  std::for_each(_dvals->begin(), _dvals->end(), [](RealmMap::value_type& pp) { delete pp.first; });
147  delete _dvals;
148  }
149 
150  void set_used() { _used = true; }
151 };
152 
153 //-------------------------------------------------------------------------------------------------
156 {
159  uint32_t _group_refcnt, _hash;
160  std::string _name, _description, _comment;
161  bool _is_admin;
162 
164  MessageSpec(const std::string& name, bool admin=false) : _group_refcnt(), _hash(), _name(name), _is_admin(admin) {}
166  virtual ~MessageSpec() {}
167 
172  friend std::ostream& operator<<(std::ostream& os, const MessageSpec& what);
173 };
174 
175 using MessageSpecMap = std::map<const std::string, MessageSpec>;
176 using FieldTraitOrder = std::multiset<const FieldTrait *, FieldTrait::PosCompare>;
177 
178 //-----------------------------------------------------------------------------------------
179 using CommonGroups = std::map<uint32_t, struct MessageSpec>;
180 using CommonGroupMap = std::map<unsigned, CommonGroups>;
181 
182 //-----------------------------------------------------------------------------------------
183 using Components = std::map<std::string, const XmlElement *>;
184 
185 //-------------------------------------------------------------------------------------------------
187 {
199 };
200 
201 using CSMap = std::map<comp_str, std::string>;
202 
203 //-----------------------------------------------------------------------------------------
204 inline int recover_line(const XmlElement& xf) { return xf.FindAttr("line", xf.GetLine()); }
205 
206 //-----------------------------------------------------------------------------------------
207 class push_dir
208 {
210 
211  void getdir()
212  {
213 #ifdef _MSC_VER
214  if (_getcwd(_cwd, FIX8_MAX_FLD_LENGTH) == 0)
215 #else
216  if (::getcwd(_cwd, FIX8_MAX_FLD_LENGTH) == 0)
217 #endif
218  {
219  std::ostringstream ostr;
220  ostr << "Error getting current directory (" << ::strerror(errno) << ')';
221  throw f8Exception(ostr.str());
222  }
223  }
224 
225  void chgdir(const char *to) const
226  {
227 #ifdef _MSC_VER
228  if (!to || _chdir(to))
229 #else
230  if (!to || ::chdir(to))
231 #endif
232  {
233  std::ostringstream ostr;
234  ostr << "Error changing to directory '" << (to ? to : "(null)") << '\'';
235  if (errno)
236  ostr << " (" << ::strerror(errno) << ')';
237  throw f8Exception(ostr.str());
238  }
239  }
240 
241 public:
242  explicit push_dir(const std::string& to) : _cwd() { getdir(); chgdir(to.c_str()); }
243  explicit push_dir(const char *to) : _cwd() { getdir(); chgdir(to); }
244  push_dir() : _cwd() { getdir(); }
245  ~push_dir() { chgdir(_cwd); }
246 };
247 
248 } // FIX8
249 
250 #endif // FIX8_F8C_HPP_
FieldTraits _fields
Definition: f8c.hpp:157
bool operator()(const RealmObject *p1, const RealmObject *p2) const
Definition: f8c.hpp:81
RealmMap * _dvals
Definition: f8c.hpp:136
int GetLine() const
Definition: xml.hpp:268
bool comp(const RealmObject *p1, const RealmObject *p2)
Definition: f8c.hpp:97
RealmBase::RealmType _dtype
Definition: f8c.hpp:134
f8c compilation context.
Definition: f8c.hpp:45
T FindAttr(const std::string &what, const T defValue) const
Definition: xml.hpp:209
std::string _systemns
Definition: f8c.hpp:53
virtual ~RealmObject()
Definition: f8c.hpp:74
std::string _description
Definition: f8c.hpp:132
friend std::ostream & operator<<(std::ostream &os, const MessageSpec &what)
static const TypeToCPP _typeToCPP
Definition: f8c.hpp:130
#define FIX8_MAX_FLD_LENGTH
Definition: f8config.h:571
MessageSpec(const std::string &name, bool admin=false)
Ctor.
Definition: f8c.hpp:164
FieldTrait::FieldType _ftype
Definition: f8c.hpp:133
std::map< comp_str, std::string > CSMap
Definition: f8c.hpp:201
std::map< std::string, const XmlElement * > Components
Definition: f8c.hpp:183
static const std::string _exts_ver[2]
Definition: f8c.hpp:51
std::string _name
Definition: f8c.hpp:160
virtual ~FieldSpec()
Definition: f8c.hpp:143
virtual bool comp(const RealmObject *p1, const RealmObject *p2)=0
f8c internal message representation.
Definition: f8c.hpp:155
std::map< unsigned, struct MessageSpec > GroupMap
Definition: f8c.hpp:120
std::map< FieldTrait::FieldType, std::pair< std::string, std::string >> TypeToCPP
Definition: f8c.hpp:124
void set_used()
Definition: f8c.hpp:150
static const BaseTypeMap _baseTypeMap
Definition: f8c.hpp:129
bool is_range() const
Definition: f8c.hpp:76
void getdir()
Definition: f8c.hpp:211
std::string _name
Definition: f8c.hpp:132
static const std::string _exts[count]
Definition: f8c.hpp:51
std::string _fixns
Definition: f8c.hpp:53
int recover_line(const XmlElement &xf)
Definition: f8c.hpp:204
Domain range/set static metadata base class.
Definition: field.hpp:58
std::string _comment
Definition: f8c.hpp:160
f8c character realm type.
Definition: f8c.hpp:109
virtual void print(std::ostream &os)
Definition: f8c.hpp:96
std::string _clname
Definition: f8c.hpp:53
bool _isRange
Definition: f8c.hpp:60
std::map< unsigned, struct FieldSpec > FieldSpecMap
Definition: f8c.hpp:118
Base exception class.
Definition: f8exception.hpp:49
A simple xml parser with Xpath style lookup.
Definition: xml.hpp:48
std::string _comment
Definition: f8c.hpp:132
f8c range or set domain realm.
Definition: f8c.hpp:58
RealmObject(bool isRange)
Definition: f8c.hpp:73
void print(std::ostream &os)
Definition: f8c.hpp:112
unsigned _doffset
Definition: f8c.hpp:135
std::multiset< const FieldTrait *, FieldTrait::PosCompare > FieldTraitOrder
Definition: f8c.hpp:176
friend std::ostream & operator<<(std::ostream &os, RealmObject &what)
Definition: f8c.hpp:64
void print(std::ostream &os)
Definition: f8c.hpp:105
push_dir(const std::string &to)
Definition: f8c.hpp:242
char _cwd[FIX8_MAX_FLD_LENGTH]
Definition: f8c.hpp:209
std::pair< std::pair< std::string, std::string >, std::ostream * > Output
Definition: f8c.hpp:49
std::map< std::string, unsigned > FieldToNumMap
Definition: f8c.hpp:119
std::map< RealmObject *, std::string, RealmObject::less > RealmMap
Definition: f8c.hpp:115
TypedRealm(const T &from, bool isRange)
Definition: f8c.hpp:95
f8c internal field representation.
Definition: f8c.hpp:127
Output _out[count]
Definition: f8c.hpp:50
CharRealm(const char &from, bool isRange)
Definition: f8c.hpp:111
push_dir(const char *to)
Definition: f8c.hpp:243
f8c string realm type.
Definition: f8c.hpp:102
virtual ~MessageSpec()
Dtor.
Definition: f8c.hpp:166
std::map< const std::string, MessageSpec > MessageSpecMap
Definition: f8c.hpp:175
void chgdir(const char *to) const
Definition: f8c.hpp:225
GroupMap _groups
Definition: f8c.hpp:158
static RealmObject * create(const std::string &from, FieldTrait::FieldType ftype, bool isRange=false)
Definition: f8cutils.cpp:362
virtual void print(std::ostream &os)=0
comp_str
Definition: f8c.hpp:186
f8c typed realm.
Definition: f8c.hpp:89
std::map< unsigned, CommonGroups > CommonGroupMap
Definition: f8c.hpp:180
std::map< std::string, FieldTrait::FieldType > BaseTypeMap
Definition: f8c.hpp:123
A collection of FieldTraits for a message. Which fields are required, which are present.
Definition: traits.hpp:437
unsigned _version
Definition: f8c.hpp:52
std::string _description
Definition: f8c.hpp:160
bool _is_admin
Definition: f8c.hpp:161
FieldSpec(const std::string &name, FieldTrait::FieldType ftype=FieldTrait::ft_untyped)
Definition: f8c.hpp:140
std::map< uint32_t, struct MessageSpec > CommonGroups
Definition: f8c.hpp:179
bool _used
Definition: f8c.hpp:138
uint32_t _hash
Definition: f8c.hpp:159
StringRealm(const std::string &from, bool isRange)
Definition: f8c.hpp:104
OutputFile
Definition: f8c.hpp:47
uint32_t _group_refcnt
Definition: f8c.hpp:159
std::string _beginstr
Definition: f8c.hpp:53