]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
added assignment operators from int for disambiguation of expressions such as ll=0
[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 wxLongLongNative& operator=(wxLongLong_t ll)
133 { m_ll = ll; return *this; }
134 wxLongLongNative& operator=(wxULongLong_t ll)
135 { m_ll = ll; return *this; }
136 wxLongLongNative& operator=(const wxULongLongNative &ll);
137 wxLongLongNative& operator=(int l)
138 { m_ll = l; return *this; }
139 wxLongLongNative& operator=(long l)
140 { m_ll = l; return *this; }
141 wxLongLongNative& operator=(unsigned long l)
142 { m_ll = l; return *this; }
143 #if wxUSE_LONGLONG_WX
144 wxLongLongNative& operator=(wxLongLongWx ll);
145 wxLongLongNative& operator=(const class wxULongLongWx &ll);
146 #endif
147
148
149 // from double: this one has an explicit name because otherwise we
150 // would have ambiguity with "ll = int" and also because we don't want
151 // to have implicit conversions between doubles and wxLongLongs
152 wxLongLongNative& Assign(double d)
153 { m_ll = (wxLongLong_t)d; return *this; }
154
155 // assignment operators from wxLongLongNative is ok
156
157 // accessors
158 // get high part
159 long GetHi() const
160 { return wx_truncate_cast(long, m_ll >> 32); }
161 // get low part
162 unsigned long GetLo() const
163 { return wx_truncate_cast(unsigned long, m_ll); }
164
165 // get absolute value
166 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
167 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
168
169 // convert to native long long
170 wxLongLong_t GetValue() const { return m_ll; }
171
172 // convert to long with range checking in debug mode (only!)
173 long ToLong() const
174 {
175 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
176 _T("wxLongLong to long conversion loss of precision") );
177
178 return wx_truncate_cast(long, m_ll);
179 }
180
181 // convert to double
182 double ToDouble() const { return wx_truncate_cast(double, m_ll); }
183
184 // don't provide implicit conversion to wxLongLong_t or we will have an
185 // ambiguity for all arithmetic operations
186 //operator wxLongLong_t() const { return m_ll; }
187
188 // operations
189 // addition
190 wxLongLongNative operator+(const wxLongLongNative& ll) const
191 { return wxLongLongNative(m_ll + ll.m_ll); }
192 wxLongLongNative& operator+=(const wxLongLongNative& ll)
193 { m_ll += ll.m_ll; return *this; }
194
195 wxLongLongNative operator+(const wxLongLong_t ll) const
196 { return wxLongLongNative(m_ll + ll); }
197 wxLongLongNative& operator+=(const wxLongLong_t ll)
198 { m_ll += ll; return *this; }
199
200 // pre increment
201 wxLongLongNative& operator++()
202 { m_ll++; return *this; }
203
204 // post increment
205 wxLongLongNative operator++(int)
206 { wxLongLongNative value(*this); m_ll++; return value; }
207
208 // negation operator
209 wxLongLongNative operator-() const
210 { return wxLongLongNative(-m_ll); }
211 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
212
213 // subtraction
214 wxLongLongNative operator-(const wxLongLongNative& ll) const
215 { return wxLongLongNative(m_ll - ll.m_ll); }
216 wxLongLongNative& operator-=(const wxLongLongNative& ll)
217 { m_ll -= ll.m_ll; return *this; }
218
219 wxLongLongNative operator-(const wxLongLong_t ll) const
220 { return wxLongLongNative(m_ll - ll); }
221 wxLongLongNative& operator-=(const wxLongLong_t ll)
222 { m_ll -= ll; return *this; }
223
224 // pre decrement
225 wxLongLongNative& operator--()
226 { m_ll--; return *this; }
227
228 // post decrement
229 wxLongLongNative operator--(int)
230 { wxLongLongNative value(*this); m_ll--; return value; }
231
232 // shifts
233 // left shift
234 wxLongLongNative operator<<(int shift) const
235 { return wxLongLongNative(m_ll << shift); }
236 wxLongLongNative& operator<<=(int shift)
237 { m_ll <<= shift; return *this; }
238
239 // right shift
240 wxLongLongNative operator>>(int shift) const
241 { return wxLongLongNative(m_ll >> shift); }
242 wxLongLongNative& operator>>=(int shift)
243 { m_ll >>= shift; return *this; }
244
245 // bitwise operators
246 wxLongLongNative operator&(const wxLongLongNative& ll) const
247 { return wxLongLongNative(m_ll & ll.m_ll); }
248 wxLongLongNative& operator&=(const wxLongLongNative& ll)
249 { m_ll &= ll.m_ll; return *this; }
250
251 wxLongLongNative operator|(const wxLongLongNative& ll) const
252 { return wxLongLongNative(m_ll | ll.m_ll); }
253 wxLongLongNative& operator|=(const wxLongLongNative& ll)
254 { m_ll |= ll.m_ll; return *this; }
255
256 wxLongLongNative operator^(const wxLongLongNative& ll) const
257 { return wxLongLongNative(m_ll ^ ll.m_ll); }
258 wxLongLongNative& operator^=(const wxLongLongNative& ll)
259 { m_ll ^= ll.m_ll; return *this; }
260
261 // multiplication/division
262 wxLongLongNative operator*(const wxLongLongNative& ll) const
263 { return wxLongLongNative(m_ll * ll.m_ll); }
264 wxLongLongNative operator*(long l) const
265 { return wxLongLongNative(m_ll * l); }
266 wxLongLongNative& operator*=(const wxLongLongNative& ll)
267 { m_ll *= ll.m_ll; return *this; }
268 wxLongLongNative& operator*=(long l)
269 { m_ll *= l; return *this; }
270
271 wxLongLongNative operator/(const wxLongLongNative& ll) const
272 { return wxLongLongNative(m_ll / ll.m_ll); }
273 wxLongLongNative operator/(long l) const
274 { return wxLongLongNative(m_ll / l); }
275 wxLongLongNative& operator/=(const wxLongLongNative& ll)
276 { m_ll /= ll.m_ll; return *this; }
277 wxLongLongNative& operator/=(long l)
278 { m_ll /= l; return *this; }
279
280 wxLongLongNative operator%(const wxLongLongNative& ll) const
281 { return wxLongLongNative(m_ll % ll.m_ll); }
282 wxLongLongNative operator%(long l) const
283 { return wxLongLongNative(m_ll % l); }
284
285 // comparison
286 bool operator==(const wxLongLongNative& ll) const
287 { return m_ll == ll.m_ll; }
288 bool operator==(long l) const
289 { return m_ll == l; }
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
311 // miscellaneous
312
313 // return the string representation of this number
314 wxString ToString() const;
315
316 // conversion to byte array: returns a pointer to static buffer!
317 void *asArray() const;
318
319 #if wxUSE_STD_IOSTREAM
320 // input/output
321 friend WXDLLIMPEXP_BASE
322 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
323 #endif
324
325 friend WXDLLIMPEXP_BASE
326 wxString& operator<<(wxString&, const wxLongLongNative&);
327
328 #if wxUSE_STREAMS
329 friend WXDLLIMPEXP_BASE
330 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongNative&);
331 friend WXDLLIMPEXP_BASE
332 class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongNative&);
333 #endif
334
335 private:
336 wxLongLong_t m_ll;
337 };
338
339
340 class WXDLLIMPEXP_BASE wxULongLongNative
341 {
342 public:
343 // ctors
344 // default ctor initializes to 0
345 wxULongLongNative() : m_ll(0) { }
346 // from long long
347 wxULongLongNative(wxULongLong_t ll) : m_ll(ll) { }
348 // from 2 longs
349 wxULongLongNative(unsigned long hi, unsigned long lo) : m_ll(0)
350 {
351 // assign first to avoid precision loss!
352 m_ll = ((wxULongLong_t) hi) << 32;
353 m_ll |= (wxULongLong_t) lo;
354 }
355
356 #if wxUSE_LONGLONG_WX
357 wxULongLongNative(const class wxULongLongWx &ll);
358 #endif
359
360 // default copy ctor is ok
361
362 // no dtor
363
364 // assignment operators
365 // from native 64 bit integer
366 wxULongLongNative& operator=(wxULongLong_t ll)
367 { m_ll = ll; return *this; }
368 wxULongLongNative& operator=(wxLongLong_t ll)
369 { m_ll = ll; return *this; }
370 wxULongLongNative& operator=(int l)
371 { m_ll = l; return *this; }
372 wxULongLongNative& operator=(long l)
373 { m_ll = l; return *this; }
374 wxULongLongNative& operator=(unsigned long l)
375 { m_ll = l; return *this; }
376 wxULongLongNative& operator=(const wxLongLongNative &ll)
377 { m_ll = ll.GetValue(); return *this; }
378 #if wxUSE_LONGLONG_WX
379 wxULongLongNative& operator=(wxLongLongWx ll);
380 wxULongLongNative& operator=(const class wxULongLongWx &ll);
381 #endif
382
383 // assignment operators from wxULongLongNative is ok
384
385 // accessors
386 // get high part
387 unsigned long GetHi() const
388 { return wx_truncate_cast(unsigned long, m_ll >> 32); }
389 // get low part
390 unsigned long GetLo() const
391 { return wx_truncate_cast(unsigned long, m_ll); }
392
393 // convert to native ulong long
394 wxULongLong_t GetValue() const { return m_ll; }
395
396 // convert to ulong with range checking in debug mode (only!)
397 unsigned long ToULong() const
398 {
399 wxASSERT_MSG( m_ll <= LONG_MAX,
400 _T("wxULongLong to long conversion loss of precision") );
401
402 return wx_truncate_cast(unsigned long, m_ll);
403 }
404
405 // operations
406 // addition
407 wxULongLongNative operator+(const wxULongLongNative& ll) const
408 { return wxULongLongNative(m_ll + ll.m_ll); }
409 wxULongLongNative& operator+=(const wxULongLongNative& ll)
410 { m_ll += ll.m_ll; return *this; }
411
412 wxULongLongNative operator+(const wxULongLong_t ll) const
413 { return wxULongLongNative(m_ll + ll); }
414 wxULongLongNative& operator+=(const wxULongLong_t ll)
415 { m_ll += ll; return *this; }
416
417 // pre increment
418 wxULongLongNative& operator++()
419 { m_ll++; return *this; }
420
421 // post increment
422 wxULongLongNative operator++(int)
423 { wxULongLongNative value(*this); m_ll++; return value; }
424
425 // subtraction
426 wxULongLongNative operator-(const wxULongLongNative& ll) const
427 { return wxULongLongNative(m_ll - ll.m_ll); }
428 wxULongLongNative& operator-=(const wxULongLongNative& ll)
429 { m_ll -= ll.m_ll; return *this; }
430
431 wxULongLongNative operator-(const wxULongLong_t ll) const
432 { return wxULongLongNative(m_ll - ll); }
433 wxULongLongNative& operator-=(const wxULongLong_t ll)
434 { m_ll -= ll; return *this; }
435
436 // pre decrement
437 wxULongLongNative& operator--()
438 { m_ll--; return *this; }
439
440 // post decrement
441 wxULongLongNative operator--(int)
442 { wxULongLongNative value(*this); m_ll--; return value; }
443
444 // shifts
445 // left 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 // right shift
452 wxULongLongNative operator>>(int shift) const
453 { return wxULongLongNative(m_ll >> shift); }
454 wxULongLongNative& operator>>=(int shift)
455 { m_ll >>= shift; return *this; }
456
457 // bitwise operators
458 wxULongLongNative operator&(const wxULongLongNative& ll) const
459 { return wxULongLongNative(m_ll & ll.m_ll); }
460 wxULongLongNative& operator&=(const wxULongLongNative& ll)
461 { m_ll &= ll.m_ll; return *this; }
462
463 wxULongLongNative operator|(const wxULongLongNative& ll) const
464 { return wxULongLongNative(m_ll | ll.m_ll); }
465 wxULongLongNative& operator|=(const wxULongLongNative& ll)
466 { m_ll |= ll.m_ll; return *this; }
467
468 wxULongLongNative operator^(const wxULongLongNative& ll) const
469 { return wxULongLongNative(m_ll ^ ll.m_ll); }
470 wxULongLongNative& operator^=(const wxULongLongNative& ll)
471 { m_ll ^= ll.m_ll; return *this; }
472
473 // multiplication/division
474 wxULongLongNative operator*(const wxULongLongNative& ll) const
475 { return wxULongLongNative(m_ll * ll.m_ll); }
476 wxULongLongNative operator*(unsigned long l) const
477 { return wxULongLongNative(m_ll * l); }
478 wxULongLongNative& operator*=(const wxULongLongNative& ll)
479 { m_ll *= ll.m_ll; return *this; }
480 wxULongLongNative& operator*=(unsigned long l)
481 { m_ll *= l; return *this; }
482
483 wxULongLongNative operator/(const wxULongLongNative& ll) const
484 { return wxULongLongNative(m_ll / ll.m_ll); }
485 wxULongLongNative operator/(unsigned long l) const
486 { return wxULongLongNative(m_ll / l); }
487 wxULongLongNative& operator/=(const wxULongLongNative& ll)
488 { m_ll /= ll.m_ll; return *this; }
489 wxULongLongNative& operator/=(unsigned long l)
490 { m_ll /= l; return *this; }
491
492 wxULongLongNative operator%(const wxULongLongNative& ll) const
493 { return wxULongLongNative(m_ll % ll.m_ll); }
494 wxULongLongNative operator%(unsigned long l) const
495 { return wxULongLongNative(m_ll % l); }
496
497 // comparison
498 bool operator==(const wxULongLongNative& ll) const
499 { return m_ll == ll.m_ll; }
500 bool operator==(unsigned long l) const
501 { return m_ll == l; }
502 bool operator!=(const wxULongLongNative& ll) const
503 { return m_ll != ll.m_ll; }
504 bool operator!=(unsigned long l) const
505 { return m_ll != l; }
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
523 // miscellaneous
524
525 // return the string representation of this number
526 wxString ToString() const;
527
528 // conversion to byte array: returns a pointer to static buffer!
529 void *asArray() const;
530
531 #if wxUSE_STD_IOSTREAM
532 // input/output
533 friend WXDLLIMPEXP_BASE
534 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
535 #endif
536
537 friend WXDLLIMPEXP_BASE
538 wxString& operator<<(wxString&, const wxULongLongNative&);
539
540 #if wxUSE_STREAMS
541 friend WXDLLIMPEXP_BASE
542 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongNative&);
543 friend WXDLLIMPEXP_BASE
544 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongNative&);
545 #endif
546
547 private:
548 wxULongLong_t m_ll;
549 };
550
551 inline
552 wxLongLongNative& wxLongLongNative::operator=(const wxULongLongNative &ll)
553 {
554 m_ll = ll.GetValue();
555 return *this;
556 }
557
558 #endif // wxUSE_LONGLONG_NATIVE
559
560 #if wxUSE_LONGLONG_WX
561
562 class WXDLLIMPEXP_BASE wxLongLongWx
563 {
564 public:
565 // ctors
566 // default ctor initializes to 0
567 wxLongLongWx()
568 {
569 m_lo = m_hi = 0;
570
571 #ifdef wxLONGLONG_TEST_MODE
572 m_ll = 0;
573
574 Check();
575 #endif // wxLONGLONG_TEST_MODE
576 }
577 // from long
578 wxLongLongWx(long l) { *this = l; }
579 // from 2 longs
580 wxLongLongWx(long hi, unsigned long lo)
581 {
582 m_hi = hi;
583 m_lo = lo;
584
585 #ifdef wxLONGLONG_TEST_MODE
586 m_ll = hi;
587 m_ll <<= 32;
588 m_ll |= lo;
589
590 Check();
591 #endif // wxLONGLONG_TEST_MODE
592 }
593
594 // default copy ctor is ok in both cases
595
596 // no dtor
597
598 // assignment operators
599 // from long
600 wxLongLongWx& operator=(long l)
601 {
602 m_lo = l;
603 m_hi = (l < 0 ? -1l : 0l);
604
605 #ifdef wxLONGLONG_TEST_MODE
606 m_ll = l;
607
608 Check();
609 #endif // wxLONGLONG_TEST_MODE
610
611 return *this;
612 }
613 // from int
614 wxLongLongWx& operator=(int l)
615 {
616 return operator=((long)l);
617 }
618
619 wxLongLongWx& operator=(unsigned long l)
620 {
621 m_lo = l;
622 m_hi = 0;
623
624 #ifdef wxLONGLONG_TEST_MODE
625 m_ll = l;
626
627 Check();
628 #endif // wxLONGLONG_TEST_MODE
629
630 return *this;
631 }
632 wxLongLongWx& operator=(const class wxULongLongWx &ll);
633
634 // from double
635 wxLongLongWx& Assign(double d);
636 // can't have assignment operator from 2 longs
637
638 // accessors
639 // get high part
640 long GetHi() const { return m_hi; }
641 // get low part
642 unsigned long GetLo() const { return m_lo; }
643
644 // get absolute value
645 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
646 wxLongLongWx& Abs()
647 {
648 if ( m_hi < 0 )
649 m_hi = -m_hi;
650
651 #ifdef wxLONGLONG_TEST_MODE
652 if ( m_ll < 0 )
653 m_ll = -m_ll;
654
655 Check();
656 #endif // wxLONGLONG_TEST_MODE
657
658 return *this;
659 }
660
661 // convert to long with range checking in debug mode (only!)
662 long ToLong() const
663 {
664 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
665 _T("wxLongLong to long conversion loss of precision") );
666
667 return (long)m_lo;
668 }
669
670 // convert to double
671 double ToDouble() const;
672
673 // operations
674 // addition
675 wxLongLongWx operator+(const wxLongLongWx& ll) const;
676 wxLongLongWx& operator+=(const wxLongLongWx& ll);
677 wxLongLongWx operator+(long l) const;
678 wxLongLongWx& operator+=(long l);
679
680 // pre increment operator
681 wxLongLongWx& operator++();
682
683 // post increment operator
684 wxLongLongWx& operator++(int) { return ++(*this); }
685
686 // negation operator
687 wxLongLongWx operator-() const;
688 wxLongLongWx& Negate();
689
690 // subraction
691 wxLongLongWx operator-(const wxLongLongWx& ll) const;
692 wxLongLongWx& operator-=(const wxLongLongWx& ll);
693
694 // pre decrement operator
695 wxLongLongWx& operator--();
696
697 // post decrement operator
698 wxLongLongWx& operator--(int) { return --(*this); }
699
700 // shifts
701 // left shift
702 wxLongLongWx operator<<(int shift) const;
703 wxLongLongWx& operator<<=(int shift);
704
705 // right shift
706 wxLongLongWx operator>>(int shift) const;
707 wxLongLongWx& operator>>=(int shift);
708
709 // bitwise operators
710 wxLongLongWx operator&(const wxLongLongWx& ll) const;
711 wxLongLongWx& operator&=(const wxLongLongWx& ll);
712 wxLongLongWx operator|(const wxLongLongWx& ll) const;
713 wxLongLongWx& operator|=(const wxLongLongWx& ll);
714 wxLongLongWx operator^(const wxLongLongWx& ll) const;
715 wxLongLongWx& operator^=(const wxLongLongWx& ll);
716 wxLongLongWx operator~() const;
717
718 // comparison
719 bool operator==(const wxLongLongWx& ll) const
720 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
721 #if wxUSE_LONGLONG_NATIVE
722 bool operator==(const wxLongLongNative& ll) const
723 { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); }
724 #endif
725 bool operator!=(const wxLongLongWx& ll) const
726 { return !(*this == ll); }
727 bool operator<(const wxLongLongWx& ll) const;
728 bool operator>(const wxLongLongWx& ll) const;
729 bool operator<=(const wxLongLongWx& ll) const
730 { return *this < ll || *this == ll; }
731 bool operator>=(const wxLongLongWx& ll) const
732 { return *this > ll || *this == ll; }
733
734 bool operator<(long l) const { return *this < wxLongLongWx(l); }
735 bool operator>(long l) const { return *this > wxLongLongWx(l); }
736 bool operator==(long l) const
737 {
738 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
739 : (m_hi == -1 && m_lo == (unsigned long)l);
740 }
741
742 bool operator<=(long l) const { return *this < l || *this == l; }
743 bool operator>=(long l) const { return *this > l || *this == l; }
744
745 // multiplication
746 wxLongLongWx operator*(const wxLongLongWx& ll) const;
747 wxLongLongWx& operator*=(const wxLongLongWx& ll);
748
749 // division
750 wxLongLongWx operator/(const wxLongLongWx& ll) const;
751 wxLongLongWx& operator/=(const wxLongLongWx& ll);
752
753 wxLongLongWx operator%(const wxLongLongWx& ll) const;
754
755 void Divide(const wxLongLongWx& divisor,
756 wxLongLongWx& quotient,
757 wxLongLongWx& remainder) const;
758
759 // input/output
760
761 // return the string representation of this number
762 wxString ToString() const;
763
764 void *asArray() const;
765
766 #if wxUSE_STD_IOSTREAM
767 friend WXDLLIMPEXP_BASE
768 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
769 #endif // wxUSE_STD_IOSTREAM
770
771 friend WXDLLIMPEXP_BASE
772 wxString& operator<<(wxString&, const wxLongLongWx&);
773
774 #if wxUSE_STREAMS
775 friend WXDLLIMPEXP_BASE
776 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongWx&);
777 friend WXDLLIMPEXP_BASE
778 class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongWx&);
779 #endif
780
781 private:
782 // long is at least 32 bits, so represent our 64bit number as 2 longs
783
784 long m_hi; // signed bit is in the high part
785 unsigned long m_lo;
786
787 #ifdef wxLONGLONG_TEST_MODE
788 void Check()
789 {
790 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
791 }
792
793 wxLongLong_t m_ll;
794 #endif // wxLONGLONG_TEST_MODE
795 };
796
797
798 class WXDLLIMPEXP_BASE wxULongLongWx
799 {
800 public:
801 // ctors
802 // default ctor initializes to 0
803 wxULongLongWx()
804 {
805 m_lo = m_hi = 0;
806
807 #ifdef wxLONGLONG_TEST_MODE
808 m_ll = 0;
809
810 Check();
811 #endif // wxLONGLONG_TEST_MODE
812 }
813 // from ulong
814 wxULongLongWx(unsigned long l) { *this = l; }
815 // from 2 ulongs
816 wxULongLongWx(unsigned long hi, unsigned long lo)
817 {
818 m_hi = hi;
819 m_lo = lo;
820
821 #ifdef wxLONGLONG_TEST_MODE
822 m_ll = hi;
823 m_ll <<= 32;
824 m_ll |= lo;
825
826 Check();
827 #endif // wxLONGLONG_TEST_MODE
828 }
829
830 // from signed to unsigned
831 wxULongLongWx(wxLongLongWx ll)
832 {
833 wxASSERT(ll.GetHi() >= 0);
834 m_hi = (unsigned long)ll.GetHi();
835 m_lo = ll.GetLo();
836 }
837
838 // default copy ctor is ok in both cases
839
840 // no dtor
841
842 // assignment operators
843 // from long
844 wxULongLongWx& operator=(unsigned long l)
845 {
846 m_lo = l;
847 m_hi = 0;
848
849 #ifdef wxLONGLONG_TEST_MODE
850 m_ll = l;
851
852 Check();
853 #endif // wxLONGLONG_TEST_MODE
854
855 return *this;
856 }
857 wxULongLongWx& operator=(long l)
858 {
859 m_lo = l;
860 m_hi = (unsigned long) ((l<0) ? -1l : 0);
861
862 #ifdef wxLONGLONG_TEST_MODE
863 m_ll = (wxULongLong_t) (wxLongLong_t) l;
864
865 Check();
866 #endif // wxLONGLONG_TEST_MODE
867
868 return *this;
869 }
870 wxULongLongWx& operator=(const class wxLongLongWx &ll) {
871 // Should we use an assert like it was before in the constructor?
872 // wxASSERT(ll.GetHi() >= 0);
873 m_hi = (unsigned long)ll.GetHi();
874 m_lo = ll.GetLo();
875 return *this;
876 }
877
878 // can't have assignment operator from 2 longs
879
880 // accessors
881 // get high part
882 unsigned long GetHi() const { return m_hi; }
883 // get low part
884 unsigned long GetLo() const { return m_lo; }
885
886 // convert to long with range checking in debug mode (only!)
887 unsigned long ToULong() const
888 {
889 wxASSERT_MSG( m_hi == 0ul,
890 _T("wxULongLong to long conversion loss of precision") );
891
892 return (unsigned long)m_lo;
893 }
894
895 // operations
896 // addition
897 wxULongLongWx operator+(const wxULongLongWx& ll) const;
898 wxULongLongWx& operator+=(const wxULongLongWx& ll);
899 wxULongLongWx operator+(unsigned long l) const;
900 wxULongLongWx& operator+=(unsigned long l);
901
902 // pre increment operator
903 wxULongLongWx& operator++();
904
905 // post increment operator
906 wxULongLongWx& operator++(int) { return ++(*this); }
907
908 // subtraction
909 wxLongLongWx operator-(const wxULongLongWx& ll) const;
910 wxULongLongWx& operator-=(const wxULongLongWx& ll);
911
912 // pre decrement operator
913 wxULongLongWx& operator--();
914
915 // post decrement operator
916 wxULongLongWx& operator--(int) { return --(*this); }
917
918 // shifts
919 // left shift
920 wxULongLongWx operator<<(int shift) const;
921 wxULongLongWx& operator<<=(int shift);
922
923 // right shift
924 wxULongLongWx operator>>(int shift) const;
925 wxULongLongWx& operator>>=(int shift);
926
927 // bitwise operators
928 wxULongLongWx operator&(const wxULongLongWx& ll) const;
929 wxULongLongWx& operator&=(const wxULongLongWx& ll);
930 wxULongLongWx operator|(const wxULongLongWx& ll) const;
931 wxULongLongWx& operator|=(const wxULongLongWx& ll);
932 wxULongLongWx operator^(const wxULongLongWx& ll) const;
933 wxULongLongWx& operator^=(const wxULongLongWx& ll);
934 wxULongLongWx operator~() const;
935
936 // comparison
937 bool operator==(const wxULongLongWx& ll) const
938 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
939 bool operator!=(const wxULongLongWx& ll) const
940 { return !(*this == ll); }
941 bool operator<(const wxULongLongWx& ll) const;
942 bool operator>(const wxULongLongWx& ll) const;
943 bool operator<=(const wxULongLongWx& ll) const
944 { return *this < ll || *this == ll; }
945 bool operator>=(const wxULongLongWx& ll) const
946 { return *this > ll || *this == ll; }
947
948 bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
949 bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
950 bool operator==(unsigned long l) const
951 {
952 return (m_hi == 0 && m_lo == (unsigned long)l);
953 }
954
955 bool operator<=(unsigned long l) const { return *this < l || *this == l; }
956 bool operator>=(unsigned long l) const { return *this > l || *this == l; }
957
958 // multiplication
959 wxULongLongWx operator*(const wxULongLongWx& ll) const;
960 wxULongLongWx& operator*=(const wxULongLongWx& ll);
961
962 // division
963 wxULongLongWx operator/(const wxULongLongWx& ll) const;
964 wxULongLongWx& operator/=(const wxULongLongWx& ll);
965
966 wxULongLongWx operator%(const wxULongLongWx& ll) const;
967
968 void Divide(const wxULongLongWx& divisor,
969 wxULongLongWx& quotient,
970 wxULongLongWx& remainder) const;
971
972 // input/output
973
974 // return the string representation of this number
975 wxString ToString() const;
976
977 void *asArray() const;
978
979 #if wxUSE_STD_IOSTREAM
980 friend WXDLLIMPEXP_BASE
981 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
982 #endif // wxUSE_STD_IOSTREAM
983
984 friend WXDLLIMPEXP_BASE
985 wxString& operator<<(wxString&, const wxULongLongWx&);
986
987 #if wxUSE_STREAMS
988 friend WXDLLIMPEXP_BASE
989 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongWx&);
990 friend WXDLLIMPEXP_BASE
991 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongWx&);
992 #endif
993
994 private:
995 // long is at least 32 bits, so represent our 64bit number as 2 longs
996
997 unsigned long m_hi;
998 unsigned long m_lo;
999
1000 #ifdef wxLONGLONG_TEST_MODE
1001 void Check()
1002 {
1003 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
1004 }
1005
1006 wxULongLong_t m_ll;
1007 #endif // wxLONGLONG_TEST_MODE
1008 };
1009
1010 #endif // wxUSE_LONGLONG_WX
1011
1012 // ----------------------------------------------------------------------------
1013 // binary operators
1014 // ----------------------------------------------------------------------------
1015
1016 inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
1017 inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
1018 inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
1019 inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
1020 inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
1021 inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
1022
1023 inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
1024 inline wxLongLong operator-(long l, const wxLongLong& ll)
1025 {
1026 return wxLongLong(l) - ll;
1027 }
1028
1029 inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
1030 inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
1031 inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
1032 inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
1033 inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
1034 inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
1035
1036 inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
1037
1038 inline wxLongLong operator-(unsigned long l, const wxULongLong& ull)
1039 {
1040 wxULongLong ret = wxULongLong(l) - ull;
1041 return wxLongLong((long)ret.GetHi(),ret.GetLo());
1042 }
1043
1044 #if wxUSE_LONGLONG_NATIVE && wxUSE_STREAMS
1045
1046 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxULongLong_t value);
1047 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxLongLong_t value);
1048
1049 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxULongLong_t &value);
1050 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxLongLong_t &value);
1051
1052 #endif
1053
1054 #endif // wxUSE_LONGLONG
1055
1056 #endif // _WX_LONGLONG_H