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