]> git.saurik.com Git - wxWidgets.git/blame - include/wx/longlong.h
only define globals when wxUSE_BASE to avoid duplicate definitions in wxBase and...
[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
218 { return wxLongLongNative(m_ll << shift);; }
219 wxLongLongNative& operator<<=(int shift)
220 { m_ll <<= shift; return *this; }
221
222 // right shift
223 wxLongLongNative operator>>(int shift) const
224 { return wxLongLongNative(m_ll >> shift);; }
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
dd107c50 304 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
fcc3d7cb 305#endif
8b81872f 306
51496782
WS
307 friend wxString& operator<<(wxString&, const wxLongLongNative&);
308
8b81872f
VZ
309private:
310 wxLongLong_t m_ll;
311};
312
8e38fd1f 313
bddd7a8d 314class WXDLLIMPEXP_BASE wxULongLongNative
8e38fd1f
RL
315{
316public:
317 // ctors
318 // default ctor initializes to 0
2e403d6d 319 wxULongLongNative() : m_ll(0) { }
8e38fd1f 320 // from long long
2e403d6d 321 wxULongLongNative(unsigned wxLongLong_t ll) : m_ll(ll) { }
8e38fd1f 322 // from 2 longs
2e403d6d 323 wxULongLongNative(unsigned long hi, unsigned long lo) : m_ll(0)
8e38fd1f
RL
324 {
325 // assign first to avoid precision loss!
326 m_ll = ((unsigned wxLongLong_t) hi) << 32;
327 m_ll |= (unsigned wxLongLong_t) lo;
328 }
329
330 // default copy ctor is ok
331
332 // no dtor
333
334 // assignment operators
335 // from native 64 bit integer
336 wxULongLongNative& operator=(unsigned wxLongLong_t ll)
337 { m_ll = ll; return *this; }
338
339 // assignment operators from wxULongLongNative is ok
340
341 // accessors
342 // get high part
343 unsigned long GetHi() const
344 { return (unsigned long)(m_ll >> 32); }
345 // get low part
346 unsigned long GetLo() const
347 { return (unsigned long)m_ll; }
348
349 // convert to native ulong long
350 unsigned wxLongLong_t GetValue() const { return m_ll; }
351
352 // convert to ulong with range checking in the debug mode (only!)
353 unsigned long ToULong() const
354 {
0ba9f867 355 wxASSERT_MSG( m_ll <= LONG_MAX,
8e38fd1f
RL
356 _T("wxULongLong to long conversion loss of precision") );
357
358 return (unsigned long)m_ll;
359 }
360
8e38fd1f
RL
361 // operations
362 // addition
363 wxULongLongNative operator+(const wxULongLongNative& ll) const
364 { return wxULongLongNative(m_ll + ll.m_ll); }
365 wxULongLongNative& operator+=(const wxULongLongNative& ll)
366 { m_ll += ll.m_ll; return *this; }
367
368 wxULongLongNative operator+(const unsigned wxLongLong_t ll) const
369 { return wxULongLongNative(m_ll + ll); }
370 wxULongLongNative& operator+=(const unsigned wxLongLong_t ll)
371 { m_ll += ll; return *this; }
372
373 // pre increment
374 wxULongLongNative& operator++()
375 { m_ll++; return *this; }
376
377 // post increment
2e403d6d
GD
378 wxULongLongNative operator++(int)
379 { wxULongLongNative value(*this); m_ll++; return value; }
8e38fd1f
RL
380
381 // subtraction
382 wxULongLongNative operator-(const wxULongLongNative& ll) const
383 { return wxULongLongNative(m_ll - ll.m_ll); }
384 wxULongLongNative& operator-=(const wxULongLongNative& ll)
385 { m_ll -= ll.m_ll; return *this; }
386
387 wxULongLongNative operator-(const unsigned wxLongLong_t ll) const
388 { return wxULongLongNative(m_ll - ll); }
389 wxULongLongNative& operator-=(const unsigned wxLongLong_t ll)
390 { m_ll -= ll; return *this; }
391
392 // pre decrement
393 wxULongLongNative& operator--()
394 { m_ll--; return *this; }
395
396 // post decrement
2e403d6d
GD
397 wxULongLongNative operator--(int)
398 { wxULongLongNative value(*this); m_ll--; return value; }
8e38fd1f
RL
399
400 // shifts
401 // left shift
402 wxULongLongNative operator<<(int shift) const
403 { return wxULongLongNative(m_ll << shift);; }
404 wxULongLongNative& operator<<=(int shift)
405 { m_ll <<= shift; return *this; }
406
407 // right shift
408 wxULongLongNative operator>>(int shift) const
409 { return wxULongLongNative(m_ll >> shift);; }
410 wxULongLongNative& operator>>=(int shift)
411 { m_ll >>= shift; return *this; }
412
413 // bitwise operators
414 wxULongLongNative operator&(const wxULongLongNative& ll) const
415 { return wxULongLongNative(m_ll & ll.m_ll); }
416 wxULongLongNative& operator&=(const wxULongLongNative& ll)
417 { m_ll &= ll.m_ll; return *this; }
418
419 wxULongLongNative operator|(const wxULongLongNative& ll) const
420 { return wxULongLongNative(m_ll | ll.m_ll); }
421 wxULongLongNative& operator|=(const wxULongLongNative& ll)
422 { m_ll |= ll.m_ll; return *this; }
423
424 wxULongLongNative operator^(const wxULongLongNative& ll) const
425 { return wxULongLongNative(m_ll ^ ll.m_ll); }
426 wxULongLongNative& operator^=(const wxULongLongNative& ll)
427 { m_ll ^= ll.m_ll; return *this; }
428
429 // multiplication/division
430 wxULongLongNative operator*(const wxULongLongNative& ll) const
431 { return wxULongLongNative(m_ll * ll.m_ll); }
432 wxULongLongNative operator*(unsigned long l) const
433 { return wxULongLongNative(m_ll * l); }
434 wxULongLongNative& operator*=(const wxULongLongNative& ll)
435 { m_ll *= ll.m_ll; return *this; }
436 wxULongLongNative& operator*=(unsigned long l)
437 { m_ll *= l; return *this; }
438
439 wxULongLongNative operator/(const wxULongLongNative& ll) const
440 { return wxULongLongNative(m_ll / ll.m_ll); }
441 wxULongLongNative operator/(unsigned long l) const
442 { return wxULongLongNative(m_ll / l); }
443 wxULongLongNative& operator/=(const wxULongLongNative& ll)
444 { m_ll /= ll.m_ll; return *this; }
445 wxULongLongNative& operator/=(unsigned long l)
446 { m_ll /= l; return *this; }
447
448 wxULongLongNative operator%(const wxULongLongNative& ll) const
449 { return wxULongLongNative(m_ll % ll.m_ll); }
450 wxULongLongNative operator%(unsigned long l) const
451 { return wxULongLongNative(m_ll % l); }
452
453 // comparison
454 bool operator==(const wxULongLongNative& ll) const
455 { return m_ll == ll.m_ll; }
456 bool operator==(unsigned long l) const
457 { return m_ll == l; }
458 bool operator!=(const wxULongLongNative& ll) const
459 { return m_ll != ll.m_ll; }
460 bool operator!=(unsigned long l) const
461 { return m_ll != l; }
462 bool operator<(const wxULongLongNative& ll) const
463 { return m_ll < ll.m_ll; }
464 bool operator<(unsigned long l) const
465 { return m_ll < l; }
466 bool operator>(const wxULongLongNative& ll) const
467 { return m_ll > ll.m_ll; }
468 bool operator>(unsigned long l) const
469 { return m_ll > l; }
470 bool operator<=(const wxULongLongNative& ll) const
471 { return m_ll <= ll.m_ll; }
472 bool operator<=(unsigned long l) const
473 { return m_ll <= l; }
474 bool operator>=(const wxULongLongNative& ll) const
475 { return m_ll >= ll.m_ll; }
476 bool operator>=(unsigned long l) const
477 { return m_ll >= l; }
478
479 // miscellaneous
480
481 // return the string representation of this number
482 wxString ToString() const;
483
484 // conversion to byte array: returns a pointer to static buffer!
485 void *asArray() const;
486
487#if wxUSE_STD_IOSTREAM
488 // input/output
7fcdf88f
VZ
489 friend WXDLLIMPEXP_BASE
490 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
8e38fd1f
RL
491#endif
492
7fcdf88f
VZ
493 friend WXDLLIMPEXP_BASE
494 wxString& operator<<(wxString&, const wxULongLongNative&);
51496782 495
8e38fd1f
RL
496private:
497 unsigned wxLongLong_t m_ll;
498};
499
8b81872f
VZ
500#endif // wxUSE_LONGLONG_NATIVE
501
502#if wxUSE_LONGLONG_WX
503
bddd7a8d 504class WXDLLIMPEXP_BASE wxLongLongWx
8b81872f
VZ
505{
506public:
507 // ctors
508 // default ctor initializes to 0
2a310492
VZ
509 wxLongLongWx()
510 {
511 m_lo = m_hi = 0;
512
513#ifdef wxLONGLONG_TEST_MODE
514 m_ll = 0;
515
516 Check();
517#endif // wxLONGLONG_TEST_MODE
518 }
8b81872f 519 // from long
2a310492 520 wxLongLongWx(long l) { *this = l; }
8b81872f
VZ
521 // from 2 longs
522 wxLongLongWx(long hi, unsigned long lo)
2a310492
VZ
523 {
524 m_hi = hi;
525 m_lo = lo;
526
527#ifdef wxLONGLONG_TEST_MODE
528 m_ll = hi;
529 m_ll <<= 32;
530 m_ll |= lo;
531
532 Check();
533#endif // wxLONGLONG_TEST_MODE
534 }
8b81872f
VZ
535
536 // default copy ctor is ok in both cases
537
538 // no dtor
539
540 // assignment operators
541 // from long
542 wxLongLongWx& operator=(long l)
2a310492
VZ
543 {
544 m_lo = l;
545 m_hi = (l < 0 ? -1l : 0l);
546
547#ifdef wxLONGLONG_TEST_MODE
548 m_ll = l;
549
550 Check();
551#endif // wxLONGLONG_TEST_MODE
552
553 return *this;
554 }
cd0b1709
VZ
555 // from double
556 wxLongLongWx& Assign(double d);
8b81872f
VZ
557 // can't have assignment operator from 2 longs
558
559 // accessors
560 // get high part
561 long GetHi() const { return m_hi; }
562 // get low part
563 unsigned long GetLo() const { return m_lo; }
564
cd0b1709 565 // get absolute value
2ea24d9f 566 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
2a310492
VZ
567 wxLongLongWx& Abs()
568 {
569 if ( m_hi < 0 )
570 m_hi = -m_hi;
571
572#ifdef wxLONGLONG_TEST_MODE
573 if ( m_ll < 0 )
574 m_ll = -m_ll;
575
576 Check();
577#endif // wxLONGLONG_TEST_MODE
578
579 return *this;
580 }
cd0b1709
VZ
581
582 // convert to long with range checking in the debug mode (only!)
583 long ToLong() const
584 {
2a310492 585 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
cd0b1709
VZ
586 _T("wxLongLong to long conversion loss of precision") );
587
588 return (long)m_lo;
589 }
590
8b81872f
VZ
591 // operations
592 // addition
593 wxLongLongWx operator+(const wxLongLongWx& ll) const;
594 wxLongLongWx& operator+=(const wxLongLongWx& ll);
595 wxLongLongWx operator+(long l) const;
596 wxLongLongWx& operator+=(long l);
597
598 // pre increment operator
599 wxLongLongWx& operator++();
600
601 // post increment operator
2a310492 602 wxLongLongWx& operator++(int) { return ++(*this); }
8b81872f
VZ
603
604 // negation operator
605 wxLongLongWx operator-() const;
2a310492 606 wxLongLongWx& Negate();
8b81872f
VZ
607
608 // subraction
609 wxLongLongWx operator-(const wxLongLongWx& ll) const;
610 wxLongLongWx& operator-=(const wxLongLongWx& ll);
611
612 // pre decrement operator
613 wxLongLongWx& operator--();
614
615 // post decrement operator
2a310492 616 wxLongLongWx& operator--(int) { return --(*this); }
8b81872f
VZ
617
618 // shifts
619 // left shift
620 wxLongLongWx operator<<(int shift) const;
621 wxLongLongWx& operator<<=(int shift);
622
623 // right shift
624 wxLongLongWx operator>>(int shift) const;
625 wxLongLongWx& operator>>=(int shift);
626
627 // bitwise operators
628 wxLongLongWx operator&(const wxLongLongWx& ll) const;
629 wxLongLongWx& operator&=(const wxLongLongWx& ll);
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;
635
636 // comparison
2ea24d9f
VZ
637 bool operator==(const wxLongLongWx& ll) const
638 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
842215bb
WS
639#if wxUSE_LONGLONG_NATIVE
640 bool operator==(const wxLongLongNative& ll) const
641 { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); }
642#endif
2ea24d9f
VZ
643 bool operator!=(const wxLongLongWx& ll) const
644 { return !(*this == ll); }
8b81872f
VZ
645 bool operator<(const wxLongLongWx& ll) const;
646 bool operator>(const wxLongLongWx& ll) const;
2ea24d9f
VZ
647 bool operator<=(const wxLongLongWx& ll) const
648 { return *this < ll || *this == ll; }
649 bool operator>=(const wxLongLongWx& ll) const
650 { return *this > ll || *this == ll; }
8b81872f 651
f6bcfd97
BP
652 bool operator<(long l) const { return *this < wxLongLongWx(l); }
653 bool operator>(long l) const { return *this > wxLongLongWx(l); }
654 bool operator==(long l) const
655 {
656 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
657 : (m_hi == -1 && m_lo == (unsigned long)l);
658 }
659
660 bool operator<=(long l) const { return *this < l || *this == l; }
661 bool operator>=(long l) const { return *this > l || *this == l; }
662
8b81872f
VZ
663 // multiplication
664 wxLongLongWx operator*(const wxLongLongWx& ll) const;
665 wxLongLongWx& operator*=(const wxLongLongWx& ll);
8b81872f
VZ
666
667 // division
cd0b1709
VZ
668 wxLongLongWx operator/(const wxLongLongWx& ll) const;
669 wxLongLongWx& operator/=(const wxLongLongWx& ll);
670
671 wxLongLongWx operator%(const wxLongLongWx& ll) const;
672
8b81872f
VZ
673 void Divide(const wxLongLongWx& divisor,
674 wxLongLongWx& quotient,
675 wxLongLongWx& remainder) const;
676
677 // input/output
3a994742
VZ
678
679 // return the string representation of this number
680 wxString ToString() const;
681
682 void *asArray() const;
683
cd0b1709 684#if wxUSE_STD_IOSTREAM
dd107c50 685 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
fcc3d7cb 686#endif // wxUSE_STD_IOSTREAM
8b81872f 687
51496782
WS
688 friend wxString& operator<<(wxString&, const wxLongLongWx&);
689
8b81872f
VZ
690private:
691 // long is at least 32 bits, so represent our 64bit number as 2 longs
692
693 long m_hi; // signed bit is in the high part
694 unsigned long m_lo;
2a310492
VZ
695
696#ifdef wxLONGLONG_TEST_MODE
697 void Check()
698 {
699 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
700 }
701
702 wxLongLong_t m_ll;
703#endif // wxLONGLONG_TEST_MODE
8b81872f
VZ
704};
705
8e38fd1f 706
bddd7a8d 707class WXDLLIMPEXP_BASE wxULongLongWx
8e38fd1f
RL
708{
709public:
710 // ctors
711 // default ctor initializes to 0
712 wxULongLongWx()
713 {
714 m_lo = m_hi = 0;
715
716#ifdef wxLONGLONG_TEST_MODE
717 m_ll = 0;
718
719 Check();
720#endif // wxLONGLONG_TEST_MODE
721 }
722 // from ulong
723 wxULongLongWx(unsigned long l) { *this = l; }
724 // from 2 ulongs
725 wxULongLongWx(unsigned long hi, unsigned long lo)
726 {
727 m_hi = hi;
728 m_lo = lo;
729
730#ifdef wxLONGLONG_TEST_MODE
731 m_ll = hi;
732 m_ll <<= 32;
733 m_ll |= lo;
734
735 Check();
736#endif // wxLONGLONG_TEST_MODE
737 }
738
842215bb
WS
739 // from signed to unsigned
740 wxULongLongWx(wxLongLongWx ll)
741 {
742 wxASSERT(ll.GetHi() >= 0);
743 m_hi = (unsigned long)ll.GetHi();
744 m_lo = ll.GetLo();
745 }
746
8e38fd1f
RL
747 // default copy ctor is ok in both cases
748
749 // no dtor
750
751 // assignment operators
752 // from long
753 wxULongLongWx& operator=(unsigned long l)
754 {
755 m_lo = l;
756 m_hi = 0;
757
758#ifdef wxLONGLONG_TEST_MODE
759 m_ll = l;
760
761 Check();
762#endif // wxLONGLONG_TEST_MODE
763
764 return *this;
765 }
766
767 // can't have assignment operator from 2 longs
768
769 // accessors
770 // get high part
771 unsigned long GetHi() const { return m_hi; }
772 // get low part
773 unsigned long GetLo() const { return m_lo; }
774
775 // convert to long with range checking in the debug mode (only!)
776 unsigned long ToULong() const
777 {
d7997e1a 778 wxASSERT_MSG( m_hi == 0ul,
8e38fd1f
RL
779 _T("wxULongLong to long conversion loss of precision") );
780
781 return (unsigned long)m_lo;
782 }
783
784 // operations
785 // addition
786 wxULongLongWx operator+(const wxULongLongWx& ll) const;
787 wxULongLongWx& operator+=(const wxULongLongWx& ll);
788 wxULongLongWx operator+(unsigned long l) const;
789 wxULongLongWx& operator+=(unsigned long l);
790
791 // pre increment operator
792 wxULongLongWx& operator++();
793
794 // post increment operator
795 wxULongLongWx& operator++(int) { return ++(*this); }
796
842215bb
WS
797 // substraction
798 wxLongLongWx operator-(const wxULongLongWx& ll) const;
8e38fd1f
RL
799 wxULongLongWx& operator-=(const wxULongLongWx& ll);
800
801 // pre decrement operator
802 wxULongLongWx& operator--();
803
804 // post decrement operator
805 wxULongLongWx& operator--(int) { return --(*this); }
806
807 // shifts
808 // left shift
809 wxULongLongWx operator<<(int shift) const;
810 wxULongLongWx& operator<<=(int shift);
811
812 // right shift
813 wxULongLongWx operator>>(int shift) const;
814 wxULongLongWx& operator>>=(int shift);
815
816 // bitwise operators
817 wxULongLongWx operator&(const wxULongLongWx& ll) const;
818 wxULongLongWx& operator&=(const wxULongLongWx& ll);
819 wxULongLongWx operator|(const wxULongLongWx& ll) const;
820 wxULongLongWx& operator|=(const wxULongLongWx& ll);
821 wxULongLongWx operator^(const wxULongLongWx& ll) const;
822 wxULongLongWx& operator^=(const wxULongLongWx& ll);
823 wxULongLongWx operator~() const;
824
825 // comparison
826 bool operator==(const wxULongLongWx& ll) const
827 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
828 bool operator!=(const wxULongLongWx& ll) const
829 { return !(*this == ll); }
830 bool operator<(const wxULongLongWx& ll) const;
831 bool operator>(const wxULongLongWx& ll) const;
832 bool operator<=(const wxULongLongWx& ll) const
833 { return *this < ll || *this == ll; }
834 bool operator>=(const wxULongLongWx& ll) const
835 { return *this > ll || *this == ll; }
836
837 bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
838 bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
839 bool operator==(unsigned long l) const
840 {
841 return (m_hi == 0 && m_lo == (unsigned long)l);
842 }
843
844 bool operator<=(unsigned long l) const { return *this < l || *this == l; }
845 bool operator>=(unsigned long l) const { return *this > l || *this == l; }
846
847 // multiplication
848 wxULongLongWx operator*(const wxULongLongWx& ll) const;
849 wxULongLongWx& operator*=(const wxULongLongWx& ll);
850
851 // division
852 wxULongLongWx operator/(const wxULongLongWx& ll) const;
853 wxULongLongWx& operator/=(const wxULongLongWx& ll);
854
855 wxULongLongWx operator%(const wxULongLongWx& ll) const;
856
857 void Divide(const wxULongLongWx& divisor,
858 wxULongLongWx& quotient,
859 wxULongLongWx& remainder) const;
860
861 // input/output
862
863 // return the string representation of this number
864 wxString ToString() const;
865
866 void *asArray() const;
867
868#if wxUSE_STD_IOSTREAM
869 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
870#endif // wxUSE_STD_IOSTREAM
871
51496782
WS
872 friend wxString& operator<<(wxString&, const wxULongLongWx&);
873
8e38fd1f
RL
874private:
875 // long is at least 32 bits, so represent our 64bit number as 2 longs
876
d2ce649b 877 unsigned long m_hi;
8e38fd1f
RL
878 unsigned long m_lo;
879
880#ifdef wxLONGLONG_TEST_MODE
881 void Check()
882 {
883 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
884 }
885
886 unsigned wxLongLong_t m_ll;
887#endif // wxLONGLONG_TEST_MODE
888};
889
8b81872f
VZ
890#endif // wxUSE_LONGLONG_WX
891
2ea24d9f
VZ
892// ----------------------------------------------------------------------------
893// binary operators
894// ----------------------------------------------------------------------------
895
6d56eb5c 896inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
d7997e1a
VZ
897inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
898inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
899inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
900inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
901inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
b01fb5c3 902
d7997e1a 903inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
b01fb5c3
VZ
904inline wxLongLong operator-(long l, const wxLongLong& ll)
905{
d7997e1a 906 return wxLongLong(l) - ll;
b01fb5c3 907}
2ea24d9f 908
d7997e1a
VZ
909inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
910inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
911inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
912inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
913inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
914inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
915
916inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
8e38fd1f 917
842215bb 918inline wxLongLong operator-(unsigned long l, const wxULongLong& ull)
d7997e1a 919{
842215bb
WS
920 wxULongLong ret = wxULongLong(l) - ull;
921 return wxLongLong((long)ret.GetHi(),ret.GetLo());
d7997e1a 922}
8e38fd1f 923
8b81872f 924#endif // _WX_LONGLONG_H