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__)
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."
54 #warning "Your compiler does not appear to support 64 bit integers, "\
55 "using emulation class instead."
56 #define wxUSE_LONGLONG_WX 1
59 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
60 // to disable automatic testing (useful for the test program which defines
61 // both classes) but by default we only use one class
63 #undef wxUSE_LONGLONG_NATIVE
64 #define wxUSE_LONGLONG_NATIVE 0
65 class WXDLLEXPORT wxLongLongWx
;
66 typedef wxLongLongWx wxLongLong
;
68 // if nothing is defined, use native implementation by default, of course
69 #ifndef wxUSE_LONGLONG_NATIVE
70 #define wxUSE_LONGLONG_NATIVE 1
74 #ifndef wxUSE_LONGLONG_WX
75 #define wxUSE_LONGLONG_WX 0
76 class WXDLLEXPORT wxLongLongNative
;
77 typedef wxLongLongNative wxLongLong
;
80 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
81 // typedef wxLongLong as it wants, we don't do it
83 // ----------------------------------------------------------------------------
84 // choose the appropriate class
85 // ----------------------------------------------------------------------------
87 // we use iostream for wxLongLong output
88 #include "wx/ioswrap.h"
90 #if wxUSE_LONGLONG_NATIVE
92 class WXDLLEXPORT wxLongLongNative
96 // default ctor initializes to 0
97 wxLongLongNative() { m_ll
= 0; }
99 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
101 wxLongLongNative(long hi
, unsigned long lo
)
103 // assign first to avoid precision loss!
104 m_ll
= ((wxLongLong_t
) hi
) << 32;
105 m_ll
|= (wxLongLong_t
) lo
;
108 // default copy ctor is ok
112 // assignment operators
113 // from native 64 bit integer
114 wxLongLongNative
& operator=(wxLongLong_t ll
)
115 { m_ll
= ll
; return *this; }
117 // assignment operators from wxLongLongNative is ok
122 { return (long)(m_ll
>> 32); }
124 unsigned long GetLo() const
125 { return (unsigned long)m_ll
; }
127 // get absolute value
128 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
130 // convert to native long long
131 wxLongLong_t
GetValue() const { return m_ll
; }
133 // convert to long with range checking in the debug mode (only!)
136 wxASSERT_MSG( (m_ll
>= LONG_MIN
) && (m_ll
<= LONG_MAX
),
137 _T("wxLongLong to long conversion loss of precision") );
142 // don't provide implicit conversion to wxLongLong_t or we will have an
143 // ambiguity for all arithmetic operations
144 //operator wxLongLong_t() const { return m_ll; }
148 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
149 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
150 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
151 { m_ll
+= ll
.m_ll
; return *this; }
153 wxLongLongNative
operator+(const wxLongLong_t ll
) const
154 { return wxLongLongNative(m_ll
+ ll
); }
155 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
156 { m_ll
+= ll
; return *this; }
159 wxLongLongNative
& operator++()
160 { m_ll
++; return *this; }
163 wxLongLongNative
& operator++(int)
164 { m_ll
++; return *this; }
167 wxLongLongNative
operator-() const
168 { return wxLongLongNative(-m_ll
); }
171 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
172 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
173 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
174 { m_ll
-= ll
.m_ll
; return *this; }
176 wxLongLongNative
operator-(const wxLongLong_t ll
) const
177 { return wxLongLongNative(m_ll
- ll
); }
178 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
179 { m_ll
-= ll
; return *this; }
182 wxLongLongNative
& operator--()
183 { m_ll
--; return *this; }
186 wxLongLongNative
& operator--(int)
187 { m_ll
--; return *this; }
191 wxLongLongNative
operator<<(int shift
) const
192 { return wxLongLongNative(m_ll
<< shift
);; }
193 wxLongLongNative
& operator<<=(int shift
)
194 { m_ll
<<= shift
; 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&(const wxLongLongNative
& ll
) const
204 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
205 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
206 { m_ll
&= ll
.m_ll
; return *this; }
208 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
209 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
210 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
211 { m_ll
|= ll
.m_ll
; return *this; }
213 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
214 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
215 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
216 { m_ll
^= ll
.m_ll
; return *this; }
218 // multiplication/division
219 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
220 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
221 wxLongLongNative
operator*(long l
) const
222 { return wxLongLongNative(m_ll
* l
); }
223 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
224 { m_ll
*= ll
.m_ll
; return *this; }
225 wxLongLongNative
& operator*=(long l
)
226 { m_ll
*= l
; return *this; }
228 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
229 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
230 wxLongLongNative
operator/(long l
) const
231 { return wxLongLongNative(m_ll
/ l
); }
232 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
233 { m_ll
/= ll
.m_ll
; return *this; }
234 wxLongLongNative
& operator/=(long l
)
235 { m_ll
/= l
; return *this; }
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
); }
243 bool operator==(const wxLongLongNative
& ll
) const
244 { return m_ll
== ll
.m_ll
; }
245 bool operator==(long l
) const
246 { return m_ll
== l
; }
247 bool operator!=(const wxLongLongNative
& ll
) const
248 { return m_ll
!= ll
.m_ll
; }
249 bool operator!=(long l
) const
250 { return m_ll
!= l
; }
251 bool operator<(const wxLongLongNative
& ll
) const
252 { return m_ll
< ll
.m_ll
; }
253 bool operator<(long l
) const
255 bool operator>(const wxLongLongNative
& ll
) const
256 { return m_ll
> ll
.m_ll
; }
257 bool operator>(long l
) const
259 bool operator<=(const wxLongLongNative
& ll
) const
260 { return m_ll
<= ll
.m_ll
; }
261 bool operator<=(long l
) const
262 { return m_ll
<= l
; }
263 bool operator>=(const wxLongLongNative
& ll
) const
264 { return m_ll
>= ll
.m_ll
; }
265 bool operator>=(long l
) const
266 { return m_ll
>= l
; }
269 // conversion to byte array: returns a pointer to static buffer!
270 void *asArray() const;
272 #if wxUSE_STD_IOSTREAM
274 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
281 #endif // wxUSE_LONGLONG_NATIVE
283 #if wxUSE_LONGLONG_WX
285 class WXDLLEXPORT wxLongLongWx
289 // default ctor initializes to 0
290 wxLongLongWx() { m_lo
= m_hi
= 0; }
293 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
295 wxLongLongWx(long hi
, unsigned long lo
)
296 { m_hi
= hi
; m_lo
= lo
; }
298 // default copy ctor is ok in both cases
302 // assignment operators
304 wxLongLongWx
& operator=(long l
)
305 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
306 // can't have assignment operator from 2 longs
310 long GetHi() const { return m_hi
; }
312 unsigned long GetLo() const { return m_lo
; }
316 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
317 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
318 wxLongLongWx
operator+(long l
) const;
319 wxLongLongWx
& operator+=(long l
);
321 // pre increment operator
322 wxLongLongWx
& operator++();
324 // post increment operator
325 wxLongLongWx
& operator++(int);
328 wxLongLongWx
operator-() const;
331 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
332 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
334 // pre decrement operator
335 wxLongLongWx
& operator--();
337 // post decrement operator
338 wxLongLongWx
& operator--(int);
342 wxLongLongWx
operator<<(int shift
) const;
343 wxLongLongWx
& operator<<=(int shift
);
346 wxLongLongWx
operator>>(int shift
) const;
347 wxLongLongWx
& operator>>=(int shift
);
350 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
351 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
352 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
353 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
354 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
355 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
356 wxLongLongWx
operator~() const;
359 bool operator==(const wxLongLongWx
& ll
) const;
360 bool operator!=(const wxLongLongWx
& ll
) const;
361 bool operator<(const wxLongLongWx
& ll
) const;
362 bool operator>(const wxLongLongWx
& ll
) const;
363 bool operator<=(const wxLongLongWx
& ll
) const;
364 bool operator>=(const wxLongLongWx
& ll
) const;
367 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
368 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
369 void *asArray(void) const;
372 void Divide(const wxLongLongWx
& divisor
,
373 wxLongLongWx
& quotient
,
374 wxLongLongWx
& remainder
) const;
376 #if wxUSE_STD_IOSTREAM
378 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
379 #endif // wxUSE_STD_IOSTREAM
382 // long is at least 32 bits, so represent our 64bit number as 2 longs
384 long m_hi
; // signed bit is in the high part
388 #endif // wxUSE_LONGLONG_WX
390 #endif // _WX_LONGLONG_H