]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
patches for BC++ 5.3 from Ricky Gonzales <gonzales@pyramid3.net>
[wxWidgets.git] / include / wx / longlong.h
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
22 #include "wx/defs.h"
23 #include "wx/wxchar.h"
24 #include "wx/debug.h"
25
26 #include <limits.h> // for LONG_MAX
27
28 // ----------------------------------------------------------------------------
29 // decide upon which class we will use
30 // ----------------------------------------------------------------------------
31
32 // to avoid compilation problems on 64bit machines with ambiguous method calls
33 // we will need to define this
34 #undef wxLongLongIsLong
35
36 // NB: we #define and not typedef wxLongLong_t because we want to be able to
37 // use 'unsigned wxLongLong_t' as well and because we use "#ifdef
38 // wxLongLong_t" below
39 #if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
40 #define wxLongLong_t long
41 #define wxLongLongIsLong
42 #elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
43 #define wxLongLong_t __int64
44 #elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
45 #define wxLongLong_t __int64
46 #elif defined(__GNUG__)
47 #define wxLongLong_t long long
48 #elif defined(__MWERKS__)
49 #if __option(longlong)
50 #define wxLongLong_t long long
51 #else
52 #error "The 64 bit integer support in CodeWarrior has been disabled."
53 #error "See the documentation on the 'longlong' pragma."
54 #endif
55 #else // no native long long type
56 // we don't give warnings for the compilers we know about that they don't
57 // have any 64 bit integer type
58 #if !defined(__VISAGECPP__) && \
59 !defined(__VISUALC__) && \
60 !defined(__BORLANDC__
61 #warning "Your compiler does not appear to support 64 bit integers, "\
62 "using emulation class instead."
63 #endif // known compilers without long long
64
65 #define wxUSE_LONGLONG_WX 1
66 #endif // compiler
67
68 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
69 // to disable automatic testing (useful for the test program which defines
70 // both classes) but by default we only use one class
71 #ifndef wxLongLong_t
72 #undef wxUSE_LONGLONG_NATIVE
73 #define wxUSE_LONGLONG_NATIVE 0
74 class WXDLLEXPORT wxLongLongWx;
75 typedef wxLongLongWx wxLongLong;
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
81 #endif
82
83 #ifndef wxUSE_LONGLONG_WX
84 #define wxUSE_LONGLONG_WX 0
85 class WXDLLEXPORT wxLongLongNative;
86 typedef wxLongLongNative wxLongLong;
87 #endif
88
89 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
90 // typedef wxLongLong as it wants, we don't do it
91
92 // ----------------------------------------------------------------------------
93 // choose the appropriate class
94 // ----------------------------------------------------------------------------
95
96 // we use iostream for wxLongLong output
97 #include "wx/ioswrap.h"
98
99 #if wxUSE_LONGLONG_NATIVE
100
101 class WXDLLEXPORT wxLongLongNative
102 {
103 public:
104 // ctors
105 // default ctor initializes to 0
106 wxLongLongNative() { m_ll = 0; }
107 // from long long
108 wxLongLongNative(wxLongLong_t ll) { m_ll = ll; }
109 // from 2 longs
110 wxLongLongNative(long hi, unsigned long lo)
111 {
112 // assign first to avoid precision loss!
113 m_ll = ((wxLongLong_t) hi) << 32;
114 m_ll |= (wxLongLong_t) lo;
115 }
116
117 // default copy ctor is ok
118
119 // no dtor
120
121 // assignment operators
122 // from native 64 bit integer
123 wxLongLongNative& operator=(wxLongLong_t ll)
124 { m_ll = ll; return *this; }
125
126 // from double: this one has an explicit name because otherwise we
127 // would have ambiguity with "ll = int" and also because we don't want
128 // to have implicit conversions between doubles and wxLongLongs
129 wxLongLongNative& Assign(double d)
130 { m_ll = (wxLongLong_t)d; return *this; }
131
132 // assignment operators from wxLongLongNative is ok
133
134 // accessors
135 // get high part
136 long GetHi() const
137 { return (long)(m_ll >> 32); }
138 // get low part
139 unsigned long GetLo() const
140 { return (unsigned long)m_ll; }
141
142 // get absolute value
143 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
144
145 // convert to native long long
146 wxLongLong_t GetValue() const { return m_ll; }
147
148 // convert to long with range checking in the debug mode (only!)
149 long ToLong() const
150 {
151 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
152 _T("wxLongLong to long conversion loss of precision") );
153
154 return (long)m_ll;
155 }
156
157 // don't provide implicit conversion to wxLongLong_t or we will have an
158 // ambiguity for all arithmetic operations
159 //operator wxLongLong_t() const { return m_ll; }
160
161 // operations
162 // addition
163 wxLongLongNative operator+(const wxLongLongNative& ll) const
164 { return wxLongLongNative(m_ll + ll.m_ll); }
165 wxLongLongNative& operator+=(const wxLongLongNative& ll)
166 { m_ll += ll.m_ll; return *this; }
167
168 wxLongLongNative operator+(const wxLongLong_t ll) const
169 { return wxLongLongNative(m_ll + ll); }
170 wxLongLongNative& operator+=(const wxLongLong_t ll)
171 { m_ll += ll; return *this; }
172
173 // pre increment
174 wxLongLongNative& operator++()
175 { m_ll++; return *this; }
176
177 // post increment
178 wxLongLongNative& operator++(int)
179 { m_ll++; return *this; }
180
181 // negation operator
182 wxLongLongNative operator-() const
183 { return wxLongLongNative(-m_ll); }
184
185 // subtraction
186 wxLongLongNative operator-(const wxLongLongNative& ll) const
187 { return wxLongLongNative(m_ll - ll.m_ll); }
188 wxLongLongNative& operator-=(const wxLongLongNative& ll)
189 { m_ll -= ll.m_ll; return *this; }
190
191 wxLongLongNative operator-(const wxLongLong_t ll) const
192 { return wxLongLongNative(m_ll - ll); }
193 wxLongLongNative& operator-=(const wxLongLong_t ll)
194 { m_ll -= ll; return *this; }
195
196 // pre decrement
197 wxLongLongNative& operator--()
198 { m_ll--; return *this; }
199
200 // post decrement
201 wxLongLongNative& operator--(int)
202 { m_ll--; return *this; }
203
204 // shifts
205 // left shift
206 wxLongLongNative operator<<(int shift) const
207 { return wxLongLongNative(m_ll << shift);; }
208 wxLongLongNative& operator<<=(int shift)
209 { m_ll <<= shift; return *this; }
210
211 // right shift
212 wxLongLongNative operator>>(int shift) const
213 { return wxLongLongNative(m_ll >> shift);; }
214 wxLongLongNative& operator>>=(int shift)
215 { m_ll >>= shift; return *this; }
216
217 // bitwise operators
218 wxLongLongNative operator&(const wxLongLongNative& ll) const
219 { return wxLongLongNative(m_ll & ll.m_ll); }
220 wxLongLongNative& operator&=(const wxLongLongNative& ll)
221 { m_ll &= ll.m_ll; return *this; }
222
223 wxLongLongNative operator|(const wxLongLongNative& ll) const
224 { return wxLongLongNative(m_ll | ll.m_ll); }
225 wxLongLongNative& operator|=(const wxLongLongNative& ll)
226 { m_ll |= ll.m_ll; return *this; }
227
228 wxLongLongNative operator^(const wxLongLongNative& ll) const
229 { return wxLongLongNative(m_ll ^ ll.m_ll); }
230 wxLongLongNative& operator^=(const wxLongLongNative& ll)
231 { m_ll ^= ll.m_ll; return *this; }
232
233 // multiplication/division
234 wxLongLongNative operator*(const wxLongLongNative& ll) const
235 { return wxLongLongNative(m_ll * ll.m_ll); }
236 wxLongLongNative operator*(long l) const
237 { return wxLongLongNative(m_ll * l); }
238 wxLongLongNative& operator*=(const wxLongLongNative& ll)
239 { m_ll *= ll.m_ll; return *this; }
240 wxLongLongNative& operator*=(long l)
241 { m_ll *= l; return *this; }
242
243 wxLongLongNative operator/(const wxLongLongNative& ll) const
244 { return wxLongLongNative(m_ll / ll.m_ll); }
245 wxLongLongNative operator/(long l) const
246 { return wxLongLongNative(m_ll / l); }
247 wxLongLongNative& operator/=(const wxLongLongNative& ll)
248 { m_ll /= ll.m_ll; return *this; }
249 wxLongLongNative& operator/=(long l)
250 { m_ll /= l; return *this; }
251
252 wxLongLongNative operator%(const wxLongLongNative& ll) const
253 { return wxLongLongNative(m_ll % ll.m_ll); }
254 wxLongLongNative operator%(long l) const
255 { return wxLongLongNative(m_ll % l); }
256
257 // comparison
258 bool operator==(const wxLongLongNative& ll) const
259 { return m_ll == ll.m_ll; }
260 bool operator==(long l) const
261 { return m_ll == l; }
262 bool operator!=(const wxLongLongNative& ll) const
263 { return m_ll != ll.m_ll; }
264 bool operator!=(long l) const
265 { return m_ll != l; }
266 bool operator<(const wxLongLongNative& ll) const
267 { return m_ll < ll.m_ll; }
268 bool operator<(long l) const
269 { return m_ll < l; }
270 bool operator>(const wxLongLongNative& ll) const
271 { return m_ll > ll.m_ll; }
272 bool operator>(long l) const
273 { return m_ll > l; }
274 bool operator<=(const wxLongLongNative& ll) const
275 { return m_ll <= ll.m_ll; }
276 bool operator<=(long l) const
277 { return m_ll <= l; }
278 bool operator>=(const wxLongLongNative& ll) const
279 { return m_ll >= ll.m_ll; }
280 bool operator>=(long l) const
281 { return m_ll >= l; }
282
283 // miscellaneous
284 // conversion to byte array: returns a pointer to static buffer!
285 void *asArray() const;
286
287 #if wxUSE_STD_IOSTREAM
288 // input/output
289 friend ostream& operator<<(ostream&, const wxLongLongNative&);
290 #endif
291
292 private:
293 wxLongLong_t m_ll;
294 };
295
296 #endif // wxUSE_LONGLONG_NATIVE
297
298 #if wxUSE_LONGLONG_WX
299
300 class WXDLLEXPORT wxLongLongWx
301 {
302 public:
303 // ctors
304 // default ctor initializes to 0
305 wxLongLongWx() { m_lo = m_hi = 0; }
306 // from long
307 wxLongLongWx(long l)
308 { m_lo = l; m_hi = (l < 0 ? -1l : 0l); }
309 // from 2 longs
310 wxLongLongWx(long hi, unsigned long lo)
311 { m_hi = hi; m_lo = lo; }
312
313 // default copy ctor is ok in both cases
314
315 // no dtor
316
317 // assignment operators
318 // from long
319 wxLongLongWx& operator=(long l)
320 { m_lo = l; m_hi = (l < 0 ? -1l : 0l); return *this; }
321 // from double
322 wxLongLongWx& Assign(double d);
323 // can't have assignment operator from 2 longs
324
325 // accessors
326 // get high part
327 long GetHi() const { return m_hi; }
328 // get low part
329 unsigned long GetLo() const { return m_lo; }
330
331 // get absolute value
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
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
388 bool operator==(const wxLongLongWx& ll) const;
389 bool operator!=(const wxLongLongWx& ll) const;
390 bool operator<(const wxLongLongWx& ll) const;
391 bool operator>(const wxLongLongWx& ll) const;
392 bool operator<=(const wxLongLongWx& ll) const;
393 bool operator>=(const wxLongLongWx& ll) const;
394
395 // multiplication
396 wxLongLongWx operator*(const wxLongLongWx& ll) const;
397 wxLongLongWx& operator*=(const wxLongLongWx& ll);
398
399 // division
400 wxLongLongWx operator/(const wxLongLongWx& ll) const;
401 wxLongLongWx& operator/=(const wxLongLongWx& ll);
402
403 wxLongLongWx operator%(const wxLongLongWx& ll) const;
404
405 void Divide(const wxLongLongWx& divisor,
406 wxLongLongWx& quotient,
407 wxLongLongWx& remainder) const;
408
409 // input/output
410 #if wxUSE_STD_IOSTREAM
411 friend ostream& operator<<(ostream&, const wxLongLongWx&);
412 #endif // wxUSE_STD_IOSTREAM
413
414 void *asArray(void) const;
415
416 private:
417 // long is at least 32 bits, so represent our 64bit number as 2 longs
418
419 long m_hi; // signed bit is in the high part
420 unsigned long m_lo;
421 };
422
423 #endif // wxUSE_LONGLONG_WX
424
425 #endif // _WX_LONGLONG_H