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
27 // we will need to define this
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 and because we use "#ifdef
32 // wxLongLong_t" below
33 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
34 #define wxLongLong_t long
35 #define wxLongLongIsLong
36 #elif defined(__VISUALC__)
37 #define wxLongLong_t __int64
38 #elif defined(__GNUG__)
39 #define wxLongLong_t long long
40 #elif defined(__MWERKS__)
41 #if __option(longlong)
42 #define wxLongLong_t long long
44 #error "The 64 bit integer support in CodeWarrior has been disabled."
45 #error "See the documentation on the 'longlong' pragma."
48 #warning "Your compiler does not appear to support 64 bit integers, "\
49 "using emulation class instead."
50 #define wxUSE_LONGLONG_WX 1
53 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
54 // to disable automatic testing (useful for the test program which defines
55 // both classes) but by default we only use one class
57 #undef wxUSE_LONGLONG_NATIVE
58 #define wxUSE_LONGLONG_NATIVE 0
59 class WXDLLEXPORT wxLongLongWx
;
60 typedef wxLongLongWx wxLongLong
;
62 // if nothing is defined, use native implementation by default, of course
63 #ifndef wxUSE_LONGLONG_NATIVE
64 #define wxUSE_LONGLONG_NATIVE 1
68 #ifndef wxUSE_LONGLONG_WX
69 #define wxUSE_LONGLONG_WX 0
70 class WXDLLEXPORT wxLongLongNative
;
71 typedef wxLongLongNative wxLongLong
;
74 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
75 // typedef wxLongLong as it wants, we don't do it
77 // ----------------------------------------------------------------------------
78 // choose the appropriate class
79 // ----------------------------------------------------------------------------
81 // we use iostream for wxLongLong output
82 #include "wx/ioswrap.h"
84 #if wxUSE_LONGLONG_NATIVE
86 class WXDLLEXPORT wxLongLongNative
90 // default ctor initializes to 0
91 wxLongLongNative() { m_ll
= 0; }
93 wxLongLongNative(wxLongLong_t ll
) { m_ll
= ll
; }
95 wxLongLongNative(long hi
, unsigned long lo
)
97 // assign first to avoid precision loss!
98 m_ll
= ((wxLongLong_t
) hi
) << 32;
99 m_ll
|= (wxLongLong_t
) lo
;
102 // default copy ctor is ok
106 // assignment operators
107 // from native 64 bit integer
108 wxLongLongNative
& operator=(wxLongLong_t ll
)
109 { m_ll
= ll
; return *this; }
111 // assignment operators from wxLongLongNative is ok
116 { return (long)(m_ll
>> 32); }
118 unsigned long GetLo() const
119 { return (unsigned long)m_ll
; }
121 // get absolute value
122 wxLongLongNative
& Abs() { if ( m_ll
< 0 ) m_ll
= -m_ll
; return *this; }
124 // convert to native long long
125 wxLongLong_t
GetValue() const { return m_ll
; }
127 // don't provide implicit conversion to wxLongLong_t or we will have an
128 // ambiguity for all arithmetic operations
129 //operator wxLongLong_t() const { return m_ll; }
133 wxLongLongNative
operator+(const wxLongLongNative
& ll
) const
134 { return wxLongLongNative(m_ll
+ ll
.m_ll
); }
135 wxLongLongNative
& operator+=(const wxLongLongNative
& ll
)
136 { m_ll
+= ll
.m_ll
; return *this; }
138 wxLongLongNative
operator+(const wxLongLong_t ll
) const
139 { return wxLongLongNative(m_ll
+ ll
); }
140 wxLongLongNative
& operator+=(const wxLongLong_t ll
)
141 { m_ll
+= ll
; return *this; }
144 wxLongLongNative
& operator++()
145 { m_ll
++; return *this; }
148 wxLongLongNative
& operator++(int)
149 { m_ll
++; return *this; }
152 wxLongLongNative
operator-() const
153 { return wxLongLongNative(-m_ll
); }
156 wxLongLongNative
operator-(const wxLongLongNative
& ll
) const
157 { return wxLongLongNative(m_ll
- ll
.m_ll
); }
158 wxLongLongNative
& operator-=(const wxLongLongNative
& ll
)
159 { m_ll
-= ll
.m_ll
; return *this; }
161 wxLongLongNative
operator-(const wxLongLong_t ll
) const
162 { return wxLongLongNative(m_ll
- ll
); }
163 wxLongLongNative
& operator-=(const wxLongLong_t ll
)
164 { m_ll
-= ll
; return *this; }
167 wxLongLongNative
& operator--()
168 { m_ll
--; return *this; }
171 wxLongLongNative
& operator--(int)
172 { m_ll
--; return *this; }
176 wxLongLongNative
operator<<(int shift
) const
177 { return wxLongLongNative(m_ll
<< shift
);; }
178 wxLongLongNative
& operator<<=(int shift
)
179 { m_ll
<<= shift
; return *this; }
182 wxLongLongNative
operator>>(int shift
) const
183 { return wxLongLongNative(m_ll
>> shift
);; }
184 wxLongLongNative
& operator>>=(int shift
)
185 { m_ll
>>= shift
; return *this; }
188 wxLongLongNative
operator&(const wxLongLongNative
& ll
) const
189 { return wxLongLongNative(m_ll
& ll
.m_ll
); }
190 wxLongLongNative
& operator&=(const wxLongLongNative
& ll
)
191 { m_ll
&= ll
.m_ll
; return *this; }
193 wxLongLongNative
operator|(const wxLongLongNative
& ll
) const
194 { return wxLongLongNative(m_ll
| ll
.m_ll
); }
195 wxLongLongNative
& operator|=(const wxLongLongNative
& ll
)
196 { m_ll
|= ll
.m_ll
; return *this; }
198 wxLongLongNative
operator^(const wxLongLongNative
& ll
) const
199 { return wxLongLongNative(m_ll
^ ll
.m_ll
); }
200 wxLongLongNative
& operator^=(const wxLongLongNative
& ll
)
201 { m_ll
^= ll
.m_ll
; return *this; }
203 // multiplication/division
204 wxLongLongNative
operator*(const wxLongLongNative
& ll
) const
205 { return wxLongLongNative(m_ll
* ll
.m_ll
); }
206 wxLongLongNative
operator*(long l
) const
207 { return wxLongLongNative(m_ll
* l
); }
208 wxLongLongNative
& operator*=(const wxLongLongNative
& ll
)
209 { m_ll
*= ll
.m_ll
; return *this; }
210 wxLongLongNative
& operator*=(long l
)
211 { m_ll
*= l
; 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
); }
217 wxLongLongNative
& operator/=(const wxLongLongNative
& ll
)
218 { m_ll
/= ll
.m_ll
; return *this; }
219 wxLongLongNative
& operator/=(long l
)
220 { m_ll
/= l
; return *this; }
222 wxLongLongNative
operator%(const wxLongLongNative
& ll
) const
223 { return wxLongLongNative(m_ll
% ll
.m_ll
); }
224 wxLongLongNative
operator%(long l
) const
225 { return wxLongLongNative(m_ll
% l
); }
228 bool operator==(const wxLongLongNative
& ll
) const
229 { return m_ll
== ll
.m_ll
; }
230 bool operator==(long l
) const
231 { return m_ll
== l
; }
232 bool operator!=(const wxLongLongNative
& ll
) const
233 { return m_ll
!= ll
.m_ll
; }
234 bool operator!=(long l
) const
235 { return m_ll
!= l
; }
236 bool operator<(const wxLongLongNative
& ll
) const
237 { return m_ll
< ll
.m_ll
; }
238 bool operator<(long l
) const
240 bool operator>(const wxLongLongNative
& ll
) const
241 { return m_ll
> ll
.m_ll
; }
242 bool operator>(long l
) const
244 bool operator<=(const wxLongLongNative
& ll
) const
245 { return m_ll
<= ll
.m_ll
; }
246 bool operator<=(long l
) const
247 { return m_ll
<= l
; }
248 bool operator>=(const wxLongLongNative
& ll
) const
249 { return m_ll
>= ll
.m_ll
; }
250 bool operator>=(long l
) const
251 { return m_ll
>= l
; }
254 // conversion to byte array: returns a pointer to static buffer!
255 void *asArray() const;
258 friend ostream
& operator<<(ostream
&, const wxLongLongNative
&);
264 #endif // wxUSE_LONGLONG_NATIVE
266 #if wxUSE_LONGLONG_WX
268 class WXDLLEXPORT wxLongLongWx
272 // default ctor initializes to 0
273 wxLongLongWx() { m_lo
= m_hi
= 0; }
276 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); }
278 wxLongLongWx(long hi
, unsigned long lo
)
279 { m_hi
= hi
; m_lo
= lo
; }
281 // default copy ctor is ok in both cases
285 // assignment operators
287 wxLongLongWx
& operator=(long l
)
288 { m_lo
= l
; m_hi
= (l
< 0 ? -1l : 0l); return *this; }
289 // can't have assignment operator from 2 longs
293 long GetHi() const { return m_hi
; }
295 unsigned long GetLo() const { return m_lo
; }
299 wxLongLongWx
operator+(const wxLongLongWx
& ll
) const;
300 wxLongLongWx
& operator+=(const wxLongLongWx
& ll
);
301 wxLongLongWx
operator+(long l
) const;
302 wxLongLongWx
& operator+=(long l
);
304 // pre increment operator
305 wxLongLongWx
& operator++();
307 // post increment operator
308 wxLongLongWx
& operator++(int);
311 wxLongLongWx
operator-() const;
314 wxLongLongWx
operator-(const wxLongLongWx
& ll
) const;
315 wxLongLongWx
& operator-=(const wxLongLongWx
& ll
);
317 // pre decrement operator
318 wxLongLongWx
& operator--();
320 // post decrement operator
321 wxLongLongWx
& operator--(int);
325 wxLongLongWx
operator<<(int shift
) const;
326 wxLongLongWx
& operator<<=(int shift
);
329 wxLongLongWx
operator>>(int shift
) const;
330 wxLongLongWx
& operator>>=(int shift
);
333 wxLongLongWx
operator&(const wxLongLongWx
& ll
) const;
334 wxLongLongWx
& operator&=(const wxLongLongWx
& ll
);
335 wxLongLongWx
operator|(const wxLongLongWx
& ll
) const;
336 wxLongLongWx
& operator|=(const wxLongLongWx
& ll
);
337 wxLongLongWx
operator^(const wxLongLongWx
& ll
) const;
338 wxLongLongWx
& operator^=(const wxLongLongWx
& ll
);
339 wxLongLongWx
operator~() const;
342 bool operator==(const wxLongLongWx
& ll
) const;
343 bool operator!=(const wxLongLongWx
& ll
) const;
344 bool operator<(const wxLongLongWx
& ll
) const;
345 bool operator>(const wxLongLongWx
& ll
) const;
346 bool operator<=(const wxLongLongWx
& ll
) const;
347 bool operator>=(const wxLongLongWx
& ll
) const;
350 wxLongLongWx
operator*(const wxLongLongWx
& ll
) const;
351 wxLongLongWx
& operator*=(const wxLongLongWx
& ll
);
352 void *asArray(void) const;
355 void Divide(const wxLongLongWx
& divisor
,
356 wxLongLongWx
& quotient
,
357 wxLongLongWx
& remainder
) const;
360 friend ostream
& operator<<(ostream
&, const wxLongLongWx
&);
363 // long is at least 32 bits, so represent our 64bit number as 2 longs
365 long m_hi
; // signed bit is in the high part
369 #endif // wxUSE_LONGLONG_WX
371 #endif // _WX_LONGLONG_H