]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
who forward declared wcslen() without using extern "C" around it?? and why was it...
[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 license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_LONGLONG_H
14 #define _WX_LONGLONG_H
15
16 #ifdef __GNUG__
17 #pragma interface "longlong.h"
18 #endif
19
20 #include "wx/defs.h"
21 #include "wx/string.h"
22
23 #include <limits.h> // for LONG_MAX
24
25 // define this to compile wxLongLongWx in "test" mode: the results of all
26 // calculations will be compared with the real results taken from
27 // wxLongLongNative -- this is extremely useful to find the bugs in
28 // wxLongLongWx class!
29
30 // #define wxLONGLONG_TEST_MODE
31
32 #ifdef wxLONGLONG_TEST_MODE
33 #define wxUSE_LONGLONG_WX 1
34 #define wxUSE_LONGLONG_NATIVE 1
35 #endif // wxLONGLONG_TEST_MODE
36
37 // ----------------------------------------------------------------------------
38 // decide upon which class we will use
39 // ----------------------------------------------------------------------------
40
41 // to avoid compilation problems on 64bit machines with ambiguous method calls
42 // we will need to define this
43 #undef wxLongLongIsLong
44
45 // NB: we #define and not typedef wxLongLong_t because we want to be able to
46 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
47 // wxLongLong_t" below
48
49 // first check for generic cases which are long on 64bit machine and "long
50 // long", then check for specific compilers
51 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
52 #define wxLongLong_t long
53 #define wxLongLongIsLong
54 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
55 #define wxLongLong_t __int64
56 #elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520)
57 #define wxLongLong_t __int64
58 #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8
59 #define wxLongLong_t long long
60 #elif defined(__MINGW32__)
61 #define wxLongLong_t long long
62 #elif defined(__MWERKS__)
63 #if __option(longlong)
64 #define wxLongLong_t long long
65 #else
66 #error "The 64 bit integer support in CodeWarrior has been disabled."
67 #error "See the documentation on the 'longlong' pragma."
68 #endif
69 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
70 #define wxLongLong_t long long
71 #else // no native long long type
72 // both warning and pragma warning are not portable, but at least an
73 // unknown pragma should never be an error - except that, actually, some
74 // broken compilers don't like it, so we have to disable it in this case
75 // <sigh>
76 #if !(defined(__WATCOMC__) || defined(__VISAGECPP__))
77 #pragma warning "Your compiler does not appear to support 64 bit "\
78 "integers, using emulation class instead.\n" \
79 "Please report your compiler version to " \
80 "wx-dev@lists.wxwindows.org!"
81 #endif
82
83 #define wxUSE_LONGLONG_WX 1
84 #endif // compiler
85
86 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
87 // to disable automatic testing (useful for the test program which defines
88 // both classes) but by default we only use one class
89 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
90 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
91 // this is useful in test programs and only there
92 #ifndef wxUSE_LONGLONG_NATIVE
93 #define wxUSE_LONGLONG_NATIVE 0
94 #endif
95
96 class WXDLLEXPORT wxLongLongWx;
97 #if defined(__VISUALC__) && !defined(__WIN32__)
98 #define wxLongLong wxLongLongWx
99 #else
100 typedef wxLongLongWx wxLongLong;
101 #endif
102
103 #else
104 // if nothing is defined, use native implementation by default, of course
105 #ifndef wxUSE_LONGLONG_NATIVE
106 #define wxUSE_LONGLONG_NATIVE 1
107 #endif
108 #endif
109
110 #ifndef wxUSE_LONGLONG_WX
111 #define wxUSE_LONGLONG_WX 0
112 class WXDLLEXPORT wxLongLongNative;
113 typedef wxLongLongNative wxLongLong;
114 #endif
115
116 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
117 // typedef wxLongLong as it wants, we don't do it
118
119 // ----------------------------------------------------------------------------
120 // choose the appropriate class
121 // ----------------------------------------------------------------------------
122
123 // we use iostream for wxLongLong output
124 #include "wx/ioswrap.h"
125
126 #if wxUSE_LONGLONG_NATIVE
127
128 class WXDLLEXPORT wxLongLongNative
129 {
130 public:
131 // ctors
132 // default ctor initializes to 0
133 wxLongLongNative() { m_ll = 0; }
134 // from long long
135 wxLongLongNative(wxLongLong_t ll) { m_ll = ll; }
136 // from 2 longs
137 wxLongLongNative(long hi, unsigned long lo)
138 {
139 // assign first to avoid precision loss!
140 m_ll = ((wxLongLong_t) hi) << 32;
141 m_ll |= (wxLongLong_t) lo;
142 }
143
144 // default copy ctor is ok
145
146 // no dtor
147
148 // assignment operators
149 // from native 64 bit integer
150 wxLongLongNative& operator=(wxLongLong_t ll)
151 { m_ll = ll; return *this; }
152
153 // from double: this one has an explicit name because otherwise we
154 // would have ambiguity with "ll = int" and also because we don't want
155 // to have implicit conversions between doubles and wxLongLongs
156 wxLongLongNative& Assign(double d)
157 { m_ll = (wxLongLong_t)d; return *this; }
158
159 // assignment operators from wxLongLongNative is ok
160
161 // accessors
162 // get high part
163 long GetHi() const
164 { return (long)(m_ll >> 32); }
165 // get low part
166 unsigned long GetLo() const
167 { return (unsigned long)m_ll; }
168
169 // get absolute value
170 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
171 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
172
173 // convert to native long long
174 wxLongLong_t GetValue() const { return m_ll; }
175
176 // convert to long with range checking in the debug mode (only!)
177 long ToLong() const
178 {
179 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
180 _T("wxLongLong to long conversion loss of precision") );
181
182 return (long)m_ll;
183 }
184
185 // don't provide implicit conversion to wxLongLong_t or we will have an
186 // ambiguity for all arithmetic operations
187 //operator wxLongLong_t() const { return m_ll; }
188
189 // operations
190 // addition
191 wxLongLongNative operator+(const wxLongLongNative& ll) const
192 { return wxLongLongNative(m_ll + ll.m_ll); }
193 wxLongLongNative& operator+=(const wxLongLongNative& ll)
194 { m_ll += ll.m_ll; return *this; }
195
196 wxLongLongNative operator+(const wxLongLong_t ll) const
197 { return wxLongLongNative(m_ll + ll); }
198 wxLongLongNative& operator+=(const wxLongLong_t ll)
199 { m_ll += ll; return *this; }
200
201 // pre increment
202 wxLongLongNative& operator++()
203 { m_ll++; return *this; }
204
205 // post increment
206 wxLongLongNative& operator++(int)
207 { m_ll++; return *this; }
208
209 // negation operator
210 wxLongLongNative operator-() const
211 { return wxLongLongNative(-m_ll); }
212 wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
213
214 // subtraction
215 wxLongLongNative operator-(const wxLongLongNative& ll) const
216 { return wxLongLongNative(m_ll - ll.m_ll); }
217 wxLongLongNative& operator-=(const wxLongLongNative& ll)
218 { m_ll -= ll.m_ll; return *this; }
219
220 wxLongLongNative operator-(const wxLongLong_t ll) const
221 { return wxLongLongNative(m_ll - ll); }
222 wxLongLongNative& operator-=(const wxLongLong_t ll)
223 { m_ll -= ll; return *this; }
224
225 // pre decrement
226 wxLongLongNative& operator--()
227 { m_ll--; return *this; }
228
229 // post decrement
230 wxLongLongNative& operator--(int)
231 { m_ll--; return *this; }
232
233 // shifts
234 // left shift
235 wxLongLongNative operator<<(int shift) const
236 { return wxLongLongNative(m_ll << shift);; }
237 wxLongLongNative& operator<<=(int shift)
238 { m_ll <<= shift; return *this; }
239
240 // right shift
241 wxLongLongNative operator>>(int shift) const
242 { return wxLongLongNative(m_ll >> shift);; }
243 wxLongLongNative& operator>>=(int shift)
244 { m_ll >>= shift; return *this; }
245
246 // bitwise operators
247 wxLongLongNative operator&(const wxLongLongNative& ll) const
248 { return wxLongLongNative(m_ll & ll.m_ll); }
249 wxLongLongNative& operator&=(const wxLongLongNative& ll)
250 { m_ll &= ll.m_ll; return *this; }
251
252 wxLongLongNative operator|(const wxLongLongNative& ll) const
253 { return wxLongLongNative(m_ll | ll.m_ll); }
254 wxLongLongNative& operator|=(const wxLongLongNative& ll)
255 { m_ll |= ll.m_ll; return *this; }
256
257 wxLongLongNative operator^(const wxLongLongNative& ll) const
258 { return wxLongLongNative(m_ll ^ ll.m_ll); }
259 wxLongLongNative& operator^=(const wxLongLongNative& ll)
260 { m_ll ^= ll.m_ll; return *this; }
261
262 // multiplication/division
263 wxLongLongNative operator*(const wxLongLongNative& ll) const
264 { return wxLongLongNative(m_ll * ll.m_ll); }
265 wxLongLongNative operator*(long l) const
266 { return wxLongLongNative(m_ll * l); }
267 wxLongLongNative& operator*=(const wxLongLongNative& ll)
268 { m_ll *= ll.m_ll; return *this; }
269 wxLongLongNative& operator*=(long l)
270 { m_ll *= l; return *this; }
271
272 wxLongLongNative operator/(const wxLongLongNative& ll) const
273 { return wxLongLongNative(m_ll / ll.m_ll); }
274 wxLongLongNative operator/(long l) const
275 { return wxLongLongNative(m_ll / l); }
276 wxLongLongNative& operator/=(const wxLongLongNative& ll)
277 { m_ll /= ll.m_ll; return *this; }
278 wxLongLongNative& operator/=(long l)
279 { m_ll /= l; return *this; }
280
281 wxLongLongNative operator%(const wxLongLongNative& ll) const
282 { return wxLongLongNative(m_ll % ll.m_ll); }
283 wxLongLongNative operator%(long l) const
284 { return wxLongLongNative(m_ll % l); }
285
286 // comparison
287 bool operator==(const wxLongLongNative& ll) const
288 { return m_ll == ll.m_ll; }
289 bool operator==(long l) const
290 { return m_ll == l; }
291 bool operator!=(const wxLongLongNative& ll) const
292 { return m_ll != ll.m_ll; }
293 bool operator!=(long l) const
294 { return m_ll != l; }
295 bool operator<(const wxLongLongNative& ll) const
296 { return m_ll < ll.m_ll; }
297 bool operator<(long l) const
298 { return m_ll < l; }
299 bool operator>(const wxLongLongNative& ll) const
300 { return m_ll > ll.m_ll; }
301 bool operator>(long l) const
302 { return m_ll > l; }
303 bool operator<=(const wxLongLongNative& ll) const
304 { return m_ll <= ll.m_ll; }
305 bool operator<=(long l) const
306 { return m_ll <= l; }
307 bool operator>=(const wxLongLongNative& ll) const
308 { return m_ll >= ll.m_ll; }
309 bool operator>=(long l) const
310 { return m_ll >= l; }
311
312 // miscellaneous
313
314 // return the string representation of this number
315 wxString ToString() const;
316
317 // conversion to byte array: returns a pointer to static buffer!
318 void *asArray() const;
319
320 #if wxUSE_STD_IOSTREAM
321 // input/output
322 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
323 #endif
324
325 private:
326 wxLongLong_t m_ll;
327 };
328
329 #endif // wxUSE_LONGLONG_NATIVE
330
331 #if wxUSE_LONGLONG_WX
332
333 class WXDLLEXPORT wxLongLongWx
334 {
335 public:
336 // ctors
337 // default ctor initializes to 0
338 wxLongLongWx()
339 {
340 m_lo = m_hi = 0;
341
342 #ifdef wxLONGLONG_TEST_MODE
343 m_ll = 0;
344
345 Check();
346 #endif // wxLONGLONG_TEST_MODE
347 }
348 // from long
349 wxLongLongWx(long l) { *this = l; }
350 // from 2 longs
351 wxLongLongWx(long hi, unsigned long lo)
352 {
353 m_hi = hi;
354 m_lo = lo;
355
356 #ifdef wxLONGLONG_TEST_MODE
357 m_ll = hi;
358 m_ll <<= 32;
359 m_ll |= lo;
360
361 Check();
362 #endif // wxLONGLONG_TEST_MODE
363 }
364
365 // default copy ctor is ok in both cases
366
367 // no dtor
368
369 // assignment operators
370 // from long
371 wxLongLongWx& operator=(long l)
372 {
373 m_lo = l;
374 m_hi = (l < 0 ? -1l : 0l);
375
376 #ifdef wxLONGLONG_TEST_MODE
377 m_ll = l;
378
379 Check();
380 #endif // wxLONGLONG_TEST_MODE
381
382 return *this;
383 }
384 // from double
385 wxLongLongWx& Assign(double d);
386 // can't have assignment operator from 2 longs
387
388 // accessors
389 // get high part
390 long GetHi() const { return m_hi; }
391 // get low part
392 unsigned long GetLo() const { return m_lo; }
393
394 // get absolute value
395 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
396 wxLongLongWx& Abs()
397 {
398 if ( m_hi < 0 )
399 m_hi = -m_hi;
400
401 #ifdef wxLONGLONG_TEST_MODE
402 if ( m_ll < 0 )
403 m_ll = -m_ll;
404
405 Check();
406 #endif // wxLONGLONG_TEST_MODE
407
408 return *this;
409 }
410
411 // convert to long with range checking in the debug mode (only!)
412 long ToLong() const
413 {
414 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
415 _T("wxLongLong to long conversion loss of precision") );
416
417 return (long)m_lo;
418 }
419
420 // operations
421 // addition
422 wxLongLongWx operator+(const wxLongLongWx& ll) const;
423 wxLongLongWx& operator+=(const wxLongLongWx& ll);
424 wxLongLongWx operator+(long l) const;
425 wxLongLongWx& operator+=(long l);
426
427 // pre increment operator
428 wxLongLongWx& operator++();
429
430 // post increment operator
431 wxLongLongWx& operator++(int) { return ++(*this); }
432
433 // negation operator
434 wxLongLongWx operator-() const;
435 wxLongLongWx& Negate();
436
437 // subraction
438 wxLongLongWx operator-(const wxLongLongWx& ll) const;
439 wxLongLongWx& operator-=(const wxLongLongWx& ll);
440
441 // pre decrement operator
442 wxLongLongWx& operator--();
443
444 // post decrement operator
445 wxLongLongWx& operator--(int) { return --(*this); }
446
447 // shifts
448 // left shift
449 wxLongLongWx operator<<(int shift) const;
450 wxLongLongWx& operator<<=(int shift);
451
452 // right shift
453 wxLongLongWx operator>>(int shift) const;
454 wxLongLongWx& operator>>=(int shift);
455
456 // bitwise operators
457 wxLongLongWx operator&(const wxLongLongWx& ll) const;
458 wxLongLongWx& operator&=(const wxLongLongWx& ll);
459 wxLongLongWx operator|(const wxLongLongWx& ll) const;
460 wxLongLongWx& operator|=(const wxLongLongWx& ll);
461 wxLongLongWx operator^(const wxLongLongWx& ll) const;
462 wxLongLongWx& operator^=(const wxLongLongWx& ll);
463 wxLongLongWx operator~() const;
464
465 // comparison
466 bool operator==(const wxLongLongWx& ll) const
467 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
468 bool operator!=(const wxLongLongWx& ll) const
469 { return !(*this == ll); }
470 bool operator<(const wxLongLongWx& ll) const;
471 bool operator>(const wxLongLongWx& ll) const;
472 bool operator<=(const wxLongLongWx& ll) const
473 { return *this < ll || *this == ll; }
474 bool operator>=(const wxLongLongWx& ll) const
475 { return *this > ll || *this == ll; }
476
477 bool operator<(long l) const { return *this < wxLongLongWx(l); }
478 bool operator>(long l) const { return *this > wxLongLongWx(l); }
479 bool operator==(long l) const
480 {
481 return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
482 : (m_hi == -1 && m_lo == (unsigned long)l);
483 }
484
485 bool operator<=(long l) const { return *this < l || *this == l; }
486 bool operator>=(long l) const { return *this > l || *this == l; }
487
488 // multiplication
489 wxLongLongWx operator*(const wxLongLongWx& ll) const;
490 wxLongLongWx& operator*=(const wxLongLongWx& ll);
491
492 // division
493 wxLongLongWx operator/(const wxLongLongWx& ll) const;
494 wxLongLongWx& operator/=(const wxLongLongWx& ll);
495
496 wxLongLongWx operator%(const wxLongLongWx& ll) const;
497
498 void Divide(const wxLongLongWx& divisor,
499 wxLongLongWx& quotient,
500 wxLongLongWx& remainder) const;
501
502 // input/output
503
504 // return the string representation of this number
505 wxString ToString() const;
506
507 void *asArray() const;
508
509 #if wxUSE_STD_IOSTREAM
510 friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
511 #endif // wxUSE_STD_IOSTREAM
512
513 private:
514 // long is at least 32 bits, so represent our 64bit number as 2 longs
515
516 long m_hi; // signed bit is in the high part
517 unsigned long m_lo;
518
519 #ifdef wxLONGLONG_TEST_MODE
520 void Check()
521 {
522 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
523 }
524
525 wxLongLong_t m_ll;
526 #endif // wxLONGLONG_TEST_MODE
527 };
528
529 #endif // wxUSE_LONGLONG_WX
530
531 // ----------------------------------------------------------------------------
532 // binary operators
533 // ----------------------------------------------------------------------------
534
535 inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
536 inline bool operator>(long l, const wxLongLong& ll) { return ll > l; }
537 inline bool operator<=(long l, const wxLongLong& ll) { return ll > l; }
538 inline bool operator>=(long l, const wxLongLong& ll) { return ll > l; }
539 inline bool operator==(long l, const wxLongLong& ll) { return ll > l; }
540 inline bool operator!=(long l, const wxLongLong& ll) { return ll > l; }
541
542 inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
543 inline wxLongLong operator-(long l, const wxLongLong& ll) { return ll - l; }
544
545 #endif // _WX_LONGLONG_H