libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
conversions.hpp
Go to the documentation of this file.
1 #ifndef ISIMUD_EXCHANGES_FIX_common_CONVERSIONS_HPP
2 #define ISIMUD_EXCHANGES_FIX_common_CONVERSIONS_HPP
3 /******************************************************************************
4 ** Copyright © 2020 by J.M.McGuiness, isimud@hussar.me.uk
5 **
6 ** This library is free software; you can redistribute it and/or
7 ** modify it under the terms of the GNU Lesser General Public
8 ** License as published by the Free Software Foundation; either
9 ** version 2.1 of the License, or (at your option) any later version.
10 **
11 ** This library is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ** Lesser General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public
17 ** License along with this library; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 
21 #include "types.hpp"
22 
23 #include "core/memops.hpp"
24 #include "core/ttypes.hpp"
25 
26 #include <algorithm>
27 #include <cassert>
28 
29 namespace isimud { namespace ISIMUD_VER_NAMESPACE { namespace exchanges { namespace FIX { namespace common {
30 
31 /**
32  Assumes the symbol is an ISIN.
33 */
34 inline int
35 convert(SecurityID_t const &i, char *buff, std::size_t sz) {
36  assert(i.max_size()<=sz);
37  const std::size_t sz_to_copy=std::min(i.max_size(), sz);
38  std::copy_n(i.begin(), sz_to_copy, buff);
39  return sz_to_copy;
40 }
41 
42 inline int
43 convert(std::int32_t a, char *buff, std::size_t sz) noexcept(true) {
44  return libjmmcg::tostring(a, buff, sz);
45 }
46 
47 inline int
48 convert(std::uint32_t a, char *buff, std::size_t sz) noexcept(true) {
49  return libjmmcg::tostring(a, buff, sz);
50 }
51 
52 inline int
53 convert(std::int64_t a, char *buff, std::size_t sz) noexcept(true) {
54  return libjmmcg::tostring(a, buff, sz);
55 }
56 
57 inline int
58 convert(std::uint64_t a, char *buff, std::size_t sz) noexcept(true) {
59  return libjmmcg::tostring(a, buff, sz);
60 }
61 
62 inline int
63 convert(Price_t const &a, char *buff, std::size_t sz) {
64  const double c=static_cast<double>(a)/implied_decimal_places;
65  return libjmmcg::tostring(c, buff, sz);
66 }
67 
68 template<class Ret> inline Ret
69 convert(FIX::common::field_str_const_range_t const &a)=delete;
70 
71 template<> inline std::int32_t
72 convert<std::int32_t>(FIX::common::field_str_const_range_t const &a) {
73  assert((a.second-a.first)>0);
74  return static_cast<std::int32_t>(libjmmcg::fromstring<long>(a.first, a.second-a.first));
75 }
76 
77 template<> inline std::uint32_t
78 convert<std::uint32_t>(FIX::common::field_str_const_range_t const &a) {
79  assert((a.second-a.first)>0);
80  return static_cast<std::uint32_t>(libjmmcg::fromstring<long>(a.first, a.second-a.first));
81 }
82 
83 template<> inline std::uint64_t
84 convert<std::uint64_t>(FIX::common::field_str_const_range_t const &a) {
85  assert((a.second-a.first)>0);
86  return static_cast<std::uint64_t>(libjmmcg::fromstring<long>(a.first, a.second-a.first));
87 }
88 
89 template<> inline std::string
90 convert<std::string>(FIX::common::field_str_const_range_t const &a) {
91  assert((a.second-a.first)>0);
92  return std::string(a.first, a.second);
93 }
94 
95 template<> inline Quantity_t
96 convert<Quantity_t>(FIX::common::field_str_const_range_t const &a) {
97  assert((a.second-a.first)>0);
98  return libjmmcg::fromstring<Quantity_t>(a.first, a.second-a.first);
99 }
100 
101 template<> inline SecurityID_t
102 convert<SecurityID_t>(FIX::common::field_str_const_range_t const &a) {
103  assert((a.second-a.first)>0);
104  assert((a.second-a.first)<=static_cast<long>(sizeof(SecurityID_t)));
105  SecurityID_t tmp{};
106  std::memcpy(tmp.data(), a.first, a.second-a.first);
107  return tmp;
108 }
109 
110 } } } } }
111 
112 #endif