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