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