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 #pragma warning "Your compiler does not appear to support 64 bit "\
61 "integers, using emulation class instead."
63 #define wxUSE_LONGLONG_WX 1
66 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
67 // to disable automatic testing (useful for the test program which defines
68 // both classes) but by default we only use one class
69 #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
70 #undef wxUSE_LONGLONG_NATIVE
71 #define wxUSE_LONGLONG_NATIVE 0
72 class WXDLLEXPORT wxLongLongWx
;
73 typedef wxLongLongWx wxLongLong
;
75 // if nothing is defined, use native implementation by default, of course
76 #ifndef wxUSE_LONGLONG_NATIVE
77 #define wxUSE_LONGLONG_NATIVE 1
81 #ifndef wxUSE_LONGLONG_WX
82 #define wxUSE_LONGLONG_WX 0
83 class WXDLLEXPORT wxLongLongNative
;
84 typedef wxLongLongNative wxLongLong
;
87 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
88 // typedef wxLongLong as it wants, we don't do it
90 // ----------------------------------------------------------------------------
91 // choose the appropriate class
92 // ----------------------------------------------------------------------------
94 // we use iostream for wxLongLong output
95 #include "wx/ioswrap.h"
97 #if wxUSE_LONGLONG_NATIVE
99 class WXDLLEXPORT wxLongLongNative
103 // default ctor initializes to 0
104 wxLongLongNative() { m_ll
= 0; }
106 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
108 wxLongLongNative(long hi
, unsigned long lo
)
110 // assign first to avoid precision loss!
111 m_ll
= ((wxLongLong_t
) hi
) << 32;
112 m_ll
|= (wxLongLong_t
) lo
;
115 // default copy ctor is ok
119 // assignment operators
120 // from native 64 bit integer
121 wxLongLongNative
& operator=(wxLongLong_t ll
)
122 { m_ll
= ll
; return *this; }
124 // from double: this one has an explicit name because otherwise we
125 // would have ambiguity with "ll = int" and also because we don't want
126 // to have implicit conversions between doubles and wxLongLongs
127 wxLongLongNative
& Assign(double d
)
128 { m_ll
= (wxLongLong_t
)d
; return *this; }
130 // assignment operators from wxLongLongNative is ok
135 { return (long)(m_ll
>> 32); }
137 unsigned long GetLo() const
138 { return (unsigned long)m_ll
; }
140 // get absolute value
141 wxLongLongNative
Abs() const { return wxLongLongNative(*this).Abs(); }
142 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
144 // convert to native long long
145 wxLongLong_t
GetValue() const { return m_ll
; }
147 // convert to long with range checking in the debug mode (only!)
150 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
151 _T("wxLongLong to long conversion loss of precision") );
156 // don't provide implicit conversion to wxLongLong_t or we will have an
157 // ambiguity for all arithmetic operations
158 //operator wxLongLong_t() const { return m_ll; }
162 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
163 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
164 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
165 { m_ll
+= ll
.m_ll
; return *this; }
167 wxLongLongNative
operator+(const wxLongLong_t ll
) const
168 { return wxLongLongNative(m_ll
+ ll
); }
169 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
170 { m_ll
+= ll
; return *this; }
173 wxLongLongNative
& operator++()
174 { m_ll
++; return *this; }
177 wxLongLongNative
& operator++(int)
178 { m_ll
++; return *this; }
181 wxLongLongNative
operator-() const
182 { return wxLongLongNative(-m_ll
); }
185 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
186 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
187 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
188 { m_ll
-= ll
.m_ll
; return *this; }
190 wxLongLongNative
operator-(const wxLongLong_t ll
) const
191 { return wxLongLongNative(m_ll
- ll
); }
192 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
193 { m_ll
-= ll
; return *this; }
196 wxLongLongNative
& operator--()
197 { m_ll
--; return *this; }
200 wxLongLongNative
& operator--(int)
201 { m_ll
--; return *this; }
205 wxLongLongNative
operator<<(int shift
) const
206 { return wxLongLongNative(m_ll
<< shift
);; }
207 wxLongLongNative
& operator<<=(int shift
)
208 { m_ll
<<= shift
; 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&(const wxLongLongNative
& ll
) const
218 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
219 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
220 { m_ll
&= ll
.m_ll
; return *this; }
222 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
223 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
224 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
225 { m_ll
|= ll
.m_ll
; return *this; }
227 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
228 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
229 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
230 { m_ll
^= ll
.m_ll
; return *this; }
232 // multiplication/division
233 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
234 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
235 wxLongLongNative
operator*(long l
) const
236 { return wxLongLongNative(m_ll
* l
); }
237 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
238 { m_ll
*= ll
.m_ll
; return *this; }
239 wxLongLongNative
& operator*=(long l
)
240 { m_ll
*= l
; return *this; }
242 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
243 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
244 wxLongLongNative
operator/(long l
) const
245 { return wxLongLongNative(m_ll
/ l
); }
246 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
247 { m_ll
/= ll
.m_ll
; return *this; }
248 wxLongLongNative
& operator/=(long l
)
249 { m_ll
/= l
; return *this; }
251 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
252 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
253 wxLongLongNative
operator%(long l
) const
254 { return wxLongLongNative(m_ll
% l
); }
257 bool operator==(const wxLongLongNative
& ll
) const
258 { return m_ll
== ll
.m_ll
; }
259 bool operator==(long l
) const
260 { return 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
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
276 { return m_ll
<= l
; }
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
; }
283 // conversion to byte array: returns a pointer to static buffer!
284 void *asArray() const;
286 #if wxUSE_STD_IOSTREAM
288 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
295 #endif // wxUSE_LONGLONG_NATIVE
297 #if wxUSE_LONGLONG_WX
299 class WXDLLEXPORT wxLongLongWx
303 // default ctor initializes to 0
304 wxLongLongWx() { m_lo
= m_hi
= 0; }
307 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
309 wxLongLongWx(long hi
, unsigned long lo
)
310 { m_hi
= hi
; m_lo
= lo
; }
312 // default copy ctor is ok in both cases
316 // assignment operators
318 wxLongLongWx
& operator=(long l
)
319 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
321 wxLongLongWx
& Assign(double d
);
322 // can't have assignment operator from 2 longs
326 long GetHi() const { return m_hi
; }
328 unsigned long GetLo() const { return m_lo
; }
330 // get absolute value
331 wxLongLongWx
Abs() const { return wxLongLongWx(*this).Abs(); }
332 wxLongLongWx
& Abs() { if ( m_hi
< 0 ) m_hi
= -m_hi
; return *this; }
334 // convert to long with range checking in the debug mode (only!)
337 wxASSERT_MSG( m_hi
== 0l,
338 _T("wxLongLong to long conversion loss of precision") );
345 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
346 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
347 wxLongLongWx
operator+(long l
) const;
348 wxLongLongWx
& operator+=(long l
);
350 // pre increment operator
351 wxLongLongWx
& operator++();
353 // post increment operator
354 wxLongLongWx
& operator++(int);
357 wxLongLongWx
operator-() const;
360 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
361 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
363 // pre decrement operator
364 wxLongLongWx
& operator--();
366 // post decrement operator
367 wxLongLongWx
& operator--(int);
371 wxLongLongWx
operator<<(int shift
) const;
372 wxLongLongWx
& operator<<=(int shift
);
375 wxLongLongWx
operator>>(int shift
) const;
376 wxLongLongWx
& operator>>=(int shift
);
379 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
380 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
381 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
382 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
383 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
384 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
385 wxLongLongWx
operator~() const;
388 bool operator==(const wxLongLongWx
& ll
) const
389 { return m_lo
== ll
.m_lo
&& m_hi
== ll
.m_hi
; }
390 bool operator!=(const wxLongLongWx
& ll
) const
391 { return !(*this == ll
); }
392 bool operator<(const wxLongLongWx
& ll
) const;
393 bool operator>(const wxLongLongWx
& ll
) const;
394 bool operator<=(const wxLongLongWx
& ll
) const
395 { return *this < ll
|| *this == ll
; }
396 bool operator>=(const wxLongLongWx
& ll
) const
397 { return *this > ll
|| *this == ll
; }
400 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
401 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
404 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
405 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
407 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
409 void Divide(const wxLongLongWx
& divisor
,
410 wxLongLongWx
& quotient
,
411 wxLongLongWx
& remainder
) const;
414 #if wxUSE_STD_IOSTREAM
415 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
416 #endif // wxUSE_STD_IOSTREAM
418 void *asArray(void) const;
421 // long is at least 32 bits, so represent our 64bit number as 2 longs
423 long m_hi
; // signed bit is in the high part
427 #endif // wxUSE_LONGLONG_WX
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
433 inline bool WXDLLEXPORT
operator<(long l
, const wxLongLong
& ll
) { return ll
> l
; }
434 inline bool WXDLLEXPORT
operator>(long l
, const wxLongLong
& ll
) { return ll
> l
; }
435 inline bool WXDLLEXPORT
operator<=(long l
, const wxLongLong
& ll
) { return ll
> l
; }
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
; }
440 inline wxLongLong WXDLLEXPORT
operator+(long l
, const wxLongLong
& ll
) { return ll
+ l
; }
441 inline wxLongLong WXDLLEXPORT
operator-(long l
, const wxLongLong
& ll
) { return ll
- l
; }
443 #endif // _WX_LONGLONG_H