]> git.saurik.com Git - wxWidgets.git/blame - include/wx/longlong.h
SourceForge patch #654210 to fix naming/numbering shared libs under OS X
[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
af49c4b8 16#if defined(__GNUG__) && !defined(__APPLE__)
8b81872f
VZ
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
2b5f62a0
VZ
53 #define wxLongLongSuffix l
54 #define wxLongLongFmtSpec _T("l")
8b81872f 55 #define wxLongLongIsLong
cd0b1709 56#elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
8b81872f 57 #define wxLongLong_t __int64
2b5f62a0
VZ
58 #define wxLongLongSuffix i64
59 #define wxLongLongFmtSpec _T("I64")
f6bcfd97 60#elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
ad0dc53b 61 #define wxLongLong_t __int64
2b5f62a0
VZ
62 #define wxLongLongSuffix i64
63 #define wxLongLongFmtSpec _T("I64")
64#elif (defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8) || \
65 defined(__MINGW32__) || \
66 defined(__CYGWIN__) || \
67 defined(__WXMICROWIN__) || \
68 (defined(__DJGPP__) && __DJGPP__ >= 2)
b2a9ee30 69 #define wxLongLong_t long long
2b5f62a0
VZ
70 #define wxLongLongSuffix ll
71 #define wxLongLongFmtSpec _T("ll")
8b81872f
VZ
72#elif defined(__MWERKS__)
73 #if __option(longlong)
74 #define wxLongLong_t long long
2b5f62a0
VZ
75 #define wxLongLongSuffix ll
76 #define wxLongLongFmtSpec _T("ll")
8b81872f
VZ
77 #else
78 #error "The 64 bit integer support in CodeWarrior has been disabled."
79 #error "See the documentation on the 'longlong' pragma."
80 #endif
398b582f 81#elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
92834365 82 #define wxLongLong_t long long
cd0b1709 83#else // no native long long type
9c2882d9 84 // both warning and pragma warning are not portable, but at least an
2b5f62a0 85 // unknown pragma should never be an error -- except that, actually, some
58ef67ab
VZ
86 // broken compilers don't like it, so we have to disable it in this case
87 // <sigh>
88#if !(defined(__WATCOMC__) || defined(__VISAGECPP__))
9c2882d9 89 #pragma warning "Your compiler does not appear to support 64 bit "\
58ef67ab
VZ
90 "integers, using emulation class instead.\n" \
91 "Please report your compiler version to " \
92 "wx-dev@lists.wxwindows.org!"
cff4a45c 93#endif
8db6ac55 94
8b81872f
VZ
95 #define wxUSE_LONGLONG_WX 1
96#endif // compiler
97
2b5f62a0
VZ
98// this macro allows to definea 64 bit constant in a portable way
99#define wxMakeLongLong(x, s) x ## s
100#define wxMakeLongLong2(x, s) wxMakeLongLong(x, s)
101#define wxLL(x) wxMakeLongLong2(x, wxLongLongSuffix)
102
8b81872f
VZ
103// the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
104// to disable automatic testing (useful for the test program which defines
105// both classes) but by default we only use one class
2ea24d9f 106#if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
2a310492 107 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
384223b3 108 // this is useful in test programs and only there
2a310492
VZ
109 #ifndef wxUSE_LONGLONG_NATIVE
110 #define wxUSE_LONGLONG_NATIVE 0
111 #endif
112
8b81872f 113 class WXDLLEXPORT wxLongLongWx;
d2ce649b 114 class WXDLLEXPORT wxULongLongWx;
92f5ff59
JS
115#if defined(__VISUALC__) && !defined(__WIN32__)
116 #define wxLongLong wxLongLongWx
8e38fd1f 117 #define wxULongLong wxULongLongWx
92f5ff59 118#else
8b81872f 119 typedef wxLongLongWx wxLongLong;
8e38fd1f 120 typedef wxULongLongWx wxULongLong;
92f5ff59
JS
121#endif
122
b76b015e
VZ
123#else
124 // if nothing is defined, use native implementation by default, of course
125 #ifndef wxUSE_LONGLONG_NATIVE
126 #define wxUSE_LONGLONG_NATIVE 1
127 #endif
8b81872f
VZ
128#endif
129
130#ifndef wxUSE_LONGLONG_WX
131 #define wxUSE_LONGLONG_WX 0
132 class WXDLLEXPORT wxLongLongNative;
8e38fd1f 133 class WXDLLEXPORT wxULongLongNative;
8b81872f 134 typedef wxLongLongNative wxLongLong;
8e38fd1f 135 typedef wxULongLongNative wxULongLong;
8b81872f
VZ
136#endif
137
138// NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
139// typedef wxLongLong as it wants, we don't do it
140
141// ----------------------------------------------------------------------------
142// choose the appropriate class
143// ----------------------------------------------------------------------------
144
145// we use iostream for wxLongLong output
146#include "wx/ioswrap.h"
147
148#if wxUSE_LONGLONG_NATIVE
149
150class WXDLLEXPORT wxLongLongNative
151{
152public:
153 // ctors
154 // default ctor initializes to 0
2e403d6d 155 wxLongLongNative() : m_ll(0) { }
8b81872f 156 // from long long
2e403d6d 157 wxLongLongNative(wxLongLong_t ll) : m_ll(ll) { }
8b81872f 158 // from 2 longs
2e403d6d 159 wxLongLongNative(long hi, unsigned long lo) : m_ll(0)
8b81872f
VZ
160 {
161 // assign first to avoid precision loss!
162 m_ll = ((wxLongLong_t) hi) << 32;
163 m_ll |= (wxLongLong_t) lo;
164 }
165
2f02cb89 166 // default copy ctor is ok
8b81872f
VZ
167
168 // no dtor
169
170 // assignment operators
171 // from native 64 bit integer
172 wxLongLongNative& operator=(wxLongLong_t ll)
173 { m_ll = ll; return *this; }
174
cd0b1709
VZ
175 // from double: this one has an explicit name because otherwise we
176 // would have ambiguity with "ll = int" and also because we don't want
177 // to have implicit conversions between doubles and wxLongLongs
178 wxLongLongNative& Assign(double d)
179 { m_ll = (wxLongLong_t)d; return *this; }
180
8b81872f
VZ
181 // assignment operators from wxLongLongNative is ok
182
183 // accessors
184 // get high part
185 long GetHi() const
2f02cb89 186 { return (long)(m_ll >> 32); }
8b81872f
VZ
187 // get low part
188 unsigned long GetLo() const
2f02cb89 189 { return (unsigned long)m_ll; }
8b81872f 190
b76b015e 191 // get absolute value
2ea24d9f 192 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
b76b015e
VZ
193 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
194
8b81872f
VZ
195 // convert to native long long
196 wxLongLong_t GetValue() const { return m_ll; }
197
1ef54dcf
VZ
198 // convert to long with range checking in the debug mode (only!)
199 long ToLong() const
200 {
201 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
202 _T("wxLongLong to long conversion loss of precision") );
203
204 return (long)m_ll;
205 }
206
2f02cb89
VZ
207 // don't provide implicit conversion to wxLongLong_t or we will have an
208 // ambiguity for all arithmetic operations
b76b015e 209 //operator wxLongLong_t() const { return m_ll; }
8b81872f
VZ
210
211 // operations
212 // addition
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 increment
224 wxLongLongNative& operator++()
225 { m_ll++; return *this; }
226
227 // post increment
2e403d6d
GD
228 wxLongLongNative operator++(int)
229 { wxLongLongNative value(*this); m_ll++; return value; }
8b81872f
VZ
230
231 // negation operator
232 wxLongLongNative operator-() const
233 { return wxLongLongNative(-m_ll); }
3a994742 234 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
8b81872f
VZ
235
236 // subtraction
237 wxLongLongNative operator-(const wxLongLongNative& ll) const
238 { return wxLongLongNative(m_ll - ll.m_ll); }
239 wxLongLongNative& operator-=(const wxLongLongNative& ll)
240 { m_ll -= ll.m_ll; return *this; }
241
242 wxLongLongNative operator-(const wxLongLong_t ll) const
243 { return wxLongLongNative(m_ll - ll); }
244 wxLongLongNative& operator-=(const wxLongLong_t ll)
245 { m_ll -= ll; return *this; }
246
247 // pre decrement
248 wxLongLongNative& operator--()
249 { m_ll--; return *this; }
250
251 // post decrement
2e403d6d
GD
252 wxLongLongNative operator--(int)
253 { wxLongLongNative value(*this); m_ll--; return value; }
8b81872f
VZ
254
255 // shifts
256 // left shift
257 wxLongLongNative operator<<(int shift) const
258 { return wxLongLongNative(m_ll << shift);; }
259 wxLongLongNative& operator<<=(int shift)
260 { m_ll <<= shift; return *this; }
261
262 // right shift
263 wxLongLongNative operator>>(int shift) const
264 { return wxLongLongNative(m_ll >> shift);; }
265 wxLongLongNative& operator>>=(int shift)
266 { m_ll >>= shift; return *this; }
267
268 // bitwise operators
269 wxLongLongNative operator&(const wxLongLongNative& ll) const
270 { return wxLongLongNative(m_ll & ll.m_ll); }
271 wxLongLongNative& operator&=(const wxLongLongNative& ll)
272 { m_ll &= ll.m_ll; return *this; }
273
274 wxLongLongNative operator|(const wxLongLongNative& ll) const
275 { return wxLongLongNative(m_ll | ll.m_ll); }
276 wxLongLongNative& operator|=(const wxLongLongNative& ll)
277 { m_ll |= ll.m_ll; return *this; }
278
279 wxLongLongNative operator^(const wxLongLongNative& ll) const
280 { return wxLongLongNative(m_ll ^ ll.m_ll); }
281 wxLongLongNative& operator^=(const wxLongLongNative& ll)
282 { m_ll ^= ll.m_ll; return *this; }
283
b76b015e 284 // multiplication/division
8b81872f
VZ
285 wxLongLongNative operator*(const wxLongLongNative& ll) const
286 { return wxLongLongNative(m_ll * ll.m_ll); }
2f02cb89
VZ
287 wxLongLongNative operator*(long l) const
288 { return wxLongLongNative(m_ll * l); }
8b81872f
VZ
289 wxLongLongNative& operator*=(const wxLongLongNative& ll)
290 { m_ll *= ll.m_ll; return *this; }
2f02cb89
VZ
291 wxLongLongNative& operator*=(long l)
292 { m_ll *= l; return *this; }
8b81872f
VZ
293
294 wxLongLongNative operator/(const wxLongLongNative& ll) const
295 { return wxLongLongNative(m_ll / ll.m_ll); }
b76b015e
VZ
296 wxLongLongNative operator/(long l) const
297 { return wxLongLongNative(m_ll / l); }
8b81872f
VZ
298 wxLongLongNative& operator/=(const wxLongLongNative& ll)
299 { m_ll /= ll.m_ll; return *this; }
2f02cb89
VZ
300 wxLongLongNative& operator/=(long l)
301 { m_ll /= l; return *this; }
8b81872f
VZ
302
303 wxLongLongNative operator%(const wxLongLongNative& ll) const
304 { return wxLongLongNative(m_ll % ll.m_ll); }
b76b015e
VZ
305 wxLongLongNative operator%(long l) const
306 { return wxLongLongNative(m_ll % l); }
8b81872f
VZ
307
308 // comparison
309 bool operator==(const wxLongLongNative& ll) const
310 { return m_ll == ll.m_ll; }
b76b015e
VZ
311 bool operator==(long l) const
312 { return m_ll == l; }
8b81872f
VZ
313 bool operator!=(const wxLongLongNative& ll) const
314 { return m_ll != ll.m_ll; }
b76b015e
VZ
315 bool operator!=(long l) const
316 { return m_ll != l; }
8b81872f
VZ
317 bool operator<(const wxLongLongNative& ll) const
318 { return m_ll < ll.m_ll; }
b76b015e
VZ
319 bool operator<(long l) const
320 { return m_ll < l; }
8b81872f
VZ
321 bool operator>(const wxLongLongNative& ll) const
322 { return m_ll > ll.m_ll; }
b76b015e
VZ
323 bool operator>(long l) const
324 { return m_ll > l; }
8b81872f
VZ
325 bool operator<=(const wxLongLongNative& ll) const
326 { return m_ll <= ll.m_ll; }
b76b015e
VZ
327 bool operator<=(long l) const
328 { return m_ll <= l; }
8b81872f
VZ
329 bool operator>=(const wxLongLongNative& ll) const
330 { return m_ll >= ll.m_ll; }
b76b015e
VZ
331 bool operator>=(long l) const
332 { return m_ll >= l; }
8b81872f
VZ
333
334 // miscellaneous
3a994742
VZ
335
336 // return the string representation of this number
337 wxString ToString() const;
338
8b81872f
VZ
339 // conversion to byte array: returns a pointer to static buffer!
340 void *asArray() const;
341
fcc3d7cb 342#if wxUSE_STD_IOSTREAM
8b81872f 343 // input/output
dd107c50 344 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
fcc3d7cb 345#endif
8b81872f
VZ
346
347private:
348 wxLongLong_t m_ll;
349};
350
8e38fd1f
RL
351
352class WXDLLEXPORT wxULongLongNative
353{
354public:
355 // ctors
356 // default ctor initializes to 0
2e403d6d 357 wxULongLongNative() : m_ll(0) { }
8e38fd1f 358 // from long long
2e403d6d 359 wxULongLongNative(unsigned wxLongLong_t ll) : m_ll(ll) { }
8e38fd1f 360 // from 2 longs
2e403d6d 361 wxULongLongNative(unsigned long hi, unsigned long lo) : m_ll(0)
8e38fd1f
RL
362 {
363 // assign first to avoid precision loss!
364 m_ll = ((unsigned wxLongLong_t) hi) << 32;
365 m_ll |= (unsigned wxLongLong_t) lo;
366 }
367
368 // default copy ctor is ok
369
370 // no dtor
371
372 // assignment operators
373 // from native 64 bit integer
374 wxULongLongNative& operator=(unsigned wxLongLong_t ll)
375 { m_ll = ll; return *this; }
376
377 // assignment operators from wxULongLongNative is ok
378
379 // accessors
380 // get high part
381 unsigned long GetHi() const
382 { return (unsigned long)(m_ll >> 32); }
383 // get low part
384 unsigned long GetLo() const
385 { return (unsigned long)m_ll; }
386
387 // convert to native ulong long
388 unsigned wxLongLong_t GetValue() const { return m_ll; }
389
390 // convert to ulong with range checking in the debug mode (only!)
391 unsigned long ToULong() const
392 {
0ba9f867 393 wxASSERT_MSG( m_ll <= LONG_MAX,
8e38fd1f
RL
394 _T("wxULongLong to long conversion loss of precision") );
395
396 return (unsigned long)m_ll;
397 }
398
8e38fd1f
RL
399 // operations
400 // addition
401 wxULongLongNative operator+(const wxULongLongNative& ll) const
402 { return wxULongLongNative(m_ll + ll.m_ll); }
403 wxULongLongNative& operator+=(const wxULongLongNative& ll)
404 { m_ll += ll.m_ll; return *this; }
405
406 wxULongLongNative operator+(const unsigned wxLongLong_t ll) const
407 { return wxULongLongNative(m_ll + ll); }
408 wxULongLongNative& operator+=(const unsigned wxLongLong_t ll)
409 { m_ll += ll; return *this; }
410
411 // pre increment
412 wxULongLongNative& operator++()
413 { m_ll++; return *this; }
414
415 // post increment
2e403d6d
GD
416 wxULongLongNative operator++(int)
417 { wxULongLongNative value(*this); m_ll++; return value; }
8e38fd1f
RL
418
419 // subtraction
420 wxULongLongNative operator-(const wxULongLongNative& ll) const
421 { return wxULongLongNative(m_ll - ll.m_ll); }
422 wxULongLongNative& operator-=(const wxULongLongNative& ll)
423 { m_ll -= ll.m_ll; return *this; }
424
425 wxULongLongNative operator-(const unsigned wxLongLong_t ll) const
426 { return wxULongLongNative(m_ll - ll); }
427 wxULongLongNative& operator-=(const unsigned wxLongLong_t ll)
428 { m_ll -= ll; return *this; }
429
430 // pre decrement
431 wxULongLongNative& operator--()
432 { m_ll--; return *this; }
433
434 // post decrement
2e403d6d
GD
435 wxULongLongNative operator--(int)
436 { wxULongLongNative value(*this); m_ll--; return value; }
8e38fd1f
RL
437
438 // shifts
439 // left shift
440 wxULongLongNative operator<<(int shift) const
441 { return wxULongLongNative(m_ll << shift);; }
442 wxULongLongNative& operator<<=(int shift)
443 { m_ll <<= shift; return *this; }
444
445 // right shift
446 wxULongLongNative operator>>(int shift) const
447 { return wxULongLongNative(m_ll >> shift);; }
448 wxULongLongNative& operator>>=(int shift)
449 { m_ll >>= shift; return *this; }
450
451 // bitwise operators
452 wxULongLongNative operator&(const wxULongLongNative& ll) const
453 { return wxULongLongNative(m_ll & ll.m_ll); }
454 wxULongLongNative& operator&=(const wxULongLongNative& ll)
455 { m_ll &= ll.m_ll; return *this; }
456
457 wxULongLongNative operator|(const wxULongLongNative& ll) const
458 { return wxULongLongNative(m_ll | ll.m_ll); }
459 wxULongLongNative& operator|=(const wxULongLongNative& ll)
460 { m_ll |= ll.m_ll; return *this; }
461
462 wxULongLongNative operator^(const wxULongLongNative& ll) const
463 { return wxULongLongNative(m_ll ^ ll.m_ll); }
464 wxULongLongNative& operator^=(const wxULongLongNative& ll)
465 { m_ll ^= ll.m_ll; return *this; }
466
467 // multiplication/division
468 wxULongLongNative operator*(const wxULongLongNative& ll) const
469 { return wxULongLongNative(m_ll * ll.m_ll); }
470 wxULongLongNative operator*(unsigned long l) const
471 { return wxULongLongNative(m_ll * l); }
472 wxULongLongNative& operator*=(const wxULongLongNative& ll)
473 { m_ll *= ll.m_ll; return *this; }
474 wxULongLongNative& operator*=(unsigned long l)
475 { m_ll *= l; return *this; }
476
477 wxULongLongNative operator/(const wxULongLongNative& ll) const
478 { return wxULongLongNative(m_ll / ll.m_ll); }
479 wxULongLongNative operator/(unsigned long l) const
480 { return wxULongLongNative(m_ll / l); }
481 wxULongLongNative& operator/=(const wxULongLongNative& ll)
482 { m_ll /= ll.m_ll; return *this; }
483 wxULongLongNative& operator/=(unsigned long l)
484 { m_ll /= l; return *this; }
485
486 wxULongLongNative operator%(const wxULongLongNative& ll) const
487 { return wxULongLongNative(m_ll % ll.m_ll); }
488 wxULongLongNative operator%(unsigned long l) const
489 { return wxULongLongNative(m_ll % l); }
490
491 // comparison
492 bool operator==(const wxULongLongNative& ll) const
493 { return m_ll == ll.m_ll; }
494 bool operator==(unsigned long l) const
495 { return m_ll == l; }
496 bool operator!=(const wxULongLongNative& ll) const
497 { return m_ll != ll.m_ll; }
498 bool operator!=(unsigned long l) const
499 { return m_ll != l; }
500 bool operator<(const wxULongLongNative& ll) const
501 { return m_ll < ll.m_ll; }
502 bool operator<(unsigned long l) const
503 { return m_ll < l; }
504 bool operator>(const wxULongLongNative& ll) const
505 { return m_ll > ll.m_ll; }
506 bool operator>(unsigned long l) const
507 { return m_ll > l; }
508 bool operator<=(const wxULongLongNative& ll) const
509 { return m_ll <= ll.m_ll; }
510 bool operator<=(unsigned long l) const
511 { return m_ll <= l; }
512 bool operator>=(const wxULongLongNative& ll) const
513 { return m_ll >= ll.m_ll; }
514 bool operator>=(unsigned long l) const
515 { return m_ll >= l; }
516
517 // miscellaneous
518
519 // return the string representation of this number
520 wxString ToString() const;
521
522 // conversion to byte array: returns a pointer to static buffer!
523 void *asArray() const;
524
525#if wxUSE_STD_IOSTREAM
526 // input/output
527 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
528#endif
529
530private:
531 unsigned wxLongLong_t m_ll;
532};
533
8b81872f
VZ
534#endif // wxUSE_LONGLONG_NATIVE
535
536#if wxUSE_LONGLONG_WX
537
538class WXDLLEXPORT wxLongLongWx
539{
540public:
541 // ctors
542 // default ctor initializes to 0
2a310492
VZ
543 wxLongLongWx()
544 {
545 m_lo = m_hi = 0;
546
547#ifdef wxLONGLONG_TEST_MODE
548 m_ll = 0;
549
550 Check();
551#endif // wxLONGLONG_TEST_MODE
552 }
8b81872f 553 // from long
2a310492 554 wxLongLongWx(long l) { *this = l; }
8b81872f
VZ
555 // from 2 longs
556 wxLongLongWx(long hi, unsigned long lo)
2a310492
VZ
557 {
558 m_hi = hi;
559 m_lo = lo;
560
561#ifdef wxLONGLONG_TEST_MODE
562 m_ll = hi;
563 m_ll <<= 32;
564 m_ll |= lo;
565
566 Check();
567#endif // wxLONGLONG_TEST_MODE
568 }
8b81872f
VZ
569
570 // default copy ctor is ok in both cases
571
572 // no dtor
573
574 // assignment operators
575 // from long
576 wxLongLongWx& operator=(long l)
2a310492
VZ
577 {
578 m_lo = l;
579 m_hi = (l < 0 ? -1l : 0l);
580
581#ifdef wxLONGLONG_TEST_MODE
582 m_ll = l;
583
584 Check();
585#endif // wxLONGLONG_TEST_MODE
586
587 return *this;
588 }
cd0b1709
VZ
589 // from double
590 wxLongLongWx& Assign(double d);
8b81872f
VZ
591 // can't have assignment operator from 2 longs
592
593 // accessors
594 // get high part
595 long GetHi() const { return m_hi; }
596 // get low part
597 unsigned long GetLo() const { return m_lo; }
598
cd0b1709 599 // get absolute value
2ea24d9f 600 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
2a310492
VZ
601 wxLongLongWx& Abs()
602 {
603 if ( m_hi < 0 )
604 m_hi = -m_hi;
605
606#ifdef wxLONGLONG_TEST_MODE
607 if ( m_ll < 0 )
608 m_ll = -m_ll;
609
610 Check();
611#endif // wxLONGLONG_TEST_MODE
612
613 return *this;
614 }
cd0b1709
VZ
615
616 // convert to long with range checking in the debug mode (only!)
617 long ToLong() const
618 {
2a310492 619 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
cd0b1709
VZ
620 _T("wxLongLong to long conversion loss of precision") );
621
622 return (long)m_lo;
623 }
624
8b81872f
VZ
625 // operations
626 // addition
627 wxLongLongWx operator+(const wxLongLongWx& ll) const;
628 wxLongLongWx& operator+=(const wxLongLongWx& ll);
629 wxLongLongWx operator+(long l) const;
630 wxLongLongWx& operator+=(long l);
631
632 // pre increment operator
633 wxLongLongWx& operator++();
634
635 // post increment operator
2a310492 636 wxLongLongWx& operator++(int) { return ++(*this); }
8b81872f
VZ
637
638 // negation operator
639 wxLongLongWx operator-() const;
2a310492 640 wxLongLongWx& Negate();
8b81872f
VZ
641
642 // subraction
643 wxLongLongWx operator-(const wxLongLongWx& ll) const;
644 wxLongLongWx& operator-=(const wxLongLongWx& ll);
645
646 // pre decrement operator
647 wxLongLongWx& operator--();
648
649 // post decrement operator
2a310492 650 wxLongLongWx& operator--(int) { return --(*this); }
8b81872f
VZ
651
652 // shifts
653 // left shift
654 wxLongLongWx operator<<(int shift) const;
655 wxLongLongWx& operator<<=(int shift);
656
657 // right shift
658 wxLongLongWx operator>>(int shift) const;
659 wxLongLongWx& operator>>=(int shift);
660
661 // bitwise operators
662 wxLongLongWx operator&(const wxLongLongWx& ll) const;
663 wxLongLongWx& operator&=(const wxLongLongWx& ll);
664 wxLongLongWx operator|(const wxLongLongWx& ll) const;
665 wxLongLongWx& operator|=(const wxLongLongWx& ll);
666 wxLongLongWx operator^(const wxLongLongWx& ll) const;
667 wxLongLongWx& operator^=(const wxLongLongWx& ll);
668 wxLongLongWx operator~() const;
669
670 // comparison
2ea24d9f
VZ
671 bool operator==(const wxLongLongWx& ll) const
672 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
673 bool operator!=(const wxLongLongWx& ll) const
674 { return !(*this == ll); }
8b81872f
VZ
675 bool operator<(const wxLongLongWx& ll) const;
676 bool operator>(const wxLongLongWx& ll) const;
2ea24d9f
VZ
677 bool operator<=(const wxLongLongWx& ll) const
678 { return *this < ll || *this == ll; }
679 bool operator>=(const wxLongLongWx& ll) const
680 { return *this > ll || *this == ll; }
8b81872f 681
f6bcfd97
BP
682 bool operator<(long l) const { return *this < wxLongLongWx(l); }
683 bool operator>(long l) const { return *this > wxLongLongWx(l); }
684 bool operator==(long l) const
685 {
686 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
687 : (m_hi == -1 && m_lo == (unsigned long)l);
688 }
689
690 bool operator<=(long l) const { return *this < l || *this == l; }
691 bool operator>=(long l) const { return *this > l || *this == l; }
692
8b81872f
VZ
693 // multiplication
694 wxLongLongWx operator*(const wxLongLongWx& ll) const;
695 wxLongLongWx& operator*=(const wxLongLongWx& ll);
8b81872f
VZ
696
697 // division
cd0b1709
VZ
698 wxLongLongWx operator/(const wxLongLongWx& ll) const;
699 wxLongLongWx& operator/=(const wxLongLongWx& ll);
700
701 wxLongLongWx operator%(const wxLongLongWx& ll) const;
702
8b81872f
VZ
703 void Divide(const wxLongLongWx& divisor,
704 wxLongLongWx& quotient,
705 wxLongLongWx& remainder) const;
706
707 // input/output
3a994742
VZ
708
709 // return the string representation of this number
710 wxString ToString() const;
711
712 void *asArray() const;
713
cd0b1709 714#if wxUSE_STD_IOSTREAM
dd107c50 715 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
fcc3d7cb 716#endif // wxUSE_STD_IOSTREAM
8b81872f
VZ
717
718private:
719 // long is at least 32 bits, so represent our 64bit number as 2 longs
720
721 long m_hi; // signed bit is in the high part
722 unsigned long m_lo;
2a310492
VZ
723
724#ifdef wxLONGLONG_TEST_MODE
725 void Check()
726 {
727 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
728 }
729
730 wxLongLong_t m_ll;
731#endif // wxLONGLONG_TEST_MODE
8b81872f
VZ
732};
733
8e38fd1f
RL
734
735class WXDLLEXPORT wxULongLongWx
736{
737public:
738 // ctors
739 // default ctor initializes to 0
740 wxULongLongWx()
741 {
742 m_lo = m_hi = 0;
743
744#ifdef wxLONGLONG_TEST_MODE
745 m_ll = 0;
746
747 Check();
748#endif // wxLONGLONG_TEST_MODE
749 }
750 // from ulong
751 wxULongLongWx(unsigned long l) { *this = l; }
752 // from 2 ulongs
753 wxULongLongWx(unsigned long hi, unsigned long lo)
754 {
755 m_hi = hi;
756 m_lo = lo;
757
758#ifdef wxLONGLONG_TEST_MODE
759 m_ll = hi;
760 m_ll <<= 32;
761 m_ll |= lo;
762
763 Check();
764#endif // wxLONGLONG_TEST_MODE
765 }
766
767 // default copy ctor is ok in both cases
768
769 // no dtor
770
771 // assignment operators
772 // from long
773 wxULongLongWx& operator=(unsigned long l)
774 {
775 m_lo = l;
776 m_hi = 0;
777
778#ifdef wxLONGLONG_TEST_MODE
779 m_ll = l;
780
781 Check();
782#endif // wxLONGLONG_TEST_MODE
783
784 return *this;
785 }
786
787 // can't have assignment operator from 2 longs
788
789 // accessors
790 // get high part
791 unsigned long GetHi() const { return m_hi; }
792 // get low part
793 unsigned long GetLo() const { return m_lo; }
794
795 // convert to long with range checking in the debug mode (only!)
796 unsigned long ToULong() const
797 {
d7997e1a 798 wxASSERT_MSG( m_hi == 0ul,
8e38fd1f
RL
799 _T("wxULongLong to long conversion loss of precision") );
800
801 return (unsigned long)m_lo;
802 }
803
804 // operations
805 // addition
806 wxULongLongWx operator+(const wxULongLongWx& ll) const;
807 wxULongLongWx& operator+=(const wxULongLongWx& ll);
808 wxULongLongWx operator+(unsigned long l) const;
809 wxULongLongWx& operator+=(unsigned long l);
810
811 // pre increment operator
812 wxULongLongWx& operator++();
813
814 // post increment operator
815 wxULongLongWx& operator++(int) { return ++(*this); }
816
d7997e1a 817 // subraction (FIXME: should return wxLongLong)
8e38fd1f
RL
818 wxULongLongWx operator-(const wxULongLongWx& ll) const;
819 wxULongLongWx& operator-=(const wxULongLongWx& ll);
820
821 // pre decrement operator
822 wxULongLongWx& operator--();
823
824 // post decrement operator
825 wxULongLongWx& operator--(int) { return --(*this); }
826
827 // shifts
828 // left shift
829 wxULongLongWx operator<<(int shift) const;
830 wxULongLongWx& operator<<=(int shift);
831
832 // right shift
833 wxULongLongWx operator>>(int shift) const;
834 wxULongLongWx& operator>>=(int shift);
835
836 // bitwise operators
837 wxULongLongWx operator&(const wxULongLongWx& ll) const;
838 wxULongLongWx& operator&=(const wxULongLongWx& ll);
839 wxULongLongWx operator|(const wxULongLongWx& ll) const;
840 wxULongLongWx& operator|=(const wxULongLongWx& ll);
841 wxULongLongWx operator^(const wxULongLongWx& ll) const;
842 wxULongLongWx& operator^=(const wxULongLongWx& ll);
843 wxULongLongWx operator~() const;
844
845 // comparison
846 bool operator==(const wxULongLongWx& ll) const
847 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
848 bool operator!=(const wxULongLongWx& ll) const
849 { return !(*this == ll); }
850 bool operator<(const wxULongLongWx& ll) const;
851 bool operator>(const wxULongLongWx& ll) const;
852 bool operator<=(const wxULongLongWx& ll) const
853 { return *this < ll || *this == ll; }
854 bool operator>=(const wxULongLongWx& ll) const
855 { return *this > ll || *this == ll; }
856
857 bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
858 bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
859 bool operator==(unsigned long l) const
860 {
861 return (m_hi == 0 && m_lo == (unsigned long)l);
862 }
863
864 bool operator<=(unsigned long l) const { return *this < l || *this == l; }
865 bool operator>=(unsigned long l) const { return *this > l || *this == l; }
866
867 // multiplication
868 wxULongLongWx operator*(const wxULongLongWx& ll) const;
869 wxULongLongWx& operator*=(const wxULongLongWx& ll);
870
871 // division
872 wxULongLongWx operator/(const wxULongLongWx& ll) const;
873 wxULongLongWx& operator/=(const wxULongLongWx& ll);
874
875 wxULongLongWx operator%(const wxULongLongWx& ll) const;
876
877 void Divide(const wxULongLongWx& divisor,
878 wxULongLongWx& quotient,
879 wxULongLongWx& remainder) const;
880
881 // input/output
882
883 // return the string representation of this number
884 wxString ToString() const;
885
886 void *asArray() const;
887
888#if wxUSE_STD_IOSTREAM
889 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
890#endif // wxUSE_STD_IOSTREAM
891
892private:
893 // long is at least 32 bits, so represent our 64bit number as 2 longs
894
d2ce649b 895 unsigned long m_hi;
8e38fd1f
RL
896 unsigned long m_lo;
897
898#ifdef wxLONGLONG_TEST_MODE
899 void Check()
900 {
901 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
902 }
903
904 unsigned wxLongLong_t m_ll;
905#endif // wxLONGLONG_TEST_MODE
906};
907
8b81872f
VZ
908#endif // wxUSE_LONGLONG_WX
909
2ea24d9f
VZ
910// ----------------------------------------------------------------------------
911// binary operators
912// ----------------------------------------------------------------------------
913
6d56eb5c 914inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
d7997e1a
VZ
915inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
916inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
917inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
918inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
919inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
b01fb5c3 920
d7997e1a 921inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
b01fb5c3
VZ
922inline wxLongLong operator-(long l, const wxLongLong& ll)
923{
d7997e1a 924 return wxLongLong(l) - ll;
b01fb5c3 925}
2ea24d9f 926
d7997e1a
VZ
927inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
928inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
929inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
930inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
931inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
932inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
933
934inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
8e38fd1f 935
d7997e1a
VZ
936// FIXME: this should return wxLongLong
937inline wxULongLong operator-(unsigned long l, const wxULongLong& ull)
938{
939 return wxULongLong(l) - ull;
940}
8e38fd1f 941
8b81872f 942#endif // _WX_LONGLONG_H