]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
Don't specialize std::numeric_limits<> for wxLongLong when using VC6.
[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_FWD_BASE wxLongLongWx;
71 class WXDLLIMPEXP_FWD_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_FWD_BASE wxLongLongNative;
90 class WXDLLIMPEXP_FWD_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(wxInt32 hi, wxUint32 lo)
117 {
118 // cast to wxLongLong_t 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 wxInt32 GetHi() const
164 { return wx_truncate_cast(wxInt32, m_ll >> 32); }
165 // get low part
166 wxUint32 GetLo() const
167 { return wx_truncate_cast(wxUint32, 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 wxT("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(wxUint32 hi, wxUint32 lo) : m_ll(0)
354 {
355 // cast to wxLongLong_t 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 wxUint32 GetHi() const
396 { return wx_truncate_cast(wxUint32, m_ll >> 32); }
397 // get low part
398 wxUint32 GetLo() const
399 { return wx_truncate_cast(wxUint32, 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 wxT("wxULongLong to long conversion loss of precision") );
409
410 return wx_truncate_cast(unsigned long, m_ll);
411 }
412
413 // convert to double
414 double ToDouble() const { return wx_truncate_cast(double, m_ll); }
415
416 // operations
417 // addition
418 wxULongLongNative operator+(const wxULongLongNative& ll) const
419 { return wxULongLongNative(m_ll + ll.m_ll); }
420 wxULongLongNative& operator+=(const wxULongLongNative& ll)
421 { m_ll += ll.m_ll; return *this; }
422
423 wxULongLongNative operator+(const wxULongLong_t ll) const
424 { return wxULongLongNative(m_ll + ll); }
425 wxULongLongNative& operator+=(const wxULongLong_t ll)
426 { m_ll += ll; return *this; }
427
428 // pre increment
429 wxULongLongNative& operator++()
430 { m_ll++; return *this; }
431
432 // post increment
433 wxULongLongNative operator++(int)
434 { wxULongLongNative value(*this); m_ll++; return value; }
435
436 // subtraction
437 wxULongLongNative operator-(const wxULongLongNative& ll) const
438 { return wxULongLongNative(m_ll - ll.m_ll); }
439 wxULongLongNative& operator-=(const wxULongLongNative& ll)
440 { m_ll -= ll.m_ll; return *this; }
441
442 wxULongLongNative operator-(const wxULongLong_t ll) const
443 { return wxULongLongNative(m_ll - ll); }
444 wxULongLongNative& operator-=(const wxULongLong_t ll)
445 { m_ll -= ll; return *this; }
446
447 // pre decrement
448 wxULongLongNative& operator--()
449 { m_ll--; return *this; }
450
451 // post decrement
452 wxULongLongNative operator--(int)
453 { wxULongLongNative value(*this); m_ll--; return value; }
454
455 // shifts
456 // left shift
457 wxULongLongNative operator<<(int shift) const
458 { return wxULongLongNative(m_ll << shift); }
459 wxULongLongNative& operator<<=(int shift)
460 { m_ll <<= shift; return *this; }
461
462 // right shift
463 wxULongLongNative operator>>(int shift) const
464 { return wxULongLongNative(m_ll >> shift); }
465 wxULongLongNative& operator>>=(int shift)
466 { m_ll >>= shift; return *this; }
467
468 // bitwise operators
469 wxULongLongNative operator&(const wxULongLongNative& ll) const
470 { return wxULongLongNative(m_ll & ll.m_ll); }
471 wxULongLongNative& operator&=(const wxULongLongNative& ll)
472 { m_ll &= ll.m_ll; return *this; }
473
474 wxULongLongNative operator|(const wxULongLongNative& ll) const
475 { return wxULongLongNative(m_ll | ll.m_ll); }
476 wxULongLongNative& operator|=(const wxULongLongNative& ll)
477 { m_ll |= ll.m_ll; return *this; }
478
479 wxULongLongNative operator^(const wxULongLongNative& ll) const
480 { return wxULongLongNative(m_ll ^ ll.m_ll); }
481 wxULongLongNative& operator^=(const wxULongLongNative& ll)
482 { m_ll ^= ll.m_ll; return *this; }
483
484 // multiplication/division
485 wxULongLongNative operator*(const wxULongLongNative& ll) const
486 { return wxULongLongNative(m_ll * ll.m_ll); }
487 wxULongLongNative operator*(unsigned long l) const
488 { return wxULongLongNative(m_ll * l); }
489 wxULongLongNative& operator*=(const wxULongLongNative& ll)
490 { m_ll *= ll.m_ll; return *this; }
491 wxULongLongNative& operator*=(unsigned long l)
492 { m_ll *= l; return *this; }
493
494 wxULongLongNative operator/(const wxULongLongNative& ll) const
495 { return wxULongLongNative(m_ll / ll.m_ll); }
496 wxULongLongNative operator/(unsigned long l) const
497 { return wxULongLongNative(m_ll / l); }
498 wxULongLongNative& operator/=(const wxULongLongNative& ll)
499 { m_ll /= ll.m_ll; return *this; }
500 wxULongLongNative& operator/=(unsigned long l)
501 { m_ll /= l; return *this; }
502
503 wxULongLongNative operator%(const wxULongLongNative& ll) const
504 { return wxULongLongNative(m_ll % ll.m_ll); }
505 wxULongLongNative operator%(unsigned long l) const
506 { return wxULongLongNative(m_ll % l); }
507
508 // comparison
509 bool operator==(const wxULongLongNative& ll) const
510 { return m_ll == ll.m_ll; }
511 bool operator==(unsigned long l) const
512 { return m_ll == l; }
513 bool operator!=(const wxULongLongNative& ll) const
514 { return m_ll != ll.m_ll; }
515 bool operator!=(unsigned long l) const
516 { return m_ll != l; }
517 bool operator<(const wxULongLongNative& ll) const
518 { return m_ll < ll.m_ll; }
519 bool operator<(unsigned long l) const
520 { return m_ll < l; }
521 bool operator>(const wxULongLongNative& ll) const
522 { return m_ll > ll.m_ll; }
523 bool operator>(unsigned long l) const
524 { return m_ll > l; }
525 bool operator<=(const wxULongLongNative& ll) const
526 { return m_ll <= ll.m_ll; }
527 bool operator<=(unsigned long l) const
528 { return m_ll <= l; }
529 bool operator>=(const wxULongLongNative& ll) const
530 { return m_ll >= ll.m_ll; }
531 bool operator>=(unsigned long l) const
532 { return m_ll >= l; }
533
534 // miscellaneous
535
536 // return the string representation of this number
537 wxString ToString() const;
538
539 // conversion to byte array: returns a pointer to static buffer!
540 void *asArray() const;
541
542 #if wxUSE_STD_IOSTREAM
543 // input/output
544 friend WXDLLIMPEXP_BASE
545 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
546 #endif
547
548 friend WXDLLIMPEXP_BASE
549 wxString& operator<<(wxString&, const wxULongLongNative&);
550
551 #if wxUSE_STREAMS
552 friend WXDLLIMPEXP_BASE
553 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongNative&);
554 friend WXDLLIMPEXP_BASE
555 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongNative&);
556 #endif
557
558 private:
559 wxULongLong_t m_ll;
560 };
561
562 inline
563 wxLongLongNative& wxLongLongNative::operator=(const wxULongLongNative &ll)
564 {
565 m_ll = ll.GetValue();
566 return *this;
567 }
568
569 #endif // wxUSE_LONGLONG_NATIVE
570
571 #if wxUSE_LONGLONG_WX
572
573 class WXDLLIMPEXP_BASE wxLongLongWx
574 {
575 public:
576 // ctors
577 // default ctor initializes to 0
578 wxLongLongWx()
579 {
580 m_lo = m_hi = 0;
581
582 #ifdef wxLONGLONG_TEST_MODE
583 m_ll = 0;
584
585 Check();
586 #endif // wxLONGLONG_TEST_MODE
587 }
588 // from long
589 wxLongLongWx(long l) { *this = l; }
590 // from 2 longs
591 wxLongLongWx(long hi, unsigned long lo)
592 {
593 m_hi = hi;
594 m_lo = lo;
595
596 #ifdef wxLONGLONG_TEST_MODE
597 m_ll = hi;
598 m_ll <<= 32;
599 m_ll |= lo;
600
601 Check();
602 #endif // wxLONGLONG_TEST_MODE
603 }
604
605 // default copy ctor is ok in both cases
606
607 // no dtor
608
609 // assignment operators
610 // from long
611 wxLongLongWx& operator=(long l)
612 {
613 m_lo = l;
614 m_hi = (l < 0 ? -1l : 0l);
615
616 #ifdef wxLONGLONG_TEST_MODE
617 m_ll = l;
618
619 Check();
620 #endif // wxLONGLONG_TEST_MODE
621
622 return *this;
623 }
624 // from int
625 wxLongLongWx& operator=(int l)
626 {
627 return operator=((long)l);
628 }
629
630 wxLongLongWx& operator=(unsigned long l)
631 {
632 m_lo = l;
633 m_hi = 0;
634
635 #ifdef wxLONGLONG_TEST_MODE
636 m_ll = l;
637
638 Check();
639 #endif // wxLONGLONG_TEST_MODE
640
641 return *this;
642 }
643
644 wxLongLongWx& operator=(unsigned int l)
645 {
646 return operator=((unsigned long)l);
647 }
648
649 wxLongLongWx& operator=(const class wxULongLongWx &ll);
650
651 // from double
652 wxLongLongWx& Assign(double d);
653 // can't have assignment operator from 2 longs
654
655 // accessors
656 // get high part
657 long GetHi() const { return m_hi; }
658 // get low part
659 unsigned long GetLo() const { return m_lo; }
660
661 // get absolute value
662 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
663 wxLongLongWx& Abs()
664 {
665 if ( m_hi < 0 )
666 m_hi = -m_hi;
667
668 #ifdef wxLONGLONG_TEST_MODE
669 if ( m_ll < 0 )
670 m_ll = -m_ll;
671
672 Check();
673 #endif // wxLONGLONG_TEST_MODE
674
675 return *this;
676 }
677
678 // convert to long with range checking in debug mode (only!)
679 long ToLong() const
680 {
681 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
682 wxT("wxLongLong to long conversion loss of precision") );
683
684 return (long)m_lo;
685 }
686
687 // convert to double
688 double ToDouble() const;
689
690 // operations
691 // addition
692 wxLongLongWx operator+(const wxLongLongWx& ll) const;
693 wxLongLongWx& operator+=(const wxLongLongWx& ll);
694 wxLongLongWx operator+(long l) const;
695 wxLongLongWx& operator+=(long l);
696
697 // pre increment operator
698 wxLongLongWx& operator++();
699
700 // post increment operator
701 wxLongLongWx& operator++(int) { return ++(*this); }
702
703 // negation operator
704 wxLongLongWx operator-() const;
705 wxLongLongWx& Negate();
706
707 // subraction
708 wxLongLongWx operator-(const wxLongLongWx& ll) const;
709 wxLongLongWx& operator-=(const wxLongLongWx& ll);
710
711 // pre decrement operator
712 wxLongLongWx& operator--();
713
714 // post decrement operator
715 wxLongLongWx& operator--(int) { return --(*this); }
716
717 // shifts
718 // left shift
719 wxLongLongWx operator<<(int shift) const;
720 wxLongLongWx& operator<<=(int shift);
721
722 // right shift
723 wxLongLongWx operator>>(int shift) const;
724 wxLongLongWx& operator>>=(int shift);
725
726 // bitwise operators
727 wxLongLongWx operator&(const wxLongLongWx& ll) const;
728 wxLongLongWx& operator&=(const wxLongLongWx& ll);
729 wxLongLongWx operator|(const wxLongLongWx& ll) const;
730 wxLongLongWx& operator|=(const wxLongLongWx& ll);
731 wxLongLongWx operator^(const wxLongLongWx& ll) const;
732 wxLongLongWx& operator^=(const wxLongLongWx& ll);
733 wxLongLongWx operator~() const;
734
735 // comparison
736 bool operator==(const wxLongLongWx& ll) const
737 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
738 #if wxUSE_LONGLONG_NATIVE
739 bool operator==(const wxLongLongNative& ll) const
740 { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); }
741 #endif
742 bool operator!=(const wxLongLongWx& ll) const
743 { return !(*this == ll); }
744 bool operator<(const wxLongLongWx& ll) const;
745 bool operator>(const wxLongLongWx& ll) const;
746 bool operator<=(const wxLongLongWx& ll) const
747 { return *this < ll || *this == ll; }
748 bool operator>=(const wxLongLongWx& ll) const
749 { return *this > ll || *this == ll; }
750
751 bool operator<(long l) const { return *this < wxLongLongWx(l); }
752 bool operator>(long l) const { return *this > wxLongLongWx(l); }
753 bool operator==(long l) const
754 {
755 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
756 : (m_hi == -1 && m_lo == (unsigned long)l);
757 }
758
759 bool operator<=(long l) const { return *this < l || *this == l; }
760 bool operator>=(long l) const { return *this > l || *this == l; }
761
762 // multiplication
763 wxLongLongWx operator*(const wxLongLongWx& ll) const;
764 wxLongLongWx& operator*=(const wxLongLongWx& ll);
765
766 // division
767 wxLongLongWx operator/(const wxLongLongWx& ll) const;
768 wxLongLongWx& operator/=(const wxLongLongWx& ll);
769
770 wxLongLongWx operator%(const wxLongLongWx& ll) const;
771
772 void Divide(const wxLongLongWx& divisor,
773 wxLongLongWx& quotient,
774 wxLongLongWx& remainder) const;
775
776 // input/output
777
778 // return the string representation of this number
779 wxString ToString() const;
780
781 void *asArray() const;
782
783 #if wxUSE_STD_IOSTREAM
784 friend WXDLLIMPEXP_BASE
785 wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
786 #endif // wxUSE_STD_IOSTREAM
787
788 friend WXDLLIMPEXP_BASE
789 wxString& operator<<(wxString&, const wxLongLongWx&);
790
791 #if wxUSE_STREAMS
792 friend WXDLLIMPEXP_BASE
793 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongWx&);
794 friend WXDLLIMPEXP_BASE
795 class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongWx&);
796 #endif
797
798 private:
799 // long is at least 32 bits, so represent our 64bit number as 2 longs
800
801 long m_hi; // signed bit is in the high part
802 unsigned long m_lo;
803
804 #ifdef wxLONGLONG_TEST_MODE
805 void Check()
806 {
807 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
808 }
809
810 wxLongLong_t m_ll;
811 #endif // wxLONGLONG_TEST_MODE
812 };
813
814
815 class WXDLLIMPEXP_BASE wxULongLongWx
816 {
817 public:
818 // ctors
819 // default ctor initializes to 0
820 wxULongLongWx()
821 {
822 m_lo = m_hi = 0;
823
824 #ifdef wxLONGLONG_TEST_MODE
825 m_ll = 0;
826
827 Check();
828 #endif // wxLONGLONG_TEST_MODE
829 }
830 // from ulong
831 wxULongLongWx(unsigned long l) { *this = l; }
832 // from 2 ulongs
833 wxULongLongWx(unsigned long hi, unsigned long lo)
834 {
835 m_hi = hi;
836 m_lo = lo;
837
838 #ifdef wxLONGLONG_TEST_MODE
839 m_ll = hi;
840 m_ll <<= 32;
841 m_ll |= lo;
842
843 Check();
844 #endif // wxLONGLONG_TEST_MODE
845 }
846
847 // from signed to unsigned
848 wxULongLongWx(wxLongLongWx ll)
849 {
850 wxASSERT(ll.GetHi() >= 0);
851 m_hi = (unsigned long)ll.GetHi();
852 m_lo = ll.GetLo();
853 }
854
855 // default copy ctor is ok in both cases
856
857 // no dtor
858
859 // assignment operators
860 // from long
861 wxULongLongWx& operator=(unsigned long l)
862 {
863 m_lo = l;
864 m_hi = 0;
865
866 #ifdef wxLONGLONG_TEST_MODE
867 m_ll = l;
868
869 Check();
870 #endif // wxLONGLONG_TEST_MODE
871
872 return *this;
873 }
874 wxULongLongWx& operator=(long l)
875 {
876 m_lo = l;
877 m_hi = (unsigned long) ((l<0) ? -1l : 0);
878
879 #ifdef wxLONGLONG_TEST_MODE
880 m_ll = (wxULongLong_t) (wxLongLong_t) l;
881
882 Check();
883 #endif // wxLONGLONG_TEST_MODE
884
885 return *this;
886 }
887 wxULongLongWx& operator=(const class wxLongLongWx &ll) {
888 // Should we use an assert like it was before in the constructor?
889 // wxASSERT(ll.GetHi() >= 0);
890 m_hi = (unsigned long)ll.GetHi();
891 m_lo = ll.GetLo();
892 return *this;
893 }
894
895 // can't have assignment operator from 2 longs
896
897 // accessors
898 // get high part
899 unsigned long GetHi() const { return m_hi; }
900 // get low part
901 unsigned long GetLo() const { return m_lo; }
902
903 // convert to long with range checking in debug mode (only!)
904 unsigned long ToULong() const
905 {
906 wxASSERT_MSG( m_hi == 0ul,
907 wxT("wxULongLong to long conversion loss of precision") );
908
909 return (unsigned long)m_lo;
910 }
911
912 // convert to double
913 double ToDouble() const;
914
915 // operations
916 // addition
917 wxULongLongWx operator+(const wxULongLongWx& ll) const;
918 wxULongLongWx& operator+=(const wxULongLongWx& ll);
919 wxULongLongWx operator+(unsigned long l) const;
920 wxULongLongWx& operator+=(unsigned long l);
921
922 // pre increment operator
923 wxULongLongWx& operator++();
924
925 // post increment operator
926 wxULongLongWx& operator++(int) { return ++(*this); }
927
928 // subtraction
929 wxLongLongWx operator-(const wxULongLongWx& ll) const;
930 wxULongLongWx& operator-=(const wxULongLongWx& ll);
931
932 // pre decrement operator
933 wxULongLongWx& operator--();
934
935 // post decrement operator
936 wxULongLongWx& operator--(int) { return --(*this); }
937
938 // shifts
939 // left shift
940 wxULongLongWx operator<<(int shift) const;
941 wxULongLongWx& operator<<=(int shift);
942
943 // right shift
944 wxULongLongWx operator>>(int shift) const;
945 wxULongLongWx& operator>>=(int shift);
946
947 // bitwise operators
948 wxULongLongWx operator&(const wxULongLongWx& ll) const;
949 wxULongLongWx& operator&=(const wxULongLongWx& ll);
950 wxULongLongWx operator|(const wxULongLongWx& ll) const;
951 wxULongLongWx& operator|=(const wxULongLongWx& ll);
952 wxULongLongWx operator^(const wxULongLongWx& ll) const;
953 wxULongLongWx& operator^=(const wxULongLongWx& ll);
954 wxULongLongWx operator~() const;
955
956 // comparison
957 bool operator==(const wxULongLongWx& ll) const
958 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
959 bool operator!=(const wxULongLongWx& ll) const
960 { return !(*this == ll); }
961 bool operator<(const wxULongLongWx& ll) const;
962 bool operator>(const wxULongLongWx& ll) const;
963 bool operator<=(const wxULongLongWx& ll) const
964 { return *this < ll || *this == ll; }
965 bool operator>=(const wxULongLongWx& ll) const
966 { return *this > ll || *this == ll; }
967
968 bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
969 bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
970 bool operator==(unsigned long l) const
971 {
972 return (m_hi == 0 && m_lo == (unsigned long)l);
973 }
974
975 bool operator<=(unsigned long l) const { return *this < l || *this == l; }
976 bool operator>=(unsigned long l) const { return *this > l || *this == l; }
977
978 // multiplication
979 wxULongLongWx operator*(const wxULongLongWx& ll) const;
980 wxULongLongWx& operator*=(const wxULongLongWx& ll);
981
982 // division
983 wxULongLongWx operator/(const wxULongLongWx& ll) const;
984 wxULongLongWx& operator/=(const wxULongLongWx& ll);
985
986 wxULongLongWx operator%(const wxULongLongWx& ll) const;
987
988 void Divide(const wxULongLongWx& divisor,
989 wxULongLongWx& quotient,
990 wxULongLongWx& remainder) const;
991
992 // input/output
993
994 // return the string representation of this number
995 wxString ToString() const;
996
997 void *asArray() const;
998
999 #if wxUSE_STD_IOSTREAM
1000 friend WXDLLIMPEXP_BASE
1001 wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
1002 #endif // wxUSE_STD_IOSTREAM
1003
1004 friend WXDLLIMPEXP_BASE
1005 wxString& operator<<(wxString&, const wxULongLongWx&);
1006
1007 #if wxUSE_STREAMS
1008 friend WXDLLIMPEXP_BASE
1009 class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongWx&);
1010 friend WXDLLIMPEXP_BASE
1011 class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongWx&);
1012 #endif
1013
1014 private:
1015 // long is at least 32 bits, so represent our 64bit number as 2 longs
1016
1017 unsigned long m_hi;
1018 unsigned long m_lo;
1019
1020 #ifdef wxLONGLONG_TEST_MODE
1021 void Check()
1022 {
1023 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
1024 }
1025
1026 wxULongLong_t m_ll;
1027 #endif // wxLONGLONG_TEST_MODE
1028 };
1029
1030 #endif // wxUSE_LONGLONG_WX
1031
1032 // ----------------------------------------------------------------------------
1033 // binary operators
1034 // ----------------------------------------------------------------------------
1035
1036 inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
1037 inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
1038 inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
1039 inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
1040 inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
1041 inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
1042
1043 inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
1044 inline wxLongLong operator-(long l, const wxLongLong& ll)
1045 {
1046 return wxLongLong(l) - ll;
1047 }
1048
1049 inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
1050 inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
1051 inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
1052 inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
1053 inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
1054 inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
1055
1056 inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
1057
1058 inline wxLongLong operator-(unsigned long l, const wxULongLong& ull)
1059 {
1060 wxULongLong ret = wxULongLong(l) - ull;
1061 return wxLongLong((long)ret.GetHi(),ret.GetLo());
1062 }
1063
1064 #if wxUSE_LONGLONG_NATIVE && wxUSE_STREAMS
1065
1066 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxULongLong_t value);
1067 WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxLongLong_t value);
1068
1069 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxULongLong_t &value);
1070 WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxLongLong_t &value);
1071
1072 #endif
1073
1074 // ----------------------------------------------------------------------------
1075 // Specialize numeric_limits<> for our long long wrapper classes.
1076 // ----------------------------------------------------------------------------
1077
1078 #if wxUSE_LONGLONG_NATIVE
1079
1080 // VC6 is known to not have __int64 specializations of numeric_limits<> in its
1081 // <limits> anyhow so don't bother including it, especially as it results in
1082 // tons of warnings because the standard header itself uses obsolete template
1083 // specialization syntax.
1084 #ifndef __VISUALC6__
1085
1086 #include <limits>
1087
1088 namespace std
1089 {
1090
1091 template<> class numeric_limits<wxLongLong>
1092 : public numeric_limits<wxLongLong_t>
1093 {
1094 };
1095
1096 template<> class numeric_limits<wxULongLong>
1097 : public numeric_limits<wxULongLong_t>
1098 {
1099 };
1100
1101 } // namespace std
1102
1103 #endif // !VC6
1104
1105 #endif // wxUSE_LONGLONG_NATIVE
1106
1107 #endif // wxUSE_LONGLONG
1108
1109 #endif // _WX_LONGLONG_H