]> git.saurik.com Git - wxWidgets.git/blame - include/wx/longlong.h
Some BC++ issues
[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
VZ
58 // both warning and pragma warning are not portable, but at least an
59 // unknown pragma should never be an error
60 #pragma warning "Your compiler does not appear to support 64 bit "\
61 "integers, using emulation class instead."
cd0b1709 62
8b81872f
VZ
63 #define wxUSE_LONGLONG_WX 1
64#endif // compiler
65
66// the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
67// to disable automatic testing (useful for the test program which defines
68// both classes) but by default we only use one class
2ea24d9f 69#if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
8b81872f
VZ
70 #undef wxUSE_LONGLONG_NATIVE
71 #define wxUSE_LONGLONG_NATIVE 0
72 class WXDLLEXPORT wxLongLongWx;
73 typedef wxLongLongWx wxLongLong;
b76b015e
VZ
74#else
75 // if nothing is defined, use native implementation by default, of course
76 #ifndef wxUSE_LONGLONG_NATIVE
77 #define wxUSE_LONGLONG_NATIVE 1
78 #endif
8b81872f
VZ
79#endif
80
81#ifndef wxUSE_LONGLONG_WX
82 #define wxUSE_LONGLONG_WX 0
83 class WXDLLEXPORT wxLongLongNative;
84 typedef wxLongLongNative wxLongLong;
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
95#include "wx/ioswrap.h"
96
97#if wxUSE_LONGLONG_NATIVE
98
99class WXDLLEXPORT wxLongLongNative
100{
101public:
102 // ctors
103 // default ctor initializes to 0
104 wxLongLongNative() { m_ll = 0; }
105 // from long long
106 wxLongLongNative(wxLongLong_t ll) { m_ll = ll; }
107 // from 2 longs
108 wxLongLongNative(long hi, unsigned long lo)
109 {
110 // assign first to avoid precision loss!
111 m_ll = ((wxLongLong_t) hi) << 32;
112 m_ll |= (wxLongLong_t) lo;
113 }
114
2f02cb89 115 // default copy ctor is ok
8b81872f
VZ
116
117 // no dtor
118
119 // assignment operators
120 // from native 64 bit integer
121 wxLongLongNative& operator=(wxLongLong_t ll)
122 { m_ll = ll; return *this; }
123
cd0b1709
VZ
124 // from double: this one has an explicit name because otherwise we
125 // would have ambiguity with "ll = int" and also because we don't want
126 // to have implicit conversions between doubles and wxLongLongs
127 wxLongLongNative& Assign(double d)
128 { m_ll = (wxLongLong_t)d; return *this; }
129
8b81872f
VZ
130 // assignment operators from wxLongLongNative is ok
131
132 // accessors
133 // get high part
134 long GetHi() const
2f02cb89 135 { return (long)(m_ll >> 32); }
8b81872f
VZ
136 // get low part
137 unsigned long GetLo() const
2f02cb89 138 { return (unsigned long)m_ll; }
8b81872f 139
b76b015e 140 // get absolute value
2ea24d9f 141 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
b76b015e
VZ
142 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
143
8b81872f
VZ
144 // convert to native long long
145 wxLongLong_t GetValue() const { return m_ll; }
146
1ef54dcf
VZ
147 // convert to long with range checking in the debug mode (only!)
148 long ToLong() const
149 {
150 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
151 _T("wxLongLong to long conversion loss of precision") );
152
153 return (long)m_ll;
154 }
155
2f02cb89
VZ
156 // don't provide implicit conversion to wxLongLong_t or we will have an
157 // ambiguity for all arithmetic operations
b76b015e 158 //operator wxLongLong_t() const { return m_ll; }
8b81872f
VZ
159
160 // operations
161 // addition
162 wxLongLongNative operator+(const wxLongLongNative& ll) const
163 { return wxLongLongNative(m_ll + ll.m_ll); }
164 wxLongLongNative& operator+=(const wxLongLongNative& ll)
165 { m_ll += ll.m_ll; return *this; }
166
167 wxLongLongNative operator+(const wxLongLong_t ll) const
168 { return wxLongLongNative(m_ll + ll); }
169 wxLongLongNative& operator+=(const wxLongLong_t ll)
170 { m_ll += ll; return *this; }
171
172 // pre increment
173 wxLongLongNative& operator++()
174 { m_ll++; return *this; }
175
176 // post increment
177 wxLongLongNative& operator++(int)
178 { m_ll++; return *this; }
179
180 // negation operator
181 wxLongLongNative operator-() const
182 { return wxLongLongNative(-m_ll); }
183
184 // subtraction
185 wxLongLongNative operator-(const wxLongLongNative& ll) const
186 { return wxLongLongNative(m_ll - ll.m_ll); }
187 wxLongLongNative& operator-=(const wxLongLongNative& ll)
188 { m_ll -= ll.m_ll; return *this; }
189
190 wxLongLongNative operator-(const wxLongLong_t ll) const
191 { return wxLongLongNative(m_ll - ll); }
192 wxLongLongNative& operator-=(const wxLongLong_t ll)
193 { m_ll -= ll; return *this; }
194
195 // pre decrement
196 wxLongLongNative& operator--()
197 { m_ll--; return *this; }
198
199 // post decrement
200 wxLongLongNative& operator--(int)
201 { m_ll--; return *this; }
202
203 // shifts
204 // left shift
205 wxLongLongNative operator<<(int shift) const
206 { return wxLongLongNative(m_ll << shift);; }
207 wxLongLongNative& operator<<=(int shift)
208 { m_ll <<= shift; return *this; }
209
210 // right shift
211 wxLongLongNative operator>>(int shift) const
212 { return wxLongLongNative(m_ll >> shift);; }
213 wxLongLongNative& operator>>=(int shift)
214 { m_ll >>= shift; return *this; }
215
216 // bitwise operators
217 wxLongLongNative operator&(const wxLongLongNative& ll) const
218 { return wxLongLongNative(m_ll & ll.m_ll); }
219 wxLongLongNative& operator&=(const wxLongLongNative& ll)
220 { m_ll &= ll.m_ll; return *this; }
221
222 wxLongLongNative operator|(const wxLongLongNative& ll) const
223 { return wxLongLongNative(m_ll | ll.m_ll); }
224 wxLongLongNative& operator|=(const wxLongLongNative& ll)
225 { m_ll |= ll.m_ll; return *this; }
226
227 wxLongLongNative operator^(const wxLongLongNative& ll) const
228 { return wxLongLongNative(m_ll ^ ll.m_ll); }
229 wxLongLongNative& operator^=(const wxLongLongNative& ll)
230 { m_ll ^= ll.m_ll; return *this; }
231
b76b015e 232 // multiplication/division
8b81872f
VZ
233 wxLongLongNative operator*(const wxLongLongNative& ll) const
234 { return wxLongLongNative(m_ll * ll.m_ll); }
2f02cb89
VZ
235 wxLongLongNative operator*(long l) const
236 { return wxLongLongNative(m_ll * l); }
8b81872f
VZ
237 wxLongLongNative& operator*=(const wxLongLongNative& ll)
238 { m_ll *= ll.m_ll; return *this; }
2f02cb89
VZ
239 wxLongLongNative& operator*=(long l)
240 { m_ll *= l; return *this; }
8b81872f
VZ
241
242 wxLongLongNative operator/(const wxLongLongNative& ll) const
243 { return wxLongLongNative(m_ll / ll.m_ll); }
b76b015e
VZ
244 wxLongLongNative operator/(long l) const
245 { return wxLongLongNative(m_ll / l); }
8b81872f
VZ
246 wxLongLongNative& operator/=(const wxLongLongNative& ll)
247 { m_ll /= ll.m_ll; return *this; }
2f02cb89
VZ
248 wxLongLongNative& operator/=(long l)
249 { m_ll /= l; return *this; }
8b81872f
VZ
250
251 wxLongLongNative operator%(const wxLongLongNative& ll) const
252 { return wxLongLongNative(m_ll % ll.m_ll); }
b76b015e
VZ
253 wxLongLongNative operator%(long l) const
254 { return wxLongLongNative(m_ll % l); }
8b81872f
VZ
255
256 // comparison
257 bool operator==(const wxLongLongNative& ll) const
258 { return m_ll == ll.m_ll; }
b76b015e
VZ
259 bool operator==(long l) const
260 { return m_ll == l; }
8b81872f
VZ
261 bool operator!=(const wxLongLongNative& ll) const
262 { return m_ll != ll.m_ll; }
b76b015e
VZ
263 bool operator!=(long l) const
264 { return m_ll != l; }
8b81872f
VZ
265 bool operator<(const wxLongLongNative& ll) const
266 { return m_ll < ll.m_ll; }
b76b015e
VZ
267 bool operator<(long l) const
268 { return m_ll < l; }
8b81872f
VZ
269 bool operator>(const wxLongLongNative& ll) const
270 { return m_ll > ll.m_ll; }
b76b015e
VZ
271 bool operator>(long l) const
272 { return m_ll > l; }
8b81872f
VZ
273 bool operator<=(const wxLongLongNative& ll) const
274 { return m_ll <= ll.m_ll; }
b76b015e
VZ
275 bool operator<=(long l) const
276 { return m_ll <= l; }
8b81872f
VZ
277 bool operator>=(const wxLongLongNative& ll) const
278 { return m_ll >= ll.m_ll; }
b76b015e
VZ
279 bool operator>=(long l) const
280 { return m_ll >= l; }
8b81872f
VZ
281
282 // miscellaneous
283 // conversion to byte array: returns a pointer to static buffer!
284 void *asArray() const;
285
fcc3d7cb 286#if wxUSE_STD_IOSTREAM
8b81872f 287 // input/output
162b0c3b 288 friend ostream& operator<<(ostream&, const wxLongLongNative&);
fcc3d7cb 289#endif
8b81872f
VZ
290
291private:
292 wxLongLong_t m_ll;
293};
294
295#endif // wxUSE_LONGLONG_NATIVE
296
297#if wxUSE_LONGLONG_WX
298
299class WXDLLEXPORT wxLongLongWx
300{
301public:
302 // ctors
303 // default ctor initializes to 0
304 wxLongLongWx() { m_lo = m_hi = 0; }
305 // from long
306 wxLongLongWx(long l)
307 { m_lo = l; m_hi = (l < 0 ? -1l : 0l); }
308 // from 2 longs
309 wxLongLongWx(long hi, unsigned long lo)
310 { m_hi = hi; m_lo = lo; }
311
312 // default copy ctor is ok in both cases
313
314 // no dtor
315
316 // assignment operators
317 // from long
318 wxLongLongWx& operator=(long l)
319 { m_lo = l; m_hi = (l < 0 ? -1l : 0l); return *this; }
cd0b1709
VZ
320 // from double
321 wxLongLongWx& Assign(double d);
8b81872f
VZ
322 // can't have assignment operator from 2 longs
323
324 // accessors
325 // get high part
326 long GetHi() const { return m_hi; }
327 // get low part
328 unsigned long GetLo() const { return m_lo; }
329
cd0b1709 330 // get absolute value
2ea24d9f 331 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
cd0b1709
VZ
332 wxLongLongWx& Abs() { if ( m_hi < 0 ) m_hi = -m_hi; return *this; }
333
334 // convert to long with range checking in the debug mode (only!)
335 long ToLong() const
336 {
337 wxASSERT_MSG( m_hi == 0l,
338 _T("wxLongLong to long conversion loss of precision") );
339
340 return (long)m_lo;
341 }
342
8b81872f
VZ
343 // operations
344 // addition
345 wxLongLongWx operator+(const wxLongLongWx& ll) const;
346 wxLongLongWx& operator+=(const wxLongLongWx& ll);
347 wxLongLongWx operator+(long l) const;
348 wxLongLongWx& operator+=(long l);
349
350 // pre increment operator
351 wxLongLongWx& operator++();
352
353 // post increment operator
354 wxLongLongWx& operator++(int);
355
356 // negation operator
357 wxLongLongWx operator-() const;
358
359 // subraction
360 wxLongLongWx operator-(const wxLongLongWx& ll) const;
361 wxLongLongWx& operator-=(const wxLongLongWx& ll);
362
363 // pre decrement operator
364 wxLongLongWx& operator--();
365
366 // post decrement operator
367 wxLongLongWx& operator--(int);
368
369 // shifts
370 // left shift
371 wxLongLongWx operator<<(int shift) const;
372 wxLongLongWx& operator<<=(int shift);
373
374 // right shift
375 wxLongLongWx operator>>(int shift) const;
376 wxLongLongWx& operator>>=(int shift);
377
378 // bitwise operators
379 wxLongLongWx operator&(const wxLongLongWx& ll) const;
380 wxLongLongWx& operator&=(const wxLongLongWx& ll);
381 wxLongLongWx operator|(const wxLongLongWx& ll) const;
382 wxLongLongWx& operator|=(const wxLongLongWx& ll);
383 wxLongLongWx operator^(const wxLongLongWx& ll) const;
384 wxLongLongWx& operator^=(const wxLongLongWx& ll);
385 wxLongLongWx operator~() const;
386
387 // comparison
2ea24d9f
VZ
388 bool operator==(const wxLongLongWx& ll) const
389 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
390 bool operator!=(const wxLongLongWx& ll) const
391 { return !(*this == ll); }
8b81872f
VZ
392 bool operator<(const wxLongLongWx& ll) const;
393 bool operator>(const wxLongLongWx& ll) const;
2ea24d9f
VZ
394 bool operator<=(const wxLongLongWx& ll) const
395 { return *this < ll || *this == ll; }
396 bool operator>=(const wxLongLongWx& ll) const
397 { return *this > ll || *this == ll; }
8b81872f
VZ
398
399 // multiplication
400 wxLongLongWx operator*(const wxLongLongWx& ll) const;
401 wxLongLongWx& operator*=(const wxLongLongWx& ll);
8b81872f
VZ
402
403 // division
cd0b1709
VZ
404 wxLongLongWx operator/(const wxLongLongWx& ll) const;
405 wxLongLongWx& operator/=(const wxLongLongWx& ll);
406
407 wxLongLongWx operator%(const wxLongLongWx& ll) const;
408
8b81872f
VZ
409 void Divide(const wxLongLongWx& divisor,
410 wxLongLongWx& quotient,
411 wxLongLongWx& remainder) const;
412
413 // input/output
cd0b1709 414#if wxUSE_STD_IOSTREAM
162b0c3b 415 friend ostream& operator<<(ostream&, const wxLongLongWx&);
fcc3d7cb 416#endif // wxUSE_STD_IOSTREAM
8b81872f 417
cd0b1709
VZ
418 void *asArray(void) const;
419
8b81872f
VZ
420private:
421 // long is at least 32 bits, so represent our 64bit number as 2 longs
422
423 long m_hi; // signed bit is in the high part
424 unsigned long m_lo;
425};
426
427#endif // wxUSE_LONGLONG_WX
428
2ea24d9f
VZ
429// ----------------------------------------------------------------------------
430// binary operators
431// ----------------------------------------------------------------------------
432
433inline bool WXDLLEXPORT operator<(long l, const wxLongLong& ll) { return ll > l; }
434inline bool WXDLLEXPORT operator>(long l, const wxLongLong& ll) { return ll > l; }
435inline bool WXDLLEXPORT operator<=(long l, const wxLongLong& ll) { return ll > l; }
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; }
439
440inline wxLongLong WXDLLEXPORT operator+(long l, const wxLongLong& ll) { return ll + l; }
441inline wxLongLong WXDLLEXPORT operator-(long l, const wxLongLong& ll) { return ll - l; }
442
8b81872f 443#endif // _WX_LONGLONG_H