]>
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 | ||
8e38fd1f RL |
180 | // implicit conversion for times when we want a native type |
181 | // or wxLongLong, NOT wxLongLongNative and without having | |
182 | // to ifdef user code for each use. | |
183 | operator wxLongLong_t() const { return m_ll; } | |
184 | ||
1ef54dcf VZ |
185 | // convert to long with range checking in the debug mode (only!) |
186 | long ToLong() const | |
187 | { | |
188 | wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX), | |
189 | _T("wxLongLong to long conversion loss of precision") ); | |
190 | ||
191 | return (long)m_ll; | |
192 | } | |
193 | ||
2f02cb89 VZ |
194 | // don't provide implicit conversion to wxLongLong_t or we will have an |
195 | // ambiguity for all arithmetic operations | |
b76b015e | 196 | //operator wxLongLong_t() const { return m_ll; } |
8b81872f VZ |
197 | |
198 | // operations | |
199 | // addition | |
200 | wxLongLongNative operator+(const wxLongLongNative& ll) const | |
201 | { return wxLongLongNative(m_ll + ll.m_ll); } | |
202 | wxLongLongNative& operator+=(const wxLongLongNative& ll) | |
203 | { m_ll += ll.m_ll; return *this; } | |
204 | ||
205 | wxLongLongNative operator+(const wxLongLong_t ll) const | |
206 | { return wxLongLongNative(m_ll + ll); } | |
207 | wxLongLongNative& operator+=(const wxLongLong_t ll) | |
208 | { m_ll += ll; return *this; } | |
209 | ||
210 | // pre increment | |
211 | wxLongLongNative& operator++() | |
212 | { m_ll++; return *this; } | |
213 | ||
214 | // post increment | |
215 | wxLongLongNative& operator++(int) | |
216 | { m_ll++; return *this; } | |
217 | ||
218 | // negation operator | |
219 | wxLongLongNative operator-() const | |
220 | { return wxLongLongNative(-m_ll); } | |
3a994742 | 221 | wxLongLongNative& Negate() { m_ll = -m_ll; return *this; } |
8b81872f VZ |
222 | |
223 | // subtraction | |
224 | wxLongLongNative operator-(const wxLongLongNative& ll) const | |
225 | { return wxLongLongNative(m_ll - ll.m_ll); } | |
226 | wxLongLongNative& operator-=(const wxLongLongNative& ll) | |
227 | { m_ll -= ll.m_ll; return *this; } | |
228 | ||
229 | wxLongLongNative operator-(const wxLongLong_t ll) const | |
230 | { return wxLongLongNative(m_ll - ll); } | |
231 | wxLongLongNative& operator-=(const wxLongLong_t ll) | |
232 | { m_ll -= ll; return *this; } | |
233 | ||
234 | // pre decrement | |
235 | wxLongLongNative& operator--() | |
236 | { m_ll--; return *this; } | |
237 | ||
238 | // post decrement | |
239 | wxLongLongNative& operator--(int) | |
240 | { m_ll--; return *this; } | |
241 | ||
242 | // shifts | |
243 | // left shift | |
244 | wxLongLongNative operator<<(int shift) const | |
245 | { return wxLongLongNative(m_ll << shift);; } | |
246 | wxLongLongNative& operator<<=(int shift) | |
247 | { m_ll <<= shift; return *this; } | |
248 | ||
249 | // right shift | |
250 | wxLongLongNative operator>>(int shift) const | |
251 | { return wxLongLongNative(m_ll >> shift);; } | |
252 | wxLongLongNative& operator>>=(int shift) | |
253 | { m_ll >>= shift; return *this; } | |
254 | ||
255 | // bitwise operators | |
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 | ||
266 | wxLongLongNative operator^(const wxLongLongNative& ll) const | |
267 | { return wxLongLongNative(m_ll ^ ll.m_ll); } | |
268 | wxLongLongNative& operator^=(const wxLongLongNative& ll) | |
269 | { m_ll ^= ll.m_ll; return *this; } | |
270 | ||
b76b015e | 271 | // multiplication/division |
8b81872f VZ |
272 | wxLongLongNative operator*(const wxLongLongNative& ll) const |
273 | { return wxLongLongNative(m_ll * ll.m_ll); } | |
2f02cb89 VZ |
274 | wxLongLongNative operator*(long l) const |
275 | { return wxLongLongNative(m_ll * l); } | |
8b81872f VZ |
276 | wxLongLongNative& operator*=(const wxLongLongNative& ll) |
277 | { m_ll *= ll.m_ll; return *this; } | |
2f02cb89 VZ |
278 | wxLongLongNative& operator*=(long l) |
279 | { m_ll *= l; return *this; } | |
8b81872f VZ |
280 | |
281 | wxLongLongNative operator/(const wxLongLongNative& ll) const | |
282 | { return wxLongLongNative(m_ll / ll.m_ll); } | |
b76b015e VZ |
283 | wxLongLongNative operator/(long l) const |
284 | { return wxLongLongNative(m_ll / l); } | |
8b81872f VZ |
285 | wxLongLongNative& operator/=(const wxLongLongNative& ll) |
286 | { m_ll /= ll.m_ll; return *this; } | |
2f02cb89 VZ |
287 | wxLongLongNative& operator/=(long l) |
288 | { m_ll /= l; return *this; } | |
8b81872f VZ |
289 | |
290 | wxLongLongNative operator%(const wxLongLongNative& ll) const | |
291 | { return wxLongLongNative(m_ll % ll.m_ll); } | |
b76b015e VZ |
292 | wxLongLongNative operator%(long l) const |
293 | { return wxLongLongNative(m_ll % l); } | |
8b81872f VZ |
294 | |
295 | // comparison | |
296 | bool operator==(const wxLongLongNative& ll) const | |
297 | { return m_ll == ll.m_ll; } | |
b76b015e VZ |
298 | bool operator==(long l) const |
299 | { return m_ll == l; } | |
8b81872f VZ |
300 | bool operator!=(const wxLongLongNative& ll) const |
301 | { return m_ll != ll.m_ll; } | |
b76b015e VZ |
302 | bool operator!=(long l) const |
303 | { return m_ll != l; } | |
8b81872f VZ |
304 | bool operator<(const wxLongLongNative& ll) const |
305 | { return m_ll < ll.m_ll; } | |
b76b015e VZ |
306 | bool operator<(long l) const |
307 | { return m_ll < l; } | |
8b81872f VZ |
308 | bool operator>(const wxLongLongNative& ll) const |
309 | { return m_ll > ll.m_ll; } | |
b76b015e VZ |
310 | bool operator>(long l) const |
311 | { return m_ll > l; } | |
8b81872f VZ |
312 | bool operator<=(const wxLongLongNative& ll) const |
313 | { return m_ll <= ll.m_ll; } | |
b76b015e VZ |
314 | bool operator<=(long l) const |
315 | { return m_ll <= l; } | |
8b81872f VZ |
316 | bool operator>=(const wxLongLongNative& ll) const |
317 | { return m_ll >= ll.m_ll; } | |
b76b015e VZ |
318 | bool operator>=(long l) const |
319 | { return m_ll >= l; } | |
8b81872f VZ |
320 | |
321 | // miscellaneous | |
3a994742 VZ |
322 | |
323 | // return the string representation of this number | |
324 | wxString ToString() const; | |
325 | ||
8b81872f VZ |
326 | // conversion to byte array: returns a pointer to static buffer! |
327 | void *asArray() const; | |
328 | ||
fcc3d7cb | 329 | #if wxUSE_STD_IOSTREAM |
8b81872f | 330 | // input/output |
dd107c50 | 331 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&); |
fcc3d7cb | 332 | #endif |
8b81872f VZ |
333 | |
334 | private: | |
335 | wxLongLong_t m_ll; | |
336 | }; | |
337 | ||
8e38fd1f RL |
338 | |
339 | class WXDLLEXPORT wxULongLongNative | |
340 | { | |
341 | public: | |
342 | // ctors | |
343 | // default ctor initializes to 0 | |
344 | wxULongLongNative() { m_ll = 0; } | |
345 | // from long long | |
346 | wxULongLongNative(unsigned wxLongLong_t ll) { m_ll = ll; } | |
347 | // from 2 longs | |
348 | wxULongLongNative(unsigned long hi, unsigned long lo) | |
349 | { | |
350 | // assign first to avoid precision loss! | |
351 | m_ll = ((unsigned wxLongLong_t) hi) << 32; | |
352 | m_ll |= (unsigned wxLongLong_t) lo; | |
353 | } | |
354 | ||
355 | // default copy ctor is ok | |
356 | ||
357 | // no dtor | |
358 | ||
359 | // assignment operators | |
360 | // from native 64 bit integer | |
361 | wxULongLongNative& operator=(unsigned wxLongLong_t ll) | |
362 | { m_ll = ll; return *this; } | |
363 | ||
364 | // assignment operators from wxULongLongNative is ok | |
365 | ||
366 | // accessors | |
367 | // get high part | |
368 | unsigned long GetHi() const | |
369 | { return (unsigned long)(m_ll >> 32); } | |
370 | // get low part | |
371 | unsigned long GetLo() const | |
372 | { return (unsigned long)m_ll; } | |
373 | ||
374 | // convert to native ulong long | |
375 | unsigned wxLongLong_t GetValue() const { return m_ll; } | |
376 | ||
377 | // convert to ulong with range checking in the debug mode (only!) | |
378 | unsigned long ToULong() const | |
379 | { | |
0ba9f867 | 380 | wxASSERT_MSG( m_ll <= LONG_MAX, |
8e38fd1f RL |
381 | _T("wxULongLong to long conversion loss of precision") ); |
382 | ||
383 | return (unsigned long)m_ll; | |
384 | } | |
385 | ||
386 | // don't provide implicit conversion to unsigned wxLongLong_t or we | |
387 | // will have an ambiguity for all arithmetic operations | |
388 | //operator wxULongLong_t() const { return m_ll; } | |
389 | ||
390 | // operations | |
391 | // addition | |
392 | wxULongLongNative operator+(const wxULongLongNative& ll) const | |
393 | { return wxULongLongNative(m_ll + ll.m_ll); } | |
394 | wxULongLongNative& operator+=(const wxULongLongNative& ll) | |
395 | { m_ll += ll.m_ll; return *this; } | |
396 | ||
397 | wxULongLongNative operator+(const unsigned wxLongLong_t ll) const | |
398 | { return wxULongLongNative(m_ll + ll); } | |
399 | wxULongLongNative& operator+=(const unsigned wxLongLong_t ll) | |
400 | { m_ll += ll; return *this; } | |
401 | ||
402 | // pre increment | |
403 | wxULongLongNative& operator++() | |
404 | { m_ll++; return *this; } | |
405 | ||
406 | // post increment | |
407 | wxULongLongNative& operator++(int) | |
408 | { m_ll++; return *this; } | |
409 | ||
410 | // subtraction | |
411 | wxULongLongNative operator-(const wxULongLongNative& ll) const | |
412 | { return wxULongLongNative(m_ll - ll.m_ll); } | |
413 | wxULongLongNative& operator-=(const wxULongLongNative& ll) | |
414 | { m_ll -= ll.m_ll; return *this; } | |
415 | ||
416 | wxULongLongNative operator-(const unsigned wxLongLong_t ll) const | |
417 | { return wxULongLongNative(m_ll - ll); } | |
418 | wxULongLongNative& operator-=(const unsigned wxLongLong_t ll) | |
419 | { m_ll -= ll; return *this; } | |
420 | ||
421 | // pre decrement | |
422 | wxULongLongNative& operator--() | |
423 | { m_ll--; return *this; } | |
424 | ||
425 | // post decrement | |
426 | wxULongLongNative& operator--(int) | |
427 | { m_ll--; return *this; } | |
428 | ||
429 | // shifts | |
430 | // left shift | |
431 | wxULongLongNative operator<<(int shift) const | |
432 | { return wxULongLongNative(m_ll << shift);; } | |
433 | wxULongLongNative& operator<<=(int shift) | |
434 | { m_ll <<= shift; return *this; } | |
435 | ||
436 | // right shift | |
437 | wxULongLongNative operator>>(int shift) const | |
438 | { return wxULongLongNative(m_ll >> shift);; } | |
439 | wxULongLongNative& operator>>=(int shift) | |
440 | { m_ll >>= shift; return *this; } | |
441 | ||
442 | // bitwise operators | |
443 | wxULongLongNative operator&(const wxULongLongNative& ll) const | |
444 | { return wxULongLongNative(m_ll & ll.m_ll); } | |
445 | wxULongLongNative& operator&=(const wxULongLongNative& ll) | |
446 | { m_ll &= ll.m_ll; return *this; } | |
447 | ||
448 | wxULongLongNative operator|(const wxULongLongNative& ll) const | |
449 | { return wxULongLongNative(m_ll | ll.m_ll); } | |
450 | wxULongLongNative& operator|=(const wxULongLongNative& ll) | |
451 | { m_ll |= ll.m_ll; return *this; } | |
452 | ||
453 | wxULongLongNative operator^(const wxULongLongNative& ll) const | |
454 | { return wxULongLongNative(m_ll ^ ll.m_ll); } | |
455 | wxULongLongNative& operator^=(const wxULongLongNative& ll) | |
456 | { m_ll ^= ll.m_ll; return *this; } | |
457 | ||
458 | // multiplication/division | |
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 | wxULongLongNative& operator/=(const wxULongLongNative& ll) | |
473 | { m_ll /= ll.m_ll; return *this; } | |
474 | wxULongLongNative& operator/=(unsigned long l) | |
475 | { m_ll /= l; return *this; } | |
476 | ||
477 | wxULongLongNative operator%(const wxULongLongNative& ll) const | |
478 | { return wxULongLongNative(m_ll % ll.m_ll); } | |
479 | wxULongLongNative operator%(unsigned long l) const | |
480 | { return wxULongLongNative(m_ll % l); } | |
481 | ||
482 | // comparison | |
483 | bool operator==(const wxULongLongNative& ll) const | |
484 | { return m_ll == ll.m_ll; } | |
485 | bool operator==(unsigned long l) const | |
486 | { return m_ll == l; } | |
487 | bool operator!=(const wxULongLongNative& ll) const | |
488 | { return m_ll != ll.m_ll; } | |
489 | bool operator!=(unsigned long l) const | |
490 | { return m_ll != l; } | |
491 | bool operator<(const wxULongLongNative& ll) const | |
492 | { return m_ll < ll.m_ll; } | |
493 | bool operator<(unsigned long l) const | |
494 | { return m_ll < l; } | |
495 | bool operator>(const wxULongLongNative& ll) const | |
496 | { return m_ll > ll.m_ll; } | |
497 | bool operator>(unsigned long l) const | |
498 | { return m_ll > l; } | |
499 | bool operator<=(const wxULongLongNative& ll) const | |
500 | { return m_ll <= ll.m_ll; } | |
501 | bool operator<=(unsigned long l) const | |
502 | { return m_ll <= l; } | |
503 | bool operator>=(const wxULongLongNative& ll) const | |
504 | { return m_ll >= ll.m_ll; } | |
505 | bool operator>=(unsigned long l) const | |
506 | { return m_ll >= l; } | |
507 | ||
508 | // miscellaneous | |
509 | ||
510 | // return the string representation of this number | |
511 | wxString ToString() const; | |
512 | ||
513 | // conversion to byte array: returns a pointer to static buffer! | |
514 | void *asArray() const; | |
515 | ||
516 | #if wxUSE_STD_IOSTREAM | |
517 | // input/output | |
518 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&); | |
519 | #endif | |
520 | ||
521 | private: | |
522 | unsigned wxLongLong_t m_ll; | |
523 | }; | |
524 | ||
8b81872f VZ |
525 | #endif // wxUSE_LONGLONG_NATIVE |
526 | ||
527 | #if wxUSE_LONGLONG_WX | |
528 | ||
529 | class WXDLLEXPORT wxLongLongWx | |
530 | { | |
531 | public: | |
532 | // ctors | |
533 | // default ctor initializes to 0 | |
2a310492 VZ |
534 | wxLongLongWx() |
535 | { | |
536 | m_lo = m_hi = 0; | |
537 | ||
538 | #ifdef wxLONGLONG_TEST_MODE | |
539 | m_ll = 0; | |
540 | ||
541 | Check(); | |
542 | #endif // wxLONGLONG_TEST_MODE | |
543 | } | |
8b81872f | 544 | // from long |
2a310492 | 545 | wxLongLongWx(long l) { *this = l; } |
8b81872f VZ |
546 | // from 2 longs |
547 | wxLongLongWx(long hi, unsigned long lo) | |
2a310492 VZ |
548 | { |
549 | m_hi = hi; | |
550 | m_lo = lo; | |
551 | ||
552 | #ifdef wxLONGLONG_TEST_MODE | |
553 | m_ll = hi; | |
554 | m_ll <<= 32; | |
555 | m_ll |= lo; | |
556 | ||
557 | Check(); | |
558 | #endif // wxLONGLONG_TEST_MODE | |
559 | } | |
8b81872f VZ |
560 | |
561 | // default copy ctor is ok in both cases | |
562 | ||
563 | // no dtor | |
564 | ||
565 | // assignment operators | |
566 | // from long | |
567 | wxLongLongWx& operator=(long l) | |
2a310492 VZ |
568 | { |
569 | m_lo = l; | |
570 | m_hi = (l < 0 ? -1l : 0l); | |
571 | ||
572 | #ifdef wxLONGLONG_TEST_MODE | |
573 | m_ll = l; | |
574 | ||
575 | Check(); | |
576 | #endif // wxLONGLONG_TEST_MODE | |
577 | ||
578 | return *this; | |
579 | } | |
cd0b1709 VZ |
580 | // from double |
581 | wxLongLongWx& Assign(double d); | |
8b81872f VZ |
582 | // can't have assignment operator from 2 longs |
583 | ||
584 | // accessors | |
585 | // get high part | |
586 | long GetHi() const { return m_hi; } | |
587 | // get low part | |
588 | unsigned long GetLo() const { return m_lo; } | |
589 | ||
cd0b1709 | 590 | // get absolute value |
2ea24d9f | 591 | wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); } |
2a310492 VZ |
592 | wxLongLongWx& Abs() |
593 | { | |
594 | if ( m_hi < 0 ) | |
595 | m_hi = -m_hi; | |
596 | ||
597 | #ifdef wxLONGLONG_TEST_MODE | |
598 | if ( m_ll < 0 ) | |
599 | m_ll = -m_ll; | |
600 | ||
601 | Check(); | |
602 | #endif // wxLONGLONG_TEST_MODE | |
603 | ||
604 | return *this; | |
605 | } | |
cd0b1709 VZ |
606 | |
607 | // convert to long with range checking in the debug mode (only!) | |
608 | long ToLong() const | |
609 | { | |
2a310492 | 610 | wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l), |
cd0b1709 VZ |
611 | _T("wxLongLong to long conversion loss of precision") ); |
612 | ||
613 | return (long)m_lo; | |
614 | } | |
615 | ||
8b81872f VZ |
616 | // operations |
617 | // addition | |
618 | wxLongLongWx operator+(const wxLongLongWx& ll) const; | |
619 | wxLongLongWx& operator+=(const wxLongLongWx& ll); | |
620 | wxLongLongWx operator+(long l) const; | |
621 | wxLongLongWx& operator+=(long l); | |
622 | ||
623 | // pre increment operator | |
624 | wxLongLongWx& operator++(); | |
625 | ||
626 | // post increment operator | |
2a310492 | 627 | wxLongLongWx& operator++(int) { return ++(*this); } |
8b81872f VZ |
628 | |
629 | // negation operator | |
630 | wxLongLongWx operator-() const; | |
2a310492 | 631 | wxLongLongWx& Negate(); |
8b81872f VZ |
632 | |
633 | // subraction | |
634 | wxLongLongWx operator-(const wxLongLongWx& ll) const; | |
635 | wxLongLongWx& operator-=(const wxLongLongWx& ll); | |
636 | ||
637 | // pre decrement operator | |
638 | wxLongLongWx& operator--(); | |
639 | ||
640 | // post decrement operator | |
2a310492 | 641 | wxLongLongWx& operator--(int) { return --(*this); } |
8b81872f VZ |
642 | |
643 | // shifts | |
644 | // left shift | |
645 | wxLongLongWx operator<<(int shift) const; | |
646 | wxLongLongWx& operator<<=(int shift); | |
647 | ||
648 | // right shift | |
649 | wxLongLongWx operator>>(int shift) const; | |
650 | wxLongLongWx& operator>>=(int shift); | |
651 | ||
652 | // bitwise operators | |
653 | wxLongLongWx operator&(const wxLongLongWx& ll) const; | |
654 | wxLongLongWx& operator&=(const wxLongLongWx& ll); | |
655 | wxLongLongWx operator|(const wxLongLongWx& ll) const; | |
656 | wxLongLongWx& operator|=(const wxLongLongWx& ll); | |
657 | wxLongLongWx operator^(const wxLongLongWx& ll) const; | |
658 | wxLongLongWx& operator^=(const wxLongLongWx& ll); | |
659 | wxLongLongWx operator~() const; | |
660 | ||
661 | // comparison | |
2ea24d9f VZ |
662 | bool operator==(const wxLongLongWx& ll) const |
663 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
664 | bool operator!=(const wxLongLongWx& ll) const | |
665 | { return !(*this == ll); } | |
8b81872f VZ |
666 | bool operator<(const wxLongLongWx& ll) const; |
667 | bool operator>(const wxLongLongWx& ll) const; | |
2ea24d9f VZ |
668 | bool operator<=(const wxLongLongWx& ll) const |
669 | { return *this < ll || *this == ll; } | |
670 | bool operator>=(const wxLongLongWx& ll) const | |
671 | { return *this > ll || *this == ll; } | |
8b81872f | 672 | |
f6bcfd97 BP |
673 | bool operator<(long l) const { return *this < wxLongLongWx(l); } |
674 | bool operator>(long l) const { return *this > wxLongLongWx(l); } | |
675 | bool operator==(long l) const | |
676 | { | |
677 | return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l) | |
678 | : (m_hi == -1 && m_lo == (unsigned long)l); | |
679 | } | |
680 | ||
681 | bool operator<=(long l) const { return *this < l || *this == l; } | |
682 | bool operator>=(long l) const { return *this > l || *this == l; } | |
683 | ||
8b81872f VZ |
684 | // multiplication |
685 | wxLongLongWx operator*(const wxLongLongWx& ll) const; | |
686 | wxLongLongWx& operator*=(const wxLongLongWx& ll); | |
8b81872f VZ |
687 | |
688 | // division | |
cd0b1709 VZ |
689 | wxLongLongWx operator/(const wxLongLongWx& ll) const; |
690 | wxLongLongWx& operator/=(const wxLongLongWx& ll); | |
691 | ||
692 | wxLongLongWx operator%(const wxLongLongWx& ll) const; | |
693 | ||
8b81872f VZ |
694 | void Divide(const wxLongLongWx& divisor, |
695 | wxLongLongWx& quotient, | |
696 | wxLongLongWx& remainder) const; | |
697 | ||
698 | // input/output | |
3a994742 VZ |
699 | |
700 | // return the string representation of this number | |
701 | wxString ToString() const; | |
702 | ||
703 | void *asArray() const; | |
704 | ||
cd0b1709 | 705 | #if wxUSE_STD_IOSTREAM |
dd107c50 | 706 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&); |
fcc3d7cb | 707 | #endif // wxUSE_STD_IOSTREAM |
8b81872f VZ |
708 | |
709 | private: | |
710 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
711 | ||
712 | long m_hi; // signed bit is in the high part | |
713 | unsigned long m_lo; | |
2a310492 VZ |
714 | |
715 | #ifdef wxLONGLONG_TEST_MODE | |
716 | void Check() | |
717 | { | |
718 | wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo ); | |
719 | } | |
720 | ||
721 | wxLongLong_t m_ll; | |
722 | #endif // wxLONGLONG_TEST_MODE | |
8b81872f VZ |
723 | }; |
724 | ||
8e38fd1f RL |
725 | |
726 | class WXDLLEXPORT wxULongLongWx | |
727 | { | |
728 | public: | |
729 | // ctors | |
730 | // default ctor initializes to 0 | |
731 | wxULongLongWx() | |
732 | { | |
733 | m_lo = m_hi = 0; | |
734 | ||
735 | #ifdef wxLONGLONG_TEST_MODE | |
736 | m_ll = 0; | |
737 | ||
738 | Check(); | |
739 | #endif // wxLONGLONG_TEST_MODE | |
740 | } | |
741 | // from ulong | |
742 | wxULongLongWx(unsigned long l) { *this = l; } | |
743 | // from 2 ulongs | |
744 | wxULongLongWx(unsigned long hi, unsigned long lo) | |
745 | { | |
746 | m_hi = hi; | |
747 | m_lo = lo; | |
748 | ||
749 | #ifdef wxLONGLONG_TEST_MODE | |
750 | m_ll = hi; | |
751 | m_ll <<= 32; | |
752 | m_ll |= lo; | |
753 | ||
754 | Check(); | |
755 | #endif // wxLONGLONG_TEST_MODE | |
756 | } | |
757 | ||
758 | // default copy ctor is ok in both cases | |
759 | ||
760 | // no dtor | |
761 | ||
762 | // assignment operators | |
763 | // from long | |
764 | wxULongLongWx& operator=(unsigned long l) | |
765 | { | |
766 | m_lo = l; | |
767 | m_hi = 0; | |
768 | ||
769 | #ifdef wxLONGLONG_TEST_MODE | |
770 | m_ll = l; | |
771 | ||
772 | Check(); | |
773 | #endif // wxLONGLONG_TEST_MODE | |
774 | ||
775 | return *this; | |
776 | } | |
777 | ||
778 | // can't have assignment operator from 2 longs | |
779 | ||
780 | // accessors | |
781 | // get high part | |
782 | unsigned long GetHi() const { return m_hi; } | |
783 | // get low part | |
784 | unsigned long GetLo() const { return m_lo; } | |
785 | ||
786 | // convert to long with range checking in the debug mode (only!) | |
787 | unsigned long ToULong() const | |
788 | { | |
789 | wxASSERT_MSG( (m_hi == 0l), | |
790 | _T("wxULongLong to long conversion loss of precision") ); | |
791 | ||
792 | return (unsigned long)m_lo; | |
793 | } | |
794 | ||
795 | // operations | |
796 | // addition | |
797 | wxULongLongWx operator+(const wxULongLongWx& ll) const; | |
798 | wxULongLongWx& operator+=(const wxULongLongWx& ll); | |
799 | wxULongLongWx operator+(unsigned long l) const; | |
800 | wxULongLongWx& operator+=(unsigned long l); | |
801 | ||
802 | // pre increment operator | |
803 | wxULongLongWx& operator++(); | |
804 | ||
805 | // post increment operator | |
806 | wxULongLongWx& operator++(int) { return ++(*this); } | |
807 | ||
808 | // subraction | |
809 | wxULongLongWx operator-(const wxULongLongWx& ll) const; | |
810 | wxULongLongWx& operator-=(const wxULongLongWx& ll); | |
811 | ||
812 | // pre decrement operator | |
813 | wxULongLongWx& operator--(); | |
814 | ||
815 | // post decrement operator | |
816 | wxULongLongWx& operator--(int) { return --(*this); } | |
817 | ||
818 | // shifts | |
819 | // left shift | |
820 | wxULongLongWx operator<<(int shift) const; | |
821 | wxULongLongWx& operator<<=(int shift); | |
822 | ||
823 | // right shift | |
824 | wxULongLongWx operator>>(int shift) const; | |
825 | wxULongLongWx& operator>>=(int shift); | |
826 | ||
827 | // bitwise operators | |
828 | wxULongLongWx operator&(const wxULongLongWx& ll) const; | |
829 | wxULongLongWx& operator&=(const wxULongLongWx& ll); | |
830 | wxULongLongWx operator|(const wxULongLongWx& ll) const; | |
831 | wxULongLongWx& operator|=(const wxULongLongWx& ll); | |
832 | wxULongLongWx operator^(const wxULongLongWx& ll) const; | |
833 | wxULongLongWx& operator^=(const wxULongLongWx& ll); | |
834 | wxULongLongWx operator~() const; | |
835 | ||
836 | // comparison | |
837 | bool operator==(const wxULongLongWx& ll) const | |
838 | { return m_lo == ll.m_lo && m_hi == ll.m_hi; } | |
839 | bool operator!=(const wxULongLongWx& ll) const | |
840 | { return !(*this == ll); } | |
841 | bool operator<(const wxULongLongWx& ll) const; | |
842 | bool operator>(const wxULongLongWx& ll) const; | |
843 | bool operator<=(const wxULongLongWx& ll) const | |
844 | { return *this < ll || *this == ll; } | |
845 | bool operator>=(const wxULongLongWx& ll) const | |
846 | { return *this > ll || *this == ll; } | |
847 | ||
848 | bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); } | |
849 | bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); } | |
850 | bool operator==(unsigned long l) const | |
851 | { | |
852 | return (m_hi == 0 && m_lo == (unsigned long)l); | |
853 | } | |
854 | ||
855 | bool operator<=(unsigned long l) const { return *this < l || *this == l; } | |
856 | bool operator>=(unsigned long l) const { return *this > l || *this == l; } | |
857 | ||
858 | // multiplication | |
859 | wxULongLongWx operator*(const wxULongLongWx& ll) const; | |
860 | wxULongLongWx& operator*=(const wxULongLongWx& ll); | |
861 | ||
862 | // division | |
863 | wxULongLongWx operator/(const wxULongLongWx& ll) const; | |
864 | wxULongLongWx& operator/=(const wxULongLongWx& ll); | |
865 | ||
866 | wxULongLongWx operator%(const wxULongLongWx& ll) const; | |
867 | ||
868 | void Divide(const wxULongLongWx& divisor, | |
869 | wxULongLongWx& quotient, | |
870 | wxULongLongWx& remainder) const; | |
871 | ||
872 | // input/output | |
873 | ||
874 | // return the string representation of this number | |
875 | wxString ToString() const; | |
876 | ||
877 | void *asArray() const; | |
878 | ||
879 | #if wxUSE_STD_IOSTREAM | |
880 | friend wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&); | |
881 | #endif // wxUSE_STD_IOSTREAM | |
882 | ||
883 | private: | |
884 | // long is at least 32 bits, so represent our 64bit number as 2 longs | |
885 | ||
886 | unsigned long m_hi | |
887 | unsigned long m_lo; | |
888 | ||
889 | #ifdef wxLONGLONG_TEST_MODE | |
890 | void Check() | |
891 | { | |
892 | wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo ); | |
893 | } | |
894 | ||
895 | unsigned wxLongLong_t m_ll; | |
896 | #endif // wxLONGLONG_TEST_MODE | |
897 | }; | |
898 | ||
8b81872f VZ |
899 | #endif // wxUSE_LONGLONG_WX |
900 | ||
2ea24d9f VZ |
901 | // ---------------------------------------------------------------------------- |
902 | // binary operators | |
903 | // ---------------------------------------------------------------------------- | |
904 | ||
6d56eb5c VZ |
905 | inline bool operator<(long l, const wxLongLong& ll) { return ll > l; } |
906 | inline bool operator>(long l, const wxLongLong& ll) { return ll > l; } | |
907 | inline bool operator<=(long l, const wxLongLong& ll) { return ll > l; } | |
908 | inline bool operator>=(long l, const wxLongLong& ll) { return ll > l; } | |
909 | inline bool operator==(long l, const wxLongLong& ll) { return ll > l; } | |
910 | inline bool operator!=(long l, const wxLongLong& ll) { return ll > l; } | |
911 | ||
b01fb5c3 VZ |
912 | inline wxLongLong operator+(long l, const wxLongLong& ll) |
913 | { | |
914 | return ll + wxLongLong(l); | |
915 | } | |
916 | ||
917 | inline wxLongLong operator-(long l, const wxLongLong& ll) | |
918 | { | |
919 | return ll - wxLongLong(l); | |
920 | } | |
2ea24d9f | 921 | |
8e38fd1f RL |
922 | inline bool operator<(unsigned long l, const wxULongLong& ll) { return ll > l; } |
923 | inline bool operator>(unsigned long l, const wxULongLong& ll) { return ll > l; } | |
924 | inline bool operator<=(unsigned long l, const wxULongLong& ll) { return ll > l; } | |
925 | inline bool operator>=(unsigned long l, const wxULongLong& ll) { return ll > l; } | |
926 | inline bool operator==(unsigned long l, const wxULongLong& ll) { return ll > l; } | |
927 | inline bool operator!=(unsigned long l, const wxULongLong& ll) { return ll > l; } | |
928 | ||
929 | inline wxULongLong operator+(unsigned long l, const wxULongLong& ll) { return ll + l; } | |
930 | inline wxULongLong operator-(unsigned long l, const wxULongLong& ll) { return ll - l; } | |
931 | ||
8b81872f | 932 | #endif // _WX_LONGLONG_H |