fix8  version 1.4.0
Open Source C++ FIX Framework
f8types.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 THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE
20 COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
21 KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO
23 THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE,
24 YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
25 
26 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT
27 HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED
28 ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
29 CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT
30 NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
31 THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
32 HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33 
34 */
35 //-------------------------------------------------------------------------------------------------
36 #ifndef FIX8_TYPES_HPP_
37 #define FIX8_TYPES_HPP_
38 
39 //-------------------------------------------------------------------------------------------------
40 #include <map>
41 #include <algorithm>
42 
43 //-------------------------------------------------------------------------------------------------
44 namespace FIX8 {
45 
46 //-------------------------------------------------------------------------------------------------
47 using f8String = std::string;
48 #if defined FIX8_USE_SINGLE_PRECISION
49 using fp_type = float;
50 #else
51 using fp_type = double;
52 #endif
53 
54 //-------------------------------------------------------------------------------------------------
57 
59 const unsigned char default_field_separator(0x1);
60 
62 const unsigned char default_assignment_separator('=');
63 
64 //-------------------------------------------------------------------------------------------------
66 
68 template<typename Key, typename Val>
69 struct _pair
70 {
71  Key _key;
72  Val _value;
73 
74  const Key& first() const { return _key; }
75  const Val& second() const { return _value; }
76 
78  static bool Less(const _pair& p1, const _pair& p2) { return p1._key < p2._key; }
79 };
80 
82 
83 template<typename Val>
84 struct _pair<const char *, Val>
85 {
86  const char *_key;
87  Val _value;
88 
89  const char *first() const { return _key; }
90  const Val& second() const { return _value; }
91 
93  static bool Less(const _pair& p1, const _pair& p2) { return ::strcmp(p1._key, p2._key) < 0; }
94 };
95 
97 
99 template<typename Key, typename Val>
101 {
102 public:
104  using iterator = Pair*;
105  using const_iterator = const Pair*;
106 
107 #ifndef _MSC_VER
108 private:
109 #endif
112 
114  const size_t _pairsz;
115 
119  const_iterator _find(const Key& key) const
120  {
121  const_iterator res(std::lower_bound (begin(), end(), reinterpret_cast<const Pair&>(key), Pair::Less));
123  return res != end() && !Pair::Less(reinterpret_cast<const Pair&>(key), *res) ? res : nullptr;
124  }
125 
126 public:
128  GeneratedTable(const_iterator pairs, const size_t pairsz)
129  : _pairs(pairs), _pairsz(pairsz) {}
130 
133  const_iterator begin() const { return _pairs; }
134 
137  const_iterator end() const { return _pairs + _pairsz; }
138 
143  const Val& find_ref(const Key& key) const
144  {
145  const_iterator res(_find(key));
146  if (res)
147  return res->_value;
148  throw InvalidMetadata<Key>(key);
149  }
150 
154  const Val *find_ptr(const Key& key) const
155  {
156  const_iterator res(_find(key));
157  return res ? &res->_value : nullptr;
158  }
159 
163  const_iterator find_pair_ptr(const Key& key) const
164  {
165  const_iterator res(_find(key));
166  return res ? res : nullptr;
167  }
168 
172  const_iterator at(const size_t idx) const { return idx < _pairsz ? _pairs + idx : nullptr; }
173 
176  size_t size() const { return _pairsz; }
177 };
178 
179 //-------------------------------------------------------------------------------------------------
182 
185 template<typename K, typename T, typename Comp>
187 {
188 public:
189  using iterator = T*;
190  using const_iterator = const T*;
191  using result = std::pair<iterator, bool>;
192 
193 private:
194  const size_t _reserve;
195  size_t _sz, _rsz;
196  T *_arr;
197 
198  using internal_result = std::pair<iterator, iterator>;
199  using const_internal_result = std::pair<const_iterator, const_iterator>;
200 
205  static size_t calc_reserve(size_t sz, size_t res)
206  {
207  if (!sz) // special case - reserve means number to reserve, not %
208  return res;
209  const size_t val(sz * res / 100);
210  return val ? val : 1;
211  }
212 
213 public:
218  presorted_set(const_iterator arr_start, const size_t sz, const size_t reserve=FIX8_RESERVE_PERCENT) : _reserve(reserve),
219  _sz(sz), _rsz(_sz + calc_reserve(_sz, _reserve)), _arr(new T[_rsz])
220  { memcpy(_arr, arr_start, _sz * sizeof(T)); }
221 
224  presorted_set(const presorted_set& from) : _reserve(from._reserve),
225  _sz(from._sz), _rsz(from._rsz), _arr(from._arr ? new T[_rsz] : 0)
226  { if (_arr) memcpy(_arr, from._arr, _sz * sizeof(T)); }
227 
231  explicit presorted_set(const size_t sz=0, const size_t reserve=FIX8_RESERVE_PERCENT) : _reserve(reserve),
232  _sz(sz), _rsz(_sz + calc_reserve(_sz, _reserve)), _arr() {}
233 
235  ~presorted_set() { delete[] _arr; }
236 
241  iterator find(const T what, bool& answer)
242  {
243  const internal_result res(std::equal_range (_arr, _arr + _sz, what, Comp()));
244  answer = res.first != res.second;
245  return res.first;
246  }
247 
252  iterator find(const K key, bool& answer) { return find(T(key), answer); }
253 
257  const_iterator find(const K key) const { return find(T(key)); }
258 
262  const_iterator find(const T what) const
263  {
264  const const_internal_result res(std::equal_range (_arr, _arr + _sz, what, Comp()));
265  return res.first != res.second ? res.first : end();
266  }
267 
271  iterator find(const K key) { return find(T(key)); }
272 
276  iterator find(const T what)
277  {
278  internal_result res(std::equal_range (_arr, _arr + _sz, what, Comp()));
279  return res.first != res.second ? res.first : end();
280  }
281 
285  const_iterator at(const size_t idx) const { return idx < _sz ? _arr + idx : end(); }
286 
291  {
292  if (!_sz)
293  {
294  _arr = new T[_rsz];
295  memcpy(_arr, what, sizeof(T));
296  ++_sz;
297  return result(_arr, true);
298  }
299 
300  bool answer;
301  iterator where(find(*what, answer));
302  if (answer)
303  return result(end(), false);
304 
305  if (_sz < _rsz)
306  {
307  memmove(where + 1, where, (end() - where) * sizeof(T));
308  memcpy(where, what, sizeof(T));
309  }
310  else
311  {
312  iterator new_arr(new T[_rsz = _sz + calc_reserve(_sz, _reserve)]);
313  const size_t wptr(where - _arr);
314  if (wptr > 0)
315  memcpy(new_arr, _arr, sizeof(T) * wptr);
316  memcpy(new_arr + wptr, what, sizeof(T));
317  memcpy(new_arr + wptr + 1, where, (end() - where) * sizeof(T));
318  delete[] _arr;
319  _arr = new_arr;
320  }
321  ++_sz;
322  return result(where, true);
323  }
324 
329  static size_t distance(const_iterator what_begin, const_iterator what_end)
330  { return what_end - what_begin; }
331 
335  void insert(const_iterator what_begin, const_iterator what_end)
336  {
337  for (const_iterator ptr(what_begin); ptr < what_end; ++ptr)
338  if (!insert(ptr).second)
339  break;
340  }
341 
343  void clear() { _sz = 0; }
344 
347  size_t size() const { return _sz; }
348 
351  bool empty() const { return _sz == 0; }
352 
355  size_t rsize() const { return _rsz; }
356 
359  iterator begin() { return _arr; }
360 
363  iterator end() { return _arr + _sz; }
364 
367  const_iterator begin() const { return _arr; }
368 
371  const_iterator end() const { return _arr + _sz; }
372 };
373 
374 //-------------------------------------------------------------------------------------------------
376 
378 template<typename T, typename... args> struct Type2Type {};
379 
380 //-------------------------------------------------------------------------------------------------
382 struct null_insert { template <typename T> null_insert& operator<<(const T&) { return *this; } };
383 
384 } // FIX8
385 
386 #endif // F8_TYPES_HPP_
size_t rsize() const
Definition: f8types.hpp:355
Indicates a static metadata lookup failed. With the exception of user defined fields there should nev...
size_t size() const
Definition: f8types.hpp:347
const_iterator find_pair_ptr(const Key &key) const
Definition: f8types.hpp:163
const_iterator begin() const
Definition: f8types.hpp:367
static bool Less(const _pair &p1, const _pair &p2)
Sort functor.
Definition: f8types.hpp:93
iterator begin()
Definition: f8types.hpp:359
const Val & second() const
Definition: f8types.hpp:90
null_insert & operator<<(const T &)
Definition: f8types.hpp:382
const_iterator begin() const
Definition: f8types.hpp:133
static size_t calc_reserve(size_t sz, size_t res)
Definition: f8types.hpp:205
std::pair< const_iterator, const_iterator > const_internal_result
Definition: f8types.hpp:199
const size_t _reserve
Definition: f8types.hpp:194
Pair abstraction for use with GeneratedTable.
Definition: f8types.hpp:69
std::pair< iterator, iterator > internal_result
Definition: f8types.hpp:198
iterator find(const K key)
Definition: f8types.hpp:271
const T * const_iterator
Definition: f8types.hpp:190
const_iterator _find(const Key &key) const
Definition: f8types.hpp:119
Val _value
Definition: f8types.hpp:72
double fp_type
Definition: f8types.hpp:51
void insert(const_iterator what_begin, const_iterator what_end)
Definition: f8types.hpp:335
ProcessModel
Supported session process models.
Definition: f8types.hpp:56
iterator find(const K key, bool &answer)
Definition: f8types.hpp:252
const_iterator end() const
Definition: f8types.hpp:371
bool empty() const
Definition: f8types.hpp:351
size_t size() const
Definition: f8types.hpp:176
result insert(const_iterator what)
Definition: f8types.hpp:290
const Val & second() const
Definition: f8types.hpp:75
Type2Type idiom. Variadic template version. Kudos to Andrei Alexandrescu.
Definition: f8types.hpp:378
#define FIX8_RESERVE_PERCENT
Definition: f8config.h:676
std::pair< iterator, bool > result
Definition: f8types.hpp:191
const size_t _pairsz
The number of elements in the data set.
Definition: f8types.hpp:114
const char * first() const
Definition: f8types.hpp:89
const unsigned char default_assignment_separator('=')
default FIX assignment separator (=)
const_iterator find(const K key) const
Definition: f8types.hpp:257
presorted_set(const presorted_set &from)
Definition: f8types.hpp:224
iterator find(const T what, bool &answer)
Definition: f8types.hpp:241
const unsigned char default_field_separator(0x1)
default FIX field separator (^A)
const_iterator find(const T what) const
Definition: f8types.hpp:262
presorted_set(const_iterator arr_start, const size_t sz, const size_t reserve=FIX8_RESERVE_PERCENT)
Definition: f8types.hpp:218
const_iterator _pairs
The actual data set.
Definition: f8types.hpp:111
const_iterator at(const size_t idx) const
Definition: f8types.hpp:172
const Val & find_ref(const Key &key) const
Definition: f8types.hpp:143
const_iterator at(const size_t idx) const
Definition: f8types.hpp:285
static size_t distance(const_iterator what_begin, const_iterator what_end)
Definition: f8types.hpp:329
GeneratedTable(const_iterator pairs, const size_t pairsz)
Ctor.
Definition: f8types.hpp:128
static bool Less(const _pair &p1, const _pair &p2)
Sort functor.
Definition: f8types.hpp:78
const Val * find_ptr(const Key &key) const
Definition: f8types.hpp:154
Template provides insert operator that inserts nothing.
Definition: f8types.hpp:382
iterator end()
Definition: f8types.hpp:363
const Key & first() const
Definition: f8types.hpp:74
Fast map for statically generated data types. Assumes table is sorted. Complexity is O(logN)...
Definition: f8types.hpp:100
std::string f8String
Definition: f8types.hpp:47
const_iterator end() const
Definition: f8types.hpp:137
presorted_set(const size_t sz=0, const size_t reserve=FIX8_RESERVE_PERCENT)
Definition: f8types.hpp:231
iterator find(const T what)
Definition: f8types.hpp:276