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