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