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 // we don't give warnings for the compilers we know about that they don't
59 // have any 64 bit integer type
60 #if 0 // !defined(__VISAGECPP__) && \
61 !defined(__VISUALC__
) && \
62 !defined(__BORLANDC__
)
63 #warning "Your compiler does not appear to support 64 bit integers, "\
64 "using emulation class instead."
65 #endif // known compilers without long long
67 #define wxUSE_LONGLONG_WX 1
70 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
71 // to disable automatic testing (useful for the test program which defines
72 // both classes) but by default we only use one class
73 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
74 #undef wxUSE_LONGLONG_NATIVE
75 #define wxUSE_LONGLONG_NATIVE 0
76 class WXDLLEXPORT wxLongLongWx
;
77 typedef wxLongLongWx wxLongLong
;
79 // if nothing is defined, use native implementation by default, of course
80 #ifndef wxUSE_LONGLONG_NATIVE
81 #define wxUSE_LONGLONG_NATIVE 1
85 #ifndef wxUSE_LONGLONG_WX
86 #define wxUSE_LONGLONG_WX 0
87 class WXDLLEXPORT wxLongLongNative
;
88 typedef wxLongLongNative wxLongLong
;
91 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
92 // typedef wxLongLong as it wants, we don't do it
94 // ----------------------------------------------------------------------------
95 // choose the appropriate class
96 // ----------------------------------------------------------------------------
98 // we use iostream for wxLongLong output
99 #include "wx/ioswrap.h"
101 #if wxUSE_LONGLONG_NATIVE
103 class WXDLLEXPORT wxLongLongNative
107 // default ctor initializes to 0
108 wxLongLongNative() { m_ll
= 0; }
110 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
112 wxLongLongNative(long hi
, unsigned long lo
)
114 // assign first to avoid precision loss!
115 m_ll
= ((wxLongLong_t
) hi
) << 32;
116 m_ll
|= (wxLongLong_t
) lo
;
119 // default copy ctor is ok
123 // assignment operators
124 // from native 64 bit integer
125 wxLongLongNative
& operator=(wxLongLong_t ll
)
126 { m_ll
= ll
; return *this; }
128 // from double: this one has an explicit name because otherwise we
129 // would have ambiguity with "ll = int" and also because we don't want
130 // to have implicit conversions between doubles and wxLongLongs
131 wxLongLongNative
& Assign(double d
)
132 { m_ll
= (wxLongLong_t
)d
; return *this; }
134 // assignment operators from wxLongLongNative is ok
139 { return (long)(m_ll
>> 32); }
141 unsigned long GetLo() const
142 { return (unsigned long)m_ll
; }
144 // get absolute value
145 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
146 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
148 // convert to native long long
149 wxLongLong_t
GetValue() const { return m_ll
; }
151 // convert to long with range checking in the debug mode (only!)
154 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
155 _T("wxLongLong to long conversion loss of precision") );
160 // don't provide implicit conversion to wxLongLong_t or we will have an
161 // ambiguity for all arithmetic operations
162 //operator wxLongLong_t() const { return m_ll; }
166 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
167 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
168 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
169 { m_ll
+= ll
.m_ll
; return *this; }
171 wxLongLongNative
operator+(const wxLongLong_t ll
) const
172 { return wxLongLongNative(m_ll
+ ll
); }
173 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
174 { m_ll
+= ll
; return *this; }
177 wxLongLongNative
& operator++()
178 { m_ll
++; return *this; }
181 wxLongLongNative
& operator++(int)
182 { m_ll
++; return *this; }
185 wxLongLongNative
operator-() const
186 { return wxLongLongNative(-m_ll
); }
189 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
190 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
191 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
192 { m_ll
-= ll
.m_ll
; return *this; }
194 wxLongLongNative
operator-(const wxLongLong_t ll
) const
195 { return wxLongLongNative(m_ll
- ll
); }
196 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
197 { m_ll
-= ll
; return *this; }
200 wxLongLongNative
& operator--()
201 { m_ll
--; return *this; }
204 wxLongLongNative
& operator--(int)
205 { m_ll
--; return *this; }
209 wxLongLongNative
operator<<(int shift
) const
210 { return wxLongLongNative(m_ll
<< shift
);; }
211 wxLongLongNative
& operator<<=(int shift
)
212 { m_ll
<<= shift
; return *this; }
215 wxLongLongNative
operator>>(int shift
) const
216 { return wxLongLongNative(m_ll
>> shift
);; }
217 wxLongLongNative
& operator>>=(int shift
)
218 { m_ll
>>= shift
; return *this; }
221 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
222 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
223 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
224 { m_ll
&= ll
.m_ll
; return *this; }
226 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
227 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
228 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
229 { m_ll
|= ll
.m_ll
; return *this; }
231 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
232 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
233 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
234 { m_ll
^= ll
.m_ll
; return *this; }
236 // multiplication/division
237 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
238 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
239 wxLongLongNative
operator*(long l
) const
240 { return wxLongLongNative(m_ll
* l
); }
241 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
242 { m_ll
*= ll
.m_ll
; return *this; }
243 wxLongLongNative
& operator*=(long l
)
244 { m_ll
*= l
; return *this; }
246 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
247 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
248 wxLongLongNative
operator/(long l
) const
249 { return wxLongLongNative(m_ll
/ l
); }
250 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
251 { m_ll
/= ll
.m_ll
; return *this; }
252 wxLongLongNative
& operator/=(long l
)
253 { m_ll
/= l
; return *this; }
255 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
256 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
257 wxLongLongNative
operator%(long l
) const
258 { return wxLongLongNative(m_ll
% l
); }
261 bool operator==(const wxLongLongNative
& ll
) const
262 { return m_ll
== ll
.m_ll
; }
263 bool operator==(long l
) const
264 { return m_ll
== l
; }
265 bool operator!=(const wxLongLongNative
& ll
) const
266 { return m_ll
!= ll
.m_ll
; }
267 bool operator!=(long l
) const
268 { return m_ll
!= l
; }
269 bool operator<(const wxLongLongNative
& ll
) const
270 { return m_ll
< ll
.m_ll
; }
271 bool operator<(long l
) const
273 bool operator>(const wxLongLongNative
& ll
) const
274 { return m_ll
> ll
.m_ll
; }
275 bool operator>(long l
) const
277 bool operator<=(const wxLongLongNative
& ll
) const
278 { return m_ll
<= ll
.m_ll
; }
279 bool operator<=(long l
) const
280 { return m_ll
<= l
; }
281 bool operator>=(const wxLongLongNative
& ll
) const
282 { return m_ll
>= ll
.m_ll
; }
283 bool operator>=(long l
) const
284 { return m_ll
>= l
; }
287 // conversion to byte array: returns a pointer to static buffer!
288 void *asArray() const;
290 #if wxUSE_STD_IOSTREAM
292 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
299 #endif // wxUSE_LONGLONG_NATIVE
301 #if wxUSE_LONGLONG_WX
303 class WXDLLEXPORT wxLongLongWx
307 // default ctor initializes to 0
308 wxLongLongWx() { m_lo
= m_hi
= 0; }
311 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
313 wxLongLongWx(long hi
, unsigned long lo
)
314 { m_hi
= hi
; m_lo
= lo
; }
316 // default copy ctor is ok in both cases
320 // assignment operators
322 wxLongLongWx
& operator=(long l
)
323 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
325 wxLongLongWx
& Assign(double d
);
326 // can't have assignment operator from 2 longs
330 long GetHi() const { return m_hi
; }
332 unsigned long GetLo() const { return m_lo
; }
334 // get absolute value
335 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
336 wxLongLongWx
& Abs() { if ( m_hi
< 0 ) m_hi
= -m_hi
; return *this; }
338 // convert to long with range checking in the debug mode (only!)
341 wxASSERT_MSG( m_hi
== 0l,
342 _T("wxLongLong to long conversion loss of precision") );
349 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
350 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
351 wxLongLongWx
operator+(long l
) const;
352 wxLongLongWx
& operator+=(long l
);
354 // pre increment operator
355 wxLongLongWx
& operator++();
357 // post increment operator
358 wxLongLongWx
& operator++(int);
361 wxLongLongWx
operator-() const;
364 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
365 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
367 // pre decrement operator
368 wxLongLongWx
& operator--();
370 // post decrement operator
371 wxLongLongWx
& operator--(int);
375 wxLongLongWx
operator<<(int shift
) const;
376 wxLongLongWx
& operator<<=(int shift
);
379 wxLongLongWx
operator>>(int shift
) const;
380 wxLongLongWx
& operator>>=(int shift
);
383 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
384 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
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;
392 bool operator==(const wxLongLongWx
& ll
) const
393 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
394 bool operator!=(const wxLongLongWx
& ll
) const
395 { return !(*this == ll
); }
396 bool operator<(const wxLongLongWx
& ll
) const;
397 bool operator>(const wxLongLongWx
& ll
) const;
398 bool operator<=(const wxLongLongWx
& ll
) const
399 { return *this < ll
|| *this == ll
; }
400 bool operator>=(const wxLongLongWx
& ll
) const
401 { return *this > ll
|| *this == ll
; }
404 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
405 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
408 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
409 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
411 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
413 void Divide(const wxLongLongWx
& divisor
,
414 wxLongLongWx
& quotient
,
415 wxLongLongWx
& remainder
) const;
418 #if wxUSE_STD_IOSTREAM
419 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
420 #endif // wxUSE_STD_IOSTREAM
422 void *asArray(void) const;
425 // long is at least 32 bits, so represent our 64bit number as 2 longs
427 long m_hi
; // signed bit is in the high part
431 #endif // wxUSE_LONGLONG_WX
433 // ----------------------------------------------------------------------------
435 // ----------------------------------------------------------------------------
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
; }
442 inline bool WXDLLEXPORT
operator!=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
444 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
445 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
447 #endif // _WX_LONGLONG_H