]>
Commit | Line | Data |
---|---|---|
8b81872f VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/longlong.h | |
3 | // Purpose: declaration of wxLongLong class - best implementation of a 64 | |
4 | // bit integer for the current platform. | |
5 | // Author: Jeffrey C. Ollie <jeff@ollie.clive.ia.us>, Vadim Zeitlin | |
8b81872f VZ |
6 | // Modified by: |
7 | // Created: 10.02.99 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 10 | // Licence: wxWindows licence |
8b81872f VZ |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | #ifndef _WX_LONGLONG_H | |
14 | #define _WX_LONGLONG_H | |
15 | ||
1ef54dcf | 16 | #include "wx/defs.h" |
3a994742 | 17 | #include "wx/string.h" |
1ef54dcf VZ |
18 | |
19 | #include <limits.h> // for LONG_MAX | |
20 | ||
2a310492 VZ |
21 | // define this to compile wxLongLongWx in "test" mode: the results of all |
22 | // calculations will be compared with the real results taken from | |
617ec456 VZ |
23 | // wxLongLongNative -- this is extremely useful to find the bugs in |
24 | // wxLongLongWx class! | |
25 | ||
f6bcfd97 | 26 | // #define wxLONGLONG_TEST_MODE |
2a310492 VZ |
27 | |
28 | #ifdef wxLONGLONG_TEST_MODE | |
29 | #define wxUSE_LONGLONG_WX 1 | |
30 | #define wxUSE_LONGLONG_NATIVE 1 | |
31 | #endif // wxLONGLONG_TEST_MODE | |
2ea24d9f | 32 | |
8b81872f VZ |
33 | // ---------------------------------------------------------------------------- |
34 | // decide upon which class we will use | |
35 | // ---------------------------------------------------------------------------- | |
36 | ||
8fb37472 | 37 | #ifndef wxLongLong_t |
9c2882d9 | 38 | // both warning and pragma warning are not portable, but at least an |
2b5f62a0 | 39 | // unknown pragma should never be an error -- except that, actually, some |
58ef67ab VZ |
40 | // broken compilers don't like it, so we have to disable it in this case |
41 | // <sigh> | |
8fb37472 VZ |
42 | #if !(defined(__WATCOMC__) || defined(__VISAGECPP__)) |
43 | #pragma warning "Your compiler does not appear to support 64 bit "\ | |
44 | "integers, using emulation class instead.\n" \ | |
45 | "Please report your compiler version to " \ | |
77ffb593 | 46 | "wx-dev@lists.wxwidgets.org!" |
8fb37472 | 47 | #endif |
8db6ac55 | 48 | |
8b81872f VZ |
49 | #define wxUSE_LONGLONG_WX 1 |
50 | #endif // compiler | |
51 | ||
52 | // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE | |
53 | // to disable automatic testing (useful for the test program which defines | |
54 | // both classes) but by default we only use one class | |
2ea24d9f | 55 | #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t) |
2a310492 | 56 | // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set: |
384223b3 | 57 | // this is useful in test programs and only there |
2a310492 VZ |
58 | #ifndef wxUSE_LONGLONG_NATIVE |
59 | #define wxUSE_LONGLONG_NATIVE 0 | |
60 | #endif | |
61 | ||
bddd7a8d VZ |
62 | class WXDLLIMPEXP_BASE wxLongLongWx; |
63 | class WXDLLIMPEXP_BASE wxULongLongWx; | |
92f5ff59 JS |
64 | #if defined(__VISUALC__) && !defined(__WIN32__) |
65 | #define wxLongLong wxLongLongWx | |
8e38fd1f | 66 | #define wxULongLong wxULongLongWx |
92f5ff59 | 67 | #else |
8b81872f | 68 | typedef wxLongLongWx wxLongLong; |
8e38fd1f | 69 | typedef wxULongLongWx wxULongLong; |
92f5ff59 JS |
70 | #endif |
71 | ||
b76b015e VZ |
72 | #else |
73 | // if nothing is defined, use native implementation by default, of course | |
74 | #ifndef wxUSE_LONGLONG_NATIVE | |
75 | #define wxUSE_LONGLONG_NATIVE 1 | |
76 | #endif | |
8b81872f VZ |
77 | #endif |
78 | ||
79 | #ifndef wxUSE_LONGLONG_WX | |
80 | #define wxUSE_LONGLONG_WX 0 | |
bddd7a8d VZ |
81 | class WXDLLIMPEXP_BASE wxLongLongNative; |
82 | class WXDLLIMPEXP_BASE wxULongLongNative; | |
8b81872f | 83 | typedef wxLongLongNative wxLongLong; |
8e38fd1f | 84 | typedef wxULongLongNative wxULongLong; |
8b81872f VZ |
85 | #endif |
86 | ||
87 | // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should | |
88 | // typedef wxLongLong as it wants, we don't do it | |
89 | ||
90 | // ---------------------------------------------------------------------------- | |
91 | // choose the appropriate class | |
92 | // ---------------------------------------------------------------------------- | |
93 | ||
94 | // we use iostream for wxLongLong output | |
65f19af1 | 95 | #include "wx/iosfwrap.h" |
8b81872f VZ |
96 | |
97 | #if wxUSE_LONGLONG_NATIVE | |
98 | ||
bddd7a8d | 99 | class WXDLLIMPEXP_BASE wxLongLongNative |
8b81872f VZ |
100 | { |
101 | public: | |
102 | // ctors | |
103 | // default ctor initializes to 0 | |
2e403d6d | 104 | wxLongLongNative() : m_ll(0) { } |
8b81872f | 105 | // from long long |
2e403d6d | 106 | wxLongLongNative(wxLongLong_t ll) : m_ll(ll) { } |
8b81872f | 107 | // from 2 longs |
2e403d6d | 108 | wxLongLongNative(long hi, unsigned long lo) : m_ll(0) |
8b81872f VZ |
109 | { |
110 | // assign first to avoid precision loss! | |
111 | m_ll = ((wxLongLong_t) hi) << 32; | |
112 | m_ll |= (wxLongLong_t) lo; | |
113 | } | |
842215bb WS |
114 | #if wxUSE_LONGLONG_WX |
115 | wxLongLongNative(wxLongLongWx ll); | |
116 | #endif | |
8b81872f | 117 | |
2f02cb89 | 118 | // default copy ctor is ok |
8b81872f VZ |
119 | |
120 | // no dtor | |
121 | ||
122 | // assignment operators | |
123 | // from native 64 bit integer | |
124 | wxLongLongNative& operator=(wxLongLong_t ll) | |
125 | { m_ll = ll; return *this; } | |
842215bb WS |
126 | #if wxUSE_LONGLONG_WX |
127 | wxLongLongNative& operator=(wxLongLongWx ll); | |
128 | #endif | |
129 | ||
8b81872f | 130 | |
cd0b1709 VZ |
131 | // from double: this one has an explicit name because otherwise we |
132 | // would have ambiguity with "ll = int" and also because we don't want | |
133 | // to have implicit conversions between doubles and wxLongLongs | |
134 | wxLongLongNative& Assign(double d) | |
135 | { m_ll = (wxLongLong_t)d; return *this; } | |
136 | ||
8b81872f VZ |
137 | // assignment operators from wxLongLongNative is ok |
138 | ||
139 | // accessors | |
140 | // get high part | |
141 | long GetHi() const | |
17a1ebd1 | 142 | { return wx_truncate_cast(long, m_ll >> 32); } |
8b81872f VZ |
143 | // get low part |
144 | unsigned long GetLo() const | |
17a1ebd1 | 145 | { return wx_truncate_cast(unsigned long, m_ll); } |
8b81872f | 146 | |
b76b015e | 147 | // get absolute value |
2ea24d9f | 148 | wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); } |
b76b015e VZ |
149 | wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; } |
150 | ||
8b81872f VZ |
151 | // convert to native long long |
152 | wxLongLong_t GetValue() const { return m_ll; } | |
153 | ||
90e572f1 | 154 | // convert to long with range checking in debug mode (only!) |
1ef54dcf VZ |
155 | long ToLong() const |
156 | { | |
157 | wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX), | |
158 | _T("wxLongLong to long conversion loss of precision") ); | |
159 | ||
17a1ebd1 | 160 | return wx_truncate_cast(long, m_ll); |
1ef54dcf VZ |
161 | } |
162 | ||
e7aaf2de | 163 | // convert to double |
17a1ebd1 | 164 | double ToDouble() const { return wx_truncate_cast(double, m_ll); } |
e7aaf2de | 165 | |
2f02cb89 VZ |
166 | // don't provide implicit conversion to wxLongLong_t or we will have an |
167 | // ambiguity for all arithmetic operations | |
b76b015e | 168 | //operator wxLongLong_t() const { return m_ll; } |
8b81872f VZ |
169 | |
170 | // operations | |
171 | // addition | |
172 | wxLongLongNative operator+(const wxLongLongNative& ll) const | |
173 | { return wxLongLongNative(m_ll + ll.m_ll); } | |
174 | wxLongLongNative& operator+=(const wxLongLongNative& ll) | |
175 | { m_ll += ll.m_ll; return *this; } | |
176 | ||
177 | wxLongLongNative operator+(const wxLongLong_t ll) const | |
178 | { return wxLongLongNative(m_ll + ll); } | |
179 | wxLongLongNative& operator+=(const wxLongLong_t ll) | |
180 | { m_ll += ll; return *this; } | |
181 | ||
182 | // pre increment | |
183 | wxLongLongNative& operator++() | |
184 | { m_ll++; return *this; } | |
185 | ||
186 | // post increment | |
2e403d6d GD |
187 | wxLongLongNative operator++(int) |
188 | { wxLongLongNative value(*this); m_ll++; return value; } | |
8b81872f VZ |
189 | |
190 | // negation operator | |
191 | wxLongLongNative operator-() const | |
192 | { return wxLongLongNative(-m_ll); } | |
3a994742 | 193 | wxLongLongNative& Negate() { m_ll = -m_ll; return *this; } |
8b81872f VZ |
194 | |
195 | // subtraction | |
196 | wxLongLongNative operator-(const wxLongLongNative& ll) const | |
197 | { return wxLongLongNative(m_ll - ll.m_ll); } | |
198 | wxLongLongNative& operator-=(const wxLongLongNative& ll) | |
199 | { m_ll -= ll.m_ll; return *this; } | |
200 | ||
201 | wxLongLongNative operator-(const wxLongLong_t ll) const | |
202 | { return wxLongLongNative(m_ll - ll); } | |
203 | wxLongLongNative& operator-=(const wxLongLong_t ll) | |
204 | { m_ll -= ll; return *this; } | |
205 | ||
206 | // pre decrement | |
207 | wxLongLongNative& operator--() | |
208 | { m_ll--; return *this; } | |
209 | ||
210 | // post decrement | |
2e403d6d GD |
211 | wxLongLongNative operator--(int) |
212 | { wxLongLongNative value(*this); m_ll--; return value; } | |
8b81872f VZ |
213 | |
214 | // shifts | |
215 | // left shift | |
216 | wxLongLongNative operator<<(int shift) const | |
d0ee33f5 | 217 | { return wxLongLongNative(m_ll << shift); } |
8b81872f VZ |
218 | wxLongLongNative& operator<<=(int shift) |
219 | { m_ll <<= shift; return *this; } | |
220 | ||
221 | // right shift | |
222 | wxLongLongNative operator>>(int shift) const | |
d0ee33f5 | 223 | { return wxLongLongNative(m_ll >> shift); } |
8b81872f VZ |
224 | wxLongLongNative& operator>>=(int shift) |
225 | { m_ll >>= shift; return *this; } | |
226 | ||
227 | // bitwise operators | |
228 | wxLongLongNative operator&(const wxLongLongNative& ll) const | |
229 | { return wxLongLongNative(m_ll & ll.m_ll); } | |
230 | wxLongLongNative& operator&=(const wxLongLongNative& ll) | |
231 | { m_ll &= ll.m_ll; return *this; } | |
232 | ||
233 | wxLongLongNative operator|(const wxLongLongNative& ll) const | |
234 | { return wxLongLongNative(m_ll | ll.m_ll); } | |
235 | wxLongLongNative& operator|=(const wxLongLongNative& ll) | |
236 | { m_ll |= ll.m_ll; return *this; } | |
237 | ||
238 | wxLongLongNative operator^(const wxLongLongNative& ll) const | |
239 | { return wxLongLongNative(m_ll ^ ll.m_ll); } | |
240 | wxLongLongNative& operator^=(const wxLongLongNative& ll) | |
241 | { m_ll ^= ll.m_ll; return *this; } | |
242 | ||
b76b015e | 243 | // multiplication/division |
8b81872f VZ |
244 | wxLongLongNative operator*(const wxLongLongNative& ll) const |
245 | { return wxLongLongNative(m_ll * ll.m_ll); } | |
2f02cb89 VZ |
246 | wxLongLongNative operator*(long l) const |
247 | { return wxLongLongNative(m_ll * l); } | |
8b81872f VZ |
248 | wxLongLongNative& operator*=(const wxLongLongNative& ll) |
249 | { m_ll *= ll.m_ll; return *this; } | |
2f02cb89 VZ |
250 | wxLongLongNative& operator*=(long l) |
251 | { m_ll *= l; return *this; } | |
8b81872f VZ |
252 | |
253 | wxLongLongNative operator/(const wxLongLongNative& ll) const | |
254 | { return wxLongLongNative(m_ll / ll.m_ll); } | |
b76b015e VZ |
255 | wxLongLongNative operator/(long l) const |
256 | { return wxLongLongNative(m_ll / l); } | |
8b81872f VZ |
257 | wxLongLongNative& operator/=(const wxLongLongNative& ll) |
258 | { m_ll /= ll.m_ll; return *this; } | |
2f02cb89 VZ |
259 | wxLongLongNative& operator/=(long l) |
260 | { m_ll /= l; return *this; } | |
8b81872f VZ |
261 | |
262 | wxLongLongNative operator%(const wxLongLongNative& ll) const | |
263 | { return wxLongLongNative(m_ll % ll.m_ll); } | |
b76b015e VZ |
264 | wxLongLongNative operator%(long l) const |
265 | { return wxLongLongNative(m_ll % l); } | |
8b81872f VZ |
266 | |
267 | // comparison | |
268 | bool operator==(const wxLongLongNative& ll) const | |
269 | { return m_ll == ll.m_ll; } | |
b76b015e VZ |
270 | bool operator==(long l) const |
271 | { return m_ll == l; } | |
8b81872f VZ |
272 | bool operator!=(const wxLongLongNative& ll) const |
273 | { return m_ll != ll.m_ll; } | |
b76b015e VZ |
274 | bool operator!=(long l) const |
275 | { return m_ll != l; } | |
8b81872f VZ |
276 | bool operator<(const wxLongLongNative& ll) const |
277 | { return m_ll < ll.m_ll; } | |
b76b015e VZ |
278 | bool operator<(long l) const |
279 | { return m_ll < l; } | |
8b81872f VZ |
280 | bool operator>(const wxLongLongNative& ll) const |
281 | { return m_ll > ll.m_ll; } | |
b76b015e VZ |
282 | bool operator>(long l) const |
283 | { return m_ll > l; } | |
8b81872f VZ |
284 | bool operator<=(const wxLongLongNative& ll) const |
285 | { return m_ll <= ll.m_ll; } | |
b76b015e VZ |
286 | bool operator<=(long l) const |
287 | { return m_ll <= l; } | |
8b81872f VZ |
288 | bool operator>=(const wxLongLongNative& ll) const |
289 | { return m_ll >= ll.m_ll; } | |
b76b015e VZ |
290 | bool operator>=(long l) const |
291 | { return m_ll >= l; } | |
8b81872f VZ |
292 | |
293 | // miscellaneous | |
3a994742 VZ |
294 | |
295 | // return the string representation of this number | |
296 | wxString ToString() const; | |
297 | ||
8b81872f VZ |
298 | // conversion to byte array: returns a pointer to static buffer! |
299 | void *asArray() const; | |
300 | ||
fcc3d7cb | 301 | #if wxUSE_STD_IOSTREAM |
8b81872f | 302 | // input/output |
a353dc98 VZ |
303 | friend WXDLLIMPEXP_BASE |
304 | wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&); | |
fcc3d7cb | 305 | #endif |
8b81872f | 306 | |
a353dc98 VZ |
307 | friend WXDLLIMPEXP_BASE |
308 | wxString& operator<<(wxString&, const wxLongLongNative&); | |
51496782 | 309 | |
8b81872f VZ |
310 | private: |
311 | wxLongLong_t m_ll; | |
312 | }; | |
313 | ||
8e38fd1f | 314 | |
bddd7a8d | 315 | class WXDLLIMPEXP_BASE wxULongLongNative |
8e38fd1f RL |
316 | { |
317 | public: | |
318 | // ctors | |
319 | // default ctor initializes to 0 | |
2e403d6d | 320 | wxULongLongNative() : m_ll(0) { } |
8e38fd1f | 321 | // from long long |
60582201 | 322 | wxULongLongNative(wxULongLong_t ll) : m_ll(ll) { } |
8e38fd1f | 323 | // from 2 longs |
2e403d6d | 324 | wxULongLongNative(unsigned long hi, unsigned long lo) : m_ll(0) |
8e38fd1f RL |
325 | { |
326 | // assign first to avoid precision loss! | |
60582201 WS |
327 | m_ll = ((wxULongLong_t) hi) << 32; |
328 | m_ll |= (wxULongLong_t) lo; | |
8e38fd1f RL |
329 | } |
330 | ||
331 | // default copy ctor is ok | |
332 | ||
333 | // no dtor | |
334 | ||
335 | // assignment operators | |
336 | // from native 64 bit integer | |
60582201 | 337 | wxULongLongNative& operator=(wxULongLong_t ll) |
8e38fd1f RL |
338 | { m_ll = ll; return *this; } |
339 | ||
340 | // assignment operators from wxULongLongNative is ok | |
341 | ||
342 | // accessors | |
343 | // get high part | |
344 | unsigned long GetHi() const | |
17a1ebd1 | 345 | { return wx_truncate_cast(unsigned long, m_ll >> 32); } |
8e38fd1f RL |
346 | // get low part |
347 | unsigned long GetLo() const | |
17a1ebd1 | 348 | { return wx_truncate_cast(unsigned long, m_ll); } |
8e38fd1f RL |
349 | |
350 | // convert to native ulong long | |
60582201 | 351 | wxULongLong_t GetValue() const { return m_ll; } |
8e38fd1f | 352 | |
90e572f1 | 353 | // convert to ulong with range checking in debug mode (only!) |
8e38fd1f RL |
354 | unsigned long ToULong() const |
355 | { | |
0ba9f867 | 356 | wxASSERT_MSG( m_ll <= LONG_MAX, |
8e38fd1f RL |
357 | _T("wxULongLong to long conversion loss of precision") ); |
358 | ||
17a1ebd1 | 359 | return wx_truncate_cast(unsigned long, m_ll); |
8e38fd1f RL |
360 | } |
361 | ||
8e38fd1f RL |
362 | // operations |
363 | // addition | |
364 | wxULongLongNative operator+(const wxULongLongNative& ll) const | |
365 | { return wxULongLongNative(m_ll + ll.m_ll); } | |
366 | wxULongLongNative& operator+=(const wxULongLongNative& ll) | |
367 | { m_ll += ll.m_ll; return *this; } | |
368 | ||
60582201 | 369 | wxULongLongNative operator+(const wxULongLong_t ll) const |
8e38fd1f | 370 | { return wxULongLongNative(m_ll + ll); } |
60582201 | 371 | wxULongLongNative& operator+=(const wxULongLong_t ll) |
8e38fd1f RL |
372 | { m_ll += ll; return *this; } |
373 | ||
374 | // pre increment | |
375 | wxULongLongNative& operator++() | |
376 | { m_ll++; return *this; } | |
377 | ||
378 | // post increment | |
2e403d6d GD |
379 | wxULongLongNative operator++(int) |
380 | { wxULongLongNative value(*this); m_ll++; return value; } | |
8e38fd1f RL |
381 | |
382 | // subtraction | |
383 | wxULongLongNative operator-(const wxULongLongNative& ll) const | |
384 | { return wxULongLongNative(m_ll - ll.m_ll); } | |
385 | wxULongLongNative& operator-=(const wxULongLongNative& ll) | |
386 | { m_ll -= ll.m_ll; return *this; } | |
387 | ||
60582201 | 388 | wxULongLongNative operator-(const wxULongLong_t ll) const |
8e38fd1f | 389 | { return wxULongLongNative(m_ll - ll); } |
60582201 | 390 | wxULongLongNative& operator-=(const wxULongLong_t ll) |
8e38fd1f RL |
391 | { m_ll -= ll; return *this; } |
392 | ||
393 | // pre decrement | |
394 | wxULongLongNative& operator--() | |
395 | { m_ll--; return *this; } | |
396 | ||
397 | // post decrement | |
2e403d6d GD |
398 | wxULongLongNative operator--(int) |
399 | { wxULongLongNative value(*this); m_ll--; return value; } | |
8e38fd1f RL |
400 | |
401 | // shifts | |
402 | // left shift | |
403 | wxULongLongNative operator<<(int shift) const | |
d0ee33f5 | 404 | { return wxULongLongNative(m_ll << shift); } |
8e38fd1f RL |
405 | wxULongLongNative& operator<<=(int shift) |
406 | { m_ll <<= shift; return *this; } | |
407 | ||
408 | // right shift | |
409 | wxULongLongNative operator>>(int shift) const | |
d0ee33f5 | 410 | { return wxULongLongNative(m_ll >> shift); } |
8e38fd1f RL |
411 | wxULongLongNative& operator>>=(int shift) |
412 | { m_ll >>= shift; return *this; } | |
413 | ||
414 | // bitwise operators | |
415 | wxULongLongNative operator&(const wxULongLongNative& ll) const | |
416 | { return wxULongLongNative(m_ll & ll.m_ll); } | |
417 | wxULongLongNative& operator&=(const wxULongLongNative& ll) | |
418 | { m_ll &= ll.m_ll; return *this; } | |
419 | ||
420 | wxULongLongNative operator|(const wxULongLongNative& ll) const | |
421 | { return wxULongLongNative(m_ll | ll.m_ll); } | |
422 | wxULongLongNative& operator|=(const wxULongLongNative& ll) | |
423 | { m_ll |= ll.m_ll; return *this; } | |
424 | ||
425 | wxULongLongNative operator^(const wxULongLongNative& ll) const | |
426 | { return wxULongLongNative(m_ll ^ ll.m_ll); } | |
427 | wxULongLongNative& operator^=(const wxULongLongNative& ll) | |
428 | { m_ll ^= ll.m_ll; return *this; } | |
429 | ||
430 | // multiplication/division | |
431 | wxULongLongNative operator*(const wxULongLongNative& ll) const | |
432 | { return wxULongLongNative(m_ll * ll.m_ll); } | |
433 | wxULongLongNative operator*(unsigned long l) const | |
434 | { return wxULongLongNative(m_ll * l); } | |
435 | wxULongLongNative& operator*=(const wxULongLongNative& ll) | |
436 | { m_ll *= ll.m_ll; return *this; } | |
437 | wxULongLongNative& operator*=(unsigned long l) | |
438 | { m_ll *= l; return *this; } | |
439 | ||
440 | wxULongLongNative operator/(const wxULongLongNative& ll) const | |
441 | { return wxULongLongNative(m_ll / ll.m_ll); } | |
442 | wxULongLongNative operator/(unsigned long l) const | |
443 | { return wxULongLongNative(m_ll / l); } | |
444 | wxULongLongNative& operator/=(const wxULongLongNative& ll) | |
445 | { m_ll /= ll.m_ll; return *this; } | |
446 | wxULongLongNative& operator/=(unsigned long l) | |
447 | { m_ll /= l; return *this; } | |
448 | ||
449 | wxULongLongNative operator%(const wxULongLongNative& ll) const | |
450 | { return wxULongLongNative(m_ll % ll.m_ll); } | |
451 | wxULongLongNative operator%(unsigned long l) const | |
452 | { return wxULongLongNative(m_ll % l); } | |
453 | ||
454 | // comparison | |
455 | bool operator==(const wxULongLongNative& ll) const | |
456 | { return m_ll == ll.m_ll; } | |
457 | bool operator==(unsigned long l) const | |
458 | { return m_ll == l; } | |
459 | bool operator!=(const wxULongLongNative& ll) const | |
460 | { return m_ll != ll.m_ll; } | |
461 | bool operator!=(unsigned long l) const | |
462 | { return m_ll != l; } | |
463 | bool operator<(const wxULongLongNative& ll) const | |
464 | { return m_ll < ll.m_ll; } | |
465 | bool operator<(unsigned long l) const | |
466 | { return m_ll < l; } | |
467 | bool operator>(const wxULongLongNative& ll) const | |
468 | { return m_ll > ll.m_ll; } | |
469 | bool operator>(unsigned long l) const | |
470 | { return m_ll > l; } | |
471 | bool operator<=(const wxULongLongNative& ll) const | |
472 | { return m_ll <= ll.m_ll; } | |
473 | bool operator<=(unsigned long l) const | |
474 | { return m_ll <= l; } | |
475 | bool operator>=(const wxULongLongNative& ll) const | |
476 | { return m_ll >= ll.m_ll; } | |
477 | bool operator>=(unsigned long l) const | |
478 | { return m_ll >= l; } | |
479 | ||
480 | // miscellaneous | |
481 | ||
482 | // return the string representation of this number | |
483 | wxString ToString() const; | |
484 | ||
485 | // conversion to byte array: returns a pointer to static buffer! | |
486 | void *asArray() const; | |
487 | ||
488 | #if wxUSE_STD_IOSTREAM | |
489 | // input/output | |
7fcdf88f VZ |
490 | friend WXDLLIMPEXP_BASE |
491 | wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&); | |
8e38fd1f RL |
492 | #endif |
493 | ||
7fcdf88f VZ |
494 | friend WXDLLIMPEXP_BASE |
495 | wxString& operator<<(wxString&, const wxULongLongNative&); | |
51496782 | 496 | |
8e38fd1f | 497 | private: |
60582201 | 498 | wxULongLong_t m_ll; |
8e38fd1f RL |
499 | }; |
500 | ||
8b81872f VZ |
501 | #endif // wxUSE_LONGLONG_NATIVE |
502 | ||
503 | #if wxUSE_LONGLONG_WX | |
504 | ||
bddd7a8d | 505 | class WXDLLIMPEXP_BASE wxLongLongWx |
8b81872f VZ |
506 | { |
507 | public: | |
508 | // ctors | |
509 | // default ctor initializes to 0 | |
2a310492 VZ |
510 | wxLongLongWx() |
511 | { | |
512 | m_lo = m_hi = 0; | |
513 | ||
514 | #ifdef wxLONGLONG_TEST_MODE | |
515 | m_ll = 0; | |
516 | ||
517 | Check(); | |
518 | #endif // wxLONGLONG_TEST_MODE | |
519 | } | |
8b81872f | 520 | // from long |
2a310492 | 521 | wxLongLongWx(long l) { *this = l; } |
8b81872f VZ |
522 | // from 2 longs |
523 | wxLongLongWx(long hi, unsigned long lo) | |
2a310492 VZ |
524 | { |
525 | m_hi = hi; | |
526 | m_lo = lo; | |
527 | ||
528 | #ifdef wxLONGLONG_TEST_MODE | |
529 | m_ll = hi; | |
530 | m_ll <<= 32; | |
531 | m_ll |= lo; | |
532 | ||
533 | Check(); | |
534 | #endif // wxLONGLONG_TEST_MODE | |
535 | } | |
8b81872f VZ |
536 | |
537 | // default copy ctor is ok in both cases | |
538 | ||
539 | // no dtor | |
540 | ||
541 | // assignment operators | |
542 | // from long | |
543 | wxLongLongWx& operator=(long l) | |
2a310492 VZ |
544 | { |
545 | m_lo = l; | |
546 | m_hi = (l < 0 ? -1l : 0l); | |
547 | ||
548 | #ifdef wxLONGLONG_TEST_MODE | |
549 | m_ll = l; | |
550 | ||
551 | Check(); | |
552 | #endif // wxLONGLONG_TEST_MODE | |
553 | ||
554 | return *this; | |
555 | } | |
cd0b1709 VZ |
556 | // from double |
557 | wxLongLongWx& Assign(double d); | |
8b81872f VZ |
558 | // can't have assignment operator from 2 longs |
559 | ||
560 | // accessors | |
561 | // get high part | |
562 | long GetHi() const { return m_hi; } | |
563 | // get low part | |
564 | unsigned long GetLo() const { return m_lo; } | |
565 | ||
cd0b1709 | 566 | // get absolute value |
2ea24d9f | 567 | wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); } |
2a310492 VZ |
568 | wxLongLongWx& Abs() |
569 | { | |
570 | if ( m_hi < 0 ) | |
571 | m_hi = -m_hi; | |
572 | ||
573 | #ifdef wxLONGLONG_TEST_MODE | |
574 | if ( m_ll < 0 ) | |
575 | m_ll = -m_ll; | |
576 | ||
577 | Check(); | |
578 | #endif // wxLONGLONG_TEST_MODE | |
579 | ||
580 | return *this; | |
581 | } | |
cd0b1709 | 582 | |
90e572f1 | 583 | // convert to long with range checking in debug mode (only!) |
cd0b1709 VZ |
584 | long ToLong() const |
585 | { | |
2a310492 | 586 | wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l), |
cd0b1709 VZ |
587 | _T("wxLongLong to long conversion loss of precision") ); |
588 | ||
589 | return (long)m_lo; | |
590 | } | |
591 | ||
e7aaf2de VZ |
592 | // convert to double |
593 | double ToDouble() const; | |
594 | ||
8b81872f VZ |
595 | // operations |
596 | // addition | |
597 | wxLongLongWx operator+(const wxLongLongWx& ll) const; | |
598 | wxLongLongWx& operator+=(const wxLongLongWx& ll); | |
599 | wxLongLongWx operator+(long l) const; | |
600 | wxLongLongWx& operator+=(long l); | |
601 | ||
602 | // pre increment operator | |
603 | wxLongLongWx& operator++(); | |
604 | ||
605 | // post increment operator | |
2a310492 | 606 | wxLongLongWx& operator++(int) { return ++(*this); } |
8b81872f VZ |
607 | |
608 | // negation operator | |
609 | wxLongLongWx operator-() const; | |
2a310492 | 610 | wxLongLongWx& Negate(); |
8b81872f VZ |
611 | |
612 | // subraction | |
613 | wxLongLongWx operator-(const wxLongLongWx& ll) const; | |
614 | wxLongLongWx& operator-=(const wxLongLongWx& ll); | |
615 | ||
616 | // pre decrement operator | |
617 | wxLongLongWx& operator--(); | |
618 | ||
619 | // post decrement operator | |
2a310492 | 620 | wxLongLongWx& operator--(int) { return --(*this); } |
8b81872f VZ |
621 | |
622 | // shifts | |
623 | // left shift | |
624 | wxLongLongWx operator<<(int shift) const; | |
625 | wxLongLongWx& operator<<=(int shift); | |
626 | ||
627 | // right shift | |
628 | wxLongLongWx operator>>(int shift) const; | |
629 | wxLongLongWx& operator>>=(int shift); | |
630 | ||
631 | // bitwise operators | |
632 | wxLongLongWx operator&(const wxLongLongWx& ll) const; | |
633 | wxLongLongWx& operator&=(const wxLongLongWx& ll); | |
634 | wxLongLongWx operator|(const wxLongLongWx& ll) const; | |
635 | wxLongLongWx& operator|=(const wxLongLongWx& ll); | |
636 | wxLongLongWx operator^(const wxLongLongWx& ll) const; | |
637 | wxLongLongWx& operator^=(const wxLongLongWx& ll); | |
638 | wxLongLongWx operator~() const; | |
639 | ||
640 | // comparison | |
2ea24d9f VZ |
641 | bool operator==(const wxLongLongWx& ll) const |
642 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
842215bb WS |
643 | #if wxUSE_LONGLONG_NATIVE |
644 | bool operator==(const wxLongLongNative& ll) const | |
645 | { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); } | |
646 | #endif | |
2ea24d9f VZ |
647 | bool operator!=(const wxLongLongWx& ll) const |
648 | { return !(*this == ll); } | |
8b81872f VZ |
649 | bool operator<(const wxLongLongWx& ll) const; |
650 | bool operator>(const wxLongLongWx& ll) const; | |
2ea24d9f VZ |
651 | bool operator<=(const wxLongLongWx& ll) const |
652 | { return *this < ll || *this == ll; } | |
653 | bool operator>=(const wxLongLongWx& ll) const | |
654 | { return *this > ll || *this == ll; } | |
8b81872f | 655 | |
f6bcfd97 BP |
656 | bool operator<(long l) const { return *this < wxLongLongWx(l); } |
657 | bool operator>(long l) const { return *this > wxLongLongWx(l); } | |
658 | bool operator==(long l) const | |
659 | { | |
660 | return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l) | |
661 | : (m_hi == -1 && m_lo == (unsigned long)l); | |
662 | } | |
663 | ||
664 | bool operator<=(long l) const { return *this < l || *this == l; } | |
665 | bool operator>=(long l) const { return *this > l || *this == l; } | |
666 | ||
8b81872f VZ |
667 | // multiplication |
668 | wxLongLongWx operator*(const wxLongLongWx& ll) const; | |
669 | wxLongLongWx& operator*=(const wxLongLongWx& ll); | |
8b81872f VZ |
670 | |
671 | // division | |
cd0b1709 VZ |
672 | wxLongLongWx operator/(const wxLongLongWx& ll) const; |
673 | wxLongLongWx& operator/=(const wxLongLongWx& ll); | |
674 | ||
675 | wxLongLongWx operator%(const wxLongLongWx& ll) const; | |
676 | ||
8b81872f VZ |
677 | void Divide(const wxLongLongWx& divisor, |
678 | wxLongLongWx& quotient, | |
679 | wxLongLongWx& remainder) const; | |
680 | ||
681 | // input/output | |
3a994742 VZ |
682 | |
683 | // return the string representation of this number | |
684 | wxString ToString() const; | |
685 | ||
686 | void *asArray() const; | |
687 | ||
cd0b1709 | 688 | #if wxUSE_STD_IOSTREAM |
a353dc98 VZ |
689 | friend WXDLLIMPEXP_BASE |
690 | wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&); | |
fcc3d7cb | 691 | #endif // wxUSE_STD_IOSTREAM |
8b81872f | 692 | |
a353dc98 VZ |
693 | friend WXDLLIMPEXP_BASE |
694 | wxString& operator<<(wxString&, const wxLongLongWx&); | |
51496782 | 695 | |
8b81872f VZ |
696 | private: |
697 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
698 | ||
699 | long m_hi; // signed bit is in the high part | |
700 | unsigned long m_lo; | |
2a310492 VZ |
701 | |
702 | #ifdef wxLONGLONG_TEST_MODE | |
703 | void Check() | |
704 | { | |
705 | wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo ); | |
706 | } | |
707 | ||
708 | wxLongLong_t m_ll; | |
709 | #endif // wxLONGLONG_TEST_MODE | |
8b81872f VZ |
710 | }; |
711 | ||
8e38fd1f | 712 | |
bddd7a8d | 713 | class WXDLLIMPEXP_BASE wxULongLongWx |
8e38fd1f RL |
714 | { |
715 | public: | |
716 | // ctors | |
717 | // default ctor initializes to 0 | |
718 | wxULongLongWx() | |
719 | { | |
720 | m_lo = m_hi = 0; | |
721 | ||
722 | #ifdef wxLONGLONG_TEST_MODE | |
723 | m_ll = 0; | |
724 | ||
725 | Check(); | |
726 | #endif // wxLONGLONG_TEST_MODE | |
727 | } | |
728 | // from ulong | |
729 | wxULongLongWx(unsigned long l) { *this = l; } | |
730 | // from 2 ulongs | |
731 | wxULongLongWx(unsigned long hi, unsigned long lo) | |
732 | { | |
733 | m_hi = hi; | |
734 | m_lo = lo; | |
735 | ||
736 | #ifdef wxLONGLONG_TEST_MODE | |
737 | m_ll = hi; | |
738 | m_ll <<= 32; | |
739 | m_ll |= lo; | |
740 | ||
741 | Check(); | |
742 | #endif // wxLONGLONG_TEST_MODE | |
743 | } | |
744 | ||
842215bb WS |
745 | // from signed to unsigned |
746 | wxULongLongWx(wxLongLongWx ll) | |
747 | { | |
748 | wxASSERT(ll.GetHi() >= 0); | |
749 | m_hi = (unsigned long)ll.GetHi(); | |
750 | m_lo = ll.GetLo(); | |
751 | } | |
752 | ||
8e38fd1f RL |
753 | // default copy ctor is ok in both cases |
754 | ||
755 | // no dtor | |
756 | ||
757 | // assignment operators | |
758 | // from long | |
759 | wxULongLongWx& operator=(unsigned long l) | |
760 | { | |
761 | m_lo = l; | |
762 | m_hi = 0; | |
763 | ||
764 | #ifdef wxLONGLONG_TEST_MODE | |
765 | m_ll = l; | |
766 | ||
767 | Check(); | |
768 | #endif // wxLONGLONG_TEST_MODE | |
769 | ||
770 | return *this; | |
771 | } | |
772 | ||
773 | // can't have assignment operator from 2 longs | |
774 | ||
775 | // accessors | |
776 | // get high part | |
777 | unsigned long GetHi() const { return m_hi; } | |
778 | // get low part | |
779 | unsigned long GetLo() const { return m_lo; } | |
780 | ||
90e572f1 | 781 | // convert to long with range checking in debug mode (only!) |
8e38fd1f RL |
782 | unsigned long ToULong() const |
783 | { | |
d7997e1a | 784 | wxASSERT_MSG( m_hi == 0ul, |
8e38fd1f RL |
785 | _T("wxULongLong to long conversion loss of precision") ); |
786 | ||
787 | return (unsigned long)m_lo; | |
788 | } | |
789 | ||
790 | // operations | |
791 | // addition | |
792 | wxULongLongWx operator+(const wxULongLongWx& ll) const; | |
793 | wxULongLongWx& operator+=(const wxULongLongWx& ll); | |
794 | wxULongLongWx operator+(unsigned long l) const; | |
795 | wxULongLongWx& operator+=(unsigned long l); | |
796 | ||
797 | // pre increment operator | |
798 | wxULongLongWx& operator++(); | |
799 | ||
800 | // post increment operator | |
801 | wxULongLongWx& operator++(int) { return ++(*this); } | |
802 | ||
43e8916f | 803 | // subtraction |
842215bb | 804 | wxLongLongWx operator-(const wxULongLongWx& ll) const; |
8e38fd1f RL |
805 | wxULongLongWx& operator-=(const wxULongLongWx& ll); |
806 | ||
807 | // pre decrement operator | |
808 | wxULongLongWx& operator--(); | |
809 | ||
810 | // post decrement operator | |
811 | wxULongLongWx& operator--(int) { return --(*this); } | |
812 | ||
813 | // shifts | |
814 | // left shift | |
815 | wxULongLongWx operator<<(int shift) const; | |
816 | wxULongLongWx& operator<<=(int shift); | |
817 | ||
818 | // right shift | |
819 | wxULongLongWx operator>>(int shift) const; | |
820 | wxULongLongWx& operator>>=(int shift); | |
821 | ||
822 | // bitwise operators | |
823 | wxULongLongWx operator&(const wxULongLongWx& ll) const; | |
824 | wxULongLongWx& operator&=(const wxULongLongWx& ll); | |
825 | wxULongLongWx operator|(const wxULongLongWx& ll) const; | |
826 | wxULongLongWx& operator|=(const wxULongLongWx& ll); | |
827 | wxULongLongWx operator^(const wxULongLongWx& ll) const; | |
828 | wxULongLongWx& operator^=(const wxULongLongWx& ll); | |
829 | wxULongLongWx operator~() const; | |
830 | ||
831 | // comparison | |
832 | bool operator==(const wxULongLongWx& ll) const | |
833 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
834 | bool operator!=(const wxULongLongWx& ll) const | |
835 | { return !(*this == ll); } | |
836 | bool operator<(const wxULongLongWx& ll) const; | |
837 | bool operator>(const wxULongLongWx& ll) const; | |
838 | bool operator<=(const wxULongLongWx& ll) const | |
839 | { return *this < ll || *this == ll; } | |
840 | bool operator>=(const wxULongLongWx& ll) const | |
841 | { return *this > ll || *this == ll; } | |
842 | ||
843 | bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); } | |
844 | bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); } | |
845 | bool operator==(unsigned long l) const | |
846 | { | |
847 | return (m_hi == 0 && m_lo == (unsigned long)l); | |
848 | } | |
849 | ||
850 | bool operator<=(unsigned long l) const { return *this < l || *this == l; } | |
851 | bool operator>=(unsigned long l) const { return *this > l || *this == l; } | |
852 | ||
853 | // multiplication | |
854 | wxULongLongWx operator*(const wxULongLongWx& ll) const; | |
855 | wxULongLongWx& operator*=(const wxULongLongWx& ll); | |
856 | ||
857 | // division | |
858 | wxULongLongWx operator/(const wxULongLongWx& ll) const; | |
859 | wxULongLongWx& operator/=(const wxULongLongWx& ll); | |
860 | ||
861 | wxULongLongWx operator%(const wxULongLongWx& ll) const; | |
862 | ||
863 | void Divide(const wxULongLongWx& divisor, | |
864 | wxULongLongWx& quotient, | |
865 | wxULongLongWx& remainder) const; | |
866 | ||
867 | // input/output | |
868 | ||
869 | // return the string representation of this number | |
870 | wxString ToString() const; | |
871 | ||
872 | void *asArray() const; | |
873 | ||
874 | #if wxUSE_STD_IOSTREAM | |
a353dc98 VZ |
875 | friend WXDLLIMPEXP_BASE |
876 | wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&); | |
8e38fd1f RL |
877 | #endif // wxUSE_STD_IOSTREAM |
878 | ||
a353dc98 VZ |
879 | friend WXDLLIMPEXP_BASE |
880 | wxString& operator<<(wxString&, const wxULongLongWx&); | |
51496782 | 881 | |
8e38fd1f RL |
882 | private: |
883 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
884 | ||
d2ce649b | 885 | unsigned long m_hi; |
8e38fd1f RL |
886 | unsigned long m_lo; |
887 | ||
888 | #ifdef wxLONGLONG_TEST_MODE | |
889 | void Check() | |
890 | { | |
891 | wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo ); | |
892 | } | |
893 | ||
60582201 | 894 | wxULongLong_t m_ll; |
8e38fd1f RL |
895 | #endif // wxLONGLONG_TEST_MODE |
896 | }; | |
897 | ||
8b81872f VZ |
898 | #endif // wxUSE_LONGLONG_WX |
899 | ||
2ea24d9f VZ |
900 | // ---------------------------------------------------------------------------- |
901 | // binary operators | |
902 | // ---------------------------------------------------------------------------- | |
903 | ||
6d56eb5c | 904 | inline bool operator<(long l, const wxLongLong& ll) { return ll > l; } |
d7997e1a VZ |
905 | inline bool operator>(long l, const wxLongLong& ll) { return ll < l; } |
906 | inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; } | |
907 | inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; } | |
908 | inline bool operator==(long l, const wxLongLong& ll) { return ll == l; } | |
909 | inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; } | |
b01fb5c3 | 910 | |
d7997e1a | 911 | inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; } |
b01fb5c3 VZ |
912 | inline wxLongLong operator-(long l, const wxLongLong& ll) |
913 | { | |
d7997e1a | 914 | return wxLongLong(l) - ll; |
b01fb5c3 | 915 | } |
2ea24d9f | 916 | |
d7997e1a VZ |
917 | inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; } |
918 | inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; } | |
919 | inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; } | |
920 | inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; } | |
921 | inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; } | |
922 | inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; } | |
923 | ||
924 | inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; } | |
8e38fd1f | 925 | |
842215bb | 926 | inline wxLongLong operator-(unsigned long l, const wxULongLong& ull) |
d7997e1a | 927 | { |
842215bb WS |
928 | wxULongLong ret = wxULongLong(l) - ull; |
929 | return wxLongLong((long)ret.GetHi(),ret.GetLo()); | |
d7997e1a | 930 | } |
8e38fd1f | 931 | |
8b81872f | 932 | #endif // _WX_LONGLONG_H |