OnixS C++ Eurex T7 Market and Reference Data Interface (EMDI, RDI, EOBI) Handlers  7.4.2
API documentation
Decimal.cpp
Go to the documentation of this file.
1 /*
2 * Copyright 2005-2012 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 
21 
22 #include <OnixS/FIX/Core/Messaging.h>
23 
24 #include <ostream>
25 #include <iostream>
26 
27 namespace OnixS {
28 namespace Eurex {
29 namespace MarketData {
30 
32 
34  DecimalMantissa mantissa,
35  DecimalExponent exponent)
36  : mantissa_(mantissa),
37  exponent_(exponent)
38 {
39 }
40 
41 Decimal::Decimal (int value)
42 {
43  CORE::Decimal structured;
44 
45  if (CORE::Decimal::
46  reconstruct(OnixS::Decimal(value), structured))
47  {
48  mantissa_ = structured.mantissa();
49  exponent_ = structured.exponent();
50  }
51  else
52  {
53  throw std::invalid_argument(
54  "Cannot reconstruct decimal value from Double one. ");
55  }
56 }
57 
59 {
60  CORE::Decimal structured;
61 
62  if (CORE::Decimal::
63  reconstruct(OnixS::Decimal(value, 8u), structured))
64  {
65  mantissa_ = structured.mantissa();
66  exponent_ = structured.exponent();
67  }
68  else
69  {
70  throw std::invalid_argument(
71  "Cannot reconstruct decimal value from Double one. ");
72  }
73 }
74 
76  Double value, size_t precision)
77 {
78  CORE::Decimal structured;
79 
80  if (CORE::Decimal::reconstruct(
81  OnixS::Decimal(value, precision), structured))
82  {
83  mantissa_ = structured.mantissa();
84  exponent_ = structured.exponent();
85  }
86  else
87  {
88  throw std::invalid_argument(
89  "Cannot reconstruct decimal value from Double one. ");
90  }
91 }
92 
94  const Decimal& other)
95  : mantissa_(other.mantissa_),
96  exponent_(other.exponent_)
97 {
98 }
99 
100 bool
102  const Decimal& other) const
103 {
104  return OnixS::Decimal(
105  mantissa_, exponent_)
106  ==
107  OnixS::Decimal(
108  other.mantissa_,
109  other.exponent_);
110 }
111 
112 bool
114  const Decimal& other) const
115 {
116  return OnixS::Decimal(
117  mantissa_, exponent_)
118  !=
119  OnixS::Decimal(
120  other.mantissa_,
121  other.exponent_);
122 }
123 
124 bool operator < (const Decimal& l, const Decimal& r)
125 {
126  return OnixS::Decimal(l.mantissa(), l.exponent()) < OnixS::Decimal(r.mantissa(), r.exponent());
127 }
128 
129 bool operator > (const Decimal& l, const Decimal& r)
130 {
131  return OnixS::Decimal(l.mantissa(), l.exponent()) > OnixS::Decimal(r.mantissa(), r.exponent());
132 }
133 
134 bool operator <= (const Decimal& l, const Decimal& r)
135 {
136  return OnixS::Decimal(l.mantissa(), l.exponent()) <= OnixS::Decimal(r.mantissa(), r.exponent());
137 }
138 
139 bool operator >= (const Decimal& l, const Decimal& r)
140 {
141  return OnixS::Decimal(l.mantissa(), l.exponent()) >= OnixS::Decimal(r.mantissa(), r.exponent());
142 }
143 
144 Decimal&
146 {
147  mantissa_ = other.mantissa_;
148  exponent_ = other.exponent_;
149 
150  return *this;
151 }
152 
153 bool
155  Int32& number) const
156 {
157  return OnixS::Decimal(
158  mantissa_, exponent_).
159  toNumber(&number);
160 }
161 
162 bool
164  UInt32& number) const
165 {
166  return OnixS::Decimal(
167  mantissa_, exponent_).
168  toNumber(&number);
169 }
170 
171 bool
173  Int64& number) const
174 {
175  OnixS::Int64 native;
176 
177  if (OnixS::Decimal(
178  mantissa_, exponent_).
179  toNumber(&native))
180  {
181  number = native;
182 
183  return true;
184  }
185 
186  return false;
187 }
188 
189 bool
191  UInt64& number) const
192 {
193  OnixS::UInt64 native;
194 
195  if (OnixS::Decimal(
196  mantissa_, exponent_).
197  toNumber(&native))
198  {
199  number = native;
200 
201  return true;
202  }
203 
204  return false;
205 }
206 
207 bool
209  Double& number) const
210 {
211  return OnixS::Decimal(
212  mantissa_, exponent_).
213  toNumber(&number);
214 }
215 
216 void
217 Decimal::toString(std::string& str) const
218 {
219  OnixS::DecimalTraits::toString(
220  OnixS::Decimal(mantissa_, exponent_),
221  str);
222 }
223 
224 bool
226  const char* buffer,
227  size_t bufferSize,
228  Decimal& value)
229 {
230  OnixS::Decimal number;
231 
232  if (OnixS::DecimalTraits::
233  parse(buffer, bufferSize, number))
234  {
235  CORE::Decimal structured;
236 
237  if (CORE::Decimal::
238  reconstruct(number, structured))
239  {
240  value.mantissa(structured.mantissa());
241  value.exponent(structured.exponent());
242 
243  return true;
244  }
245  }
246 
247  return false;
248 }
249 
250 Decimal
252  const char* buffer,
253  size_t bufferSize)
254 {
255  OnixS::Decimal number;
256 
257  if (OnixS::DecimalTraits::
258  parse(buffer, bufferSize, number))
259  {
260  CORE::Decimal structured;
261 
262  if (CORE::Decimal::
263  reconstruct(number, structured))
264  {
265  return Decimal(
266  structured.mantissa(),
267  structured.exponent());
268  }
269 
270  throw std::overflow_error(
271  "Number is too huge to fit into decimal range. ");
272  }
273 
274  throw std::invalid_argument(
275  "Text presentation of decimal is invalid. ");
276 }
277 
278 }}}
279 
280 namespace std
281 {
282 std::ostream&
283  operator <<(std::ostream& o,
285 {
286  return o << data.toString();
287 }
288 
289 }
290 
291 
bool operator<(const Decimal &l, const Decimal &r)
Definition: Decimal.cpp:124
Decimal(DecimalMantissa mantissa=0, DecimalExponent exponent=0)
Initializes instance from compound components.
Definition: Decimal.cpp:33
static bool tryParse(const char *buffer, size_t bufferSize, Decimal &)
Definition: Decimal.cpp:225
Decimal & operator=(const Decimal &other)
Reinitializes instance from another one.
Definition: Decimal.cpp:145
STL namespace.
std::string toString() const
Returns text presentation of decimal.
Definition: Numeric.h:289
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)
Definition: Decimal.cpp:134
DecimalExponent exponent() const
Returns exponent part of decimal.
Definition: Numeric.h:214
bool operator>=(const Decimal &l, const Decimal &r)
Definition: Decimal.cpp:139
bool operator!=(const Decimal &) const
Compares two numbers.
Definition: Decimal.cpp:113
void toString(std::string &) const
Appends text presentation to given string.
Definition: Decimal.cpp:217
bool operator==(const Decimal &) const
Compares two numbers.
Definition: Decimal.cpp:101
ONIXS_EUREX_EMDI_API std::ostream & operator<<(std::ostream &stream, const FeedEngineSettings &settings)
bool toNumber(Int32 &) const
Definition: Decimal.cpp:154
static Decimal parse(const char *buffer, size_t bufferSize)
Definition: Decimal.cpp:251
bool operator>(const Decimal &l, const Decimal &r)
Definition: Decimal.cpp:129