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(__WIN16__)
43 #define wxLongLong_t long
44 #define wxLongLongIsLong
45 #elif defined(__VISUALC__) || defined( __VMS__ )
46 #define wxLongLong_t __int64
47 #elif defined(__GNUG__)
48 #define wxLongLong_t long long
49 #elif defined(__MWERKS__)
50 #if __option(longlong)
51 #define wxLongLong_t long long
53 #error "The 64 bit integer support in CodeWarrior has been disabled."
54 #error "See the documentation on the 'longlong' pragma."
57 #if !defined(__VISAGECPP__)
58 // Visualage does not support this pragma
59 #warning "Your compiler does not appear to support 64 bit integers, "\
60 "using emulation class instead."
62 #define wxUSE_LONGLONG_WX 1
65 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
66 // to disable automatic testing (useful for the test program which defines
67 // both classes) but by default we only use one class
69 #undef wxUSE_LONGLONG_NATIVE
70 #define wxUSE_LONGLONG_NATIVE 0
71 class WXDLLEXPORT wxLongLongWx
;
72 typedef wxLongLongWx wxLongLong
;
74 // if nothing is defined, use native implementation by default, of course
75 #ifndef wxUSE_LONGLONG_NATIVE
76 #define wxUSE_LONGLONG_NATIVE 1
80 #ifndef wxUSE_LONGLONG_WX
81 #define wxUSE_LONGLONG_WX 0
82 class WXDLLEXPORT wxLongLongNative
;
83 typedef wxLongLongNative wxLongLong
;
86 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
87 // typedef wxLongLong as it wants, we don't do it
89 // ----------------------------------------------------------------------------
90 // choose the appropriate class
91 // ----------------------------------------------------------------------------
93 // we use iostream for wxLongLong output
94 #include "wx/ioswrap.h"
96 #if wxUSE_LONGLONG_NATIVE
98 class WXDLLEXPORT wxLongLongNative
102 // default ctor initializes to 0
103 wxLongLongNative() { m_ll
= 0; }
105 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
107 wxLongLongNative(long hi
, unsigned long lo
)
109 // assign first to avoid precision loss!
110 m_ll
= ((wxLongLong_t
) hi
) << 32;
111 m_ll
|= (wxLongLong_t
) lo
;
114 // default copy ctor is ok
118 // assignment operators
119 // from native 64 bit integer
120 wxLongLongNative
& operator=(wxLongLong_t ll
)
121 { m_ll
= ll
; return *this; }
123 // assignment operators from wxLongLongNative is ok
128 { return (long)(m_ll
>> 32); }
130 unsigned long GetLo() const
131 { return (unsigned long)m_ll
; }
133 // get absolute value
134 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
136 // convert to native long long
137 wxLongLong_t
GetValue() const { return m_ll
; }
139 // convert to long with range checking in the debug mode (only!)
142 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
143 _T("wxLongLong to long conversion loss of precision") );
148 // don't provide implicit conversion to wxLongLong_t or we will have an
149 // ambiguity for all arithmetic operations
150 //operator wxLongLong_t() const { return m_ll; }
154 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
155 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
156 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
157 { m_ll
+= ll
.m_ll
; return *this; }
159 wxLongLongNative
operator+(const wxLongLong_t ll
) const
160 { return wxLongLongNative(m_ll
+ ll
); }
161 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
162 { m_ll
+= ll
; return *this; }
165 wxLongLongNative
& operator++()
166 { m_ll
++; return *this; }
169 wxLongLongNative
& operator++(int)
170 { m_ll
++; return *this; }
173 wxLongLongNative
operator-() const
174 { return wxLongLongNative(-m_ll
); }
177 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
178 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
179 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
180 { m_ll
-= ll
.m_ll
; return *this; }
182 wxLongLongNative
operator-(const wxLongLong_t ll
) const
183 { return wxLongLongNative(m_ll
- ll
); }
184 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
185 { m_ll
-= ll
; return *this; }
188 wxLongLongNative
& operator--()
189 { m_ll
--; return *this; }
192 wxLongLongNative
& operator--(int)
193 { m_ll
--; return *this; }
197 wxLongLongNative
operator<<(int shift
) const
198 { return wxLongLongNative(m_ll
<< shift
);; }
199 wxLongLongNative
& operator<<=(int shift
)
200 { m_ll
<<= shift
; return *this; }
203 wxLongLongNative
operator>>(int shift
) const
204 { return wxLongLongNative(m_ll
>> shift
);; }
205 wxLongLongNative
& operator>>=(int shift
)
206 { m_ll
>>= shift
; return *this; }
209 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
210 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
211 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
212 { m_ll
&= ll
.m_ll
; 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 // multiplication/division
225 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
226 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
227 wxLongLongNative
operator*(long l
) const
228 { return wxLongLongNative(m_ll
* l
); }
229 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
230 { m_ll
*= ll
.m_ll
; return *this; }
231 wxLongLongNative
& operator*=(long l
)
232 { m_ll
*= l
; return *this; }
234 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
235 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
236 wxLongLongNative
operator/(long l
) const
237 { return wxLongLongNative(m_ll
/ l
); }
238 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
239 { m_ll
/= ll
.m_ll
; return *this; }
240 wxLongLongNative
& operator/=(long l
)
241 { m_ll
/= l
; return *this; }
243 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
244 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
245 wxLongLongNative
operator%(long l
) const
246 { return wxLongLongNative(m_ll
% l
); }
249 bool operator==(const wxLongLongNative
& ll
) const
250 { return m_ll
== ll
.m_ll
; }
251 bool operator==(long l
) const
252 { return m_ll
== l
; }
253 bool operator!=(const wxLongLongNative
& ll
) const
254 { return m_ll
!= ll
.m_ll
; }
255 bool operator!=(long l
) const
256 { return m_ll
!= l
; }
257 bool operator<(const wxLongLongNative
& ll
) const
258 { return m_ll
< ll
.m_ll
; }
259 bool operator<(long l
) const
261 bool operator>(const wxLongLongNative
& ll
) const
262 { return m_ll
> ll
.m_ll
; }
263 bool operator>(long l
) const
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
272 { return m_ll
>= l
; }
275 // conversion to byte array: returns a pointer to static buffer!
276 void *asArray() const;
278 #if wxUSE_STD_IOSTREAM
280 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
287 #endif // wxUSE_LONGLONG_NATIVE
289 #if wxUSE_LONGLONG_WX
291 class WXDLLEXPORT wxLongLongWx
295 // default ctor initializes to 0
296 wxLongLongWx() { m_lo
= m_hi
= 0; }
299 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
301 wxLongLongWx(long hi
, unsigned long lo
)
302 { m_hi
= hi
; m_lo
= lo
; }
304 // default copy ctor is ok in both cases
308 // assignment operators
310 wxLongLongWx
& operator=(long l
)
311 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
312 // can't have assignment operator from 2 longs
316 long GetHi() const { return m_hi
; }
318 unsigned long GetLo() const { return m_lo
; }
322 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
323 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
324 wxLongLongWx
operator+(long l
) const;
325 wxLongLongWx
& operator+=(long l
);
327 // pre increment operator
328 wxLongLongWx
& operator++();
330 // post increment operator
331 wxLongLongWx
& operator++(int);
334 wxLongLongWx
operator-() const;
337 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
338 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
340 // pre decrement operator
341 wxLongLongWx
& operator--();
343 // post decrement operator
344 wxLongLongWx
& operator--(int);
348 wxLongLongWx
operator<<(int shift
) const;
349 wxLongLongWx
& operator<<=(int shift
);
352 wxLongLongWx
operator>>(int shift
) const;
353 wxLongLongWx
& operator>>=(int shift
);
356 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
357 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
358 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
359 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
360 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
361 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
362 wxLongLongWx
operator~() const;
365 bool operator==(const wxLongLongWx
& ll
) const;
366 bool operator!=(const wxLongLongWx
& ll
) const;
367 bool operator<(const wxLongLongWx
& ll
) const;
368 bool operator>(const wxLongLongWx
& ll
) const;
369 bool operator<=(const wxLongLongWx
& ll
) const;
370 bool operator>=(const wxLongLongWx
& ll
) const;
373 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
374 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
375 void *asArray(void) const;
378 void Divide(const wxLongLongWx
& divisor
,
379 wxLongLongWx
& quotient
,
380 wxLongLongWx
& remainder
) const;
382 #if wxUSE_STD_IOSTREAM
384 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
385 #endif // wxUSE_STD_IOSTREAM
388 // long is at least 32 bits, so represent our 64bit number as 2 longs
390 long m_hi
; // signed bit is in the high part
394 #endif // wxUSE_LONGLONG_WX
396 #endif // _WX_LONGLONG_H