OnixS C++ Eurex T7 Market and Reference Data (EMDI, MDI, RDI, EOBI) Handlers  16.1.0
API documentation
Numeric.h
Go to the documentation of this file.
1 /*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable OnixS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19 
20 #pragma once
21 
22 #include <string>
23 #include <stdexcept>
24 #include <iosfwd>
25 
27 
28 namespace OnixS
29 {
30  namespace Eurex
31  {
32  namespace MarketData
33  {
34  typedef char Int8;
35  typedef unsigned char UInt8;
36 
37  typedef short Int16;
38  typedef unsigned short UInt16;
39 
40  typedef int Int32;
41  typedef unsigned int UInt32;
42 
43 #if defined (_MSC_VER)
44  typedef signed long long Int64;
45  typedef unsigned long long UInt64;
46 #elif defined (__GNUC__)
47 
48 #if defined (__LP64__)
49  typedef signed long Int64;
50  typedef unsigned long UInt64;
51 #else
52  typedef signed long long Int64;
53  typedef unsigned long long UInt64;
54 #endif
55 
56 #endif
57  typedef double Double;
58 
59  typedef Int64 DecimalMantissa;
60  typedef Int32 DecimalExponent;
61 
62  /// Decimal type for better precision.
63  class
64  ONIXS_EUREX_EMDI_API
65  Decimal
66  {
67  public:
68  /// Initializes instance from compound components.
69  Decimal (
70  DecimalMantissa mantissa = 0,
71  DecimalExponent exponent = 0);
72 
73  explicit Decimal (int);
74 
75  /// Converts from Double value using
76  /// default conversion precision.
77  explicit Decimal (Double value);
78 
79  /// Converts Double value to decimal.
80  /// @param value double precision floating-point value.
81  /// @param precision defines conversion precision.
82  Decimal (Double value, size_t precision);
83 
84  /// Initializes as copy of given value.
85  Decimal (const Decimal& other);
86 
87  /// Returns mantissa part of decimal.
88  DecimalMantissa mantissa() const;
89 
90  /// Updates mantissa part of decimal.
91  void mantissa (DecimalMantissa);
92 
93  /// Returns exponent part of decimal.
94  DecimalExponent exponent() const;
95 
96  /// Updates exponent part of decimal.
97  void exponent (DecimalExponent);
98 
99  /// Compares two numbers.
100  bool operator == (const Decimal&) const;
101 
102  /// Compares two numbers.
103  bool operator != (const Decimal&) const;
104 
105  /// Casts to whole integer number as regular
106  /// floating point value is casted.
107  /// @throw domain_error exception on failure.
108  operator Int32() const;
109 
110  /// Casts to whole integer number as regular
111  /// floating point value is casted.
112  /// @throw domain_error exception on failure.
113  operator UInt32() const;
114 
115  /// Casts to whole integer number as regular
116  /// floating point value is casted.
117  /// @throw domain_error exception on failure.
118  operator Int64() const;
119 
120  /// Casts to whole integer number as regular
121  /// floating point value is casted.
122  /// @throw domain_error exception on failure.
123  operator UInt64() const;
124 
125  /// Casts to whole floating point as regular
126  /// value is casted to a smaller precision.
127  /// @throw domain_error exception on failure.
128  operator Double() const;
129 
130  /// Casts to whole integer number as regular
131  /// floating point value is casted.
132  /// @return false if conversion fails.
133  bool toNumber (Int32&) const;
134 
135  /// Casts to whole integer number as regular
136  /// floating point value is casted.
137  /// @return false if conversion fails.
138  bool toNumber (UInt32&) const;
139 
140  /// Casts to whole integer number as regular
141  /// floating point value is casted.
142  /// @return false if conversion fails.
143  bool toNumber (Int64&) const;
144 
145  /// Casts to whole integer number as regular
146  /// floating point value is casted.
147  /// @return false if conversion fails.
148  bool toNumber (UInt64&) const;
149 
150  /// Casts to floating point number.
151  /// @return false if conversion fails.
152  bool toNumber (Double&) const;
153 
154  /// Appends text presentation to given string.
155  void toString (std::string&) const;
156 
157  /// Returns text presentation of decimal.
158  std::string toString() const;
159 
160  /// Reinitializes instance from another one.
161  Decimal&
162  operator = (
163  const Decimal& other);
164 
165  /// Attempts to parse decimal value
166  /// from its string/text presentation.
167  /// @return false on parsing failure.
168  static
169  bool
170  tryParse (
171  const char* buffer,
172  size_t bufferSize,
173  Decimal&);
174 
175  /// Parses decimal from string presentation.
176  /// @throw std::exception on failure.
177  static
178  Decimal
179  parse (
180  const char* buffer,
181  size_t bufferSize);
182 
183  private:
184  /// Mantissa.
185  DecimalMantissa mantissa_;
186 
187  /// Exponent.
188  DecimalExponent exponent_;
189  };
190 
191  bool operator < (const Decimal& l, const Decimal& r);
192  bool operator > (const Decimal& l, const Decimal& r);
193  bool operator <= (const Decimal& l, const Decimal& r);
194  bool operator >= (const Decimal& l, const Decimal& r);
195 
196 
197  inline
198  DecimalMantissa
200  {
201  return mantissa_;
202  }
203 
204  inline
205  void
207  DecimalMantissa value)
208  {
209  mantissa_ = value;
210  }
211 
212  inline
213  DecimalExponent
215  {
216  return exponent_;
217  }
218 
219  inline
220  void
222  DecimalExponent value)
223  {
224  exponent_ = value;
225  }
226 
227  inline
228  Decimal::operator Int32() const
229  {
230  Int32 number;
231 
232  if (toNumber (number) )
233  return number;
234 
235  throw std::domain_error (
236  "Cannot cast value to target type. ");
237  }
238 
239  inline
240  Decimal::operator UInt32() const
241  {
242  UInt32 number;
243 
244  if (toNumber (number) )
245  return number;
246 
247  throw std::domain_error (
248  "Cannot cast value to target type. ");
249  }
250 
251  inline
252  Decimal::operator Int64() const
253  {
254  Int64 number;
255 
256  if (toNumber (number) )
257  return number;
258 
259  throw std::domain_error (
260  "Cannot cast value to target type. ");
261  }
262 
263  inline
264  Decimal::operator UInt64() const
265  {
266  UInt64 number;
267 
268  if (toNumber (number) )
269  return number;
270 
271  throw std::domain_error (
272  "Cannot cast value to target type. ");
273  }
274 
275  inline
276  Decimal::operator Double() const
277  {
278  Double number;
279 
280  if (toNumber (number) )
281  return number;
282 
283  throw std::domain_error (
284  "Cannot cast value to target type. ");
285  }
286 
287  inline
288  std::string
290  {
291  std::string presentation;
292  toString (presentation);
293  return presentation;
294  }
295 
296  /// Helper class for conversion from string to number.
297  struct ONIXS_EUREX_EMDI_API Number
298  {
299  static
300  bool
301  tryParse (
302  const char* buffer,
303  size_t bufferSize,
304  Int32& number);
305 
306  static
307  bool
308  tryParse (
309  const char* buffer,
310  size_t bufferSize,
311  UInt32& number);
312 
313  static
314  bool
315  tryParse (
316  const char* buffer,
317  size_t bufferSize,
318  Int64& number);
319 
320  static
321  bool
322  tryParse (
323  const char* buffer,
324  size_t bufferSize,
325  UInt64& number);
326 
327  static
328  bool
329  tryParse (
330  const char* buffer,
331  size_t bufferSize,
332  Double& number);
333 
334  static
335  bool
336  tryParse (
337  const char* buffer,
338  size_t bufferSize,
339  Decimal& number);
340  };
341  }
342  }
343 }
344 
345 namespace std
346 {
347  // Outputs into standard stream.
348  ONIXS_EUREX_EMDI_API
349  std::ostream&
350  operator <<(std::ostream&,
352 }
unsigned char UInt8
Definition: Numeric.h:35
bool operator>=(const Decimal &l, const Decimal &r)
bool operator==(const FieldValueRef &ref, const std::string &str)
STL namespace.
std::ostream & operator<<(std::ostream &os, const Message &message)
std::string toString() const
Returns text presentation of decimal.
Definition: Numeric.h:289
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &)
DecimalMantissa mantissa() const
Returns mantissa part of decimal.
Definition: Numeric.h:199
unsigned int UInt32
Definition: Numeric.h:41
Definition: Defines.h:30
Decimal type for better precision.
Definition: Numeric.h:63
bool operator<=(const Decimal &l, const Decimal &r)
DecimalExponent exponent() const
Returns exponent part of decimal.
Definition: Numeric.h:214
bool operator<(const Decimal &l, const Decimal &r)
unsigned short UInt16
Definition: Numeric.h:38
bool operator!=(const FieldValueRef &ref, const std::string &str)
bool toNumber(Int32 &) const
bool operator>(const Decimal &l, const Decimal &r)
Helper class for conversion from string to number.
Definition: Numeric.h:297