]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
wxFrame for wxUniv
[wxWidgets.git] / include / wx / longlong.h
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
49 // first check for generic cases which are long on 64bit machine and "long
50 // long", then check for specific compilers
51 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
52 #define wxLongLong_t long
53 #define wxLongLongIsLong
54 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8
55 #define wxLongLong_t long long
56 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
57 #define wxLongLong_t __int64
58 #elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
59 #define wxLongLong_t __int64
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
67 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
68 #define wxLongLong_t long long
69 #else // no native long long type
70 // both warning and pragma warning are not portable, but at least an
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__))
75 #pragma warning "Your compiler does not appear to support 64 bit "\
76 "integers, using emulation class instead.\n" \
77 "Please report your compiler version to " \
78 "wx-dev@lists.wxwindows.org!"
79 #endif
80
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
87 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
88 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
89 // this is useful in test programs and only there
90 #ifndef wxUSE_LONGLONG_NATIVE
91 #define wxUSE_LONGLONG_NATIVE 0
92 #endif
93
94 class WXDLLEXPORT wxLongLongWx;
95 #if defined(__VISUALC__) && !defined(__WIN32__)
96 #define wxLongLong wxLongLongWx
97 #else
98 typedef wxLongLongWx wxLongLong;
99 #endif
100
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
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
126 class WXDLLEXPORT wxLongLongNative
127 {
128 public:
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
142 // default copy ctor is ok
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
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
157 // assignment operators from wxLongLongNative is ok
158
159 // accessors
160 // get high part
161 long GetHi() const
162 { return (long)(m_ll >> 32); }
163 // get low part
164 unsigned long GetLo() const
165 { return (unsigned long)m_ll; }
166
167 // get absolute value
168 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
169 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
170
171 // convert to native long long
172 wxLongLong_t GetValue() const { return m_ll; }
173
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
183 // don't provide implicit conversion to wxLongLong_t or we will have an
184 // ambiguity for all arithmetic operations
185 //operator wxLongLong_t() const { return m_ll; }
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); }
210 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
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
260 // multiplication/division
261 wxLongLongNative operator*(const wxLongLongNative& ll) const
262 { return wxLongLongNative(m_ll * ll.m_ll); }
263 wxLongLongNative operator*(long l) const
264 { return wxLongLongNative(m_ll * l); }
265 wxLongLongNative& operator*=(const wxLongLongNative& ll)
266 { m_ll *= ll.m_ll; return *this; }
267 wxLongLongNative& operator*=(long l)
268 { m_ll *= l; return *this; }
269
270 wxLongLongNative operator/(const wxLongLongNative& ll) const
271 { return wxLongLongNative(m_ll / ll.m_ll); }
272 wxLongLongNative operator/(long l) const
273 { return wxLongLongNative(m_ll / l); }
274 wxLongLongNative& operator/=(const wxLongLongNative& ll)
275 { m_ll /= ll.m_ll; return *this; }
276 wxLongLongNative& operator/=(long l)
277 { m_ll /= l; return *this; }
278
279 wxLongLongNative operator%(const wxLongLongNative& ll) const
280 { return wxLongLongNative(m_ll % ll.m_ll); }
281 wxLongLongNative operator%(long l) const
282 { return wxLongLongNative(m_ll % l); }
283
284 // comparison
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 bool operator>=(const wxLongLongNative& ll) const
306 { return m_ll >= ll.m_ll; }
307 bool operator>=(long l) const
308 { return m_ll >= l; }
309
310 // miscellaneous
311
312 // return the string representation of this number
313 wxString ToString() const;
314
315 // conversion to byte array: returns a pointer to static buffer!
316 void *asArray() const;
317
318 #if wxUSE_STD_IOSTREAM
319 // input/output
320 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
321 #endif
322
323 private:
324 wxLongLong_t m_ll;
325 };
326
327 #endif // wxUSE_LONGLONG_NATIVE
328
329 #if wxUSE_LONGLONG_WX
330
331 class WXDLLEXPORT wxLongLongWx
332 {
333 public:
334 // ctors
335 // default ctor initializes to 0
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 }
346 // from long
347 wxLongLongWx(long l) { *this = l; }
348 // from 2 longs
349 wxLongLongWx(long hi, unsigned long lo)
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 }
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)
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 }
382 // from double
383 wxLongLongWx& Assign(double d);
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
392 // get absolute value
393 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
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 }
408
409 // convert to long with range checking in the debug mode (only!)
410 long ToLong() const
411 {
412 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
413 _T("wxLongLong to long conversion loss of precision") );
414
415 return (long)m_lo;
416 }
417
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
429 wxLongLongWx& operator++(int) { return ++(*this); }
430
431 // negation operator
432 wxLongLongWx operator-() const;
433 wxLongLongWx& Negate();
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
443 wxLongLongWx& operator--(int) { return --(*this); }
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
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); }
468 bool operator<(const wxLongLongWx& ll) const;
469 bool operator>(const wxLongLongWx& ll) const;
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; }
474
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
486 // multiplication
487 wxLongLongWx operator*(const wxLongLongWx& ll) const;
488 wxLongLongWx& operator*=(const wxLongLongWx& ll);
489
490 // division
491 wxLongLongWx operator/(const wxLongLongWx& ll) const;
492 wxLongLongWx& operator/=(const wxLongLongWx& ll);
493
494 wxLongLongWx operator%(const wxLongLongWx& ll) const;
495
496 void Divide(const wxLongLongWx& divisor,
497 wxLongLongWx& quotient,
498 wxLongLongWx& remainder) const;
499
500 // input/output
501
502 // return the string representation of this number
503 wxString ToString() const;
504
505 void *asArray() const;
506
507 #if wxUSE_STD_IOSTREAM
508 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
509 #endif // wxUSE_STD_IOSTREAM
510
511 private:
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;
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
525 };
526
527 #endif // wxUSE_LONGLONG_WX
528
529 // ----------------------------------------------------------------------------
530 // binary operators
531 // ----------------------------------------------------------------------------
532
533 inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
534 inline bool operator>(long l, const wxLongLong& ll) { return ll > l; }
535 inline bool operator<=(long l, const wxLongLong& ll) { return ll > l; }
536 inline bool operator>=(long l, const wxLongLong& ll) { return ll > l; }
537 inline bool operator==(long l, const wxLongLong& ll) { return ll > l; }
538 inline bool operator!=(long l, const wxLongLong& ll) { return ll > l; }
539
540 inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
541 inline wxLongLong operator-(long l, const wxLongLong& ll) { return ll - l; }
542
543 #endif // _WX_LONGLONG_H