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