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 // ----------------------------------------------------------------------------
29 // decide upon which class we will use
30 // ----------------------------------------------------------------------------
32 // to avoid compilation problems on 64bit machines with ambiguous method calls
33 // we will need to define this
34 #undef wxLongLongIsLong
36 // NB: we #define and not typedef wxLongLong_t because we want to be able to
37 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
38 // wxLongLong_t" below
39 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
40 #define wxLongLong_t long
41 #define wxLongLongIsLong
42 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
43 #define wxLongLong_t __int64
44 #elif defined(__GNUG__)
45 #define wxLongLong_t long long
46 #elif defined(__MWERKS__)
47 #if __option(longlong)
48 #define wxLongLong_t long long
50 #error "The 64 bit integer support in CodeWarrior has been disabled."
51 #error "See the documentation on the 'longlong' pragma."
53 #else // no native long long type
54 // we don't give warnings for the compilers we know about that they don't
55 // have any 64 bit integer type
56 #if !defined(__VISAGECPP__) && !defined(__VISUALC__)
57 #warning "Your compiler does not appear to support 64 bit integers, "\
58 "using emulation class instead."
59 #endif // known compilers without long long
61 #define wxUSE_LONGLONG_WX 1
64 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
65 // to disable automatic testing (useful for the test program which defines
66 // both classes) but by default we only use one class
68 #undef wxUSE_LONGLONG_NATIVE
69 #define wxUSE_LONGLONG_NATIVE 0
70 class WXDLLEXPORT wxLongLongWx
;
71 typedef wxLongLongWx wxLongLong
;
73 // if nothing is defined, use native implementation by default, of course
74 #ifndef wxUSE_LONGLONG_NATIVE
75 #define wxUSE_LONGLONG_NATIVE 1
79 #ifndef wxUSE_LONGLONG_WX
80 #define wxUSE_LONGLONG_WX 0
81 class WXDLLEXPORT wxLongLongNative
;
82 typedef wxLongLongNative wxLongLong
;
85 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
86 // typedef wxLongLong as it wants, we don't do it
88 // ----------------------------------------------------------------------------
89 // choose the appropriate class
90 // ----------------------------------------------------------------------------
92 // we use iostream for wxLongLong output
93 #include "wx/ioswrap.h"
95 #if wxUSE_LONGLONG_NATIVE
97 class WXDLLEXPORT wxLongLongNative
101 // default ctor initializes to 0
102 wxLongLongNative() { m_ll
= 0; }
104 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
106 wxLongLongNative(long hi
, unsigned long lo
)
108 // assign first to avoid precision loss!
109 m_ll
= ((wxLongLong_t
) hi
) << 32;
110 m_ll
|= (wxLongLong_t
) lo
;
113 // default copy ctor is ok
117 // assignment operators
118 // from native 64 bit integer
119 wxLongLongNative
& operator=(wxLongLong_t ll
)
120 { m_ll
= ll
; return *this; }
122 // from double: this one has an explicit name because otherwise we
123 // would have ambiguity with "ll = int" and also because we don't want
124 // to have implicit conversions between doubles and wxLongLongs
125 wxLongLongNative
& Assign(double d
)
126 { m_ll
= (wxLongLong_t
)d
; return *this; }
128 // assignment operators from wxLongLongNative is ok
133 { return (long)(m_ll
>> 32); }
135 unsigned long GetLo() const
136 { return (unsigned long)m_ll
; }
138 // get absolute value
139 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
141 // convert to native long long
142 wxLongLong_t
GetValue() const { return m_ll
; }
144 // convert to long with range checking in the debug mode (only!)
147 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
148 _T("wxLongLong to long conversion loss of precision") );
153 // don't provide implicit conversion to wxLongLong_t or we will have an
154 // ambiguity for all arithmetic operations
155 //operator wxLongLong_t() const { return m_ll; }
159 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
160 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
161 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
162 { m_ll
+= ll
.m_ll
; return *this; }
164 wxLongLongNative
operator+(const wxLongLong_t ll
) const
165 { return wxLongLongNative(m_ll
+ ll
); }
166 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
167 { m_ll
+= ll
; return *this; }
170 wxLongLongNative
& operator++()
171 { m_ll
++; return *this; }
174 wxLongLongNative
& operator++(int)
175 { m_ll
++; return *this; }
178 wxLongLongNative
operator-() const
179 { return wxLongLongNative(-m_ll
); }
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; }
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; }
193 wxLongLongNative
& operator--()
194 { m_ll
--; return *this; }
197 wxLongLongNative
& operator--(int)
198 { m_ll
--; return *this; }
202 wxLongLongNative
operator<<(int shift
) const
203 { return wxLongLongNative(m_ll
<< shift
);; }
204 wxLongLongNative
& operator<<=(int shift
)
205 { m_ll
<<= shift
; 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&(const wxLongLongNative
& ll
) const
215 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
216 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
217 { m_ll
&= ll
.m_ll
; return *this; }
219 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
220 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
221 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
222 { m_ll
|= ll
.m_ll
; return *this; }
224 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
225 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
226 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
227 { m_ll
^= ll
.m_ll
; return *this; }
229 // multiplication/division
230 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
231 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
232 wxLongLongNative
operator*(long l
) const
233 { return wxLongLongNative(m_ll
* l
); }
234 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
235 { m_ll
*= ll
.m_ll
; return *this; }
236 wxLongLongNative
& operator*=(long l
)
237 { m_ll
*= l
; return *this; }
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
); }
254 bool operator==(const wxLongLongNative
& ll
) const
255 { return m_ll
== ll
.m_ll
; }
256 bool operator==(long l
) const
257 { return m_ll
== l
; }
258 bool operator!=(const wxLongLongNative
& ll
) const
259 { return m_ll
!= ll
.m_ll
; }
260 bool operator!=(long l
) const
261 { return m_ll
!= l
; }
262 bool operator<(const wxLongLongNative
& ll
) const
263 { return m_ll
< ll
.m_ll
; }
264 bool operator<(long l
) const
266 bool operator>(const wxLongLongNative
& ll
) const
267 { return m_ll
> ll
.m_ll
; }
268 bool operator>(long l
) const
270 bool operator<=(const wxLongLongNative
& ll
) const
271 { return m_ll
<= ll
.m_ll
; }
272 bool operator<=(long l
) const
273 { return m_ll
<= l
; }
274 bool operator>=(const wxLongLongNative
& ll
) const
275 { return m_ll
>= ll
.m_ll
; }
276 bool operator>=(long l
) const
277 { return m_ll
>= l
; }
280 // conversion to byte array: returns a pointer to static buffer!
281 void *asArray() const;
283 #if wxUSE_STD_IOSTREAM
285 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
292 #endif // wxUSE_LONGLONG_NATIVE
294 #if wxUSE_LONGLONG_WX
296 class WXDLLEXPORT wxLongLongWx
300 // default ctor initializes to 0
301 wxLongLongWx() { m_lo
= m_hi
= 0; }
304 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
306 wxLongLongWx(long hi
, unsigned long lo
)
307 { m_hi
= hi
; m_lo
= lo
; }
309 // default copy ctor is ok in both cases
313 // assignment operators
315 wxLongLongWx
& operator=(long l
)
316 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
318 wxLongLongWx
& Assign(double d
);
319 // can't have assignment operator from 2 longs
323 long GetHi() const { return m_hi
; }
325 unsigned long GetLo() const { return m_lo
; }
327 // get absolute value
328 wxLongLongWx
& Abs() { if ( m_hi
< 0 ) m_hi
= -m_hi
; return *this; }
330 // convert to long with range checking in the debug mode (only!)
333 wxASSERT_MSG( m_hi
== 0l,
334 _T("wxLongLong to long conversion loss of precision") );
341 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
342 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
343 wxLongLongWx
operator+(long l
) const;
344 wxLongLongWx
& operator+=(long l
);
346 // pre increment operator
347 wxLongLongWx
& operator++();
349 // post increment operator
350 wxLongLongWx
& operator++(int);
353 wxLongLongWx
operator-() const;
356 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
357 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
359 // pre decrement operator
360 wxLongLongWx
& operator--();
362 // post decrement operator
363 wxLongLongWx
& operator--(int);
367 wxLongLongWx
operator<<(int shift
) const;
368 wxLongLongWx
& operator<<=(int shift
);
371 wxLongLongWx
operator>>(int shift
) const;
372 wxLongLongWx
& operator>>=(int shift
);
375 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
376 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
377 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
378 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
379 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
380 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
381 wxLongLongWx
operator~() const;
384 bool operator==(const wxLongLongWx
& ll
) const;
385 bool operator!=(const wxLongLongWx
& ll
) const;
386 bool operator<(const wxLongLongWx
& ll
) const;
387 bool operator>(const wxLongLongWx
& ll
) const;
388 bool operator<=(const wxLongLongWx
& ll
) const;
389 bool operator>=(const wxLongLongWx
& ll
) const;
392 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
393 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
396 wxLongLongWx
operator/(const wxLongLongWx
& ll
) const;
397 wxLongLongWx
& operator/=(const wxLongLongWx
& ll
);
399 wxLongLongWx
operator%(const wxLongLongWx
& ll
) const;
401 void Divide(const wxLongLongWx
& divisor
,
402 wxLongLongWx
& quotient
,
403 wxLongLongWx
& remainder
) const;
406 #if wxUSE_STD_IOSTREAM
407 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
408 #endif // wxUSE_STD_IOSTREAM
410 void *asArray(void) const;
413 // long is at least 32 bits, so represent our 64bit number as 2 longs
415 long m_hi
; // signed bit is in the high part
419 #endif // wxUSE_LONGLONG_WX
421 #endif // _WX_LONGLONG_H