]> git.saurik.com Git - wxWidgets.git/blame - include/wx/longlong.h
Implement SelectAll for OSX WebKit.
[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>
65571936 10// Licence: wxWindows licence
8b81872f
VZ
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef _WX_LONGLONG_H
14#define _WX_LONGLONG_H
15
1ef54dcf 16#include "wx/defs.h"
216a72f3
VZ
17
18#if wxUSE_LONGLONG
19
3a994742 20#include "wx/string.h"
1ef54dcf
VZ
21
22#include <limits.h> // for LONG_MAX
23
2a310492
VZ
24// define this to compile wxLongLongWx in "test" mode: the results of all
25// calculations will be compared with the real results taken from
617ec456
VZ
26// wxLongLongNative -- this is extremely useful to find the bugs in
27// wxLongLongWx class!
28
f6bcfd97 29// #define wxLONGLONG_TEST_MODE
2a310492
VZ
30
31#ifdef wxLONGLONG_TEST_MODE
32 #define wxUSE_LONGLONG_WX 1
33 #define wxUSE_LONGLONG_NATIVE 1
34#endif // wxLONGLONG_TEST_MODE
2ea24d9f 35
8b81872f
VZ
36// ----------------------------------------------------------------------------
37// decide upon which class we will use
38// ----------------------------------------------------------------------------
39
8fb37472 40#ifndef wxLongLong_t
9c2882d9 41 // both warning and pragma warning are not portable, but at least an
2b5f62a0 42 // unknown pragma should never be an error -- except that, actually, some
58ef67ab
VZ
43 // broken compilers don't like it, so we have to disable it in this case
44 // <sigh>
216a72f3
VZ
45 #ifdef __GNUC__
46 #warning "Your compiler does not appear to support 64 bit "\
47 "integers, using emulation class instead.\n" \
48 "Please report your compiler version to " \
49 "wx-dev@lists.wxwidgets.org!"
50 #elif !(defined(__WATCOMC__) || defined(__VISAGECPP__))
8fb37472
VZ
51 #pragma warning "Your compiler does not appear to support 64 bit "\
52 "integers, using emulation class instead.\n" \
53 "Please report your compiler version to " \
77ffb593 54 "wx-dev@lists.wxwidgets.org!"
8fb37472 55 #endif
8db6ac55 56
8b81872f
VZ
57 #define wxUSE_LONGLONG_WX 1
58#endif // compiler
59
60// the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
61// to disable automatic testing (useful for the test program which defines
62// both classes) but by default we only use one class
2ea24d9f 63#if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
2a310492 64 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
384223b3 65 // this is useful in test programs and only there
2a310492
VZ
66 #ifndef wxUSE_LONGLONG_NATIVE
67 #define wxUSE_LONGLONG_NATIVE 0
68 #endif
69
b5dbe15d
VS
70 class WXDLLIMPEXP_FWD_BASE wxLongLongWx;
71 class WXDLLIMPEXP_FWD_BASE wxULongLongWx;
92f5ff59
JS
72#if defined(__VISUALC__) && !defined(__WIN32__)
73 #define wxLongLong wxLongLongWx
8e38fd1f 74 #define wxULongLong wxULongLongWx
92f5ff59 75#else
8b81872f 76 typedef wxLongLongWx wxLongLong;
8e38fd1f 77 typedef wxULongLongWx wxULongLong;
92f5ff59
JS
78#endif
79
b76b015e
VZ
80#else
81 // if nothing is defined, use native implementation by default, of course
82 #ifndef wxUSE_LONGLONG_NATIVE
83 #define wxUSE_LONGLONG_NATIVE 1
84 #endif
8b81872f
VZ
85#endif
86
87#ifndef wxUSE_LONGLONG_WX
88 #define wxUSE_LONGLONG_WX 0
b5dbe15d
VS
89 class WXDLLIMPEXP_FWD_BASE wxLongLongNative;
90 class WXDLLIMPEXP_FWD_BASE wxULongLongNative;
8b81872f 91 typedef wxLongLongNative wxLongLong;
8e38fd1f 92 typedef wxULongLongNative wxULongLong;
8b81872f
VZ
93#endif
94
95// NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
96// typedef wxLongLong as it wants, we don't do it
97
98// ----------------------------------------------------------------------------
99// choose the appropriate class
100// ----------------------------------------------------------------------------
101
102// we use iostream for wxLongLong output
65f19af1 103#include "wx/iosfwrap.h"
8b81872f
VZ
104
105#if wxUSE_LONGLONG_NATIVE
106
bddd7a8d 107class WXDLLIMPEXP_BASE wxLongLongNative
8b81872f
VZ
108{
109public:
110 // ctors
111 // default ctor initializes to 0
2e403d6d 112 wxLongLongNative() : m_ll(0) { }
8b81872f 113 // from long long
2e403d6d 114 wxLongLongNative(wxLongLong_t ll) : m_ll(ll) { }
8b81872f 115 // from 2 longs
05059b3a 116 wxLongLongNative(wxInt32 hi, wxUint32 lo)
8b81872f 117 {
05059b3a 118 // cast to wxLongLong_t first to avoid precision loss!
8b81872f
VZ
119 m_ll = ((wxLongLong_t) hi) << 32;
120 m_ll |= (wxLongLong_t) lo;
121 }
842215bb
WS
122#if wxUSE_LONGLONG_WX
123 wxLongLongNative(wxLongLongWx ll);
124#endif
8b81872f 125
2f02cb89 126 // default copy ctor is ok
8b81872f
VZ
127
128 // no dtor
129
130 // assignment operators
131 // from native 64 bit integer
38b2e0de 132#ifndef wxLongLongIsLong
8b81872f
VZ
133 wxLongLongNative& operator=(wxLongLong_t ll)
134 { m_ll = ll; return *this; }
216a72f3
VZ
135 wxLongLongNative& operator=(wxULongLong_t ll)
136 { m_ll = ll; return *this; }
38b2e0de 137#endif // !wxLongLongNative
216a72f3 138 wxLongLongNative& operator=(const wxULongLongNative &ll);
a4d2944e
VZ
139 wxLongLongNative& operator=(int l)
140 { m_ll = l; return *this; }
216a72f3
VZ
141 wxLongLongNative& operator=(long l)
142 { m_ll = l; return *this; }
62675f1e
VZ
143 wxLongLongNative& operator=(unsigned int l)
144 { m_ll = l; return *this; }
216a72f3
VZ
145 wxLongLongNative& operator=(unsigned long l)
146 { m_ll = l; return *this; }
842215bb
WS
147#if wxUSE_LONGLONG_WX
148 wxLongLongNative& operator=(wxLongLongWx ll);
216a72f3 149 wxLongLongNative& operator=(const class wxULongLongWx &ll);
842215bb
WS
150#endif
151
8b81872f 152
cd0b1709
VZ
153 // from double: this one has an explicit name because otherwise we
154 // would have ambiguity with "ll = int" and also because we don't want
155 // to have implicit conversions between doubles and wxLongLongs
156 wxLongLongNative& Assign(double d)
157 { m_ll = (wxLongLong_t)d; return *this; }
158
8b81872f
VZ
159 // assignment operators from wxLongLongNative is ok
160
161 // accessors
162 // get high part
05059b3a
VZ
163 wxInt32 GetHi() const
164 { return wx_truncate_cast(wxInt32, m_ll >> 32); }
8b81872f 165 // get low part
05059b3a
VZ
166 wxUint32 GetLo() const
167 { return wx_truncate_cast(wxUint32, m_ll); }
8b81872f 168
b76b015e 169 // get absolute value
2ea24d9f 170 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
b76b015e
VZ
171 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
172
8b81872f
VZ
173 // convert to native long long
174 wxLongLong_t GetValue() const { return m_ll; }
175
90e572f1 176 // convert to long with range checking in debug mode (only!)
1ef54dcf
VZ
177 long ToLong() const
178 {
179 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
9a83f860 180 wxT("wxLongLong to long conversion loss of precision") );
1ef54dcf 181
17a1ebd1 182 return wx_truncate_cast(long, m_ll);
1ef54dcf
VZ
183 }
184
e7aaf2de 185 // convert to double
17a1ebd1 186 double ToDouble() const { return wx_truncate_cast(double, m_ll); }
e7aaf2de 187
2f02cb89
VZ
188 // don't provide implicit conversion to wxLongLong_t or we will have an
189 // ambiguity for all arithmetic operations
b76b015e 190 //operator wxLongLong_t() const { return m_ll; }
8b81872f
VZ
191
192 // operations
193 // addition
194 wxLongLongNative operator+(const wxLongLongNative& ll) const
195 { return wxLongLongNative(m_ll + ll.m_ll); }
196 wxLongLongNative& operator+=(const wxLongLongNative& ll)
197 { m_ll += ll.m_ll; return *this; }
198
199 wxLongLongNative operator+(const wxLongLong_t ll) const
200 { return wxLongLongNative(m_ll + ll); }
201 wxLongLongNative& operator+=(const wxLongLong_t ll)
202 { m_ll += ll; return *this; }
203
204 // pre increment
205 wxLongLongNative& operator++()
206 { m_ll++; return *this; }
207
208 // post increment
2e403d6d
GD
209 wxLongLongNative operator++(int)
210 { wxLongLongNative value(*this); m_ll++; return value; }
8b81872f
VZ
211
212 // negation operator
213 wxLongLongNative operator-() const
214 { return wxLongLongNative(-m_ll); }
3a994742 215 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
8b81872f
VZ
216
217 // subtraction
218 wxLongLongNative operator-(const wxLongLongNative& ll) const
219 { return wxLongLongNative(m_ll - ll.m_ll); }
220 wxLongLongNative& operator-=(const wxLongLongNative& ll)
221 { m_ll -= ll.m_ll; return *this; }
222
223 wxLongLongNative operator-(const wxLongLong_t ll) const
224 { return wxLongLongNative(m_ll - ll); }
225 wxLongLongNative& operator-=(const wxLongLong_t ll)
226 { m_ll -= ll; return *this; }
227
228 // pre decrement
229 wxLongLongNative& operator--()
230 { m_ll--; return *this; }
231
232 // post decrement
2e403d6d
GD
233 wxLongLongNative operator--(int)
234 { wxLongLongNative value(*this); m_ll--; return value; }
8b81872f
VZ
235
236 // shifts
237 // left shift
238 wxLongLongNative operator<<(int shift) const
d0ee33f5 239 { return wxLongLongNative(m_ll << shift); }
8b81872f
VZ
240 wxLongLongNative& operator<<=(int shift)
241 { m_ll <<= shift; return *this; }
242
243 // right shift
244 wxLongLongNative operator>>(int shift) const
d0ee33f5 245 { return wxLongLongNative(m_ll >> shift); }
8b81872f
VZ
246 wxLongLongNative& operator>>=(int shift)
247 { m_ll >>= shift; return *this; }
248
249 // bitwise operators
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 wxLongLongNative operator^(const wxLongLongNative& ll) const
261 { return wxLongLongNative(m_ll ^ ll.m_ll); }
262 wxLongLongNative& operator^=(const wxLongLongNative& ll)
263 { m_ll ^= ll.m_ll; return *this; }
264
b76b015e 265 // multiplication/division
8b81872f
VZ
266 wxLongLongNative operator*(const wxLongLongNative& ll) const
267 { return wxLongLongNative(m_ll * ll.m_ll); }
2f02cb89
VZ
268 wxLongLongNative operator*(long l) const
269 { return wxLongLongNative(m_ll * l); }
8b81872f
VZ
270 wxLongLongNative& operator*=(const wxLongLongNative& ll)
271 { m_ll *= ll.m_ll; return *this; }
2f02cb89
VZ
272 wxLongLongNative& operator*=(long l)
273 { m_ll *= l; return *this; }
8b81872f
VZ
274
275 wxLongLongNative operator/(const wxLongLongNative& ll) const
276 { return wxLongLongNative(m_ll / ll.m_ll); }
b76b015e
VZ
277 wxLongLongNative operator/(long l) const
278 { return wxLongLongNative(m_ll / l); }
8b81872f
VZ
279 wxLongLongNative& operator/=(const wxLongLongNative& ll)
280 { m_ll /= ll.m_ll; return *this; }
2f02cb89
VZ
281 wxLongLongNative& operator/=(long l)
282 { m_ll /= l; return *this; }
8b81872f
VZ
283
284 wxLongLongNative operator%(const wxLongLongNative& ll) const
285 { return wxLongLongNative(m_ll % ll.m_ll); }
b76b015e
VZ
286 wxLongLongNative operator%(long l) const
287 { return wxLongLongNative(m_ll % l); }
8b81872f
VZ
288
289 // comparison
290 bool operator==(const wxLongLongNative& ll) const
291 { return m_ll == ll.m_ll; }
b76b015e
VZ
292 bool operator==(long l) const
293 { return m_ll == l; }
8b81872f
VZ
294 bool operator!=(const wxLongLongNative& ll) const
295 { return m_ll != ll.m_ll; }
b76b015e
VZ
296 bool operator!=(long l) const
297 { return m_ll != l; }
8b81872f
VZ
298 bool operator<(const wxLongLongNative& ll) const
299 { return m_ll < ll.m_ll; }
b76b015e
VZ
300 bool operator<(long l) const
301 { return m_ll < l; }
8b81872f
VZ
302 bool operator>(const wxLongLongNative& ll) const
303 { return m_ll > ll.m_ll; }
b76b015e
VZ
304 bool operator>(long l) const
305 { return m_ll > l; }
8b81872f
VZ
306 bool operator<=(const wxLongLongNative& ll) const
307 { return m_ll <= ll.m_ll; }
b76b015e
VZ
308 bool operator<=(long l) const
309 { return m_ll <= l; }
8b81872f
VZ
310 bool operator>=(const wxLongLongNative& ll) const
311 { return m_ll >= ll.m_ll; }
b76b015e
VZ
312 bool operator>=(long l) const
313 { return m_ll >= l; }
8b81872f
VZ
314
315 // miscellaneous
3a994742
VZ
316
317 // return the string representation of this number
318 wxString ToString() const;
319
8b81872f
VZ
320 // conversion to byte array: returns a pointer to static buffer!
321 void *asArray() const;
322
fcc3d7cb 323#if wxUSE_STD_IOSTREAM
8b81872f 324 // input/output
a353dc98
VZ
325 friend WXDLLIMPEXP_BASE
326 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
fcc3d7cb 327#endif
8b81872f 328
a353dc98
VZ
329 friend WXDLLIMPEXP_BASE
330 wxString& operator<<(wxString&, const wxLongLongNative&);
51496782 331
216a72f3
VZ
332#if wxUSE_STREAMS
333 friend WXDLLIMPEXP_BASE
334 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongNative&);
335 friend WXDLLIMPEXP_BASE
336 class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongNative&);
337#endif
338
8b81872f
VZ
339private:
340 wxLongLong_t m_ll;
341};
342
8e38fd1f 343
bddd7a8d 344class WXDLLIMPEXP_BASE wxULongLongNative
8e38fd1f
RL
345{
346public:
347 // ctors
348 // default ctor initializes to 0
2e403d6d 349 wxULongLongNative() : m_ll(0) { }
8e38fd1f 350 // from long long
60582201 351 wxULongLongNative(wxULongLong_t ll) : m_ll(ll) { }
8e38fd1f 352 // from 2 longs
05059b3a 353 wxULongLongNative(wxUint32 hi, wxUint32 lo) : m_ll(0)
8e38fd1f 354 {
05059b3a 355 // cast to wxLongLong_t first to avoid precision loss!
60582201
WS
356 m_ll = ((wxULongLong_t) hi) << 32;
357 m_ll |= (wxULongLong_t) lo;
8e38fd1f
RL
358 }
359
216a72f3
VZ
360#if wxUSE_LONGLONG_WX
361 wxULongLongNative(const class wxULongLongWx &ll);
362#endif
363
8e38fd1f
RL
364 // default copy ctor is ok
365
366 // no dtor
367
368 // assignment operators
369 // from native 64 bit integer
38b2e0de 370#ifndef wxLongLongIsLong
60582201 371 wxULongLongNative& operator=(wxULongLong_t ll)
8e38fd1f 372 { m_ll = ll; return *this; }
216a72f3
VZ
373 wxULongLongNative& operator=(wxLongLong_t ll)
374 { m_ll = ll; return *this; }
38b2e0de 375#endif // !wxLongLongNative
a4d2944e
VZ
376 wxULongLongNative& operator=(int l)
377 { m_ll = l; return *this; }
216a72f3
VZ
378 wxULongLongNative& operator=(long l)
379 { m_ll = l; return *this; }
62675f1e
VZ
380 wxULongLongNative& operator=(unsigned int l)
381 { m_ll = l; return *this; }
216a72f3
VZ
382 wxULongLongNative& operator=(unsigned long l)
383 { m_ll = l; return *this; }
384 wxULongLongNative& operator=(const wxLongLongNative &ll)
385 { m_ll = ll.GetValue(); return *this; }
386#if wxUSE_LONGLONG_WX
387 wxULongLongNative& operator=(wxLongLongWx ll);
388 wxULongLongNative& operator=(const class wxULongLongWx &ll);
389#endif
8e38fd1f
RL
390
391 // assignment operators from wxULongLongNative is ok
392
393 // accessors
394 // get high part
05059b3a
VZ
395 wxUint32 GetHi() const
396 { return wx_truncate_cast(wxUint32, m_ll >> 32); }
8e38fd1f 397 // get low part
05059b3a
VZ
398 wxUint32 GetLo() const
399 { return wx_truncate_cast(wxUint32, m_ll); }
8e38fd1f
RL
400
401 // convert to native ulong long
60582201 402 wxULongLong_t GetValue() const { return m_ll; }
8e38fd1f 403
90e572f1 404 // convert to ulong with range checking in debug mode (only!)
8e38fd1f
RL
405 unsigned long ToULong() const
406 {
ebbbd51b 407 wxASSERT_MSG( m_ll <= ULONG_MAX,
9a83f860 408 wxT("wxULongLong to long conversion loss of precision") );
8e38fd1f 409
17a1ebd1 410 return wx_truncate_cast(unsigned long, m_ll);
8e38fd1f
RL
411 }
412
d870a030 413 // convert to double
4c24ca50
VZ
414 //
415 // For some completely obscure reasons compiling the cast below with
416 // VC6 in DLL builds only (!) results in "error C2520: conversion from
417 // unsigned __int64 to double not implemented, use signed __int64" so
418 // we must use a different version for that compiler.
419#ifdef __VISUALC6__
420 double ToDouble() const;
421#else
d870a030 422 double ToDouble() const { return wx_truncate_cast(double, m_ll); }
4c24ca50 423#endif
d870a030 424
8e38fd1f
RL
425 // operations
426 // addition
427 wxULongLongNative operator+(const wxULongLongNative& ll) const
428 { return wxULongLongNative(m_ll + ll.m_ll); }
429 wxULongLongNative& operator+=(const wxULongLongNative& ll)
430 { m_ll += ll.m_ll; return *this; }
431
60582201 432 wxULongLongNative operator+(const wxULongLong_t ll) const
8e38fd1f 433 { return wxULongLongNative(m_ll + ll); }
60582201 434 wxULongLongNative& operator+=(const wxULongLong_t ll)
8e38fd1f
RL
435 { m_ll += ll; return *this; }
436
437 // pre increment
438 wxULongLongNative& operator++()
439 { m_ll++; return *this; }
440
441 // post increment
2e403d6d
GD
442 wxULongLongNative operator++(int)
443 { wxULongLongNative value(*this); m_ll++; return value; }
8e38fd1f
RL
444
445 // subtraction
446 wxULongLongNative operator-(const wxULongLongNative& ll) const
447 { return wxULongLongNative(m_ll - ll.m_ll); }
448 wxULongLongNative& operator-=(const wxULongLongNative& ll)
449 { m_ll -= ll.m_ll; return *this; }
450
60582201 451 wxULongLongNative operator-(const wxULongLong_t ll) const
8e38fd1f 452 { return wxULongLongNative(m_ll - ll); }
60582201 453 wxULongLongNative& operator-=(const wxULongLong_t ll)
8e38fd1f
RL
454 { m_ll -= ll; return *this; }
455
456 // pre decrement
457 wxULongLongNative& operator--()
458 { m_ll--; return *this; }
459
460 // post decrement
2e403d6d
GD
461 wxULongLongNative operator--(int)
462 { wxULongLongNative value(*this); m_ll--; return value; }
8e38fd1f
RL
463
464 // shifts
465 // left shift
466 wxULongLongNative operator<<(int shift) const
d0ee33f5 467 { return wxULongLongNative(m_ll << shift); }
8e38fd1f
RL
468 wxULongLongNative& operator<<=(int shift)
469 { m_ll <<= shift; return *this; }
470
471 // right shift
472 wxULongLongNative operator>>(int shift) const
d0ee33f5 473 { return wxULongLongNative(m_ll >> shift); }
8e38fd1f
RL
474 wxULongLongNative& operator>>=(int shift)
475 { m_ll >>= shift; return *this; }
476
477 // bitwise operators
478 wxULongLongNative operator&(const wxULongLongNative& ll) const
479 { return wxULongLongNative(m_ll & ll.m_ll); }
480 wxULongLongNative& operator&=(const wxULongLongNative& ll)
481 { m_ll &= ll.m_ll; return *this; }
482
483 wxULongLongNative operator|(const wxULongLongNative& ll) const
484 { return wxULongLongNative(m_ll | ll.m_ll); }
485 wxULongLongNative& operator|=(const wxULongLongNative& ll)
486 { m_ll |= ll.m_ll; return *this; }
487
488 wxULongLongNative operator^(const wxULongLongNative& ll) const
489 { return wxULongLongNative(m_ll ^ ll.m_ll); }
490 wxULongLongNative& operator^=(const wxULongLongNative& ll)
491 { m_ll ^= ll.m_ll; return *this; }
492
493 // multiplication/division
494 wxULongLongNative operator*(const wxULongLongNative& ll) const
495 { return wxULongLongNative(m_ll * ll.m_ll); }
496 wxULongLongNative operator*(unsigned long l) const
497 { return wxULongLongNative(m_ll * l); }
498 wxULongLongNative& operator*=(const wxULongLongNative& ll)
499 { m_ll *= ll.m_ll; return *this; }
500 wxULongLongNative& operator*=(unsigned long l)
501 { m_ll *= l; return *this; }
502
503 wxULongLongNative operator/(const wxULongLongNative& ll) const
504 { return wxULongLongNative(m_ll / ll.m_ll); }
505 wxULongLongNative operator/(unsigned long l) const
506 { return wxULongLongNative(m_ll / l); }
507 wxULongLongNative& operator/=(const wxULongLongNative& ll)
508 { m_ll /= ll.m_ll; return *this; }
509 wxULongLongNative& operator/=(unsigned long l)
510 { m_ll /= l; return *this; }
511
512 wxULongLongNative operator%(const wxULongLongNative& ll) const
513 { return wxULongLongNative(m_ll % ll.m_ll); }
514 wxULongLongNative operator%(unsigned long l) const
515 { return wxULongLongNative(m_ll % l); }
516
517 // comparison
518 bool operator==(const wxULongLongNative& ll) const
519 { return m_ll == ll.m_ll; }
520 bool operator==(unsigned long l) const
521 { return m_ll == l; }
522 bool operator!=(const wxULongLongNative& ll) const
523 { return m_ll != ll.m_ll; }
524 bool operator!=(unsigned long l) const
525 { return m_ll != l; }
526 bool operator<(const wxULongLongNative& ll) const
527 { return m_ll < ll.m_ll; }
528 bool operator<(unsigned long l) const
529 { return m_ll < l; }
530 bool operator>(const wxULongLongNative& ll) const
531 { return m_ll > ll.m_ll; }
532 bool operator>(unsigned long l) const
533 { return m_ll > l; }
534 bool operator<=(const wxULongLongNative& ll) const
535 { return m_ll <= ll.m_ll; }
536 bool operator<=(unsigned long l) const
537 { return m_ll <= l; }
538 bool operator>=(const wxULongLongNative& ll) const
539 { return m_ll >= ll.m_ll; }
540 bool operator>=(unsigned long l) const
541 { return m_ll >= l; }
542
543 // miscellaneous
544
545 // return the string representation of this number
546 wxString ToString() const;
547
548 // conversion to byte array: returns a pointer to static buffer!
549 void *asArray() const;
550
551#if wxUSE_STD_IOSTREAM
552 // input/output
7fcdf88f
VZ
553 friend WXDLLIMPEXP_BASE
554 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
8e38fd1f
RL
555#endif
556
7fcdf88f
VZ
557 friend WXDLLIMPEXP_BASE
558 wxString& operator<<(wxString&, const wxULongLongNative&);
51496782 559
216a72f3
VZ
560#if wxUSE_STREAMS
561 friend WXDLLIMPEXP_BASE
562 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongNative&);
563 friend WXDLLIMPEXP_BASE
564 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongNative&);
565#endif
566
8e38fd1f 567private:
60582201 568 wxULongLong_t m_ll;
8e38fd1f
RL
569};
570
216a72f3
VZ
571inline
572wxLongLongNative& wxLongLongNative::operator=(const wxULongLongNative &ll)
573{
574 m_ll = ll.GetValue();
575 return *this;
576}
577
8b81872f
VZ
578#endif // wxUSE_LONGLONG_NATIVE
579
580#if wxUSE_LONGLONG_WX
581
bddd7a8d 582class WXDLLIMPEXP_BASE wxLongLongWx
8b81872f
VZ
583{
584public:
585 // ctors
586 // default ctor initializes to 0
2a310492
VZ
587 wxLongLongWx()
588 {
589 m_lo = m_hi = 0;
590
591#ifdef wxLONGLONG_TEST_MODE
592 m_ll = 0;
593
594 Check();
595#endif // wxLONGLONG_TEST_MODE
596 }
8b81872f 597 // from long
2a310492 598 wxLongLongWx(long l) { *this = l; }
8b81872f
VZ
599 // from 2 longs
600 wxLongLongWx(long hi, unsigned long lo)
2a310492
VZ
601 {
602 m_hi = hi;
603 m_lo = lo;
604
605#ifdef wxLONGLONG_TEST_MODE
606 m_ll = hi;
607 m_ll <<= 32;
608 m_ll |= lo;
609
610 Check();
611#endif // wxLONGLONG_TEST_MODE
612 }
8b81872f
VZ
613
614 // default copy ctor is ok in both cases
615
616 // no dtor
617
618 // assignment operators
619 // from long
620 wxLongLongWx& operator=(long l)
2a310492
VZ
621 {
622 m_lo = l;
623 m_hi = (l < 0 ? -1l : 0l);
624
625#ifdef wxLONGLONG_TEST_MODE
626 m_ll = l;
627
628 Check();
629#endif // wxLONGLONG_TEST_MODE
630
631 return *this;
632 }
a4d2944e
VZ
633 // from int
634 wxLongLongWx& operator=(int l)
635 {
636 return operator=((long)l);
637 }
638
216a72f3
VZ
639 wxLongLongWx& operator=(unsigned long l)
640 {
641 m_lo = l;
642 m_hi = 0;
643
644#ifdef wxLONGLONG_TEST_MODE
645 m_ll = l;
646
647 Check();
648#endif // wxLONGLONG_TEST_MODE
649
650 return *this;
651 }
62675f1e
VZ
652
653 wxLongLongWx& operator=(unsigned int l)
654 {
655 return operator=((unsigned long)l);
656 }
657
216a72f3
VZ
658 wxLongLongWx& operator=(const class wxULongLongWx &ll);
659
660 // from double
cd0b1709 661 wxLongLongWx& Assign(double d);
8b81872f
VZ
662 // can't have assignment operator from 2 longs
663
664 // accessors
665 // get high part
666 long GetHi() const { return m_hi; }
667 // get low part
668 unsigned long GetLo() const { return m_lo; }
669
cd0b1709 670 // get absolute value
2ea24d9f 671 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
2a310492
VZ
672 wxLongLongWx& Abs()
673 {
674 if ( m_hi < 0 )
675 m_hi = -m_hi;
676
677#ifdef wxLONGLONG_TEST_MODE
678 if ( m_ll < 0 )
679 m_ll = -m_ll;
680
681 Check();
682#endif // wxLONGLONG_TEST_MODE
683
684 return *this;
685 }
cd0b1709 686
90e572f1 687 // convert to long with range checking in debug mode (only!)
cd0b1709
VZ
688 long ToLong() const
689 {
2a310492 690 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
9a83f860 691 wxT("wxLongLong to long conversion loss of precision") );
cd0b1709
VZ
692
693 return (long)m_lo;
694 }
695
e7aaf2de
VZ
696 // convert to double
697 double ToDouble() const;
698
8b81872f
VZ
699 // operations
700 // addition
701 wxLongLongWx operator+(const wxLongLongWx& ll) const;
702 wxLongLongWx& operator+=(const wxLongLongWx& ll);
703 wxLongLongWx operator+(long l) const;
704 wxLongLongWx& operator+=(long l);
705
706 // pre increment operator
707 wxLongLongWx& operator++();
708
709 // post increment operator
2a310492 710 wxLongLongWx& operator++(int) { return ++(*this); }
8b81872f
VZ
711
712 // negation operator
713 wxLongLongWx operator-() const;
2a310492 714 wxLongLongWx& Negate();
8b81872f
VZ
715
716 // subraction
717 wxLongLongWx operator-(const wxLongLongWx& ll) const;
718 wxLongLongWx& operator-=(const wxLongLongWx& ll);
719
720 // pre decrement operator
721 wxLongLongWx& operator--();
722
723 // post decrement operator
2a310492 724 wxLongLongWx& operator--(int) { return --(*this); }
8b81872f
VZ
725
726 // shifts
727 // left shift
728 wxLongLongWx operator<<(int shift) const;
729 wxLongLongWx& operator<<=(int shift);
730
731 // right shift
732 wxLongLongWx operator>>(int shift) const;
733 wxLongLongWx& operator>>=(int shift);
734
735 // bitwise operators
736 wxLongLongWx operator&(const wxLongLongWx& ll) const;
737 wxLongLongWx& operator&=(const wxLongLongWx& ll);
738 wxLongLongWx operator|(const wxLongLongWx& ll) const;
739 wxLongLongWx& operator|=(const wxLongLongWx& ll);
740 wxLongLongWx operator^(const wxLongLongWx& ll) const;
741 wxLongLongWx& operator^=(const wxLongLongWx& ll);
742 wxLongLongWx operator~() const;
743
744 // comparison
2ea24d9f
VZ
745 bool operator==(const wxLongLongWx& ll) const
746 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
842215bb
WS
747#if wxUSE_LONGLONG_NATIVE
748 bool operator==(const wxLongLongNative& ll) const
749 { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); }
750#endif
2ea24d9f
VZ
751 bool operator!=(const wxLongLongWx& ll) const
752 { return !(*this == ll); }
8b81872f
VZ
753 bool operator<(const wxLongLongWx& ll) const;
754 bool operator>(const wxLongLongWx& ll) const;
2ea24d9f
VZ
755 bool operator<=(const wxLongLongWx& ll) const
756 { return *this < ll || *this == ll; }
757 bool operator>=(const wxLongLongWx& ll) const
758 { return *this > ll || *this == ll; }
8b81872f 759
f6bcfd97
BP
760 bool operator<(long l) const { return *this < wxLongLongWx(l); }
761 bool operator>(long l) const { return *this > wxLongLongWx(l); }
762 bool operator==(long l) const
763 {
764 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
765 : (m_hi == -1 && m_lo == (unsigned long)l);
766 }
767
768 bool operator<=(long l) const { return *this < l || *this == l; }
769 bool operator>=(long l) const { return *this > l || *this == l; }
770
8b81872f
VZ
771 // multiplication
772 wxLongLongWx operator*(const wxLongLongWx& ll) const;
773 wxLongLongWx& operator*=(const wxLongLongWx& ll);
8b81872f
VZ
774
775 // division
cd0b1709
VZ
776 wxLongLongWx operator/(const wxLongLongWx& ll) const;
777 wxLongLongWx& operator/=(const wxLongLongWx& ll);
778
779 wxLongLongWx operator%(const wxLongLongWx& ll) const;
780
8b81872f
VZ
781 void Divide(const wxLongLongWx& divisor,
782 wxLongLongWx& quotient,
783 wxLongLongWx& remainder) const;
784
785 // input/output
3a994742
VZ
786
787 // return the string representation of this number
788 wxString ToString() const;
789
790 void *asArray() const;
791
cd0b1709 792#if wxUSE_STD_IOSTREAM
a353dc98
VZ
793 friend WXDLLIMPEXP_BASE
794 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
fcc3d7cb 795#endif // wxUSE_STD_IOSTREAM
8b81872f 796
a353dc98
VZ
797 friend WXDLLIMPEXP_BASE
798 wxString& operator<<(wxString&, const wxLongLongWx&);
51496782 799
216a72f3
VZ
800#if wxUSE_STREAMS
801 friend WXDLLIMPEXP_BASE
802 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongWx&);
803 friend WXDLLIMPEXP_BASE
804 class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongWx&);
805#endif
806
8b81872f
VZ
807private:
808 // long is at least 32 bits, so represent our 64bit number as 2 longs
809
810 long m_hi; // signed bit is in the high part
811 unsigned long m_lo;
2a310492
VZ
812
813#ifdef wxLONGLONG_TEST_MODE
814 void Check()
815 {
816 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
817 }
818
819 wxLongLong_t m_ll;
820#endif // wxLONGLONG_TEST_MODE
8b81872f
VZ
821};
822
8e38fd1f 823
bddd7a8d 824class WXDLLIMPEXP_BASE wxULongLongWx
8e38fd1f
RL
825{
826public:
827 // ctors
828 // default ctor initializes to 0
829 wxULongLongWx()
830 {
831 m_lo = m_hi = 0;
832
833#ifdef wxLONGLONG_TEST_MODE
834 m_ll = 0;
835
836 Check();
837#endif // wxLONGLONG_TEST_MODE
838 }
839 // from ulong
840 wxULongLongWx(unsigned long l) { *this = l; }
841 // from 2 ulongs
842 wxULongLongWx(unsigned long hi, unsigned long lo)
843 {
844 m_hi = hi;
845 m_lo = lo;
846
847#ifdef wxLONGLONG_TEST_MODE
848 m_ll = hi;
849 m_ll <<= 32;
850 m_ll |= lo;
851
852 Check();
853#endif // wxLONGLONG_TEST_MODE
854 }
855
842215bb
WS
856 // from signed to unsigned
857 wxULongLongWx(wxLongLongWx ll)
858 {
859 wxASSERT(ll.GetHi() >= 0);
860 m_hi = (unsigned long)ll.GetHi();
861 m_lo = ll.GetLo();
862 }
863
8e38fd1f
RL
864 // default copy ctor is ok in both cases
865
866 // no dtor
867
868 // assignment operators
869 // from long
870 wxULongLongWx& operator=(unsigned long l)
871 {
872 m_lo = l;
873 m_hi = 0;
874
875#ifdef wxLONGLONG_TEST_MODE
876 m_ll = l;
877
878 Check();
879#endif // wxLONGLONG_TEST_MODE
880
881 return *this;
882 }
216a72f3
VZ
883 wxULongLongWx& operator=(long l)
884 {
885 m_lo = l;
886 m_hi = (unsigned long) ((l<0) ? -1l : 0);
887
888#ifdef wxLONGLONG_TEST_MODE
889 m_ll = (wxULongLong_t) (wxLongLong_t) l;
890
891 Check();
892#endif // wxLONGLONG_TEST_MODE
893
894 return *this;
895 }
896 wxULongLongWx& operator=(const class wxLongLongWx &ll) {
897 // Should we use an assert like it was before in the constructor?
898 // wxASSERT(ll.GetHi() >= 0);
899 m_hi = (unsigned long)ll.GetHi();
900 m_lo = ll.GetLo();
901 return *this;
902 }
8e38fd1f
RL
903
904 // can't have assignment operator from 2 longs
905
906 // accessors
907 // get high part
908 unsigned long GetHi() const { return m_hi; }
909 // get low part
910 unsigned long GetLo() const { return m_lo; }
911
90e572f1 912 // convert to long with range checking in debug mode (only!)
8e38fd1f
RL
913 unsigned long ToULong() const
914 {
d7997e1a 915 wxASSERT_MSG( m_hi == 0ul,
9a83f860 916 wxT("wxULongLong to long conversion loss of precision") );
8e38fd1f
RL
917
918 return (unsigned long)m_lo;
919 }
920
d870a030
VZ
921 // convert to double
922 double ToDouble() const;
923
8e38fd1f
RL
924 // operations
925 // addition
926 wxULongLongWx operator+(const wxULongLongWx& ll) const;
927 wxULongLongWx& operator+=(const wxULongLongWx& ll);
928 wxULongLongWx operator+(unsigned long l) const;
929 wxULongLongWx& operator+=(unsigned long l);
930
931 // pre increment operator
932 wxULongLongWx& operator++();
933
934 // post increment operator
935 wxULongLongWx& operator++(int) { return ++(*this); }
936
43e8916f 937 // subtraction
842215bb 938 wxLongLongWx operator-(const wxULongLongWx& ll) const;
8e38fd1f
RL
939 wxULongLongWx& operator-=(const wxULongLongWx& ll);
940
941 // pre decrement operator
942 wxULongLongWx& operator--();
943
944 // post decrement operator
945 wxULongLongWx& operator--(int) { return --(*this); }
946
947 // shifts
948 // left shift
949 wxULongLongWx operator<<(int shift) const;
950 wxULongLongWx& operator<<=(int shift);
951
952 // right shift
953 wxULongLongWx operator>>(int shift) const;
954 wxULongLongWx& operator>>=(int shift);
955
956 // bitwise operators
957 wxULongLongWx operator&(const wxULongLongWx& ll) const;
958 wxULongLongWx& operator&=(const wxULongLongWx& ll);
959 wxULongLongWx operator|(const wxULongLongWx& ll) const;
960 wxULongLongWx& operator|=(const wxULongLongWx& ll);
961 wxULongLongWx operator^(const wxULongLongWx& ll) const;
962 wxULongLongWx& operator^=(const wxULongLongWx& ll);
963 wxULongLongWx operator~() const;
964
965 // comparison
966 bool operator==(const wxULongLongWx& ll) const
967 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
968 bool operator!=(const wxULongLongWx& ll) const
969 { return !(*this == ll); }
970 bool operator<(const wxULongLongWx& ll) const;
971 bool operator>(const wxULongLongWx& ll) const;
972 bool operator<=(const wxULongLongWx& ll) const
973 { return *this < ll || *this == ll; }
974 bool operator>=(const wxULongLongWx& ll) const
975 { return *this > ll || *this == ll; }
976
977 bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
978 bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
979 bool operator==(unsigned long l) const
980 {
981 return (m_hi == 0 && m_lo == (unsigned long)l);
982 }
983
984 bool operator<=(unsigned long l) const { return *this < l || *this == l; }
985 bool operator>=(unsigned long l) const { return *this > l || *this == l; }
986
987 // multiplication
988 wxULongLongWx operator*(const wxULongLongWx& ll) const;
989 wxULongLongWx& operator*=(const wxULongLongWx& ll);
990
991 // division
992 wxULongLongWx operator/(const wxULongLongWx& ll) const;
993 wxULongLongWx& operator/=(const wxULongLongWx& ll);
994
995 wxULongLongWx operator%(const wxULongLongWx& ll) const;
996
997 void Divide(const wxULongLongWx& divisor,
998 wxULongLongWx& quotient,
999 wxULongLongWx& remainder) const;
1000
1001 // input/output
1002
1003 // return the string representation of this number
1004 wxString ToString() const;
1005
1006 void *asArray() const;
1007
1008#if wxUSE_STD_IOSTREAM
a353dc98
VZ
1009 friend WXDLLIMPEXP_BASE
1010 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
8e38fd1f
RL
1011#endif // wxUSE_STD_IOSTREAM
1012
a353dc98
VZ
1013 friend WXDLLIMPEXP_BASE
1014 wxString& operator<<(wxString&, const wxULongLongWx&);
51496782 1015
216a72f3
VZ
1016#if wxUSE_STREAMS
1017 friend WXDLLIMPEXP_BASE
1018 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongWx&);
1019 friend WXDLLIMPEXP_BASE
1020 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongWx&);
1021#endif
1022
8e38fd1f
RL
1023private:
1024 // long is at least 32 bits, so represent our 64bit number as 2 longs
1025
d2ce649b 1026 unsigned long m_hi;
8e38fd1f
RL
1027 unsigned long m_lo;
1028
1029#ifdef wxLONGLONG_TEST_MODE
1030 void Check()
1031 {
1032 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
1033 }
1034
60582201 1035 wxULongLong_t m_ll;
8e38fd1f
RL
1036#endif // wxLONGLONG_TEST_MODE
1037};
1038
8b81872f
VZ
1039#endif // wxUSE_LONGLONG_WX
1040
2ea24d9f
VZ
1041// ----------------------------------------------------------------------------
1042// binary operators
1043// ----------------------------------------------------------------------------
1044
6d56eb5c 1045inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
d7997e1a
VZ
1046inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
1047inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
1048inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
1049inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
1050inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
b01fb5c3 1051
d7997e1a 1052inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
b01fb5c3
VZ
1053inline wxLongLong operator-(long l, const wxLongLong& ll)
1054{
d7997e1a 1055 return wxLongLong(l) - ll;
b01fb5c3 1056}
2ea24d9f 1057
d7997e1a
VZ
1058inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
1059inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
1060inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
1061inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
1062inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
1063inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
1064
1065inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
8e38fd1f 1066
842215bb 1067inline wxLongLong operator-(unsigned long l, const wxULongLong& ull)
d7997e1a 1068{
842215bb
WS
1069 wxULongLong ret = wxULongLong(l) - ull;
1070 return wxLongLong((long)ret.GetHi(),ret.GetLo());
d7997e1a 1071}
8e38fd1f 1072
216a72f3
VZ
1073#if wxUSE_LONGLONG_NATIVE && wxUSE_STREAMS
1074
1075WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxULongLong_t value);
1076WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxLongLong_t value);
1077
1078WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxULongLong_t &value);
1079WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxLongLong_t &value);
1080
1081#endif
1082
3dd4d0ec
VZ
1083// ----------------------------------------------------------------------------
1084// Specialize numeric_limits<> for our long long wrapper classes.
1085// ----------------------------------------------------------------------------
1086
1087#if wxUSE_LONGLONG_NATIVE
1088
c92e136d
VZ
1089// VC6 is known to not have __int64 specializations of numeric_limits<> in its
1090// <limits> anyhow so don't bother including it, especially as it results in
1091// tons of warnings because the standard header itself uses obsolete template
1092// specialization syntax.
1093#ifndef __VISUALC6__
1094
3dd4d0ec
VZ
1095#include <limits>
1096
1097namespace std
1098{
1099
1100template<> class numeric_limits<wxLongLong>
1101 : public numeric_limits<wxLongLong_t>
1102{
1103};
1104
1105template<> class numeric_limits<wxULongLong>
1106 : public numeric_limits<wxULongLong_t>
1107{
1108};
1109
1110} // namespace std
1111
c92e136d
VZ
1112#endif // !VC6
1113
3dd4d0ec
VZ
1114#endif // wxUSE_LONGLONG_NATIVE
1115
715efa4e
VZ
1116// ----------------------------------------------------------------------------
1117// Specialize wxArgNormalizer to allow using wxLongLong directly with wx pseudo
1118// vararg functions.
1119// ----------------------------------------------------------------------------
1120
1121// Notice that this must be done here and not in wx/strvararg.h itself because
1122// we can't include wx/longlong.h from there as this header itself includes
1123// wx/string.h which includes wx/strvararg.h too, so to avoid the circular
1124// dependencies we can only do it here (or add another header just for this but
1125// it doesn't seem necessary).
1126#include "wx/strvararg.h"
1127
1128template<>
1129struct WXDLLIMPEXP_BASE wxArgNormalizer<wxLongLong>
1130{
1131 wxArgNormalizer(wxLongLong value,
1132 const wxFormatString *fmt, unsigned index)
1133 : m_value(value)
1134 {
1135 wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_LongLongInt );
1136 }
1137
1138 wxLongLong_t get() const { return m_value.GetValue(); }
1139
1140 wxLongLong m_value;
1141};
1142
216a72f3
VZ
1143#endif // wxUSE_LONGLONG
1144
8b81872f 1145#endif // _WX_LONGLONG_H