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"
22 // ----------------------------------------------------------------------------
23 // decide upon which class we will use
24 // ----------------------------------------------------------------------------
26 // to avoid compilation problems on 64bit machines with ambiguous method calls
28 #undef wxLongLongIsLong
30 // NB: we #define and not typedef wxLongLong_t because we want to be able to
31 // use 'unsigned wxLongLong_t' as well
32 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
33 #define wxLongLong_t long
34 #define wxLongLongIsLong
35 #elif defined(__VISUALC__)
36 #define wxLongLong_t __int64
37 #elif defined(__GNUG__)
38 #define wxLongLong_t long long
39 #elif defined(__MWERKS__)
40 #if __option(longlong)
41 #define wxLongLong_t long long
43 #error "The 64 bit integer support in CodeWarrior has been disabled."
44 #error "See the documentation on the 'longlong' pragma."
47 #warning "Your compiler does not appear to support 64 bit integers, "\
48 "using emulation class instead."
49 #define wxUSE_LONGLONG_WX 1
52 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
53 // to disable automatic testing (useful for the test program which defines
54 // both classes) but by default we only use one class
56 #undef wxUSE_LONGLONG_NATIVE
57 #define wxUSE_LONGLONG_NATIVE 0
58 class WXDLLEXPORT wxLongLongWx
;
59 typedef wxLongLongWx wxLongLong
;
61 // if nothing is defined, use native implementation by default, of course
62 #ifndef wxUSE_LONGLONG_NATIVE
63 #define wxUSE_LONGLONG_NATIVE 1
67 #ifndef wxUSE_LONGLONG_WX
68 #define wxUSE_LONGLONG_WX 0
69 class WXDLLEXPORT wxLongLongNative
;
70 typedef wxLongLongNative wxLongLong
;
73 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
74 // typedef wxLongLong as it wants, we don't do it
76 // ----------------------------------------------------------------------------
77 // choose the appropriate class
78 // ----------------------------------------------------------------------------
80 // we use iostream for wxLongLong output
81 #include "wx/ioswrap.h"
83 #if wxUSE_LONGLONG_NATIVE
85 class WXDLLEXPORT wxLongLongNative
89 // default ctor initializes to 0
90 wxLongLongNative() { m_ll
= 0; }
92 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
94 wxLongLongNative(long hi
, unsigned long lo
)
96 // assign first to avoid precision loss!
97 m_ll
= ((wxLongLong_t
) hi
) << 32;
98 m_ll
|= (wxLongLong_t
) lo
;
101 // default copy ctor is ok in both cases
105 // assignment operators
106 // from native 64 bit integer
107 wxLongLongNative
& operator=(wxLongLong_t ll
)
108 { m_ll
= ll
; return *this; }
110 // assignment operators from wxLongLongNative is ok
115 { return (long)((m_ll
& 0xFFFFFFFF00000000l
) >> 32); }
117 unsigned long GetLo() const
118 { return (unsigned long) (m_ll
& 0x00000000FFFFFFFFl
); }
120 // get absolute value
121 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
123 // convert to native long long
124 wxLongLong_t
GetValue() const { return m_ll
; }
126 //operator wxLongLong_t() const { return m_ll; }
130 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
131 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
132 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
133 { m_ll
+= ll
.m_ll
; return *this; }
135 wxLongLongNative
operator+(const wxLongLong_t ll
) const
136 { return wxLongLongNative(m_ll
+ ll
); }
137 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
138 { m_ll
+= ll
; return *this; }
141 wxLongLongNative
& operator++()
142 { m_ll
++; return *this; }
145 wxLongLongNative
& operator++(int)
146 { m_ll
++; return *this; }
149 wxLongLongNative
operator-() const
150 { return wxLongLongNative(-m_ll
); }
153 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
154 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
155 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
156 { m_ll
-= ll
.m_ll
; return *this; }
158 wxLongLongNative
operator-(const wxLongLong_t ll
) const
159 { return wxLongLongNative(m_ll
- ll
); }
160 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
161 { m_ll
-= ll
; return *this; }
164 wxLongLongNative
& operator--()
165 { m_ll
--; return *this; }
168 wxLongLongNative
& operator--(int)
169 { m_ll
--; return *this; }
173 wxLongLongNative
operator<<(int shift
) const
174 { return wxLongLongNative(m_ll
<< shift
);; }
175 wxLongLongNative
& operator<<=(int shift
)
176 { m_ll
<<= shift
; return *this; }
179 wxLongLongNative
operator>>(int shift
) const
180 { return wxLongLongNative(m_ll
>> shift
);; }
181 wxLongLongNative
& operator>>=(int shift
)
182 { m_ll
>>= shift
; return *this; }
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 wxLongLongNative
& ll
) const
191 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
192 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
193 { m_ll
|= ll
.m_ll
; return *this; }
195 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
196 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
197 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
198 { m_ll
^= ll
.m_ll
; return *this; }
200 // multiplication/division
201 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
202 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
203 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
204 { m_ll
*= ll
.m_ll
; return *this; }
206 wxLongLongNative
operator/(const wxLongLongNative
& ll
) const
207 { return wxLongLongNative(m_ll
/ ll
.m_ll
); }
208 wxLongLongNative
operator/(long l
) const
209 { return wxLongLongNative(m_ll
/ l
); }
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%(long l
) const
216 { return wxLongLongNative(m_ll
% l
); }
219 bool operator==(const wxLongLongNative
& ll
) const
220 { return m_ll
== ll
.m_ll
; }
221 bool operator==(long l
) const
222 { return m_ll
== l
; }
223 bool operator!=(const wxLongLongNative
& ll
) const
224 { return m_ll
!= ll
.m_ll
; }
225 bool operator!=(long l
) const
226 { return m_ll
!= l
; }
227 bool operator<(const wxLongLongNative
& ll
) const
228 { return m_ll
< ll
.m_ll
; }
229 bool operator<(long l
) const
231 bool operator>(const wxLongLongNative
& ll
) const
232 { return m_ll
> ll
.m_ll
; }
233 bool operator>(long l
) const
235 bool operator<=(const wxLongLongNative
& ll
) const
236 { return m_ll
<= ll
.m_ll
; }
237 bool operator<=(long l
) const
238 { return m_ll
<= l
; }
239 bool operator>=(const wxLongLongNative
& ll
) const
240 { return m_ll
>= ll
.m_ll
; }
241 bool operator>=(long l
) const
242 { return m_ll
>= l
; }
245 // conversion to byte array: returns a pointer to static buffer!
246 void *asArray() const;
249 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
255 #endif // wxUSE_LONGLONG_NATIVE
257 #if wxUSE_LONGLONG_WX
259 class WXDLLEXPORT wxLongLongWx
263 // default ctor initializes to 0
264 wxLongLongWx() { m_lo
= m_hi
= 0; }
267 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
269 wxLongLongWx(long hi
, unsigned long lo
)
270 { m_hi
= hi
; m_lo
= lo
; }
272 // default copy ctor is ok in both cases
276 // assignment operators
278 wxLongLongWx
& operator=(long l
)
279 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
280 // can't have assignment operator from 2 longs
284 long GetHi() const { return m_hi
; }
286 unsigned long GetLo() const { return m_lo
; }
290 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
291 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
292 wxLongLongWx
operator+(long l
) const;
293 wxLongLongWx
& operator+=(long l
);
295 // pre increment operator
296 wxLongLongWx
& operator++();
298 // post increment operator
299 wxLongLongWx
& operator++(int);
302 wxLongLongWx
operator-() const;
305 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
306 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
308 // pre decrement operator
309 wxLongLongWx
& operator--();
311 // post decrement operator
312 wxLongLongWx
& operator--(int);
316 wxLongLongWx
operator<<(int shift
) const;
317 wxLongLongWx
& operator<<=(int shift
);
320 wxLongLongWx
operator>>(int shift
) const;
321 wxLongLongWx
& operator>>=(int shift
);
324 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
325 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
326 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
327 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
328 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
329 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
330 wxLongLongWx
operator~() const;
333 bool operator==(const wxLongLongWx
& ll
) const;
334 bool operator!=(const wxLongLongWx
& ll
) const;
335 bool operator<(const wxLongLongWx
& ll
) const;
336 bool operator>(const wxLongLongWx
& ll
) const;
337 bool operator<=(const wxLongLongWx
& ll
) const;
338 bool operator>=(const wxLongLongWx
& ll
) const;
341 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
342 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
343 void *asArray(void) const;
346 void Divide(const wxLongLongWx
& divisor
,
347 wxLongLongWx
& quotient
,
348 wxLongLongWx
& remainder
) const;
351 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
354 // long is at least 32 bits, so represent our 64bit number as 2 longs
356 long m_hi
; // signed bit is in the high part
360 #endif // wxUSE_LONGLONG_WX
362 #endif // _WX_LONGLONG_H