]>
Commit | Line | Data |
---|---|---|
8b81872f VZ |
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 | |
8b81872f VZ |
6 | // Modified by: |
7 | // Created: 10.02.99 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
10 | // Licence: wxWindows license | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | #ifndef _WX_LONGLONG_H | |
14 | #define _WX_LONGLONG_H | |
15 | ||
16 | #ifdef __GNUG__ | |
17 | #pragma interface "longlong.h" | |
18 | #endif | |
19 | ||
1ef54dcf | 20 | #include "wx/defs.h" |
3a994742 | 21 | #include "wx/string.h" |
1ef54dcf VZ |
22 | |
23 | #include <limits.h> // for LONG_MAX | |
24 | ||
2a310492 VZ |
25 | // define this to compile wxLongLongWx in "test" mode: the results of all |
26 | // calculations will be compared with the real results taken from | |
617ec456 VZ |
27 | // wxLongLongNative -- this is extremely useful to find the bugs in |
28 | // wxLongLongWx class! | |
29 | ||
f6bcfd97 | 30 | // #define wxLONGLONG_TEST_MODE |
2a310492 VZ |
31 | |
32 | #ifdef wxLONGLONG_TEST_MODE | |
33 | #define wxUSE_LONGLONG_WX 1 | |
34 | #define wxUSE_LONGLONG_NATIVE 1 | |
35 | #endif // wxLONGLONG_TEST_MODE | |
2ea24d9f | 36 | |
8b81872f VZ |
37 | // ---------------------------------------------------------------------------- |
38 | // decide upon which class we will use | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // to avoid compilation problems on 64bit machines with ambiguous method calls | |
2f02cb89 | 42 | // we will need to define this |
8b81872f VZ |
43 | #undef wxLongLongIsLong |
44 | ||
45 | // NB: we #define and not typedef wxLongLong_t because we want to be able to | |
2f02cb89 VZ |
46 | // use 'unsigned wxLongLong_t' as well and because we use "#ifdef |
47 | // wxLongLong_t" below | |
8db6ac55 VZ |
48 | |
49 | // first check for generic cases which are long on 64bit machine and "long | |
50 | // long", then check for specific compilers | |
8b81872f VZ |
51 | #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8) |
52 | #define wxLongLong_t long | |
53 | #define wxLongLongIsLong | |
cd0b1709 | 54 | #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ ) |
8b81872f | 55 | #define wxLongLong_t __int64 |
f6bcfd97 | 56 | #elif defined(__BORLANDC__) && defined(__WIN32__) && (__BORLANDC__ >= 0x520) |
ad0dc53b | 57 | #define wxLongLong_t __int64 |
5bf2abe3 RD |
58 | #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG >= 8 |
59 | #define wxLongLong_t long long | |
c67d6888 | 60 | #elif defined(__MINGW32__) || defined(__CYGWIN__) || defined(__WXMICROWIN__) |
b2a9ee30 | 61 | #define wxLongLong_t long long |
8b81872f VZ |
62 | #elif defined(__MWERKS__) |
63 | #if __option(longlong) | |
64 | #define wxLongLong_t long long | |
65 | #else | |
66 | #error "The 64 bit integer support in CodeWarrior has been disabled." | |
67 | #error "See the documentation on the 'longlong' pragma." | |
68 | #endif | |
398b582f DW |
69 | #elif defined(__VISAGECPP__) && __IBMCPP__ >= 400 |
70 | #define wxLongLong_t long long | |
cd0b1709 | 71 | #else // no native long long type |
9c2882d9 | 72 | // both warning and pragma warning are not portable, but at least an |
58ef67ab VZ |
73 | // unknown pragma should never be an error - except that, actually, some |
74 | // broken compilers don't like it, so we have to disable it in this case | |
75 | // <sigh> | |
76 | #if !(defined(__WATCOMC__) || defined(__VISAGECPP__)) | |
9c2882d9 | 77 | #pragma warning "Your compiler does not appear to support 64 bit "\ |
58ef67ab VZ |
78 | "integers, using emulation class instead.\n" \ |
79 | "Please report your compiler version to " \ | |
80 | "wx-dev@lists.wxwindows.org!" | |
cff4a45c | 81 | #endif |
8db6ac55 | 82 | |
8b81872f VZ |
83 | #define wxUSE_LONGLONG_WX 1 |
84 | #endif // compiler | |
85 | ||
86 | // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE | |
87 | // to disable automatic testing (useful for the test program which defines | |
88 | // both classes) but by default we only use one class | |
2ea24d9f | 89 | #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t) |
2a310492 | 90 | // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set: |
384223b3 | 91 | // this is useful in test programs and only there |
2a310492 VZ |
92 | #ifndef wxUSE_LONGLONG_NATIVE |
93 | #define wxUSE_LONGLONG_NATIVE 0 | |
94 | #endif | |
95 | ||
8b81872f | 96 | class WXDLLEXPORT wxLongLongWx; |
92f5ff59 JS |
97 | #if defined(__VISUALC__) && !defined(__WIN32__) |
98 | #define wxLongLong wxLongLongWx | |
8e38fd1f | 99 | #define wxULongLong wxULongLongWx |
92f5ff59 | 100 | #else |
8b81872f | 101 | typedef wxLongLongWx wxLongLong; |
8e38fd1f | 102 | typedef wxULongLongWx wxULongLong; |
92f5ff59 JS |
103 | #endif |
104 | ||
b76b015e VZ |
105 | #else |
106 | // if nothing is defined, use native implementation by default, of course | |
107 | #ifndef wxUSE_LONGLONG_NATIVE | |
108 | #define wxUSE_LONGLONG_NATIVE 1 | |
109 | #endif | |
8b81872f VZ |
110 | #endif |
111 | ||
112 | #ifndef wxUSE_LONGLONG_WX | |
113 | #define wxUSE_LONGLONG_WX 0 | |
114 | class WXDLLEXPORT wxLongLongNative; | |
8e38fd1f | 115 | class WXDLLEXPORT wxULongLongNative; |
8b81872f | 116 | typedef wxLongLongNative wxLongLong; |
8e38fd1f | 117 | typedef wxULongLongNative wxULongLong; |
8b81872f VZ |
118 | #endif |
119 | ||
120 | // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should | |
121 | // typedef wxLongLong as it wants, we don't do it | |
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // choose the appropriate class | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
127 | // we use iostream for wxLongLong output | |
128 | #include "wx/ioswrap.h" | |
129 | ||
130 | #if wxUSE_LONGLONG_NATIVE | |
131 | ||
132 | class WXDLLEXPORT wxLongLongNative | |
133 | { | |
134 | public: | |
135 | // ctors | |
136 | // default ctor initializes to 0 | |
137 | wxLongLongNative() { m_ll = 0; } | |
138 | // from long long | |
139 | wxLongLongNative(wxLongLong_t ll) { m_ll = ll; } | |
140 | // from 2 longs | |
141 | wxLongLongNative(long hi, unsigned long lo) | |
142 | { | |
143 | // assign first to avoid precision loss! | |
144 | m_ll = ((wxLongLong_t) hi) << 32; | |
145 | m_ll |= (wxLongLong_t) lo; | |
146 | } | |
147 | ||
2f02cb89 | 148 | // default copy ctor is ok |
8b81872f VZ |
149 | |
150 | // no dtor | |
151 | ||
152 | // assignment operators | |
153 | // from native 64 bit integer | |
154 | wxLongLongNative& operator=(wxLongLong_t ll) | |
155 | { m_ll = ll; return *this; } | |
156 | ||
cd0b1709 VZ |
157 | // from double: this one has an explicit name because otherwise we |
158 | // would have ambiguity with "ll = int" and also because we don't want | |
159 | // to have implicit conversions between doubles and wxLongLongs | |
160 | wxLongLongNative& Assign(double d) | |
161 | { m_ll = (wxLongLong_t)d; return *this; } | |
162 | ||
8b81872f VZ |
163 | // assignment operators from wxLongLongNative is ok |
164 | ||
165 | // accessors | |
166 | // get high part | |
167 | long GetHi() const | |
2f02cb89 | 168 | { return (long)(m_ll >> 32); } |
8b81872f VZ |
169 | // get low part |
170 | unsigned long GetLo() const | |
2f02cb89 | 171 | { return (unsigned long)m_ll; } |
8b81872f | 172 | |
b76b015e | 173 | // get absolute value |
2ea24d9f | 174 | wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); } |
b76b015e VZ |
175 | wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; } |
176 | ||
8b81872f VZ |
177 | // convert to native long long |
178 | wxLongLong_t GetValue() const { return m_ll; } | |
179 | ||
1ef54dcf VZ |
180 | // convert to long with range checking in the debug mode (only!) |
181 | long ToLong() const | |
182 | { | |
183 | wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX), | |
184 | _T("wxLongLong to long conversion loss of precision") ); | |
185 | ||
186 | return (long)m_ll; | |
187 | } | |
188 | ||
2f02cb89 VZ |
189 | // don't provide implicit conversion to wxLongLong_t or we will have an |
190 | // ambiguity for all arithmetic operations | |
b76b015e | 191 | //operator wxLongLong_t() const { return m_ll; } |
8b81872f VZ |
192 | |
193 | // operations | |
194 | // addition | |
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; } | |
199 | ||
200 | wxLongLongNative operator+(const wxLongLong_t ll) const | |
201 | { return wxLongLongNative(m_ll + ll); } | |
202 | wxLongLongNative& operator+=(const wxLongLong_t ll) | |
203 | { m_ll += ll; return *this; } | |
204 | ||
205 | // pre increment | |
206 | wxLongLongNative& operator++() | |
207 | { m_ll++; return *this; } | |
208 | ||
209 | // post increment | |
210 | wxLongLongNative& operator++(int) | |
211 | { m_ll++; return *this; } | |
212 | ||
213 | // negation operator | |
214 | wxLongLongNative operator-() const | |
215 | { return wxLongLongNative(-m_ll); } | |
3a994742 | 216 | wxLongLongNative& Negate() { m_ll = -m_ll; return *this; } |
8b81872f VZ |
217 | |
218 | // subtraction | |
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; } | |
223 | ||
224 | wxLongLongNative operator-(const wxLongLong_t ll) const | |
225 | { return wxLongLongNative(m_ll - ll); } | |
226 | wxLongLongNative& operator-=(const wxLongLong_t ll) | |
227 | { m_ll -= ll; return *this; } | |
228 | ||
229 | // pre decrement | |
230 | wxLongLongNative& operator--() | |
231 | { m_ll--; return *this; } | |
232 | ||
233 | // post decrement | |
234 | wxLongLongNative& operator--(int) | |
235 | { m_ll--; return *this; } | |
236 | ||
237 | // shifts | |
238 | // left shift | |
239 | wxLongLongNative operator<<(int shift) const | |
240 | { return wxLongLongNative(m_ll << shift);; } | |
241 | wxLongLongNative& operator<<=(int shift) | |
242 | { m_ll <<= shift; return *this; } | |
243 | ||
244 | // right shift | |
245 | wxLongLongNative operator>>(int shift) const | |
246 | { return wxLongLongNative(m_ll >> shift);; } | |
247 | wxLongLongNative& operator>>=(int shift) | |
248 | { m_ll >>= shift; return *this; } | |
249 | ||
250 | // bitwise operators | |
251 | wxLongLongNative operator&(const wxLongLongNative& ll) const | |
252 | { return wxLongLongNative(m_ll & ll.m_ll); } | |
253 | wxLongLongNative& operator&=(const wxLongLongNative& ll) | |
254 | { m_ll &= ll.m_ll; return *this; } | |
255 | ||
256 | wxLongLongNative operator|(const wxLongLongNative& ll) const | |
257 | { return wxLongLongNative(m_ll | ll.m_ll); } | |
258 | wxLongLongNative& operator|=(const wxLongLongNative& ll) | |
259 | { m_ll |= ll.m_ll; return *this; } | |
260 | ||
261 | wxLongLongNative operator^(const wxLongLongNative& ll) const | |
262 | { return wxLongLongNative(m_ll ^ ll.m_ll); } | |
263 | wxLongLongNative& operator^=(const wxLongLongNative& ll) | |
264 | { m_ll ^= ll.m_ll; return *this; } | |
265 | ||
b76b015e | 266 | // multiplication/division |
8b81872f VZ |
267 | wxLongLongNative operator*(const wxLongLongNative& ll) const |
268 | { return wxLongLongNative(m_ll * ll.m_ll); } | |
2f02cb89 VZ |
269 | wxLongLongNative operator*(long l) const |
270 | { return wxLongLongNative(m_ll * l); } | |
8b81872f VZ |
271 | wxLongLongNative& operator*=(const wxLongLongNative& ll) |
272 | { m_ll *= ll.m_ll; return *this; } | |
2f02cb89 VZ |
273 | wxLongLongNative& operator*=(long l) |
274 | { m_ll *= l; return *this; } | |
8b81872f VZ |
275 | |
276 | wxLongLongNative operator/(const wxLongLongNative& ll) const | |
277 | { return wxLongLongNative(m_ll / ll.m_ll); } | |
b76b015e VZ |
278 | wxLongLongNative operator/(long l) const |
279 | { return wxLongLongNative(m_ll / l); } | |
8b81872f VZ |
280 | wxLongLongNative& operator/=(const wxLongLongNative& ll) |
281 | { m_ll /= ll.m_ll; return *this; } | |
2f02cb89 VZ |
282 | wxLongLongNative& operator/=(long l) |
283 | { m_ll /= l; return *this; } | |
8b81872f VZ |
284 | |
285 | wxLongLongNative operator%(const wxLongLongNative& ll) const | |
286 | { return wxLongLongNative(m_ll % ll.m_ll); } | |
b76b015e VZ |
287 | wxLongLongNative operator%(long l) const |
288 | { return wxLongLongNative(m_ll % l); } | |
8b81872f VZ |
289 | |
290 | // comparison | |
291 | bool operator==(const wxLongLongNative& ll) const | |
292 | { return m_ll == ll.m_ll; } | |
b76b015e VZ |
293 | bool operator==(long l) const |
294 | { return m_ll == l; } | |
8b81872f VZ |
295 | bool operator!=(const wxLongLongNative& ll) const |
296 | { return m_ll != ll.m_ll; } | |
b76b015e VZ |
297 | bool operator!=(long l) const |
298 | { return m_ll != l; } | |
8b81872f VZ |
299 | bool operator<(const wxLongLongNative& ll) const |
300 | { return m_ll < ll.m_ll; } | |
b76b015e VZ |
301 | bool operator<(long l) const |
302 | { return m_ll < l; } | |
8b81872f VZ |
303 | bool operator>(const wxLongLongNative& ll) const |
304 | { return m_ll > ll.m_ll; } | |
b76b015e VZ |
305 | bool operator>(long l) const |
306 | { return m_ll > l; } | |
8b81872f VZ |
307 | bool operator<=(const wxLongLongNative& ll) const |
308 | { return m_ll <= ll.m_ll; } | |
b76b015e VZ |
309 | bool operator<=(long l) const |
310 | { return m_ll <= l; } | |
8b81872f VZ |
311 | bool operator>=(const wxLongLongNative& ll) const |
312 | { return m_ll >= ll.m_ll; } | |
b76b015e VZ |
313 | bool operator>=(long l) const |
314 | { return m_ll >= l; } | |
8b81872f VZ |
315 | |
316 | // miscellaneous | |
3a994742 VZ |
317 | |
318 | // return the string representation of this number | |
319 | wxString ToString() const; | |
320 | ||
8b81872f VZ |
321 | // conversion to byte array: returns a pointer to static buffer! |
322 | void *asArray() const; | |
323 | ||
fcc3d7cb | 324 | #if wxUSE_STD_IOSTREAM |
8b81872f | 325 | // input/output |
dd107c50 | 326 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&); |
fcc3d7cb | 327 | #endif |
8b81872f VZ |
328 | |
329 | private: | |
330 | wxLongLong_t m_ll; | |
331 | }; | |
332 | ||
8e38fd1f RL |
333 | |
334 | class WXDLLEXPORT wxULongLongNative | |
335 | { | |
336 | public: | |
337 | // ctors | |
338 | // default ctor initializes to 0 | |
339 | wxULongLongNative() { m_ll = 0; } | |
340 | // from long long | |
341 | wxULongLongNative(unsigned wxLongLong_t ll) { m_ll = ll; } | |
342 | // from 2 longs | |
343 | wxULongLongNative(unsigned long hi, unsigned long lo) | |
344 | { | |
345 | // assign first to avoid precision loss! | |
346 | m_ll = ((unsigned wxLongLong_t) hi) << 32; | |
347 | m_ll |= (unsigned wxLongLong_t) lo; | |
348 | } | |
349 | ||
350 | // default copy ctor is ok | |
351 | ||
352 | // no dtor | |
353 | ||
354 | // assignment operators | |
355 | // from native 64 bit integer | |
356 | wxULongLongNative& operator=(unsigned wxLongLong_t ll) | |
357 | { m_ll = ll; return *this; } | |
358 | ||
359 | // assignment operators from wxULongLongNative is ok | |
360 | ||
361 | // accessors | |
362 | // get high part | |
363 | unsigned long GetHi() const | |
364 | { return (unsigned long)(m_ll >> 32); } | |
365 | // get low part | |
366 | unsigned long GetLo() const | |
367 | { return (unsigned long)m_ll; } | |
368 | ||
369 | // convert to native ulong long | |
370 | unsigned wxLongLong_t GetValue() const { return m_ll; } | |
371 | ||
372 | // convert to ulong with range checking in the debug mode (only!) | |
373 | unsigned long ToULong() const | |
374 | { | |
0ba9f867 | 375 | wxASSERT_MSG( m_ll <= LONG_MAX, |
8e38fd1f RL |
376 | _T("wxULongLong to long conversion loss of precision") ); |
377 | ||
378 | return (unsigned long)m_ll; | |
379 | } | |
380 | ||
8e38fd1f RL |
381 | // operations |
382 | // addition | |
383 | wxULongLongNative operator+(const wxULongLongNative& ll) const | |
384 | { return wxULongLongNative(m_ll + ll.m_ll); } | |
385 | wxULongLongNative& operator+=(const wxULongLongNative& ll) | |
386 | { m_ll += ll.m_ll; return *this; } | |
387 | ||
388 | wxULongLongNative operator+(const unsigned wxLongLong_t ll) const | |
389 | { return wxULongLongNative(m_ll + ll); } | |
390 | wxULongLongNative& operator+=(const unsigned wxLongLong_t ll) | |
391 | { m_ll += ll; return *this; } | |
392 | ||
393 | // pre increment | |
394 | wxULongLongNative& operator++() | |
395 | { m_ll++; return *this; } | |
396 | ||
397 | // post increment | |
398 | wxULongLongNative& operator++(int) | |
399 | { m_ll++; return *this; } | |
400 | ||
401 | // subtraction | |
402 | wxULongLongNative operator-(const wxULongLongNative& ll) const | |
403 | { return wxULongLongNative(m_ll - ll.m_ll); } | |
404 | wxULongLongNative& operator-=(const wxULongLongNative& ll) | |
405 | { m_ll -= ll.m_ll; return *this; } | |
406 | ||
407 | wxULongLongNative operator-(const unsigned wxLongLong_t ll) const | |
408 | { return wxULongLongNative(m_ll - ll); } | |
409 | wxULongLongNative& operator-=(const unsigned wxLongLong_t ll) | |
410 | { m_ll -= ll; return *this; } | |
411 | ||
412 | // pre decrement | |
413 | wxULongLongNative& operator--() | |
414 | { m_ll--; return *this; } | |
415 | ||
416 | // post decrement | |
417 | wxULongLongNative& operator--(int) | |
418 | { m_ll--; return *this; } | |
419 | ||
420 | // shifts | |
421 | // left shift | |
422 | wxULongLongNative operator<<(int shift) const | |
423 | { return wxULongLongNative(m_ll << shift);; } | |
424 | wxULongLongNative& operator<<=(int shift) | |
425 | { m_ll <<= shift; return *this; } | |
426 | ||
427 | // right shift | |
428 | wxULongLongNative operator>>(int shift) const | |
429 | { return wxULongLongNative(m_ll >> shift);; } | |
430 | wxULongLongNative& operator>>=(int shift) | |
431 | { m_ll >>= shift; return *this; } | |
432 | ||
433 | // bitwise operators | |
434 | wxULongLongNative operator&(const wxULongLongNative& ll) const | |
435 | { return wxULongLongNative(m_ll & ll.m_ll); } | |
436 | wxULongLongNative& operator&=(const wxULongLongNative& ll) | |
437 | { m_ll &= ll.m_ll; return *this; } | |
438 | ||
439 | wxULongLongNative operator|(const wxULongLongNative& ll) const | |
440 | { return wxULongLongNative(m_ll | ll.m_ll); } | |
441 | wxULongLongNative& operator|=(const wxULongLongNative& ll) | |
442 | { m_ll |= ll.m_ll; return *this; } | |
443 | ||
444 | wxULongLongNative operator^(const wxULongLongNative& ll) const | |
445 | { return wxULongLongNative(m_ll ^ ll.m_ll); } | |
446 | wxULongLongNative& operator^=(const wxULongLongNative& ll) | |
447 | { m_ll ^= ll.m_ll; return *this; } | |
448 | ||
449 | // multiplication/division | |
450 | wxULongLongNative operator*(const wxULongLongNative& ll) const | |
451 | { return wxULongLongNative(m_ll * ll.m_ll); } | |
452 | wxULongLongNative operator*(unsigned long l) const | |
453 | { return wxULongLongNative(m_ll * l); } | |
454 | wxULongLongNative& operator*=(const wxULongLongNative& ll) | |
455 | { m_ll *= ll.m_ll; return *this; } | |
456 | wxULongLongNative& operator*=(unsigned long l) | |
457 | { m_ll *= l; return *this; } | |
458 | ||
459 | wxULongLongNative operator/(const wxULongLongNative& ll) const | |
460 | { return wxULongLongNative(m_ll / ll.m_ll); } | |
461 | wxULongLongNative operator/(unsigned long l) const | |
462 | { return wxULongLongNative(m_ll / l); } | |
463 | wxULongLongNative& operator/=(const wxULongLongNative& ll) | |
464 | { m_ll /= ll.m_ll; return *this; } | |
465 | wxULongLongNative& operator/=(unsigned long l) | |
466 | { m_ll /= l; return *this; } | |
467 | ||
468 | wxULongLongNative operator%(const wxULongLongNative& ll) const | |
469 | { return wxULongLongNative(m_ll % ll.m_ll); } | |
470 | wxULongLongNative operator%(unsigned long l) const | |
471 | { return wxULongLongNative(m_ll % l); } | |
472 | ||
473 | // comparison | |
474 | bool operator==(const wxULongLongNative& ll) const | |
475 | { return m_ll == ll.m_ll; } | |
476 | bool operator==(unsigned long l) const | |
477 | { return m_ll == l; } | |
478 | bool operator!=(const wxULongLongNative& ll) const | |
479 | { return m_ll != ll.m_ll; } | |
480 | bool operator!=(unsigned long l) const | |
481 | { return m_ll != l; } | |
482 | bool operator<(const wxULongLongNative& ll) const | |
483 | { return m_ll < ll.m_ll; } | |
484 | bool operator<(unsigned long l) const | |
485 | { return m_ll < l; } | |
486 | bool operator>(const wxULongLongNative& ll) const | |
487 | { return m_ll > ll.m_ll; } | |
488 | bool operator>(unsigned long l) const | |
489 | { return m_ll > l; } | |
490 | bool operator<=(const wxULongLongNative& ll) const | |
491 | { return m_ll <= ll.m_ll; } | |
492 | bool operator<=(unsigned long l) const | |
493 | { return m_ll <= l; } | |
494 | bool operator>=(const wxULongLongNative& ll) const | |
495 | { return m_ll >= ll.m_ll; } | |
496 | bool operator>=(unsigned long l) const | |
497 | { return m_ll >= l; } | |
498 | ||
499 | // miscellaneous | |
500 | ||
501 | // return the string representation of this number | |
502 | wxString ToString() const; | |
503 | ||
504 | // conversion to byte array: returns a pointer to static buffer! | |
505 | void *asArray() const; | |
506 | ||
507 | #if wxUSE_STD_IOSTREAM | |
508 | // input/output | |
509 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&); | |
510 | #endif | |
511 | ||
512 | private: | |
513 | unsigned wxLongLong_t m_ll; | |
514 | }; | |
515 | ||
8b81872f VZ |
516 | #endif // wxUSE_LONGLONG_NATIVE |
517 | ||
518 | #if wxUSE_LONGLONG_WX | |
519 | ||
520 | class WXDLLEXPORT wxLongLongWx | |
521 | { | |
522 | public: | |
523 | // ctors | |
524 | // default ctor initializes to 0 | |
2a310492 VZ |
525 | wxLongLongWx() |
526 | { | |
527 | m_lo = m_hi = 0; | |
528 | ||
529 | #ifdef wxLONGLONG_TEST_MODE | |
530 | m_ll = 0; | |
531 | ||
532 | Check(); | |
533 | #endif // wxLONGLONG_TEST_MODE | |
534 | } | |
8b81872f | 535 | // from long |
2a310492 | 536 | wxLongLongWx(long l) { *this = l; } |
8b81872f VZ |
537 | // from 2 longs |
538 | wxLongLongWx(long hi, unsigned long lo) | |
2a310492 VZ |
539 | { |
540 | m_hi = hi; | |
541 | m_lo = lo; | |
542 | ||
543 | #ifdef wxLONGLONG_TEST_MODE | |
544 | m_ll = hi; | |
545 | m_ll <<= 32; | |
546 | m_ll |= lo; | |
547 | ||
548 | Check(); | |
549 | #endif // wxLONGLONG_TEST_MODE | |
550 | } | |
8b81872f VZ |
551 | |
552 | // default copy ctor is ok in both cases | |
553 | ||
554 | // no dtor | |
555 | ||
556 | // assignment operators | |
557 | // from long | |
558 | wxLongLongWx& operator=(long l) | |
2a310492 VZ |
559 | { |
560 | m_lo = l; | |
561 | m_hi = (l < 0 ? -1l : 0l); | |
562 | ||
563 | #ifdef wxLONGLONG_TEST_MODE | |
564 | m_ll = l; | |
565 | ||
566 | Check(); | |
567 | #endif // wxLONGLONG_TEST_MODE | |
568 | ||
569 | return *this; | |
570 | } | |
cd0b1709 VZ |
571 | // from double |
572 | wxLongLongWx& Assign(double d); | |
8b81872f VZ |
573 | // can't have assignment operator from 2 longs |
574 | ||
575 | // accessors | |
576 | // get high part | |
577 | long GetHi() const { return m_hi; } | |
578 | // get low part | |
579 | unsigned long GetLo() const { return m_lo; } | |
580 | ||
cd0b1709 | 581 | // get absolute value |
2ea24d9f | 582 | wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); } |
2a310492 VZ |
583 | wxLongLongWx& Abs() |
584 | { | |
585 | if ( m_hi < 0 ) | |
586 | m_hi = -m_hi; | |
587 | ||
588 | #ifdef wxLONGLONG_TEST_MODE | |
589 | if ( m_ll < 0 ) | |
590 | m_ll = -m_ll; | |
591 | ||
592 | Check(); | |
593 | #endif // wxLONGLONG_TEST_MODE | |
594 | ||
595 | return *this; | |
596 | } | |
cd0b1709 VZ |
597 | |
598 | // convert to long with range checking in the debug mode (only!) | |
599 | long ToLong() const | |
600 | { | |
2a310492 | 601 | wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l), |
cd0b1709 VZ |
602 | _T("wxLongLong to long conversion loss of precision") ); |
603 | ||
604 | return (long)m_lo; | |
605 | } | |
606 | ||
8b81872f VZ |
607 | // operations |
608 | // addition | |
609 | wxLongLongWx operator+(const wxLongLongWx& ll) const; | |
610 | wxLongLongWx& operator+=(const wxLongLongWx& ll); | |
611 | wxLongLongWx operator+(long l) const; | |
612 | wxLongLongWx& operator+=(long l); | |
613 | ||
614 | // pre increment operator | |
615 | wxLongLongWx& operator++(); | |
616 | ||
617 | // post increment operator | |
2a310492 | 618 | wxLongLongWx& operator++(int) { return ++(*this); } |
8b81872f VZ |
619 | |
620 | // negation operator | |
621 | wxLongLongWx operator-() const; | |
2a310492 | 622 | wxLongLongWx& Negate(); |
8b81872f VZ |
623 | |
624 | // subraction | |
625 | wxLongLongWx operator-(const wxLongLongWx& ll) const; | |
626 | wxLongLongWx& operator-=(const wxLongLongWx& ll); | |
627 | ||
628 | // pre decrement operator | |
629 | wxLongLongWx& operator--(); | |
630 | ||
631 | // post decrement operator | |
2a310492 | 632 | wxLongLongWx& operator--(int) { return --(*this); } |
8b81872f VZ |
633 | |
634 | // shifts | |
635 | // left shift | |
636 | wxLongLongWx operator<<(int shift) const; | |
637 | wxLongLongWx& operator<<=(int shift); | |
638 | ||
639 | // right shift | |
640 | wxLongLongWx operator>>(int shift) const; | |
641 | wxLongLongWx& operator>>=(int shift); | |
642 | ||
643 | // bitwise operators | |
644 | wxLongLongWx operator&(const wxLongLongWx& ll) const; | |
645 | wxLongLongWx& operator&=(const wxLongLongWx& ll); | |
646 | wxLongLongWx operator|(const wxLongLongWx& ll) const; | |
647 | wxLongLongWx& operator|=(const wxLongLongWx& ll); | |
648 | wxLongLongWx operator^(const wxLongLongWx& ll) const; | |
649 | wxLongLongWx& operator^=(const wxLongLongWx& ll); | |
650 | wxLongLongWx operator~() const; | |
651 | ||
652 | // comparison | |
2ea24d9f VZ |
653 | bool operator==(const wxLongLongWx& ll) const |
654 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
655 | bool operator!=(const wxLongLongWx& ll) const | |
656 | { return !(*this == ll); } | |
8b81872f VZ |
657 | bool operator<(const wxLongLongWx& ll) const; |
658 | bool operator>(const wxLongLongWx& ll) const; | |
2ea24d9f VZ |
659 | bool operator<=(const wxLongLongWx& ll) const |
660 | { return *this < ll || *this == ll; } | |
661 | bool operator>=(const wxLongLongWx& ll) const | |
662 | { return *this > ll || *this == ll; } | |
8b81872f | 663 | |
f6bcfd97 BP |
664 | bool operator<(long l) const { return *this < wxLongLongWx(l); } |
665 | bool operator>(long l) const { return *this > wxLongLongWx(l); } | |
666 | bool operator==(long l) const | |
667 | { | |
668 | return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l) | |
669 | : (m_hi == -1 && m_lo == (unsigned long)l); | |
670 | } | |
671 | ||
672 | bool operator<=(long l) const { return *this < l || *this == l; } | |
673 | bool operator>=(long l) const { return *this > l || *this == l; } | |
674 | ||
8b81872f VZ |
675 | // multiplication |
676 | wxLongLongWx operator*(const wxLongLongWx& ll) const; | |
677 | wxLongLongWx& operator*=(const wxLongLongWx& ll); | |
8b81872f VZ |
678 | |
679 | // division | |
cd0b1709 VZ |
680 | wxLongLongWx operator/(const wxLongLongWx& ll) const; |
681 | wxLongLongWx& operator/=(const wxLongLongWx& ll); | |
682 | ||
683 | wxLongLongWx operator%(const wxLongLongWx& ll) const; | |
684 | ||
8b81872f VZ |
685 | void Divide(const wxLongLongWx& divisor, |
686 | wxLongLongWx& quotient, | |
687 | wxLongLongWx& remainder) const; | |
688 | ||
689 | // input/output | |
3a994742 VZ |
690 | |
691 | // return the string representation of this number | |
692 | wxString ToString() const; | |
693 | ||
694 | void *asArray() const; | |
695 | ||
cd0b1709 | 696 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 697 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&); |
fcc3d7cb | 698 | #endif // wxUSE_STD_IOSTREAM |
8b81872f VZ |
699 | |
700 | private: | |
701 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
702 | ||
703 | long m_hi; // signed bit is in the high part | |
704 | unsigned long m_lo; | |
2a310492 VZ |
705 | |
706 | #ifdef wxLONGLONG_TEST_MODE | |
707 | void Check() | |
708 | { | |
709 | wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo ); | |
710 | } | |
711 | ||
712 | wxLongLong_t m_ll; | |
713 | #endif // wxLONGLONG_TEST_MODE | |
8b81872f VZ |
714 | }; |
715 | ||
8e38fd1f RL |
716 | |
717 | class WXDLLEXPORT wxULongLongWx | |
718 | { | |
719 | public: | |
720 | // ctors | |
721 | // default ctor initializes to 0 | |
722 | wxULongLongWx() | |
723 | { | |
724 | m_lo = m_hi = 0; | |
725 | ||
726 | #ifdef wxLONGLONG_TEST_MODE | |
727 | m_ll = 0; | |
728 | ||
729 | Check(); | |
730 | #endif // wxLONGLONG_TEST_MODE | |
731 | } | |
732 | // from ulong | |
733 | wxULongLongWx(unsigned long l) { *this = l; } | |
734 | // from 2 ulongs | |
735 | wxULongLongWx(unsigned long hi, unsigned long lo) | |
736 | { | |
737 | m_hi = hi; | |
738 | m_lo = lo; | |
739 | ||
740 | #ifdef wxLONGLONG_TEST_MODE | |
741 | m_ll = hi; | |
742 | m_ll <<= 32; | |
743 | m_ll |= lo; | |
744 | ||
745 | Check(); | |
746 | #endif // wxLONGLONG_TEST_MODE | |
747 | } | |
748 | ||
749 | // default copy ctor is ok in both cases | |
750 | ||
751 | // no dtor | |
752 | ||
753 | // assignment operators | |
754 | // from long | |
755 | wxULongLongWx& operator=(unsigned long l) | |
756 | { | |
757 | m_lo = l; | |
758 | m_hi = 0; | |
759 | ||
760 | #ifdef wxLONGLONG_TEST_MODE | |
761 | m_ll = l; | |
762 | ||
763 | Check(); | |
764 | #endif // wxLONGLONG_TEST_MODE | |
765 | ||
766 | return *this; | |
767 | } | |
768 | ||
769 | // can't have assignment operator from 2 longs | |
770 | ||
771 | // accessors | |
772 | // get high part | |
773 | unsigned long GetHi() const { return m_hi; } | |
774 | // get low part | |
775 | unsigned long GetLo() const { return m_lo; } | |
776 | ||
777 | // convert to long with range checking in the debug mode (only!) | |
778 | unsigned long ToULong() const | |
779 | { | |
d7997e1a | 780 | wxASSERT_MSG( m_hi == 0ul, |
8e38fd1f RL |
781 | _T("wxULongLong to long conversion loss of precision") ); |
782 | ||
783 | return (unsigned long)m_lo; | |
784 | } | |
785 | ||
786 | // operations | |
787 | // addition | |
788 | wxULongLongWx operator+(const wxULongLongWx& ll) const; | |
789 | wxULongLongWx& operator+=(const wxULongLongWx& ll); | |
790 | wxULongLongWx operator+(unsigned long l) const; | |
791 | wxULongLongWx& operator+=(unsigned long l); | |
792 | ||
793 | // pre increment operator | |
794 | wxULongLongWx& operator++(); | |
795 | ||
796 | // post increment operator | |
797 | wxULongLongWx& operator++(int) { return ++(*this); } | |
798 | ||
d7997e1a | 799 | // subraction (FIXME: should return wxLongLong) |
8e38fd1f RL |
800 | wxULongLongWx operator-(const wxULongLongWx& ll) const; |
801 | wxULongLongWx& operator-=(const wxULongLongWx& ll); | |
802 | ||
803 | // pre decrement operator | |
804 | wxULongLongWx& operator--(); | |
805 | ||
806 | // post decrement operator | |
807 | wxULongLongWx& operator--(int) { return --(*this); } | |
808 | ||
809 | // shifts | |
810 | // left shift | |
811 | wxULongLongWx operator<<(int shift) const; | |
812 | wxULongLongWx& operator<<=(int shift); | |
813 | ||
814 | // right shift | |
815 | wxULongLongWx operator>>(int shift) const; | |
816 | wxULongLongWx& operator>>=(int shift); | |
817 | ||
818 | // bitwise operators | |
819 | wxULongLongWx operator&(const wxULongLongWx& ll) const; | |
820 | wxULongLongWx& operator&=(const wxULongLongWx& ll); | |
821 | wxULongLongWx operator|(const wxULongLongWx& ll) const; | |
822 | wxULongLongWx& operator|=(const wxULongLongWx& ll); | |
823 | wxULongLongWx operator^(const wxULongLongWx& ll) const; | |
824 | wxULongLongWx& operator^=(const wxULongLongWx& ll); | |
825 | wxULongLongWx operator~() const; | |
826 | ||
827 | // comparison | |
828 | bool operator==(const wxULongLongWx& ll) const | |
829 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
830 | bool operator!=(const wxULongLongWx& ll) const | |
831 | { return !(*this == ll); } | |
832 | bool operator<(const wxULongLongWx& ll) const; | |
833 | bool operator>(const wxULongLongWx& ll) const; | |
834 | bool operator<=(const wxULongLongWx& ll) const | |
835 | { return *this < ll || *this == ll; } | |
836 | bool operator>=(const wxULongLongWx& ll) const | |
837 | { return *this > ll || *this == ll; } | |
838 | ||
839 | bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); } | |
840 | bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); } | |
841 | bool operator==(unsigned long l) const | |
842 | { | |
843 | return (m_hi == 0 && m_lo == (unsigned long)l); | |
844 | } | |
845 | ||
846 | bool operator<=(unsigned long l) const { return *this < l || *this == l; } | |
847 | bool operator>=(unsigned long l) const { return *this > l || *this == l; } | |
848 | ||
849 | // multiplication | |
850 | wxULongLongWx operator*(const wxULongLongWx& ll) const; | |
851 | wxULongLongWx& operator*=(const wxULongLongWx& ll); | |
852 | ||
853 | // division | |
854 | wxULongLongWx operator/(const wxULongLongWx& ll) const; | |
855 | wxULongLongWx& operator/=(const wxULongLongWx& ll); | |
856 | ||
857 | wxULongLongWx operator%(const wxULongLongWx& ll) const; | |
858 | ||
859 | void Divide(const wxULongLongWx& divisor, | |
860 | wxULongLongWx& quotient, | |
861 | wxULongLongWx& remainder) const; | |
862 | ||
863 | // input/output | |
864 | ||
865 | // return the string representation of this number | |
866 | wxString ToString() const; | |
867 | ||
868 | void *asArray() const; | |
869 | ||
870 | #if wxUSE_STD_IOSTREAM | |
871 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&); | |
872 | #endif // wxUSE_STD_IOSTREAM | |
873 | ||
874 | private: | |
875 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
876 | ||
877 | unsigned long m_hi | |
878 | unsigned long m_lo; | |
879 | ||
880 | #ifdef wxLONGLONG_TEST_MODE | |
881 | void Check() | |
882 | { | |
883 | wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo ); | |
884 | } | |
885 | ||
886 | unsigned wxLongLong_t m_ll; | |
887 | #endif // wxLONGLONG_TEST_MODE | |
888 | }; | |
889 | ||
8b81872f VZ |
890 | #endif // wxUSE_LONGLONG_WX |
891 | ||
2ea24d9f VZ |
892 | // ---------------------------------------------------------------------------- |
893 | // binary operators | |
894 | // ---------------------------------------------------------------------------- | |
895 | ||
6d56eb5c | 896 | inline bool operator<(long l, const wxLongLong& ll) { return ll > l; } |
d7997e1a VZ |
897 | inline bool operator>(long l, const wxLongLong& ll) { return ll < l; } |
898 | inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; } | |
899 | inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; } | |
900 | inline bool operator==(long l, const wxLongLong& ll) { return ll == l; } | |
901 | inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; } | |
b01fb5c3 | 902 | |
d7997e1a | 903 | inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; } |
b01fb5c3 VZ |
904 | inline wxLongLong operator-(long l, const wxLongLong& ll) |
905 | { | |
d7997e1a | 906 | return wxLongLong(l) - ll; |
b01fb5c3 | 907 | } |
2ea24d9f | 908 | |
d7997e1a VZ |
909 | inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; } |
910 | inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; } | |
911 | inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; } | |
912 | inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; } | |
913 | inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; } | |
914 | inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; } | |
915 | ||
916 | inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; } | |
8e38fd1f | 917 | |
d7997e1a VZ |
918 | // FIXME: this should return wxLongLong |
919 | inline wxULongLong operator-(unsigned long l, const wxULongLong& ull) | |
920 | { | |
921 | return wxULongLong(l) - ull; | |
922 | } | |
8e38fd1f | 923 | |
8b81872f | 924 | #endif // _WX_LONGLONG_H |