]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/longlong.h | |
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. | |
8 | // Modified by: | |
9 | // Created: 10.02.99 | |
10 | // RCS-ID: $Id$ | |
11 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
12 | // Licence: wxWindows license | |
13 | ///////////////////////////////////////////////////////////////////////////// | |
14 | ||
15 | #ifndef _WX_LONGLONG_H | |
16 | #define _WX_LONGLONG_H | |
17 | ||
18 | #ifdef __GNUG__ | |
19 | #pragma interface "longlong.h" | |
20 | #endif | |
21 | ||
22 | #include "wx/defs.h" | |
23 | #include "wx/wxchar.h" | |
24 | #include "wx/debug.h" | |
25 | ||
26 | #include <limits.h> // for LONG_MAX | |
27 | ||
28 | // #define wxUSE_LONGLONG_WX 1 // for testing (VZ) | |
29 | ||
30 | // ---------------------------------------------------------------------------- | |
31 | // decide upon which class we will use | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
34 | // to avoid compilation problems on 64bit machines with ambiguous method calls | |
35 | // we will need to define this | |
36 | #undef wxLongLongIsLong | |
37 | ||
38 | // NB: we #define and not typedef wxLongLong_t because we want to be able to | |
39 | // use 'unsigned wxLongLong_t' as well and because we use "#ifdef | |
40 | // wxLongLong_t" below | |
41 | #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8) | |
42 | #define wxLongLong_t long | |
43 | #define wxLongLongIsLong | |
44 | #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ ) | |
45 | #define wxLongLong_t __int64 | |
46 | #elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x530) | |
47 | #define wxLongLong_t __int64 | |
48 | #elif defined(__GNUG__) | |
49 | #define wxLongLong_t long long | |
50 | #elif defined(__MWERKS__) | |
51 | #if __option(longlong) | |
52 | #define wxLongLong_t long long | |
53 | #else | |
54 | #error "The 64 bit integer support in CodeWarrior has been disabled." | |
55 | #error "See the documentation on the 'longlong' pragma." | |
56 | #endif | |
57 | #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400 | |
58 | #define wxLongLong_t long long | |
59 | #else // no native long long type | |
60 | // both warning and pragma warning are not portable, but at least an | |
61 | // unknown pragma should never be an error. | |
62 | // Err, actually, Watcom C++ doesn't like it. | |
63 | // (well, if the compilers are _that_ broken, I'm removing it (VZ)) | |
64 | #if 0 //ndef __WATCOMC__ | |
65 | #pragma warning "Your compiler does not appear to support 64 bit "\ | |
66 | "integers, using emulation class instead." | |
67 | #endif | |
68 | ||
69 | #define wxUSE_LONGLONG_WX 1 | |
70 | #endif // compiler | |
71 | ||
72 | // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE | |
73 | // to disable automatic testing (useful for the test program which defines | |
74 | // both classes) but by default we only use one class | |
75 | #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t) | |
76 | #undef wxUSE_LONGLONG_NATIVE | |
77 | #define wxUSE_LONGLONG_NATIVE 0 | |
78 | class WXDLLEXPORT wxLongLongWx; | |
79 | typedef wxLongLongWx wxLongLong; | |
80 | #else | |
81 | // if nothing is defined, use native implementation by default, of course | |
82 | #ifndef wxUSE_LONGLONG_NATIVE | |
83 | #define wxUSE_LONGLONG_NATIVE 1 | |
84 | #endif | |
85 | #endif | |
86 | ||
87 | #ifndef wxUSE_LONGLONG_WX | |
88 | #define wxUSE_LONGLONG_WX 0 | |
89 | class WXDLLEXPORT wxLongLongNative; | |
90 | typedef wxLongLongNative wxLongLong; | |
91 | #endif | |
92 | ||
93 | // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should | |
94 | // typedef wxLongLong as it wants, we don't do it | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // choose the appropriate class | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | // we use iostream for wxLongLong output | |
101 | #include "wx/ioswrap.h" | |
102 | ||
103 | #if wxUSE_LONGLONG_NATIVE | |
104 | ||
105 | class WXDLLEXPORT wxLongLongNative | |
106 | { | |
107 | public: | |
108 | // ctors | |
109 | // default ctor initializes to 0 | |
110 | wxLongLongNative() { m_ll = 0; } | |
111 | // from long long | |
112 | wxLongLongNative(wxLongLong_t ll) { m_ll = ll; } | |
113 | // from 2 longs | |
114 | wxLongLongNative(long hi, unsigned long lo) | |
115 | { | |
116 | // assign first to avoid precision loss! | |
117 | m_ll = ((wxLongLong_t) hi) << 32; | |
118 | m_ll |= (wxLongLong_t) lo; | |
119 | } | |
120 | ||
121 | // default copy ctor is ok | |
122 | ||
123 | // no dtor | |
124 | ||
125 | // assignment operators | |
126 | // from native 64 bit integer | |
127 | wxLongLongNative& operator=(wxLongLong_t ll) | |
128 | { m_ll = ll; return *this; } | |
129 | ||
130 | // from double: this one has an explicit name because otherwise we | |
131 | // would have ambiguity with "ll = int" and also because we don't want | |
132 | // to have implicit conversions between doubles and wxLongLongs | |
133 | wxLongLongNative& Assign(double d) | |
134 | { m_ll = (wxLongLong_t)d; return *this; } | |
135 | ||
136 | // assignment operators from wxLongLongNative is ok | |
137 | ||
138 | // accessors | |
139 | // get high part | |
140 | long GetHi() const | |
141 | { return (long)(m_ll >> 32); } | |
142 | // get low part | |
143 | unsigned long GetLo() const | |
144 | { return (unsigned long)m_ll; } | |
145 | ||
146 | // get absolute value | |
147 | wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); } | |
148 | wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; } | |
149 | ||
150 | // convert to native long long | |
151 | wxLongLong_t GetValue() const { return m_ll; } | |
152 | ||
153 | // convert to long with range checking in the debug mode (only!) | |
154 | long ToLong() const | |
155 | { | |
156 | wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX), | |
157 | _T("wxLongLong to long conversion loss of precision") ); | |
158 | ||
159 | return (long)m_ll; | |
160 | } | |
161 | ||
162 | // don't provide implicit conversion to wxLongLong_t or we will have an | |
163 | // ambiguity for all arithmetic operations | |
164 | //operator wxLongLong_t() const { return m_ll; } | |
165 | ||
166 | // operations | |
167 | // addition | |
168 | wxLongLongNative operator+(const wxLongLongNative& ll) const | |
169 | { return wxLongLongNative(m_ll + ll.m_ll); } | |
170 | wxLongLongNative& operator+=(const wxLongLongNative& ll) | |
171 | { m_ll += ll.m_ll; return *this; } | |
172 | ||
173 | wxLongLongNative operator+(const wxLongLong_t ll) const | |
174 | { return wxLongLongNative(m_ll + ll); } | |
175 | wxLongLongNative& operator+=(const wxLongLong_t ll) | |
176 | { m_ll += ll; return *this; } | |
177 | ||
178 | // pre increment | |
179 | wxLongLongNative& operator++() | |
180 | { m_ll++; return *this; } | |
181 | ||
182 | // post increment | |
183 | wxLongLongNative& operator++(int) | |
184 | { m_ll++; return *this; } | |
185 | ||
186 | // negation operator | |
187 | wxLongLongNative operator-() const | |
188 | { return wxLongLongNative(-m_ll); } | |
189 | ||
190 | // subtraction | |
191 | wxLongLongNative operator-(const wxLongLongNative& ll) const | |
192 | { return wxLongLongNative(m_ll - ll.m_ll); } | |
193 | wxLongLongNative& operator-=(const wxLongLongNative& ll) | |
194 | { m_ll -= ll.m_ll; return *this; } | |
195 | ||
196 | wxLongLongNative operator-(const wxLongLong_t ll) const | |
197 | { return wxLongLongNative(m_ll - ll); } | |
198 | wxLongLongNative& operator-=(const wxLongLong_t ll) | |
199 | { m_ll -= ll; return *this; } | |
200 | ||
201 | // pre decrement | |
202 | wxLongLongNative& operator--() | |
203 | { m_ll--; return *this; } | |
204 | ||
205 | // post decrement | |
206 | wxLongLongNative& operator--(int) | |
207 | { m_ll--; return *this; } | |
208 | ||
209 | // shifts | |
210 | // left shift | |
211 | wxLongLongNative operator<<(int shift) const | |
212 | { return wxLongLongNative(m_ll << shift);; } | |
213 | wxLongLongNative& operator<<=(int shift) | |
214 | { m_ll <<= shift; return *this; } | |
215 | ||
216 | // right shift | |
217 | wxLongLongNative operator>>(int shift) const | |
218 | { return wxLongLongNative(m_ll >> shift);; } | |
219 | wxLongLongNative& operator>>=(int shift) | |
220 | { m_ll >>= shift; return *this; } | |
221 | ||
222 | // bitwise operators | |
223 | wxLongLongNative operator&(const wxLongLongNative& ll) const | |
224 | { return wxLongLongNative(m_ll & ll.m_ll); } | |
225 | wxLongLongNative& operator&=(const wxLongLongNative& ll) | |
226 | { m_ll &= ll.m_ll; return *this; } | |
227 | ||
228 | wxLongLongNative operator|(const wxLongLongNative& ll) const | |
229 | { return wxLongLongNative(m_ll | ll.m_ll); } | |
230 | wxLongLongNative& operator|=(const wxLongLongNative& ll) | |
231 | { m_ll |= ll.m_ll; return *this; } | |
232 | ||
233 | wxLongLongNative operator^(const wxLongLongNative& ll) const | |
234 | { return wxLongLongNative(m_ll ^ ll.m_ll); } | |
235 | wxLongLongNative& operator^=(const wxLongLongNative& ll) | |
236 | { m_ll ^= ll.m_ll; return *this; } | |
237 | ||
238 | // multiplication/division | |
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; } | |
247 | ||
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); } | |
252 | wxLongLongNative& operator/=(const wxLongLongNative& ll) | |
253 | { m_ll /= ll.m_ll; return *this; } | |
254 | wxLongLongNative& operator/=(long l) | |
255 | { m_ll /= l; return *this; } | |
256 | ||
257 | wxLongLongNative operator%(const wxLongLongNative& ll) const | |
258 | { return wxLongLongNative(m_ll % ll.m_ll); } | |
259 | wxLongLongNative operator%(long l) const | |
260 | { return wxLongLongNative(m_ll % l); } | |
261 | ||
262 | // comparison | |
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; } | |
267 | bool operator!=(const wxLongLongNative& ll) const | |
268 | { return m_ll != ll.m_ll; } | |
269 | bool operator!=(long l) const | |
270 | { return m_ll != l; } | |
271 | bool operator<(const wxLongLongNative& ll) const | |
272 | { return m_ll < ll.m_ll; } | |
273 | bool operator<(long l) const | |
274 | { return m_ll < l; } | |
275 | bool operator>(const wxLongLongNative& ll) const | |
276 | { return m_ll > ll.m_ll; } | |
277 | bool operator>(long l) const | |
278 | { return m_ll > l; } | |
279 | bool operator<=(const wxLongLongNative& ll) const | |
280 | { return m_ll <= ll.m_ll; } | |
281 | bool operator<=(long l) const | |
282 | { return m_ll <= l; } | |
283 | bool operator>=(const wxLongLongNative& ll) const | |
284 | { return m_ll >= ll.m_ll; } | |
285 | bool operator>=(long l) const | |
286 | { return m_ll >= l; } | |
287 | ||
288 | // miscellaneous | |
289 | // conversion to byte array: returns a pointer to static buffer! | |
290 | void *asArray() const; | |
291 | ||
292 | #if wxUSE_STD_IOSTREAM | |
293 | // input/output | |
294 | friend ostream& operator<<(ostream&, const wxLongLongNative&); | |
295 | #endif | |
296 | ||
297 | private: | |
298 | wxLongLong_t m_ll; | |
299 | }; | |
300 | ||
301 | #endif // wxUSE_LONGLONG_NATIVE | |
302 | ||
303 | #if wxUSE_LONGLONG_WX | |
304 | ||
305 | class WXDLLEXPORT wxLongLongWx | |
306 | { | |
307 | public: | |
308 | // ctors | |
309 | // default ctor initializes to 0 | |
310 | wxLongLongWx() { m_lo = m_hi = 0; } | |
311 | // from long | |
312 | wxLongLongWx(long l) | |
313 | { m_lo = l; m_hi = (l < 0 ? -1l : 0l); } | |
314 | // from 2 longs | |
315 | wxLongLongWx(long hi, unsigned long lo) | |
316 | { m_hi = hi; m_lo = lo; } | |
317 | ||
318 | // default copy ctor is ok in both cases | |
319 | ||
320 | // no dtor | |
321 | ||
322 | // assignment operators | |
323 | // from long | |
324 | wxLongLongWx& operator=(long l) | |
325 | { m_lo = l; m_hi = (l < 0 ? -1l : 0l); return *this; } | |
326 | // from double | |
327 | wxLongLongWx& Assign(double d); | |
328 | // can't have assignment operator from 2 longs | |
329 | ||
330 | // accessors | |
331 | // get high part | |
332 | long GetHi() const { return m_hi; } | |
333 | // get low part | |
334 | unsigned long GetLo() const { return m_lo; } | |
335 | ||
336 | // get absolute value | |
337 | wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); } | |
338 | wxLongLongWx& Abs() { if ( m_hi < 0 ) m_hi = -m_hi; return *this; } | |
339 | ||
340 | // convert to long with range checking in the debug mode (only!) | |
341 | long ToLong() const | |
342 | { | |
343 | wxASSERT_MSG( m_hi == 0l, | |
344 | _T("wxLongLong to long conversion loss of precision") ); | |
345 | ||
346 | return (long)m_lo; | |
347 | } | |
348 | ||
349 | // operations | |
350 | // addition | |
351 | wxLongLongWx operator+(const wxLongLongWx& ll) const; | |
352 | wxLongLongWx& operator+=(const wxLongLongWx& ll); | |
353 | wxLongLongWx operator+(long l) const; | |
354 | wxLongLongWx& operator+=(long l); | |
355 | ||
356 | // pre increment operator | |
357 | wxLongLongWx& operator++(); | |
358 | ||
359 | // post increment operator | |
360 | wxLongLongWx& operator++(int); | |
361 | ||
362 | // negation operator | |
363 | wxLongLongWx operator-() const; | |
364 | ||
365 | // subraction | |
366 | wxLongLongWx operator-(const wxLongLongWx& ll) const; | |
367 | wxLongLongWx& operator-=(const wxLongLongWx& ll); | |
368 | ||
369 | // pre decrement operator | |
370 | wxLongLongWx& operator--(); | |
371 | ||
372 | // post decrement operator | |
373 | wxLongLongWx& operator--(int); | |
374 | ||
375 | // shifts | |
376 | // left shift | |
377 | wxLongLongWx operator<<(int shift) const; | |
378 | wxLongLongWx& operator<<=(int shift); | |
379 | ||
380 | // right shift | |
381 | wxLongLongWx operator>>(int shift) const; | |
382 | wxLongLongWx& operator>>=(int shift); | |
383 | ||
384 | // bitwise operators | |
385 | wxLongLongWx operator&(const wxLongLongWx& ll) const; | |
386 | wxLongLongWx& operator&=(const wxLongLongWx& ll); | |
387 | wxLongLongWx operator|(const wxLongLongWx& ll) const; | |
388 | wxLongLongWx& operator|=(const wxLongLongWx& ll); | |
389 | wxLongLongWx operator^(const wxLongLongWx& ll) const; | |
390 | wxLongLongWx& operator^=(const wxLongLongWx& ll); | |
391 | wxLongLongWx operator~() const; | |
392 | ||
393 | // comparison | |
394 | bool operator==(const wxLongLongWx& ll) const | |
395 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
396 | bool operator!=(const wxLongLongWx& ll) const | |
397 | { return !(*this == ll); } | |
398 | bool operator<(const wxLongLongWx& ll) const; | |
399 | bool operator>(const wxLongLongWx& ll) const; | |
400 | bool operator<=(const wxLongLongWx& ll) const | |
401 | { return *this < ll || *this == ll; } | |
402 | bool operator>=(const wxLongLongWx& ll) const | |
403 | { return *this > ll || *this == ll; } | |
404 | ||
405 | // multiplication | |
406 | wxLongLongWx operator*(const wxLongLongWx& ll) const; | |
407 | wxLongLongWx& operator*=(const wxLongLongWx& ll); | |
408 | ||
409 | // division | |
410 | wxLongLongWx operator/(const wxLongLongWx& ll) const; | |
411 | wxLongLongWx& operator/=(const wxLongLongWx& ll); | |
412 | ||
413 | wxLongLongWx operator%(const wxLongLongWx& ll) const; | |
414 | ||
415 | void Divide(const wxLongLongWx& divisor, | |
416 | wxLongLongWx& quotient, | |
417 | wxLongLongWx& remainder) const; | |
418 | ||
419 | // input/output | |
420 | #if wxUSE_STD_IOSTREAM | |
421 | friend ostream& operator<<(ostream&, const wxLongLongWx&); | |
422 | #endif // wxUSE_STD_IOSTREAM | |
423 | ||
424 | void *asArray(void) const; | |
425 | ||
426 | private: | |
427 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
428 | ||
429 | long m_hi; // signed bit is in the high part | |
430 | unsigned long m_lo; | |
431 | }; | |
432 | ||
433 | #endif // wxUSE_LONGLONG_WX | |
434 | ||
435 | // ---------------------------------------------------------------------------- | |
436 | // binary operators | |
437 | // ---------------------------------------------------------------------------- | |
438 | ||
439 | inline bool WXDLLEXPORT operator<(long l, const wxLongLong& ll) { return ll > l; } | |
440 | inline bool WXDLLEXPORT operator>(long l, const wxLongLong& ll) { return ll > l; } | |
441 | inline bool WXDLLEXPORT operator<=(long l, const wxLongLong& ll) { return ll > l; } | |
442 | inline bool WXDLLEXPORT operator>=(long l, const wxLongLong& ll) { return ll > l; } | |
443 | inline bool WXDLLEXPORT operator==(long l, const wxLongLong& ll) { return ll > l; } | |
444 | inline bool WXDLLEXPORT operator!=(long l, const wxLongLong& ll) { return ll > l; } | |
445 | ||
446 | inline wxLongLong WXDLLEXPORT operator+(long l, const wxLongLong& ll) { return ll + l; } | |
447 | inline wxLongLong WXDLLEXPORT operator-(long l, const wxLongLong& ll) { return ll - l; } | |
448 | ||
449 | #endif // _WX_LONGLONG_H |