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