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 #else // no native long long type
58 // both warning and pragma warning are not portable, but at least an
59 // unknown pragma should never be an error.
60 // Err, actually, Watcom C++ doesn't like it.
62 #pragma warning "Your compiler does not appear to support 64 bit "\
63 "integers, using emulation class instead."
66 #define wxUSE_LONGLONG_WX 1
69 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
70 // to disable automatic testing (useful for the test program which defines
71 // both classes) but by default we only use one class
72 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
73 #undef wxUSE_LONGLONG_NATIVE
74 #define wxUSE_LONGLONG_NATIVE 0
75 class WXDLLEXPORT wxLongLongWx
;
76 typedef wxLongLongWx wxLongLong
;
78 // if nothing is defined, use native implementation by default, of course
79 #ifndef wxUSE_LONGLONG_NATIVE
80 #define wxUSE_LONGLONG_NATIVE 1
84 #ifndef wxUSE_LONGLONG_WX
85 #define wxUSE_LONGLONG_WX 0
86 class WXDLLEXPORT wxLongLongNative
;
87 typedef wxLongLongNative wxLongLong
;
90 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
91 // typedef wxLongLong as it wants, we don't do it
93 // ----------------------------------------------------------------------------
94 // choose the appropriate class
95 // ----------------------------------------------------------------------------
97 // we use iostream for wxLongLong output
98 #include "wx/ioswrap.h"
100 #if wxUSE_LONGLONG_NATIVE
102 class WXDLLEXPORT wxLongLongNative
106 // default ctor initializes to 0
107 wxLongLongNative() { m_ll
= 0; }
109 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
111 wxLongLongNative(long hi
, unsigned long lo
)
113 // assign first to avoid precision loss!
114 m_ll
= ((wxLongLong_t
) hi
) << 32;
115 m_ll
|= (wxLongLong_t
) lo
;
118 // default copy ctor is ok
122 // assignment operators
123 // from native 64 bit integer
124 wxLongLongNative
& operator=(wxLongLong_t ll
)
125 { m_ll
= ll
; return *this; }
127 // from double: this one has an explicit name because otherwise we
128 // would have ambiguity with "ll = int" and also because we don't want
129 // to have implicit conversions between doubles and wxLongLongs
130 wxLongLongNative
& Assign(double d
)
131 { m_ll
= (wxLongLong_t
)d
; return *this; }
133 // assignment operators from wxLongLongNative is ok
138 { return (long)(m_ll
>> 32); }
140 unsigned long GetLo() const
141 { return (unsigned long)m_ll
; }
143 // get absolute value
144 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
145 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
147 // convert to native long long
148 wxLongLong_t
GetValue() const { return m_ll
; }
150 // convert to long with range checking in the debug mode (only!)
153 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
154 _T("wxLongLong to long conversion loss of precision") );
159 // don't provide implicit conversion to wxLongLong_t or we will have an
160 // ambiguity for all arithmetic operations
161 //operator wxLongLong_t() const { return m_ll; }
165 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
166 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
167 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
168 { m_ll
+= ll
.m_ll
; return *this; }
170 wxLongLongNative
operator+(const wxLongLong_t ll
) const
171 { return wxLongLongNative(m_ll
+ ll
); }
172 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
173 { m_ll
+= ll
; return *this; }
176 wxLongLongNative
& operator++()
177 { m_ll
++; return *this; }
180 wxLongLongNative
& operator++(int)
181 { m_ll
++; return *this; }
184 wxLongLongNative
operator-() const
185 { return wxLongLongNative(-m_ll
); }
188 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
189 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
190 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
191 { m_ll
-= ll
.m_ll
; return *this; }
193 wxLongLongNative
operator-(const wxLongLong_t ll
) const
194 { return wxLongLongNative(m_ll
- ll
); }
195 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
196 { m_ll
-= ll
; return *this; }
199 wxLongLongNative
& operator--()
200 { m_ll
--; return *this; }
203 wxLongLongNative
& operator--(int)
204 { m_ll
--; return *this; }
208 wxLongLongNative
operator<<(int shift
) const
209 { return wxLongLongNative(m_ll
<< shift
);; }
210 wxLongLongNative
& operator<<=(int shift
)
211 { m_ll
<<= shift
; return *this; }
214 wxLongLongNative
operator>>(int shift
) const
215 { return wxLongLongNative(m_ll
>> shift
);; }
216 wxLongLongNative
& operator>>=(int shift
)
217 { m_ll
>>= shift
; return *this; }
220 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
221 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
222 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
223 { m_ll
&= ll
.m_ll
; return *this; }
225 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
226 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
227 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
228 { m_ll
|= ll
.m_ll
; return *this; }
230 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
231 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
232 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
233 { m_ll
^= ll
.m_ll
; return *this; }
235 // multiplication/division
236 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
237 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
238 wxLongLongNative
operator*(long l
) const
239 { return wxLongLongNative(m_ll
* l
); }
240 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
241 { m_ll
*= ll
.m_ll
; return *this; }
242 wxLongLongNative
& operator*=(long l
)
243 { m_ll
*= l
; return *this; }
245 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
246 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
247 wxLongLongNative
operator/(long l
) const
248 { return wxLongLongNative(m_ll
/ l
); }
249 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
250 { m_ll
/= ll
.m_ll
; return *this; }
251 wxLongLongNative
& operator/=(long l
)
252 { m_ll
/= l
; return *this; }
254 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
255 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
256 wxLongLongNative
operator%(long l
) const
257 { return wxLongLongNative(m_ll
% l
); }
260 bool operator==(const wxLongLongNative
& ll
) const
261 { return m_ll
== ll
.m_ll
; }
262 bool operator==(long l
) const
263 { return m_ll
== l
; }
264 bool operator!=(const wxLongLongNative
& ll
) const
265 { return m_ll
!= ll
.m_ll
; }
266 bool operator!=(long l
) const
267 { return m_ll
!= l
; }
268 bool operator<(const wxLongLongNative
& ll
) const
269 { return m_ll
< ll
.m_ll
; }
270 bool operator<(long l
) const
272 bool operator>(const wxLongLongNative
& ll
) const
273 { return m_ll
> ll
.m_ll
; }
274 bool operator>(long l
) const
276 bool operator<=(const wxLongLongNative
& ll
) const
277 { return m_ll
<= ll
.m_ll
; }
278 bool operator<=(long l
) const
279 { return m_ll
<= l
; }
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
; }
286 // conversion to byte array: returns a pointer to static buffer!
287 void *asArray() const;
289 #if wxUSE_STD_IOSTREAM
291 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
298 #endif // wxUSE_LONGLONG_NATIVE
300 #if wxUSE_LONGLONG_WX
302 class WXDLLEXPORT wxLongLongWx
306 // default ctor initializes to 0
307 wxLongLongWx() { m_lo
= m_hi
= 0; }
310 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
312 wxLongLongWx(long hi
, unsigned long lo
)
313 { m_hi
= hi
; m_lo
= lo
; }
315 // default copy ctor is ok in both cases
319 // assignment operators
321 wxLongLongWx
& operator=(long l
)
322 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
324 wxLongLongWx
& Assign(double d
);
325 // can't have assignment operator from 2 longs
329 long GetHi() const { return m_hi
; }
331 unsigned long GetLo() const { return m_lo
; }
333 // get absolute value
334 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
335 wxLongLongWx
& Abs() { if ( m_hi
< 0 ) m_hi
= -m_hi
; return *this; }
337 // convert to long with range checking in the debug mode (only!)
340 wxASSERT_MSG( m_hi
== 0l,
341 _T("wxLongLong to long conversion loss of precision") );
348 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
349 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
350 wxLongLongWx
operator+(long l
) const;
351 wxLongLongWx
& operator+=(long l
);
353 // pre increment operator
354 wxLongLongWx
& operator++();
356 // post increment operator
357 wxLongLongWx
& operator++(int);
360 wxLongLongWx
operator-() const;
363 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
364 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
366 // pre decrement operator
367 wxLongLongWx
& operator--();
369 // post decrement operator
370 wxLongLongWx
& operator--(int);
374 wxLongLongWx
operator<<(int shift
) const;
375 wxLongLongWx
& operator<<=(int shift
);
378 wxLongLongWx
operator>>(int shift
) const;
379 wxLongLongWx
& operator>>=(int shift
);
382 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
383 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
384 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
385 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
386 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
387 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
388 wxLongLongWx
operator~() const;
391 bool operator==(const wxLongLongWx
& ll
) const
392 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
393 bool operator!=(const wxLongLongWx
& ll
) const
394 { return !(*this == ll
); }
395 bool operator<(const wxLongLongWx
& ll
) const;
396 bool operator>(const wxLongLongWx
& ll
) const;
397 bool operator<=(const wxLongLongWx
& ll
) const
398 { return *this < ll
|| *this == ll
; }
399 bool operator>=(const wxLongLongWx
& ll
) const
400 { return *this > ll
|| *this == ll
; }
403 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
404 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
407 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
408 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
410 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
412 void Divide(const wxLongLongWx
& divisor
,
413 wxLongLongWx
& quotient
,
414 wxLongLongWx
& remainder
) const;
417 #if wxUSE_STD_IOSTREAM
418 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
419 #endif // wxUSE_STD_IOSTREAM
421 void *asArray(void) const;
424 // long is at least 32 bits, so represent our 64bit number as 2 longs
426 long m_hi
; // signed bit is in the high part
430 #endif // wxUSE_LONGLONG_WX
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 inline bool WXDLLEXPORT
operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
437 inline bool WXDLLEXPORT
operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
438 inline bool WXDLLEXPORT
operator<=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
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
; }
443 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
444 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
446 #endif // _WX_LONGLONG_H