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