OnixS C++ Tradeweb Approved Publication Arrangement (APA) Handler  1.2.2.18
API documentation
Decimal.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 
28 
29 namespace OnixS
30 {
31  namespace Tradeweb
32  {
33  namespace MarketData
34  {
35  namespace Apa
36  {
39 
40  /// Decimal type for better precision.
41  ///
42  class ONIXS_TRADEWEB_APA_API Decimal
43  {
44  public:
45 
46  /// Default constructor. Initializes the decimal value to zero.
47  ///
48  Decimal ();
49 
50  /// Initializes instance from compound components.
51  ///
52  Decimal (DecimalMantissa mantissa, DecimalExponent exponent);
53 
54  /// Initializes instance with zero exponent.
55  ///
56  explicit Decimal (SInt32 value);
57 
58  /// Converts from Double value using
59  /// default conversion precision.
60  explicit Decimal (Double value);
61 
62  /// Converts Double value to decimal.
63  /// @param value double precision floating-point value.
64  /// @param precision defines conversion precision.
65  Decimal (Double value, size_t precision);
66 
67  /// Initializes as copy of given value.
68  ///
69  Decimal (const Decimal& other);
70 
71  /// Returns mantissa part of decimal.
72  ///
73  DecimalMantissa mantissa() const;
74 
75  /// Returns exponent part of decimal.
76  ///
77  DecimalExponent exponent() const;
78 
79  /// Compares two numbers.
80  ///
81  bool operator == (const Decimal&) const;
82 
83  /// Compares two numbers.
84  ///
85  bool operator != (const Decimal&) const;
86 
87  /// Casts to whole integer number as regular
88  /// floating point value is casted.
89  /// @throw domain_error exception on failure.
90  operator SInt32() const;
91 
92  /// Casts to whole integer number as regular
93  /// floating point value is casted.
94  /// @throw domain_error exception on failure.
95  operator UInt32() const;
96 
97  /// Casts to whole integer number as regular
98  /// floating point value is casted.
99  /// @throw domain_error exception on failure.
100  operator SInt64() const;
101 
102  /// Casts to whole integer number as regular
103  /// floating point value is casted.
104  /// @throw domain_error exception on failure.
105  operator UInt64() const;
106 
107  /// Casts to whole floating point as regular
108  /// value is casted to a smaller precision.
109  /// @throw domain_error exception on failure.
110  operator Double() const;
111 
112  /// Casts to whole integer number as regular
113  /// floating point value is casted.
114  /// @return false if conversion fails.
115  bool toNumber (SInt32&) const;
116 
117  /// Casts to whole integer number as regular
118  /// floating point value is casted.
119  /// @return false if conversion fails.
120  bool toNumber (UInt32&) const;
121 
122  /// Casts to whole integer number as regular
123  /// floating point value is casted.
124  /// @return false if conversion fails.
125  bool toNumber (SInt64&) const;
126 
127  /// Casts to whole integer number as regular
128  /// floating point value is casted.
129  /// @return false if conversion fails.
130  bool toNumber (UInt64&) const;
131 
132  /// Casts to floating point number.
133  /// @return false if conversion fails.
134  bool toNumber (Double&) const;
135 
136  /// Appends text presentation to given string.
137  ///
138  void toString (std::string&) const;
139 
140  /// Returns text presentation of decimal.
141  ///
142  std::string toString() const;
143 
144  /// Reinitializes instance from another one.
145  ///
146  Decimal&
147  operator = (
148  const Decimal& other);
149 
150  /// Attempts to parse decimal value
151  /// from its string/text presentation.
152  /// @return false on parsing failure.
153  static
154  bool tryParse (const char* buffer, size_t bufferSize, Decimal&);
155 
156  /// Parses decimal from string presentation.
157  /// @throw std::exception on failure.
158  static
159  Decimal parse (const char* buffer, size_t bufferSize);
160 
161  private:
162  /// Mantissa.
163  ///
164  DecimalMantissa mantissa_;
165 
166  /// Exponent.
167  ///
168  DecimalExponent exponent_;
169  };
170 
171  ONIXS_TRADEWEB_APA_API bool operator < (const Decimal& l, const Decimal& r);
172  ONIXS_TRADEWEB_APA_API bool operator > (const Decimal& l, const Decimal& r);
173  ONIXS_TRADEWEB_APA_API bool operator <= (const Decimal& l, const Decimal& r);
174  ONIXS_TRADEWEB_APA_API bool operator >= (const Decimal& l, const Decimal& r);
175 
177  : mantissa_ (0),
178  exponent_ (0)
179  {
180  }
181 
182  inline DecimalMantissa Decimal::mantissa() const
183  {
184  return mantissa_;
185  }
186 
187  inline DecimalExponent Decimal::exponent() const
188  {
189  return exponent_;
190  }
191 
192  inline Decimal::operator SInt32() const
193  {
194  SInt32 number;
195 
196  if (toNumber (number) )
197  return number;
198 
199  throw std::domain_error ("Cannot cast value to target type. ");
200  }
201 
202  inline Decimal::operator UInt32() const
203  {
204  UInt32 number;
205 
206  if (toNumber (number) )
207  return number;
208 
209  throw std::domain_error ("Cannot cast value to target type. ");
210  }
211 
212  inline Decimal::operator SInt64() const
213  {
214  SInt64 number;
215 
216  if (toNumber (number) )
217  return number;
218 
219  throw std::domain_error ("Cannot cast value to target type. ");
220  }
221 
222  inline Decimal::operator UInt64() const
223  {
224  UInt64 number;
225 
226  if (toNumber (number) )
227  return number;
228 
229  throw std::domain_error ("Cannot cast value to target type. ");
230  }
231 
232  inline Decimal::operator Double() const
233  {
234  Double number;
235 
236  if (toNumber (number) )
237  return number;
238 
239  throw std::domain_error ("Cannot cast value to target type. ");
240  }
241 
242  inline std::string Decimal::toString() const
243  {
244  std::string presentation;
245  toString (presentation);
246  return presentation;
247  }
248 
249 
250  inline
251  void toStr(std::string& str, const Decimal& value)
252  {
253  std::string presentation;
254  value.toString (presentation);
255  str += presentation;
256  }
257  }
258  }
259  }
260 }
261 
262 namespace std
263 {
264  /// Outputs into standard stream.
265  ///
266  ONIXS_TRADEWEB_APA_API std::ostream& operator <<(std::ostream&, const OnixS::Tradeweb::MarketData::Apa::Decimal&);
267 }
268 
269 
void toString(std::string &) const
ONIXS_TRADEWEB_APA_API bool operator<=(const Decimal &l, const Decimal &r)
bool operator==(const StrRef &left, const StrRef &right)
Compares StrRef instance with another one.
Definition: String.h:244
DecimalExponent exponent() const
Definition: Decimal.h:187
STL namespace.
bool operator!=(const StrRef &left, const StrRef &right)
Compares with another instance.
Definition: String.h:258
ONIXS_TRADEWEB_APA_API bool operator>(const Decimal &l, const Decimal &r)
DecimalMantissa mantissa() const
Definition: Decimal.h:182
ONIXS_TRADEWEB_APA_API bool operator<(const Decimal &l, const Decimal &r)
ONIXS_TRADEWEB_APA_API std::ostream & operator<<(std::ostream &stream, const DataSource &ds)
void toStr(std::string &str, const Decimal &value)
Definition: Decimal.h:251
ONIXS_TRADEWEB_APA_API bool operator>=(const Decimal &l, const Decimal &r)