]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
removed __throws_bad_alloc import, this apparently breaks CW8.3 compilation and...
[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 licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_LONGLONG_H
14 #define _WX_LONGLONG_H
15
16 #include "wx/defs.h"
17
18 #if wxUSE_LONGLONG
19
20 #include "wx/string.h"
21
22 #include <limits.h> // for LONG_MAX
23
24 // define this to compile wxLongLongWx in "test" mode: the results of all
25 // calculations will be compared with the real results taken from
26 // wxLongLongNative -- this is extremely useful to find the bugs in
27 // wxLongLongWx class!
28
29 // #define wxLONGLONG_TEST_MODE
30
31 #ifdef wxLONGLONG_TEST_MODE
32 #define wxUSE_LONGLONG_WX 1
33 #define wxUSE_LONGLONG_NATIVE 1
34 #endif // wxLONGLONG_TEST_MODE
35
36 // ----------------------------------------------------------------------------
37 // decide upon which class we will use
38 // ----------------------------------------------------------------------------
39
40 #ifndef wxLongLong_t
41 // both warning and pragma warning are not portable, but at least an
42 // unknown pragma should never be an error -- except that, actually, some
43 // broken compilers don't like it, so we have to disable it in this case
44 // <sigh>
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__))
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 " \
54 "wx-dev@lists.wxwidgets.org!"
55 #endif
56
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
63 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
64 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
65 // this is useful in test programs and only there
66 #ifndef wxUSE_LONGLONG_NATIVE
67 #define wxUSE_LONGLONG_NATIVE 0
68 #endif
69
70 class WXDLLIMPEXP_BASE wxLongLongWx;
71 class WXDLLIMPEXP_BASE wxULongLongWx;
72 #if defined(__VISUALC__) && !defined(__WIN32__)
73 #define wxLongLong wxLongLongWx
74 #define wxULongLong wxULongLongWx
75 #else
76 typedef wxLongLongWx wxLongLong;
77 typedef wxULongLongWx wxULongLong;
78 #endif
79
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
85 #endif
86
87 #ifndef wxUSE_LONGLONG_WX
88 #define wxUSE_LONGLONG_WX 0
89 class WXDLLIMPEXP_BASE wxLongLongNative;
90 class WXDLLIMPEXP_BASE wxULongLongNative;
91 typedef wxLongLongNative wxLongLong;
92 typedef wxULongLongNative wxULongLong;
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
103 #include "wx/iosfwrap.h"
104
105 #if wxUSE_LONGLONG_NATIVE
106
107 class WXDLLIMPEXP_BASE wxLongLongNative
108 {
109 public:
110 // ctors
111 // default ctor initializes to 0
112 wxLongLongNative() : m_ll(0) { }
113 // from long long
114 wxLongLongNative(wxLongLong_t ll) : m_ll(ll) { }
115 // from 2 longs
116 wxLongLongNative(long hi, unsigned long lo) : m_ll(0)
117 {
118 // assign first to avoid precision loss!
119 m_ll = ((wxLongLong_t) hi) << 32;
120 m_ll |= (wxLongLong_t) lo;
121 }
122 #if wxUSE_LONGLONG_WX
123 wxLongLongNative(wxLongLongWx ll);
124 #endif
125
126 // default copy ctor is ok
127
128 // no dtor
129
130 // assignment operators
131 // from native 64 bit integer
132 #ifndef wxLongLongIsLong
133 wxLongLongNative& operator=(wxLongLong_t ll)
134 { m_ll = ll; return *this; }
135 wxLongLongNative& operator=(wxULongLong_t ll)
136 { m_ll = ll; return *this; }
137 #endif // !wxLongLongNative
138 wxLongLongNative& operator=(const wxULongLongNative &ll);
139 wxLongLongNative& operator=(int l)
140 { m_ll = l; return *this; }
141 wxLongLongNative& operator=(long l)
142 { m_ll = l; return *this; }
143 wxLongLongNative& operator=(unsigned int l)
144 { m_ll = l; return *this; }
145 wxLongLongNative& operator=(unsigned long l)
146 { m_ll = l; return *this; }
147 #if wxUSE_LONGLONG_WX
148 wxLongLongNative& operator=(wxLongLongWx ll);
149 wxLongLongNative& operator=(const class wxULongLongWx &ll);
150 #endif
151
152
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
159 // assignment operators from wxLongLongNative is ok
160
161 // accessors
162 // get high part
163 long GetHi() const
164 { return wx_truncate_cast(long, m_ll >> 32); }
165 // get low part
166 unsigned long GetLo() const
167 { return wx_truncate_cast(unsigned long, m_ll); }
168
169 // get absolute value
170 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
171 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
172
173 // convert to native long long
174 wxLongLong_t GetValue() const { return m_ll; }
175
176 // convert to long with range checking in debug mode (only!)
177 long ToLong() const
178 {
179 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
180 _T("wxLongLong to long conversion loss of precision") );
181
182 return wx_truncate_cast(long, m_ll);
183 }
184
185 // convert to double
186 double ToDouble() const { return wx_truncate_cast(double, m_ll); }
187
188 // don't provide implicit conversion to wxLongLong_t or we will have an
189 // ambiguity for all arithmetic operations
190 //operator wxLongLong_t() const { return m_ll; }
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
209 wxLongLongNative operator++(int)
210 { wxLongLongNative value(*this); m_ll++; return value; }
211
212 // negation operator
213 wxLongLongNative operator-() const
214 { return wxLongLongNative(-m_ll); }
215 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
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
233 wxLongLongNative operator--(int)
234 { wxLongLongNative value(*this); m_ll--; return value; }
235
236 // shifts
237 // left shift
238 wxLongLongNative operator<<(int shift) const
239 { return wxLongLongNative(m_ll << shift); }
240 wxLongLongNative& operator<<=(int shift)
241 { m_ll <<= shift; return *this; }
242
243 // right shift
244 wxLongLongNative operator>>(int shift) const
245 { return wxLongLongNative(m_ll >> shift); }
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
265 // multiplication/division
266 wxLongLongNative operator*(const wxLongLongNative& ll) const
267 { return wxLongLongNative(m_ll * ll.m_ll); }
268 wxLongLongNative operator*(long l) const
269 { return wxLongLongNative(m_ll * l); }
270 wxLongLongNative& operator*=(const wxLongLongNative& ll)
271 { m_ll *= ll.m_ll; return *this; }
272 wxLongLongNative& operator*=(long l)
273 { m_ll *= l; return *this; }
274
275 wxLongLongNative operator/(const wxLongLongNative& ll) const
276 { return wxLongLongNative(m_ll / ll.m_ll); }
277 wxLongLongNative operator/(long l) const
278 { return wxLongLongNative(m_ll / l); }
279 wxLongLongNative& operator/=(const wxLongLongNative& ll)
280 { m_ll /= ll.m_ll; return *this; }
281 wxLongLongNative& operator/=(long l)
282 { m_ll /= l; return *this; }
283
284 wxLongLongNative operator%(const wxLongLongNative& ll) const
285 { return wxLongLongNative(m_ll % ll.m_ll); }
286 wxLongLongNative operator%(long l) const
287 { return wxLongLongNative(m_ll % l); }
288
289 // comparison
290 bool operator==(const wxLongLongNative& ll) const
291 { return m_ll == ll.m_ll; }
292 bool operator==(long l) const
293 { return m_ll == l; }
294 bool operator!=(const wxLongLongNative& ll) const
295 { return m_ll != ll.m_ll; }
296 bool operator!=(long l) const
297 { return m_ll != l; }
298 bool operator<(const wxLongLongNative& ll) const
299 { return m_ll < ll.m_ll; }
300 bool operator<(long l) const
301 { return m_ll < l; }
302 bool operator>(const wxLongLongNative& ll) const
303 { return m_ll > ll.m_ll; }
304 bool operator>(long l) const
305 { return m_ll > l; }
306 bool operator<=(const wxLongLongNative& ll) const
307 { return m_ll <= ll.m_ll; }
308 bool operator<=(long l) const
309 { return m_ll <= l; }
310 bool operator>=(const wxLongLongNative& ll) const
311 { return m_ll >= ll.m_ll; }
312 bool operator>=(long l) const
313 { return m_ll >= l; }
314
315 // miscellaneous
316
317 // return the string representation of this number
318 wxString ToString() const;
319
320 // conversion to byte array: returns a pointer to static buffer!
321 void *asArray() const;
322
323 #if wxUSE_STD_IOSTREAM
324 // input/output
325 friend WXDLLIMPEXP_BASE
326 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
327 #endif
328
329 friend WXDLLIMPEXP_BASE
330 wxString& operator<<(wxString&, const wxLongLongNative&);
331
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
339 private:
340 wxLongLong_t m_ll;
341 };
342
343
344 class WXDLLIMPEXP_BASE wxULongLongNative
345 {
346 public:
347 // ctors
348 // default ctor initializes to 0
349 wxULongLongNative() : m_ll(0) { }
350 // from long long
351 wxULongLongNative(wxULongLong_t ll) : m_ll(ll) { }
352 // from 2 longs
353 wxULongLongNative(unsigned long hi, unsigned long lo) : m_ll(0)
354 {
355 // assign first to avoid precision loss!
356 m_ll = ((wxULongLong_t) hi) << 32;
357 m_ll |= (wxULongLong_t) lo;
358 }
359
360 #if wxUSE_LONGLONG_WX
361 wxULongLongNative(const class wxULongLongWx &ll);
362 #endif
363
364 // default copy ctor is ok
365
366 // no dtor
367
368 // assignment operators
369 // from native 64 bit integer
370 #ifndef wxLongLongIsLong
371 wxULongLongNative& operator=(wxULongLong_t ll)
372 { m_ll = ll; return *this; }
373 wxULongLongNative& operator=(wxLongLong_t ll)
374 { m_ll = ll; return *this; }
375 #endif // !wxLongLongNative
376 wxULongLongNative& operator=(int l)
377 { m_ll = l; return *this; }
378 wxULongLongNative& operator=(long l)
379 { m_ll = l; return *this; }
380 wxULongLongNative& operator=(unsigned int l)
381 { m_ll = l; return *this; }
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
390
391 // assignment operators from wxULongLongNative is ok
392
393 // accessors
394 // get high part
395 unsigned long GetHi() const
396 { return wx_truncate_cast(unsigned long, m_ll >> 32); }
397 // get low part
398 unsigned long GetLo() const
399 { return wx_truncate_cast(unsigned long, m_ll); }
400
401 // convert to native ulong long
402 wxULongLong_t GetValue() const { return m_ll; }
403
404 // convert to ulong with range checking in debug mode (only!)
405 unsigned long ToULong() const
406 {
407 wxASSERT_MSG( m_ll <= LONG_MAX,
408 _T("wxULongLong to long conversion loss of precision") );
409
410 return wx_truncate_cast(unsigned long, m_ll);
411 }
412
413 // operations
414 // addition
415 wxULongLongNative operator+(const wxULongLongNative& ll) const
416 { return wxULongLongNative(m_ll + ll.m_ll); }
417 wxULongLongNative& operator+=(const wxULongLongNative& ll)
418 { m_ll += ll.m_ll; return *this; }
419
420 wxULongLongNative operator+(const wxULongLong_t ll) const
421 { return wxULongLongNative(m_ll + ll); }
422 wxULongLongNative& operator+=(const wxULongLong_t ll)
423 { m_ll += ll; return *this; }
424
425 // pre increment
426 wxULongLongNative& operator++()
427 { m_ll++; return *this; }
428
429 // post increment
430 wxULongLongNative operator++(int)
431 { wxULongLongNative value(*this); m_ll++; return value; }
432
433 // subtraction
434 wxULongLongNative operator-(const wxULongLongNative& ll) const
435 { return wxULongLongNative(m_ll - ll.m_ll); }
436 wxULongLongNative& operator-=(const wxULongLongNative& ll)
437 { m_ll -= ll.m_ll; return *this; }
438
439 wxULongLongNative operator-(const wxULongLong_t ll) const
440 { return wxULongLongNative(m_ll - ll); }
441 wxULongLongNative& operator-=(const wxULongLong_t ll)
442 { m_ll -= ll; return *this; }
443
444 // pre decrement
445 wxULongLongNative& operator--()
446 { m_ll--; return *this; }
447
448 // post decrement
449 wxULongLongNative operator--(int)
450 { wxULongLongNative value(*this); m_ll--; return value; }
451
452 // shifts
453 // left shift
454 wxULongLongNative operator<<(int shift) const
455 { return wxULongLongNative(m_ll << shift); }
456 wxULongLongNative& operator<<=(int shift)
457 { m_ll <<= shift; return *this; }
458
459 // right shift
460 wxULongLongNative operator>>(int shift) const
461 { return wxULongLongNative(m_ll >> shift); }
462 wxULongLongNative& operator>>=(int shift)
463 { m_ll >>= shift; return *this; }
464
465 // bitwise operators
466 wxULongLongNative operator&(const wxULongLongNative& ll) const
467 { return wxULongLongNative(m_ll & ll.m_ll); }
468 wxULongLongNative& operator&=(const wxULongLongNative& ll)
469 { m_ll &= ll.m_ll; return *this; }
470
471 wxULongLongNative operator|(const wxULongLongNative& ll) const
472 { return wxULongLongNative(m_ll | ll.m_ll); }
473 wxULongLongNative& operator|=(const wxULongLongNative& ll)
474 { m_ll |= ll.m_ll; return *this; }
475
476 wxULongLongNative operator^(const wxULongLongNative& ll) const
477 { return wxULongLongNative(m_ll ^ ll.m_ll); }
478 wxULongLongNative& operator^=(const wxULongLongNative& ll)
479 { m_ll ^= ll.m_ll; return *this; }
480
481 // multiplication/division
482 wxULongLongNative operator*(const wxULongLongNative& ll) const
483 { return wxULongLongNative(m_ll * ll.m_ll); }
484 wxULongLongNative operator*(unsigned long l) const
485 { return wxULongLongNative(m_ll * l); }
486 wxULongLongNative& operator*=(const wxULongLongNative& ll)
487 { m_ll *= ll.m_ll; return *this; }
488 wxULongLongNative& operator*=(unsigned long l)
489 { m_ll *= l; return *this; }
490
491 wxULongLongNative operator/(const wxULongLongNative& ll) const
492 { return wxULongLongNative(m_ll / ll.m_ll); }
493 wxULongLongNative operator/(unsigned long l) const
494 { return wxULongLongNative(m_ll / l); }
495 wxULongLongNative& operator/=(const wxULongLongNative& ll)
496 { m_ll /= ll.m_ll; return *this; }
497 wxULongLongNative& operator/=(unsigned long l)
498 { m_ll /= l; return *this; }
499
500 wxULongLongNative operator%(const wxULongLongNative& ll) const
501 { return wxULongLongNative(m_ll % ll.m_ll); }
502 wxULongLongNative operator%(unsigned long l) const
503 { return wxULongLongNative(m_ll % l); }
504
505 // comparison
506 bool operator==(const wxULongLongNative& ll) const
507 { return m_ll == ll.m_ll; }
508 bool operator==(unsigned long l) const
509 { return m_ll == l; }
510 bool operator!=(const wxULongLongNative& ll) const
511 { return m_ll != ll.m_ll; }
512 bool operator!=(unsigned long l) const
513 { return m_ll != l; }
514 bool operator<(const wxULongLongNative& ll) const
515 { return m_ll < ll.m_ll; }
516 bool operator<(unsigned long l) const
517 { return m_ll < l; }
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
531 // miscellaneous
532
533 // return the string representation of this number
534 wxString ToString() const;
535
536 // conversion to byte array: returns a pointer to static buffer!
537 void *asArray() const;
538
539 #if wxUSE_STD_IOSTREAM
540 // input/output
541 friend WXDLLIMPEXP_BASE
542 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
543 #endif
544
545 friend WXDLLIMPEXP_BASE
546 wxString& operator<<(wxString&, const wxULongLongNative&);
547
548 #if wxUSE_STREAMS
549 friend WXDLLIMPEXP_BASE
550 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongNative&);
551 friend WXDLLIMPEXP_BASE
552 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongNative&);
553 #endif
554
555 private:
556 wxULongLong_t m_ll;
557 };
558
559 inline
560 wxLongLongNative& wxLongLongNative::operator=(const wxULongLongNative &ll)
561 {
562 m_ll = ll.GetValue();
563 return *this;
564 }
565
566 #endif // wxUSE_LONGLONG_NATIVE
567
568 #if wxUSE_LONGLONG_WX
569
570 class WXDLLIMPEXP_BASE wxLongLongWx
571 {
572 public:
573 // ctors
574 // default ctor initializes to 0
575 wxLongLongWx()
576 {
577 m_lo = m_hi = 0;
578
579 #ifdef wxLONGLONG_TEST_MODE
580 m_ll = 0;
581
582 Check();
583 #endif // wxLONGLONG_TEST_MODE
584 }
585 // from long
586 wxLongLongWx(long l) { *this = l; }
587 // from 2 longs
588 wxLongLongWx(long hi, unsigned long lo)
589 {
590 m_hi = hi;
591 m_lo = lo;
592
593 #ifdef wxLONGLONG_TEST_MODE
594 m_ll = hi;
595 m_ll <<= 32;
596 m_ll |= lo;
597
598 Check();
599 #endif // wxLONGLONG_TEST_MODE
600 }
601
602 // default copy ctor is ok in both cases
603
604 // no dtor
605
606 // assignment operators
607 // from long
608 wxLongLongWx& operator=(long l)
609 {
610 m_lo = l;
611 m_hi = (l < 0 ? -1l : 0l);
612
613 #ifdef wxLONGLONG_TEST_MODE
614 m_ll = l;
615
616 Check();
617 #endif // wxLONGLONG_TEST_MODE
618
619 return *this;
620 }
621 // from int
622 wxLongLongWx& operator=(int l)
623 {
624 return operator=((long)l);
625 }
626
627 wxLongLongWx& operator=(unsigned long l)
628 {
629 m_lo = l;
630 m_hi = 0;
631
632 #ifdef wxLONGLONG_TEST_MODE
633 m_ll = l;
634
635 Check();
636 #endif // wxLONGLONG_TEST_MODE
637
638 return *this;
639 }
640
641 wxLongLongWx& operator=(unsigned int l)
642 {
643 return operator=((unsigned long)l);
644 }
645
646 wxLongLongWx& operator=(const class wxULongLongWx &ll);
647
648 // from double
649 wxLongLongWx& Assign(double d);
650 // can't have assignment operator from 2 longs
651
652 // accessors
653 // get high part
654 long GetHi() const { return m_hi; }
655 // get low part
656 unsigned long GetLo() const { return m_lo; }
657
658 // get absolute value
659 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
660 wxLongLongWx& Abs()
661 {
662 if ( m_hi < 0 )
663 m_hi = -m_hi;
664
665 #ifdef wxLONGLONG_TEST_MODE
666 if ( m_ll < 0 )
667 m_ll = -m_ll;
668
669 Check();
670 #endif // wxLONGLONG_TEST_MODE
671
672 return *this;
673 }
674
675 // convert to long with range checking in debug mode (only!)
676 long ToLong() const
677 {
678 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
679 _T("wxLongLong to long conversion loss of precision") );
680
681 return (long)m_lo;
682 }
683
684 // convert to double
685 double ToDouble() const;
686
687 // operations
688 // addition
689 wxLongLongWx operator+(const wxLongLongWx& ll) const;
690 wxLongLongWx& operator+=(const wxLongLongWx& ll);
691 wxLongLongWx operator+(long l) const;
692 wxLongLongWx& operator+=(long l);
693
694 // pre increment operator
695 wxLongLongWx& operator++();
696
697 // post increment operator
698 wxLongLongWx& operator++(int) { return ++(*this); }
699
700 // negation operator
701 wxLongLongWx operator-() const;
702 wxLongLongWx& Negate();
703
704 // subraction
705 wxLongLongWx operator-(const wxLongLongWx& ll) const;
706 wxLongLongWx& operator-=(const wxLongLongWx& ll);
707
708 // pre decrement operator
709 wxLongLongWx& operator--();
710
711 // post decrement operator
712 wxLongLongWx& operator--(int) { return --(*this); }
713
714 // shifts
715 // left shift
716 wxLongLongWx operator<<(int shift) const;
717 wxLongLongWx& operator<<=(int shift);
718
719 // right shift
720 wxLongLongWx operator>>(int shift) const;
721 wxLongLongWx& operator>>=(int shift);
722
723 // bitwise operators
724 wxLongLongWx operator&(const wxLongLongWx& ll) const;
725 wxLongLongWx& operator&=(const wxLongLongWx& ll);
726 wxLongLongWx operator|(const wxLongLongWx& ll) const;
727 wxLongLongWx& operator|=(const wxLongLongWx& ll);
728 wxLongLongWx operator^(const wxLongLongWx& ll) const;
729 wxLongLongWx& operator^=(const wxLongLongWx& ll);
730 wxLongLongWx operator~() const;
731
732 // comparison
733 bool operator==(const wxLongLongWx& ll) const
734 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
735 #if wxUSE_LONGLONG_NATIVE
736 bool operator==(const wxLongLongNative& ll) const
737 { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); }
738 #endif
739 bool operator!=(const wxLongLongWx& ll) const
740 { return !(*this == ll); }
741 bool operator<(const wxLongLongWx& ll) const;
742 bool operator>(const wxLongLongWx& ll) const;
743 bool operator<=(const wxLongLongWx& ll) const
744 { return *this < ll || *this == ll; }
745 bool operator>=(const wxLongLongWx& ll) const
746 { return *this > ll || *this == ll; }
747
748 bool operator<(long l) const { return *this < wxLongLongWx(l); }
749 bool operator>(long l) const { return *this > wxLongLongWx(l); }
750 bool operator==(long l) const
751 {
752 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
753 : (m_hi == -1 && m_lo == (unsigned long)l);
754 }
755
756 bool operator<=(long l) const { return *this < l || *this == l; }
757 bool operator>=(long l) const { return *this > l || *this == l; }
758
759 // multiplication
760 wxLongLongWx operator*(const wxLongLongWx& ll) const;
761 wxLongLongWx& operator*=(const wxLongLongWx& ll);
762
763 // division
764 wxLongLongWx operator/(const wxLongLongWx& ll) const;
765 wxLongLongWx& operator/=(const wxLongLongWx& ll);
766
767 wxLongLongWx operator%(const wxLongLongWx& ll) const;
768
769 void Divide(const wxLongLongWx& divisor,
770 wxLongLongWx& quotient,
771 wxLongLongWx& remainder) const;
772
773 // input/output
774
775 // return the string representation of this number
776 wxString ToString() const;
777
778 void *asArray() const;
779
780 #if wxUSE_STD_IOSTREAM
781 friend WXDLLIMPEXP_BASE
782 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
783 #endif // wxUSE_STD_IOSTREAM
784
785 friend WXDLLIMPEXP_BASE
786 wxString& operator<<(wxString&, const wxLongLongWx&);
787
788 #if wxUSE_STREAMS
789 friend WXDLLIMPEXP_BASE
790 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongWx&);
791 friend WXDLLIMPEXP_BASE
792 class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongWx&);
793 #endif
794
795 private:
796 // long is at least 32 bits, so represent our 64bit number as 2 longs
797
798 long m_hi; // signed bit is in the high part
799 unsigned long m_lo;
800
801 #ifdef wxLONGLONG_TEST_MODE
802 void Check()
803 {
804 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
805 }
806
807 wxLongLong_t m_ll;
808 #endif // wxLONGLONG_TEST_MODE
809 };
810
811
812 class WXDLLIMPEXP_BASE wxULongLongWx
813 {
814 public:
815 // ctors
816 // default ctor initializes to 0
817 wxULongLongWx()
818 {
819 m_lo = m_hi = 0;
820
821 #ifdef wxLONGLONG_TEST_MODE
822 m_ll = 0;
823
824 Check();
825 #endif // wxLONGLONG_TEST_MODE
826 }
827 // from ulong
828 wxULongLongWx(unsigned long l) { *this = l; }
829 // from 2 ulongs
830 wxULongLongWx(unsigned long hi, unsigned long lo)
831 {
832 m_hi = hi;
833 m_lo = lo;
834
835 #ifdef wxLONGLONG_TEST_MODE
836 m_ll = hi;
837 m_ll <<= 32;
838 m_ll |= lo;
839
840 Check();
841 #endif // wxLONGLONG_TEST_MODE
842 }
843
844 // from signed to unsigned
845 wxULongLongWx(wxLongLongWx ll)
846 {
847 wxASSERT(ll.GetHi() >= 0);
848 m_hi = (unsigned long)ll.GetHi();
849 m_lo = ll.GetLo();
850 }
851
852 // default copy ctor is ok in both cases
853
854 // no dtor
855
856 // assignment operators
857 // from long
858 wxULongLongWx& operator=(unsigned long l)
859 {
860 m_lo = l;
861 m_hi = 0;
862
863 #ifdef wxLONGLONG_TEST_MODE
864 m_ll = l;
865
866 Check();
867 #endif // wxLONGLONG_TEST_MODE
868
869 return *this;
870 }
871 wxULongLongWx& operator=(long l)
872 {
873 m_lo = l;
874 m_hi = (unsigned long) ((l<0) ? -1l : 0);
875
876 #ifdef wxLONGLONG_TEST_MODE
877 m_ll = (wxULongLong_t) (wxLongLong_t) l;
878
879 Check();
880 #endif // wxLONGLONG_TEST_MODE
881
882 return *this;
883 }
884 wxULongLongWx& operator=(const class wxLongLongWx &ll) {
885 // Should we use an assert like it was before in the constructor?
886 // wxASSERT(ll.GetHi() >= 0);
887 m_hi = (unsigned long)ll.GetHi();
888 m_lo = ll.GetLo();
889 return *this;
890 }
891
892 // can't have assignment operator from 2 longs
893
894 // accessors
895 // get high part
896 unsigned long GetHi() const { return m_hi; }
897 // get low part
898 unsigned long GetLo() const { return m_lo; }
899
900 // convert to long with range checking in debug mode (only!)
901 unsigned long ToULong() const
902 {
903 wxASSERT_MSG( m_hi == 0ul,
904 _T("wxULongLong to long conversion loss of precision") );
905
906 return (unsigned long)m_lo;
907 }
908
909 // operations
910 // addition
911 wxULongLongWx operator+(const wxULongLongWx& ll) const;
912 wxULongLongWx& operator+=(const wxULongLongWx& ll);
913 wxULongLongWx operator+(unsigned long l) const;
914 wxULongLongWx& operator+=(unsigned long l);
915
916 // pre increment operator
917 wxULongLongWx& operator++();
918
919 // post increment operator
920 wxULongLongWx& operator++(int) { return ++(*this); }
921
922 // subtraction
923 wxLongLongWx operator-(const wxULongLongWx& ll) const;
924 wxULongLongWx& operator-=(const wxULongLongWx& ll);
925
926 // pre decrement operator
927 wxULongLongWx& operator--();
928
929 // post decrement operator
930 wxULongLongWx& operator--(int) { return --(*this); }
931
932 // shifts
933 // left shift
934 wxULongLongWx operator<<(int shift) const;
935 wxULongLongWx& operator<<=(int shift);
936
937 // right shift
938 wxULongLongWx operator>>(int shift) const;
939 wxULongLongWx& operator>>=(int shift);
940
941 // bitwise operators
942 wxULongLongWx operator&(const wxULongLongWx& ll) const;
943 wxULongLongWx& operator&=(const wxULongLongWx& ll);
944 wxULongLongWx operator|(const wxULongLongWx& ll) const;
945 wxULongLongWx& operator|=(const wxULongLongWx& ll);
946 wxULongLongWx operator^(const wxULongLongWx& ll) const;
947 wxULongLongWx& operator^=(const wxULongLongWx& ll);
948 wxULongLongWx operator~() const;
949
950 // comparison
951 bool operator==(const wxULongLongWx& ll) const
952 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
953 bool operator!=(const wxULongLongWx& ll) const
954 { return !(*this == ll); }
955 bool operator<(const wxULongLongWx& ll) const;
956 bool operator>(const wxULongLongWx& ll) const;
957 bool operator<=(const wxULongLongWx& ll) const
958 { return *this < ll || *this == ll; }
959 bool operator>=(const wxULongLongWx& ll) const
960 { return *this > ll || *this == ll; }
961
962 bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
963 bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
964 bool operator==(unsigned long l) const
965 {
966 return (m_hi == 0 && m_lo == (unsigned long)l);
967 }
968
969 bool operator<=(unsigned long l) const { return *this < l || *this == l; }
970 bool operator>=(unsigned long l) const { return *this > l || *this == l; }
971
972 // multiplication
973 wxULongLongWx operator*(const wxULongLongWx& ll) const;
974 wxULongLongWx& operator*=(const wxULongLongWx& ll);
975
976 // division
977 wxULongLongWx operator/(const wxULongLongWx& ll) const;
978 wxULongLongWx& operator/=(const wxULongLongWx& ll);
979
980 wxULongLongWx operator%(const wxULongLongWx& ll) const;
981
982 void Divide(const wxULongLongWx& divisor,
983 wxULongLongWx& quotient,
984 wxULongLongWx& remainder) const;
985
986 // input/output
987
988 // return the string representation of this number
989 wxString ToString() const;
990
991 void *asArray() const;
992
993 #if wxUSE_STD_IOSTREAM
994 friend WXDLLIMPEXP_BASE
995 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
996 #endif // wxUSE_STD_IOSTREAM
997
998 friend WXDLLIMPEXP_BASE
999 wxString& operator<<(wxString&, const wxULongLongWx&);
1000
1001 #if wxUSE_STREAMS
1002 friend WXDLLIMPEXP_BASE
1003 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongWx&);
1004 friend WXDLLIMPEXP_BASE
1005 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongWx&);
1006 #endif
1007
1008 private:
1009 // long is at least 32 bits, so represent our 64bit number as 2 longs
1010
1011 unsigned long m_hi;
1012 unsigned long m_lo;
1013
1014 #ifdef wxLONGLONG_TEST_MODE
1015 void Check()
1016 {
1017 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
1018 }
1019
1020 wxULongLong_t m_ll;
1021 #endif // wxLONGLONG_TEST_MODE
1022 };
1023
1024 #endif // wxUSE_LONGLONG_WX
1025
1026 // ----------------------------------------------------------------------------
1027 // binary operators
1028 // ----------------------------------------------------------------------------
1029
1030 inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
1031 inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
1032 inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
1033 inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
1034 inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
1035 inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
1036
1037 inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
1038 inline wxLongLong operator-(long l, const wxLongLong& ll)
1039 {
1040 return wxLongLong(l) - ll;
1041 }
1042
1043 inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
1044 inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
1045 inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
1046 inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
1047 inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
1048 inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
1049
1050 inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
1051
1052 inline wxLongLong operator-(unsigned long l, const wxULongLong& ull)
1053 {
1054 wxULongLong ret = wxULongLong(l) - ull;
1055 return wxLongLong((long)ret.GetHi(),ret.GetLo());
1056 }
1057
1058 #if wxUSE_LONGLONG_NATIVE && wxUSE_STREAMS
1059
1060 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxULongLong_t value);
1061 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxLongLong_t value);
1062
1063 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxULongLong_t &value);
1064 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxLongLong_t &value);
1065
1066 #endif
1067
1068 #endif // wxUSE_LONGLONG
1069
1070 #endif // _WX_LONGLONG_H