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