]> git.saurik.com Git - wxWidgets.git/blame - include/wx/longlong.h
wxDialog for wxUniv
[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>
10// Licence: wxWindows license
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef _WX_LONGLONG_H
14#define _WX_LONGLONG_H
15
16#ifdef __GNUG__
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
41// to avoid compilation problems on 64bit machines with ambiguous method calls
2f02cb89 42// we will need to define this
8b81872f
VZ
43#undef wxLongLongIsLong
44
45// NB: we #define and not typedef wxLongLong_t because we want to be able to
2f02cb89
VZ
46// use 'unsigned wxLongLong_t' as well and because we use "#ifdef
47// wxLongLong_t" below
8db6ac55
VZ
48
49// first check for generic cases which are long on 64bit machine and "long
50// long", then check for specific compilers
8b81872f
VZ
51#if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
52 #define wxLongLong_t long
53 #define wxLongLongIsLong
8db6ac55
VZ
54#elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8
55 #define wxLongLong_t long long
cd0b1709 56#elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
8b81872f 57 #define wxLongLong_t __int64
f6bcfd97 58#elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
ad0dc53b 59 #define wxLongLong_t __int64
8b81872f
VZ
60#elif defined(__MWERKS__)
61 #if __option(longlong)
62 #define wxLongLong_t long long
63 #else
64 #error "The 64 bit integer support in CodeWarrior has been disabled."
65 #error "See the documentation on the 'longlong' pragma."
66 #endif
398b582f
DW
67#elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
68 #define wxLongLong_t long long
cd0b1709 69#else // no native long long type
9c2882d9 70 // both warning and pragma warning are not portable, but at least an
58ef67ab
VZ
71 // unknown pragma should never be an error - except that, actually, some
72 // broken compilers don't like it, so we have to disable it in this case
73 // <sigh>
74#if !(defined(__WATCOMC__) || defined(__VISAGECPP__))
9c2882d9 75 #pragma warning "Your compiler does not appear to support 64 bit "\
58ef67ab
VZ
76 "integers, using emulation class instead.\n" \
77 "Please report your compiler version to " \
78 "wx-dev@lists.wxwindows.org!"
cff4a45c 79#endif
8db6ac55 80
8b81872f
VZ
81 #define wxUSE_LONGLONG_WX 1
82#endif // compiler
83
84// the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
85// to disable automatic testing (useful for the test program which defines
86// both classes) but by default we only use one class
2ea24d9f 87#if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
2a310492 88 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
384223b3 89 // this is useful in test programs and only there
2a310492
VZ
90 #ifndef wxUSE_LONGLONG_NATIVE
91 #define wxUSE_LONGLONG_NATIVE 0
92 #endif
93
8b81872f 94 class WXDLLEXPORT wxLongLongWx;
92f5ff59
JS
95#if defined(__VISUALC__) && !defined(__WIN32__)
96 #define wxLongLong wxLongLongWx
97#else
8b81872f 98 typedef wxLongLongWx wxLongLong;
92f5ff59
JS
99#endif
100
b76b015e
VZ
101#else
102 // if nothing is defined, use native implementation by default, of course
103 #ifndef wxUSE_LONGLONG_NATIVE
104 #define wxUSE_LONGLONG_NATIVE 1
105 #endif
8b81872f
VZ
106#endif
107
108#ifndef wxUSE_LONGLONG_WX
109 #define wxUSE_LONGLONG_WX 0
110 class WXDLLEXPORT wxLongLongNative;
111 typedef wxLongLongNative wxLongLong;
112#endif
113
114// NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
115// typedef wxLongLong as it wants, we don't do it
116
117// ----------------------------------------------------------------------------
118// choose the appropriate class
119// ----------------------------------------------------------------------------
120
121// we use iostream for wxLongLong output
122#include "wx/ioswrap.h"
123
124#if wxUSE_LONGLONG_NATIVE
125
126class WXDLLEXPORT wxLongLongNative
127{
128public:
129 // ctors
130 // default ctor initializes to 0
131 wxLongLongNative() { m_ll = 0; }
132 // from long long
133 wxLongLongNative(wxLongLong_t ll) { m_ll = ll; }
134 // from 2 longs
135 wxLongLongNative(long hi, unsigned long lo)
136 {
137 // assign first to avoid precision loss!
138 m_ll = ((wxLongLong_t) hi) << 32;
139 m_ll |= (wxLongLong_t) lo;
140 }
141
2f02cb89 142 // default copy ctor is ok
8b81872f
VZ
143
144 // no dtor
145
146 // assignment operators
147 // from native 64 bit integer
148 wxLongLongNative& operator=(wxLongLong_t ll)
149 { m_ll = ll; return *this; }
150
cd0b1709
VZ
151 // from double: this one has an explicit name because otherwise we
152 // would have ambiguity with "ll = int" and also because we don't want
153 // to have implicit conversions between doubles and wxLongLongs
154 wxLongLongNative& Assign(double d)
155 { m_ll = (wxLongLong_t)d; return *this; }
156
8b81872f
VZ
157 // assignment operators from wxLongLongNative is ok
158
159 // accessors
160 // get high part
161 long GetHi() const
2f02cb89 162 { return (long)(m_ll >> 32); }
8b81872f
VZ
163 // get low part
164 unsigned long GetLo() const
2f02cb89 165 { return (unsigned long)m_ll; }
8b81872f 166
b76b015e 167 // get absolute value
2ea24d9f 168 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
b76b015e
VZ
169 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
170
8b81872f
VZ
171 // convert to native long long
172 wxLongLong_t GetValue() const { return m_ll; }
173
1ef54dcf
VZ
174 // convert to long with range checking in the debug mode (only!)
175 long ToLong() const
176 {
177 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
178 _T("wxLongLong to long conversion loss of precision") );
179
180 return (long)m_ll;
181 }
182
2f02cb89
VZ
183 // don't provide implicit conversion to wxLongLong_t or we will have an
184 // ambiguity for all arithmetic operations
b76b015e 185 //operator wxLongLong_t() const { return m_ll; }
8b81872f
VZ
186
187 // operations
188 // addition
189 wxLongLongNative operator+(const wxLongLongNative& ll) const
190 { return wxLongLongNative(m_ll + ll.m_ll); }
191 wxLongLongNative& operator+=(const wxLongLongNative& ll)
192 { m_ll += ll.m_ll; return *this; }
193
194 wxLongLongNative operator+(const wxLongLong_t ll) const
195 { return wxLongLongNative(m_ll + ll); }
196 wxLongLongNative& operator+=(const wxLongLong_t ll)
197 { m_ll += ll; return *this; }
198
199 // pre increment
200 wxLongLongNative& operator++()
201 { m_ll++; return *this; }
202
203 // post increment
204 wxLongLongNative& operator++(int)
205 { m_ll++; return *this; }
206
207 // negation operator
208 wxLongLongNative operator-() const
209 { return wxLongLongNative(-m_ll); }
3a994742 210 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
8b81872f
VZ
211
212 // subtraction
213 wxLongLongNative operator-(const wxLongLongNative& ll) const
214 { return wxLongLongNative(m_ll - ll.m_ll); }
215 wxLongLongNative& operator-=(const wxLongLongNative& ll)
216 { m_ll -= ll.m_ll; return *this; }
217
218 wxLongLongNative operator-(const wxLongLong_t ll) const
219 { return wxLongLongNative(m_ll - ll); }
220 wxLongLongNative& operator-=(const wxLongLong_t ll)
221 { m_ll -= ll; return *this; }
222
223 // pre decrement
224 wxLongLongNative& operator--()
225 { m_ll--; return *this; }
226
227 // post decrement
228 wxLongLongNative& operator--(int)
229 { m_ll--; return *this; }
230
231 // shifts
232 // left shift
233 wxLongLongNative operator<<(int shift) const
234 { return wxLongLongNative(m_ll << shift);; }
235 wxLongLongNative& operator<<=(int shift)
236 { m_ll <<= shift; return *this; }
237
238 // right shift
239 wxLongLongNative operator>>(int shift) const
240 { return wxLongLongNative(m_ll >> shift);; }
241 wxLongLongNative& operator>>=(int shift)
242 { m_ll >>= shift; return *this; }
243
244 // bitwise operators
245 wxLongLongNative operator&(const wxLongLongNative& ll) const
246 { return wxLongLongNative(m_ll & ll.m_ll); }
247 wxLongLongNative& operator&=(const wxLongLongNative& ll)
248 { m_ll &= ll.m_ll; return *this; }
249
250 wxLongLongNative operator|(const wxLongLongNative& ll) const
251 { return wxLongLongNative(m_ll | ll.m_ll); }
252 wxLongLongNative& operator|=(const wxLongLongNative& ll)
253 { m_ll |= ll.m_ll; return *this; }
254
255 wxLongLongNative operator^(const wxLongLongNative& ll) const
256 { return wxLongLongNative(m_ll ^ ll.m_ll); }
257 wxLongLongNative& operator^=(const wxLongLongNative& ll)
258 { m_ll ^= ll.m_ll; return *this; }
259
b76b015e 260 // multiplication/division
8b81872f
VZ
261 wxLongLongNative operator*(const wxLongLongNative& ll) const
262 { return wxLongLongNative(m_ll * ll.m_ll); }
2f02cb89
VZ
263 wxLongLongNative operator*(long l) const
264 { return wxLongLongNative(m_ll * l); }
8b81872f
VZ
265 wxLongLongNative& operator*=(const wxLongLongNative& ll)
266 { m_ll *= ll.m_ll; return *this; }
2f02cb89
VZ
267 wxLongLongNative& operator*=(long l)
268 { m_ll *= l; return *this; }
8b81872f
VZ
269
270 wxLongLongNative operator/(const wxLongLongNative& ll) const
271 { return wxLongLongNative(m_ll / ll.m_ll); }
b76b015e
VZ
272 wxLongLongNative operator/(long l) const
273 { return wxLongLongNative(m_ll / l); }
8b81872f
VZ
274 wxLongLongNative& operator/=(const wxLongLongNative& ll)
275 { m_ll /= ll.m_ll; return *this; }
2f02cb89
VZ
276 wxLongLongNative& operator/=(long l)
277 { m_ll /= l; return *this; }
8b81872f
VZ
278
279 wxLongLongNative operator%(const wxLongLongNative& ll) const
280 { return wxLongLongNative(m_ll % ll.m_ll); }
b76b015e
VZ
281 wxLongLongNative operator%(long l) const
282 { return wxLongLongNative(m_ll % l); }
8b81872f
VZ
283
284 // comparison
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 bool operator<(const wxLongLongNative& ll) const
294 { return m_ll < ll.m_ll; }
b76b015e
VZ
295 bool operator<(long l) const
296 { return m_ll < l; }
8b81872f
VZ
297 bool operator>(const wxLongLongNative& ll) const
298 { return m_ll > ll.m_ll; }
b76b015e
VZ
299 bool operator>(long l) const
300 { return m_ll > l; }
8b81872f
VZ
301 bool operator<=(const wxLongLongNative& ll) const
302 { return m_ll <= ll.m_ll; }
b76b015e
VZ
303 bool operator<=(long l) const
304 { return m_ll <= l; }
8b81872f
VZ
305 bool operator>=(const wxLongLongNative& ll) const
306 { return m_ll >= ll.m_ll; }
b76b015e
VZ
307 bool operator>=(long l) const
308 { return m_ll >= l; }
8b81872f
VZ
309
310 // miscellaneous
3a994742
VZ
311
312 // return the string representation of this number
313 wxString ToString() const;
314
8b81872f
VZ
315 // conversion to byte array: returns a pointer to static buffer!
316 void *asArray() const;
317
fcc3d7cb 318#if wxUSE_STD_IOSTREAM
8b81872f 319 // input/output
dd107c50 320 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
fcc3d7cb 321#endif
8b81872f
VZ
322
323private:
324 wxLongLong_t m_ll;
325};
326
327#endif // wxUSE_LONGLONG_NATIVE
328
329#if wxUSE_LONGLONG_WX
330
331class WXDLLEXPORT wxLongLongWx
332{
333public:
334 // ctors
335 // default ctor initializes to 0
2a310492
VZ
336 wxLongLongWx()
337 {
338 m_lo = m_hi = 0;
339
340#ifdef wxLONGLONG_TEST_MODE
341 m_ll = 0;
342
343 Check();
344#endif // wxLONGLONG_TEST_MODE
345 }
8b81872f 346 // from long
2a310492 347 wxLongLongWx(long l) { *this = l; }
8b81872f
VZ
348 // from 2 longs
349 wxLongLongWx(long hi, unsigned long lo)
2a310492
VZ
350 {
351 m_hi = hi;
352 m_lo = lo;
353
354#ifdef wxLONGLONG_TEST_MODE
355 m_ll = hi;
356 m_ll <<= 32;
357 m_ll |= lo;
358
359 Check();
360#endif // wxLONGLONG_TEST_MODE
361 }
8b81872f
VZ
362
363 // default copy ctor is ok in both cases
364
365 // no dtor
366
367 // assignment operators
368 // from long
369 wxLongLongWx& operator=(long l)
2a310492
VZ
370 {
371 m_lo = l;
372 m_hi = (l < 0 ? -1l : 0l);
373
374#ifdef wxLONGLONG_TEST_MODE
375 m_ll = l;
376
377 Check();
378#endif // wxLONGLONG_TEST_MODE
379
380 return *this;
381 }
cd0b1709
VZ
382 // from double
383 wxLongLongWx& Assign(double d);
8b81872f
VZ
384 // can't have assignment operator from 2 longs
385
386 // accessors
387 // get high part
388 long GetHi() const { return m_hi; }
389 // get low part
390 unsigned long GetLo() const { return m_lo; }
391
cd0b1709 392 // get absolute value
2ea24d9f 393 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
2a310492
VZ
394 wxLongLongWx& Abs()
395 {
396 if ( m_hi < 0 )
397 m_hi = -m_hi;
398
399#ifdef wxLONGLONG_TEST_MODE
400 if ( m_ll < 0 )
401 m_ll = -m_ll;
402
403 Check();
404#endif // wxLONGLONG_TEST_MODE
405
406 return *this;
407 }
cd0b1709
VZ
408
409 // convert to long with range checking in the debug mode (only!)
410 long ToLong() const
411 {
2a310492 412 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
cd0b1709
VZ
413 _T("wxLongLong to long conversion loss of precision") );
414
415 return (long)m_lo;
416 }
417
8b81872f
VZ
418 // operations
419 // addition
420 wxLongLongWx operator+(const wxLongLongWx& ll) const;
421 wxLongLongWx& operator+=(const wxLongLongWx& ll);
422 wxLongLongWx operator+(long l) const;
423 wxLongLongWx& operator+=(long l);
424
425 // pre increment operator
426 wxLongLongWx& operator++();
427
428 // post increment operator
2a310492 429 wxLongLongWx& operator++(int) { return ++(*this); }
8b81872f
VZ
430
431 // negation operator
432 wxLongLongWx operator-() const;
2a310492 433 wxLongLongWx& Negate();
8b81872f
VZ
434
435 // subraction
436 wxLongLongWx operator-(const wxLongLongWx& ll) const;
437 wxLongLongWx& operator-=(const wxLongLongWx& ll);
438
439 // pre decrement operator
440 wxLongLongWx& operator--();
441
442 // post decrement operator
2a310492 443 wxLongLongWx& operator--(int) { return --(*this); }
8b81872f
VZ
444
445 // shifts
446 // left shift
447 wxLongLongWx operator<<(int shift) const;
448 wxLongLongWx& operator<<=(int shift);
449
450 // right shift
451 wxLongLongWx operator>>(int shift) const;
452 wxLongLongWx& operator>>=(int shift);
453
454 // bitwise operators
455 wxLongLongWx operator&(const wxLongLongWx& ll) const;
456 wxLongLongWx& operator&=(const wxLongLongWx& ll);
457 wxLongLongWx operator|(const wxLongLongWx& ll) const;
458 wxLongLongWx& operator|=(const wxLongLongWx& ll);
459 wxLongLongWx operator^(const wxLongLongWx& ll) const;
460 wxLongLongWx& operator^=(const wxLongLongWx& ll);
461 wxLongLongWx operator~() const;
462
463 // comparison
2ea24d9f
VZ
464 bool operator==(const wxLongLongWx& ll) const
465 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
466 bool operator!=(const wxLongLongWx& ll) const
467 { return !(*this == ll); }
8b81872f
VZ
468 bool operator<(const wxLongLongWx& ll) const;
469 bool operator>(const wxLongLongWx& ll) const;
2ea24d9f
VZ
470 bool operator<=(const wxLongLongWx& ll) const
471 { return *this < ll || *this == ll; }
472 bool operator>=(const wxLongLongWx& ll) const
473 { return *this > ll || *this == ll; }
8b81872f 474
f6bcfd97
BP
475 bool operator<(long l) const { return *this < wxLongLongWx(l); }
476 bool operator>(long l) const { return *this > wxLongLongWx(l); }
477 bool operator==(long l) const
478 {
479 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
480 : (m_hi == -1 && m_lo == (unsigned long)l);
481 }
482
483 bool operator<=(long l) const { return *this < l || *this == l; }
484 bool operator>=(long l) const { return *this > l || *this == l; }
485
8b81872f
VZ
486 // multiplication
487 wxLongLongWx operator*(const wxLongLongWx& ll) const;
488 wxLongLongWx& operator*=(const wxLongLongWx& ll);
8b81872f
VZ
489
490 // division
cd0b1709
VZ
491 wxLongLongWx operator/(const wxLongLongWx& ll) const;
492 wxLongLongWx& operator/=(const wxLongLongWx& ll);
493
494 wxLongLongWx operator%(const wxLongLongWx& ll) const;
495
8b81872f
VZ
496 void Divide(const wxLongLongWx& divisor,
497 wxLongLongWx& quotient,
498 wxLongLongWx& remainder) const;
499
500 // input/output
3a994742
VZ
501
502 // return the string representation of this number
503 wxString ToString() const;
504
505 void *asArray() const;
506
cd0b1709 507#if wxUSE_STD_IOSTREAM
dd107c50 508 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
fcc3d7cb 509#endif // wxUSE_STD_IOSTREAM
8b81872f
VZ
510
511private:
512 // long is at least 32 bits, so represent our 64bit number as 2 longs
513
514 long m_hi; // signed bit is in the high part
515 unsigned long m_lo;
2a310492
VZ
516
517#ifdef wxLONGLONG_TEST_MODE
518 void Check()
519 {
520 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
521 }
522
523 wxLongLong_t m_ll;
524#endif // wxLONGLONG_TEST_MODE
8b81872f
VZ
525};
526
527#endif // wxUSE_LONGLONG_WX
528
2ea24d9f
VZ
529// ----------------------------------------------------------------------------
530// binary operators
531// ----------------------------------------------------------------------------
532
6d56eb5c
VZ
533inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
534inline bool operator>(long l, const wxLongLong& ll) { return ll > l; }
535inline bool operator<=(long l, const wxLongLong& ll) { return ll > l; }
536inline bool operator>=(long l, const wxLongLong& ll) { return ll > l; }
537inline bool operator==(long l, const wxLongLong& ll) { return ll > l; }
538inline bool operator!=(long l, const wxLongLong& ll) { return ll > l; }
539
540inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
541inline wxLongLong operator-(long l, const wxLongLong& ll) { return ll - l; }
2ea24d9f 542
8b81872f 543#endif // _WX_LONGLONG_H