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