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