1 /////////////////////////////////////////////////////////////////////////////
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.
11 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12 // Licence: wxWindows license
13 /////////////////////////////////////////////////////////////////////////////
15 #ifndef _WX_LONGLONG_H
16 #define _WX_LONGLONG_H
19 #pragma interface "longlong.h"
23 #include "wx/wxchar.h"
26 #include <limits.h> // for LONG_MAX
28 // #define wxUSE_LONGLONG_WX 1 // for testing (VZ)
30 // ----------------------------------------------------------------------------
31 // decide upon which class we will use
32 // ----------------------------------------------------------------------------
34 // to avoid compilation problems on 64bit machines with ambiguous method calls
35 // we will need to define this
36 #undef wxLongLongIsLong
38 // NB: we #define and not typedef wxLongLong_t because we want to be able to
39 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
40 // wxLongLong_t" below
41 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
42 #define wxLongLong_t long
43 #define wxLongLongIsLong
44 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
45 #define wxLongLong_t __int64
46 #elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
47 #define wxLongLong_t __int64
48 #elif defined(__GNUG__)
49 #define wxLongLong_t long long
50 #elif defined(__MWERKS__)
51 #if __option(longlong)
52 #define wxLongLong_t long long
54 #error "The 64 bit integer support in CodeWarrior has been disabled."
55 #error "See the documentation on the 'longlong' pragma."
57 #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
58 #define wxLongLong_t long long
59 #else // no native long long type
60 // both warning and pragma warning are not portable, but at least an
61 // unknown pragma should never be an error.
62 // Err, actually, Watcom C++ doesn't like it.
63 // (well, if the compilers are _that_ broken, I'm removing it (VZ))
64 #if 0 //ndef __WATCOMC__
65 #pragma warning "Your compiler does not appear to support 64 bit "\
66 "integers, using emulation class instead."
69 #define wxUSE_LONGLONG_WX 1
72 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
73 // to disable automatic testing (useful for the test program which defines
74 // both classes) but by default we only use one class
75 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
76 #undef wxUSE_LONGLONG_NATIVE
77 #define wxUSE_LONGLONG_NATIVE 0
78 class WXDLLEXPORT wxLongLongWx
;
79 typedef wxLongLongWx wxLongLong
;
81 // if nothing is defined, use native implementation by default, of course
82 #ifndef wxUSE_LONGLONG_NATIVE
83 #define wxUSE_LONGLONG_NATIVE 1
87 #ifndef wxUSE_LONGLONG_WX
88 #define wxUSE_LONGLONG_WX 0
89 class WXDLLEXPORT wxLongLongNative
;
90 typedef wxLongLongNative wxLongLong
;
93 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
94 // typedef wxLongLong as it wants, we don't do it
96 // ----------------------------------------------------------------------------
97 // choose the appropriate class
98 // ----------------------------------------------------------------------------
100 // we use iostream for wxLongLong output
101 #include "wx/ioswrap.h"
103 #if wxUSE_LONGLONG_NATIVE
105 class WXDLLEXPORT wxLongLongNative
109 // default ctor initializes to 0
110 wxLongLongNative() { m_ll
= 0; }
112 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
114 wxLongLongNative(long hi
, unsigned long lo
)
116 // assign first to avoid precision loss!
117 m_ll
= ((wxLongLong_t
) hi
) << 32;
118 m_ll
|= (wxLongLong_t
) lo
;
121 // default copy ctor is ok
125 // assignment operators
126 // from native 64 bit integer
127 wxLongLongNative
& operator=(wxLongLong_t ll
)
128 { m_ll
= ll
; return *this; }
130 // from double: this one has an explicit name because otherwise we
131 // would have ambiguity with "ll = int" and also because we don't want
132 // to have implicit conversions between doubles and wxLongLongs
133 wxLongLongNative
& Assign(double d
)
134 { m_ll
= (wxLongLong_t
)d
; return *this; }
136 // assignment operators from wxLongLongNative is ok
141 { return (long)(m_ll
>> 32); }
143 unsigned long GetLo() const
144 { return (unsigned long)m_ll
; }
146 // get absolute value
147 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
148 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
150 // convert to native long long
151 wxLongLong_t
GetValue() const { return m_ll
; }
153 // convert to long with range checking in the debug mode (only!)
156 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
157 _T("wxLongLong to long conversion loss of precision") );
162 // don't provide implicit conversion to wxLongLong_t or we will have an
163 // ambiguity for all arithmetic operations
164 //operator wxLongLong_t() const { return m_ll; }
168 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
169 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
170 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
171 { m_ll
+= ll
.m_ll
; return *this; }
173 wxLongLongNative
operator+(const wxLongLong_t ll
) const
174 { return wxLongLongNative(m_ll
+ ll
); }
175 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
176 { m_ll
+= ll
; return *this; }
179 wxLongLongNative
& operator++()
180 { m_ll
++; return *this; }
183 wxLongLongNative
& operator++(int)
184 { m_ll
++; return *this; }
187 wxLongLongNative
operator-() const
188 { return wxLongLongNative(-m_ll
); }
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; }
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; }
202 wxLongLongNative
& operator--()
203 { m_ll
--; return *this; }
206 wxLongLongNative
& operator--(int)
207 { m_ll
--; return *this; }
211 wxLongLongNative
operator<<(int shift
) const
212 { return wxLongLongNative(m_ll
<< shift
);; }
213 wxLongLongNative
& operator<<=(int shift
)
214 { m_ll
<<= shift
; return *this; }
217 wxLongLongNative
operator>>(int shift
) const
218 { return wxLongLongNative(m_ll
>> shift
);; }
219 wxLongLongNative
& operator>>=(int shift
)
220 { m_ll
>>= shift
; return *this; }
223 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
224 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
225 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
226 { m_ll
&= ll
.m_ll
; return *this; }
228 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
229 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
230 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
231 { m_ll
|= ll
.m_ll
; return *this; }
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; }
238 // multiplication/division
239 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
240 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
241 wxLongLongNative
operator*(long l
) const
242 { return wxLongLongNative(m_ll
* l
); }
243 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
244 { m_ll
*= ll
.m_ll
; return *this; }
245 wxLongLongNative
& operator*=(long l
)
246 { m_ll
*= l
; return *this; }
248 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
249 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
250 wxLongLongNative
operator/(long l
) const
251 { return wxLongLongNative(m_ll
/ l
); }
252 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
253 { m_ll
/= ll
.m_ll
; return *this; }
254 wxLongLongNative
& operator/=(long l
)
255 { m_ll
/= l
; return *this; }
257 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
258 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
259 wxLongLongNative
operator%(long l
) const
260 { return wxLongLongNative(m_ll
% l
); }
263 bool operator==(const wxLongLongNative
& ll
) const
264 { return m_ll
== ll
.m_ll
; }
265 bool operator==(long l
) const
266 { return m_ll
== l
; }
267 bool operator!=(const wxLongLongNative
& ll
) const
268 { return m_ll
!= ll
.m_ll
; }
269 bool operator!=(long l
) const
270 { return m_ll
!= l
; }
271 bool operator<(const wxLongLongNative
& ll
) const
272 { return m_ll
< ll
.m_ll
; }
273 bool operator<(long l
) const
275 bool operator>(const wxLongLongNative
& ll
) const
276 { return m_ll
> ll
.m_ll
; }
277 bool operator>(long l
) const
279 bool operator<=(const wxLongLongNative
& ll
) const
280 { return m_ll
<= ll
.m_ll
; }
281 bool operator<=(long l
) const
282 { return m_ll
<= l
; }
283 bool operator>=(const wxLongLongNative
& ll
) const
284 { return m_ll
>= ll
.m_ll
; }
285 bool operator>=(long l
) const
286 { return m_ll
>= l
; }
289 // conversion to byte array: returns a pointer to static buffer!
290 void *asArray() const;
292 #if wxUSE_STD_IOSTREAM
294 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
301 #endif // wxUSE_LONGLONG_NATIVE
303 #if wxUSE_LONGLONG_WX
305 class WXDLLEXPORT wxLongLongWx
309 // default ctor initializes to 0
310 wxLongLongWx() { m_lo
= m_hi
= 0; }
313 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
315 wxLongLongWx(long hi
, unsigned long lo
)
316 { m_hi
= hi
; m_lo
= lo
; }
318 // default copy ctor is ok in both cases
322 // assignment operators
324 wxLongLongWx
& operator=(long l
)
325 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
327 wxLongLongWx
& Assign(double d
);
328 // can't have assignment operator from 2 longs
332 long GetHi() const { return m_hi
; }
334 unsigned long GetLo() const { return m_lo
; }
336 // get absolute value
337 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
338 wxLongLongWx
& Abs() { if ( m_hi
< 0 ) m_hi
= -m_hi
; return *this; }
340 // convert to long with range checking in the debug mode (only!)
343 wxASSERT_MSG( m_hi
== 0l,
344 _T("wxLongLong to long conversion loss of precision") );
351 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
352 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
353 wxLongLongWx
operator+(long l
) const;
354 wxLongLongWx
& operator+=(long l
);
356 // pre increment operator
357 wxLongLongWx
& operator++();
359 // post increment operator
360 wxLongLongWx
& operator++(int);
363 wxLongLongWx
operator-() const;
366 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
367 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
369 // pre decrement operator
370 wxLongLongWx
& operator--();
372 // post decrement operator
373 wxLongLongWx
& operator--(int);
377 wxLongLongWx
operator<<(int shift
) const;
378 wxLongLongWx
& operator<<=(int shift
);
381 wxLongLongWx
operator>>(int shift
) const;
382 wxLongLongWx
& operator>>=(int shift
);
385 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
386 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
387 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
388 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
389 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
390 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
391 wxLongLongWx
operator~() const;
394 bool operator==(const wxLongLongWx
& ll
) const
395 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
396 bool operator!=(const wxLongLongWx
& ll
) const
397 { return !(*this == ll
); }
398 bool operator<(const wxLongLongWx
& ll
) const;
399 bool operator>(const wxLongLongWx
& ll
) const;
400 bool operator<=(const wxLongLongWx
& ll
) const
401 { return *this < ll
|| *this == ll
; }
402 bool operator>=(const wxLongLongWx
& ll
) const
403 { return *this > ll
|| *this == ll
; }
406 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
407 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
410 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
411 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
413 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
415 void Divide(const wxLongLongWx
& divisor
,
416 wxLongLongWx
& quotient
,
417 wxLongLongWx
& remainder
) const;
420 #if wxUSE_STD_IOSTREAM
421 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
422 #endif // wxUSE_STD_IOSTREAM
424 void *asArray(void) const;
427 // long is at least 32 bits, so represent our 64bit number as 2 longs
429 long m_hi
; // signed bit is in the high part
433 #endif // wxUSE_LONGLONG_WX
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 inline bool WXDLLEXPORT
operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
440 inline bool WXDLLEXPORT
operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
441 inline bool WXDLLEXPORT
operator<=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
442 inline bool WXDLLEXPORT
operator>=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
443 inline bool WXDLLEXPORT
operator==(long l
, const wxLongLong
& ll
) { return ll
> l
; }
444 inline bool WXDLLEXPORT
operator!=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
446 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
447 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
449 #endif // _WX_LONGLONG_H