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