]> git.saurik.com Git - wxWidgets.git/blame_incremental - include/wx/longlong.h
Some Motif fixes; makefile fixes; added wxTransferStreamToFile/FileToStream for wxWin
[wxWidgets.git] / include / wx / longlong.h
... / ...
CommitLineData
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// define this to compile wxLongLongWx in "test" mode: the results of all
29// calculations will be compared with the real results taken from
30// wxLongLongNative -- this is extremely useful to find the bugs in
31// wxLongLongWx class!
32
33//#define wxLONGLONG_TEST_MODE
34
35#ifdef wxLONGLONG_TEST_MODE
36 #define wxUSE_LONGLONG_WX 1
37 #define wxUSE_LONGLONG_NATIVE 1
38#endif // wxLONGLONG_TEST_MODE
39
40// ----------------------------------------------------------------------------
41// decide upon which class we will use
42// ----------------------------------------------------------------------------
43
44// to avoid compilation problems on 64bit machines with ambiguous method calls
45// we will need to define this
46#undef wxLongLongIsLong
47
48// NB: we #define and not typedef wxLongLong_t because we want to be able to
49// use 'unsigned wxLongLong_t' as well and because we use "#ifdef
50// wxLongLong_t" below
51#if defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
52 #define wxLongLong_t long
53 #define wxLongLongIsLong
54#elif (defined(__VISUALC__) && defined(__WIN32__)) || defined( __VMS__ )
55 #define wxLongLong_t __int64
56#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x530)
57 #define wxLongLong_t __int64
58#elif defined(__GNUG__)
59 #define wxLongLong_t long long
60#elif defined(__MWERKS__)
61 #if __option(longlong)
62 #define wxLongLong_t long long
63 #else
64 #error "The 64 bit integer support in CodeWarrior has been disabled."
65 #error "See the documentation on the 'longlong' pragma."
66 #endif
67#elif defined(__VISAGECPP__) && __IBMCPP__ >= 400
68 #define wxLongLong_t long long
69#else // no native long long type
70 // both warning and pragma warning are not portable, but at least an
71 // unknown pragma should never be an error.
72 // Err, actually, Watcom C++ doesn't like it.
73 // (well, if the compilers are _that_ broken, I'm removing it (VZ))
74#if 0 //ndef __WATCOMC__
75 #pragma warning "Your compiler does not appear to support 64 bit "\
76 "integers, using emulation class instead."
77#endif
78
79 #define wxUSE_LONGLONG_WX 1
80#endif // compiler
81
82// the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
83// to disable automatic testing (useful for the test program which defines
84// both classes) but by default we only use one class
85#if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
86 // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
87 // this is useful in test programs nad only there
88 #ifndef wxUSE_LONGLONG_NATIVE
89 #define wxUSE_LONGLONG_NATIVE 0
90 #endif
91
92 class WXDLLEXPORT wxLongLongWx;
93 typedef wxLongLongWx wxLongLong;
94#else
95 // if nothing is defined, use native implementation by default, of course
96 #ifndef wxUSE_LONGLONG_NATIVE
97 #define wxUSE_LONGLONG_NATIVE 1
98 #endif
99#endif
100
101#ifndef wxUSE_LONGLONG_WX
102 #define wxUSE_LONGLONG_WX 0
103 class WXDLLEXPORT wxLongLongNative;
104 typedef wxLongLongNative wxLongLong;
105#endif
106
107// NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
108// typedef wxLongLong as it wants, we don't do it
109
110// ----------------------------------------------------------------------------
111// choose the appropriate class
112// ----------------------------------------------------------------------------
113
114// we use iostream for wxLongLong output
115#include "wx/ioswrap.h"
116
117#if wxUSE_LONGLONG_NATIVE
118
119class WXDLLEXPORT wxLongLongNative
120{
121public:
122 // ctors
123 // default ctor initializes to 0
124 wxLongLongNative() { m_ll = 0; }
125 // from long long
126 wxLongLongNative(wxLongLong_t ll) { m_ll = ll; }
127 // from 2 longs
128 wxLongLongNative(long hi, unsigned long lo)
129 {
130 // assign first to avoid precision loss!
131 m_ll = ((wxLongLong_t) hi) << 32;
132 m_ll |= (wxLongLong_t) lo;
133 }
134
135 // default copy ctor is ok
136
137 // no dtor
138
139 // assignment operators
140 // from native 64 bit integer
141 wxLongLongNative& operator=(wxLongLong_t ll)
142 { m_ll = ll; return *this; }
143
144 // from double: this one has an explicit name because otherwise we
145 // would have ambiguity with "ll = int" and also because we don't want
146 // to have implicit conversions between doubles and wxLongLongs
147 wxLongLongNative& Assign(double d)
148 { m_ll = (wxLongLong_t)d; return *this; }
149
150 // assignment operators from wxLongLongNative is ok
151
152 // accessors
153 // get high part
154 long GetHi() const
155 { return (long)(m_ll >> 32); }
156 // get low part
157 unsigned long GetLo() const
158 { return (unsigned long)m_ll; }
159
160 // get absolute value
161 wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
162 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
163
164 // convert to native long long
165 wxLongLong_t GetValue() const { return m_ll; }
166
167 // convert to long with range checking in the debug mode (only!)
168 long ToLong() const
169 {
170 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
171 _T("wxLongLong to long conversion loss of precision") );
172
173 return (long)m_ll;
174 }
175
176 // don't provide implicit conversion to wxLongLong_t or we will have an
177 // ambiguity for all arithmetic operations
178 //operator wxLongLong_t() const { return m_ll; }
179
180 // operations
181 // addition
182 wxLongLongNative operator+(const wxLongLongNative& ll) const
183 { return wxLongLongNative(m_ll + ll.m_ll); }
184 wxLongLongNative& operator+=(const wxLongLongNative& ll)
185 { m_ll += ll.m_ll; return *this; }
186
187 wxLongLongNative operator+(const wxLongLong_t ll) const
188 { return wxLongLongNative(m_ll + ll); }
189 wxLongLongNative& operator+=(const wxLongLong_t ll)
190 { m_ll += ll; return *this; }
191
192 // pre increment
193 wxLongLongNative& operator++()
194 { m_ll++; return *this; }
195
196 // post increment
197 wxLongLongNative& operator++(int)
198 { m_ll++; return *this; }
199
200 // negation operator
201 wxLongLongNative operator-() const
202 { return wxLongLongNative(-m_ll); }
203
204 // subtraction
205 wxLongLongNative operator-(const wxLongLongNative& ll) const
206 { return wxLongLongNative(m_ll - ll.m_ll); }
207 wxLongLongNative& operator-=(const wxLongLongNative& ll)
208 { m_ll -= ll.m_ll; return *this; }
209
210 wxLongLongNative operator-(const wxLongLong_t ll) const
211 { return wxLongLongNative(m_ll - ll); }
212 wxLongLongNative& operator-=(const wxLongLong_t ll)
213 { m_ll -= ll; return *this; }
214
215 // pre decrement
216 wxLongLongNative& operator--()
217 { m_ll--; return *this; }
218
219 // post decrement
220 wxLongLongNative& operator--(int)
221 { m_ll--; return *this; }
222
223 // shifts
224 // left shift
225 wxLongLongNative operator<<(int shift) const
226 { return wxLongLongNative(m_ll << shift);; }
227 wxLongLongNative& operator<<=(int shift)
228 { m_ll <<= shift; return *this; }
229
230 // right shift
231 wxLongLongNative operator>>(int shift) const
232 { return wxLongLongNative(m_ll >> shift);; }
233 wxLongLongNative& operator>>=(int shift)
234 { m_ll >>= shift; return *this; }
235
236 // bitwise operators
237 wxLongLongNative operator&(const wxLongLongNative& ll) const
238 { return wxLongLongNative(m_ll & ll.m_ll); }
239 wxLongLongNative& operator&=(const wxLongLongNative& ll)
240 { m_ll &= ll.m_ll; return *this; }
241
242 wxLongLongNative operator|(const wxLongLongNative& ll) const
243 { return wxLongLongNative(m_ll | ll.m_ll); }
244 wxLongLongNative& operator|=(const wxLongLongNative& ll)
245 { m_ll |= ll.m_ll; return *this; }
246
247 wxLongLongNative operator^(const wxLongLongNative& ll) const
248 { return wxLongLongNative(m_ll ^ ll.m_ll); }
249 wxLongLongNative& operator^=(const wxLongLongNative& ll)
250 { m_ll ^= ll.m_ll; return *this; }
251
252 // multiplication/division
253 wxLongLongNative operator*(const wxLongLongNative& ll) const
254 { return wxLongLongNative(m_ll * ll.m_ll); }
255 wxLongLongNative operator*(long l) const
256 { return wxLongLongNative(m_ll * l); }
257 wxLongLongNative& operator*=(const wxLongLongNative& ll)
258 { m_ll *= ll.m_ll; return *this; }
259 wxLongLongNative& operator*=(long l)
260 { m_ll *= l; return *this; }
261
262 wxLongLongNative operator/(const wxLongLongNative& ll) const
263 { return wxLongLongNative(m_ll / ll.m_ll); }
264 wxLongLongNative operator/(long l) const
265 { return wxLongLongNative(m_ll / l); }
266 wxLongLongNative& operator/=(const wxLongLongNative& ll)
267 { m_ll /= ll.m_ll; return *this; }
268 wxLongLongNative& operator/=(long l)
269 { m_ll /= l; return *this; }
270
271 wxLongLongNative operator%(const wxLongLongNative& ll) const
272 { return wxLongLongNative(m_ll % ll.m_ll); }
273 wxLongLongNative operator%(long l) const
274 { return wxLongLongNative(m_ll % l); }
275
276 // comparison
277 bool operator==(const wxLongLongNative& ll) const
278 { return m_ll == ll.m_ll; }
279 bool operator==(long l) const
280 { return m_ll == l; }
281 bool operator!=(const wxLongLongNative& ll) const
282 { return m_ll != ll.m_ll; }
283 bool operator!=(long l) const
284 { return m_ll != l; }
285 bool operator<(const wxLongLongNative& ll) const
286 { return m_ll < ll.m_ll; }
287 bool operator<(long l) const
288 { return m_ll < l; }
289 bool operator>(const wxLongLongNative& ll) const
290 { return m_ll > ll.m_ll; }
291 bool operator>(long l) const
292 { return m_ll > l; }
293 bool operator<=(const wxLongLongNative& ll) const
294 { return m_ll <= ll.m_ll; }
295 bool operator<=(long l) const
296 { return m_ll <= l; }
297 bool operator>=(const wxLongLongNative& ll) const
298 { return m_ll >= ll.m_ll; }
299 bool operator>=(long l) const
300 { return m_ll >= l; }
301
302 // miscellaneous
303 // conversion to byte array: returns a pointer to static buffer!
304 void *asArray() const;
305
306#if wxUSE_STD_IOSTREAM
307 // input/output
308 friend ostream& operator<<(ostream&, const wxLongLongNative&);
309#endif
310
311private:
312 wxLongLong_t m_ll;
313};
314
315#endif // wxUSE_LONGLONG_NATIVE
316
317#if wxUSE_LONGLONG_WX
318
319class WXDLLEXPORT wxLongLongWx
320{
321public:
322 // ctors
323 // default ctor initializes to 0
324 wxLongLongWx()
325 {
326 m_lo = m_hi = 0;
327
328#ifdef wxLONGLONG_TEST_MODE
329 m_ll = 0;
330
331 Check();
332#endif // wxLONGLONG_TEST_MODE
333 }
334 // from long
335 wxLongLongWx(long l) { *this = l; }
336 // from 2 longs
337 wxLongLongWx(long hi, unsigned long lo)
338 {
339 m_hi = hi;
340 m_lo = lo;
341
342#ifdef wxLONGLONG_TEST_MODE
343 m_ll = hi;
344 m_ll <<= 32;
345 m_ll |= lo;
346
347 Check();
348#endif // wxLONGLONG_TEST_MODE
349 }
350
351 // default copy ctor is ok in both cases
352
353 // no dtor
354
355 // assignment operators
356 // from long
357 wxLongLongWx& operator=(long l)
358 {
359 m_lo = l;
360 m_hi = (l < 0 ? -1l : 0l);
361
362#ifdef wxLONGLONG_TEST_MODE
363 m_ll = l;
364
365 Check();
366#endif // wxLONGLONG_TEST_MODE
367
368 return *this;
369 }
370 // from double
371 wxLongLongWx& Assign(double d);
372 // can't have assignment operator from 2 longs
373
374 // accessors
375 // get high part
376 long GetHi() const { return m_hi; }
377 // get low part
378 unsigned long GetLo() const { return m_lo; }
379
380 // get absolute value
381 wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
382 wxLongLongWx& Abs()
383 {
384 if ( m_hi < 0 )
385 m_hi = -m_hi;
386
387#ifdef wxLONGLONG_TEST_MODE
388 if ( m_ll < 0 )
389 m_ll = -m_ll;
390
391 Check();
392#endif // wxLONGLONG_TEST_MODE
393
394 return *this;
395 }
396
397 // convert to long with range checking in the debug mode (only!)
398 long ToLong() const
399 {
400 wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
401 _T("wxLongLong to long conversion loss of precision") );
402
403 return (long)m_lo;
404 }
405
406 // operations
407 // addition
408 wxLongLongWx operator+(const wxLongLongWx& ll) const;
409 wxLongLongWx& operator+=(const wxLongLongWx& ll);
410 wxLongLongWx operator+(long l) const;
411 wxLongLongWx& operator+=(long l);
412
413 // pre increment operator
414 wxLongLongWx& operator++();
415
416 // post increment operator
417 wxLongLongWx& operator++(int) { return ++(*this); }
418
419 // negation operator
420 wxLongLongWx operator-() const;
421 wxLongLongWx& Negate();
422
423 // subraction
424 wxLongLongWx operator-(const wxLongLongWx& ll) const;
425 wxLongLongWx& operator-=(const wxLongLongWx& ll);
426
427 // pre decrement operator
428 wxLongLongWx& operator--();
429
430 // post decrement operator
431 wxLongLongWx& operator--(int) { return --(*this); }
432
433 // shifts
434 // left shift
435 wxLongLongWx operator<<(int shift) const;
436 wxLongLongWx& operator<<=(int shift);
437
438 // right shift
439 wxLongLongWx operator>>(int shift) const;
440 wxLongLongWx& operator>>=(int shift);
441
442 // bitwise operators
443 wxLongLongWx operator&(const wxLongLongWx& ll) const;
444 wxLongLongWx& operator&=(const wxLongLongWx& ll);
445 wxLongLongWx operator|(const wxLongLongWx& ll) const;
446 wxLongLongWx& operator|=(const wxLongLongWx& ll);
447 wxLongLongWx operator^(const wxLongLongWx& ll) const;
448 wxLongLongWx& operator^=(const wxLongLongWx& ll);
449 wxLongLongWx operator~() const;
450
451 // comparison
452 bool operator==(const wxLongLongWx& ll) const
453 { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
454 bool operator!=(const wxLongLongWx& ll) const
455 { return !(*this == ll); }
456 bool operator<(const wxLongLongWx& ll) const;
457 bool operator>(const wxLongLongWx& ll) const;
458 bool operator<=(const wxLongLongWx& ll) const
459 { return *this < ll || *this == ll; }
460 bool operator>=(const wxLongLongWx& ll) const
461 { return *this > ll || *this == ll; }
462
463 // multiplication
464 wxLongLongWx operator*(const wxLongLongWx& ll) const;
465 wxLongLongWx& operator*=(const wxLongLongWx& ll);
466
467 // division
468 wxLongLongWx operator/(const wxLongLongWx& ll) const;
469 wxLongLongWx& operator/=(const wxLongLongWx& ll);
470
471 wxLongLongWx operator%(const wxLongLongWx& ll) const;
472
473 void Divide(const wxLongLongWx& divisor,
474 wxLongLongWx& quotient,
475 wxLongLongWx& remainder) const;
476
477 // input/output
478#if wxUSE_STD_IOSTREAM
479 friend ostream& operator<<(ostream&, const wxLongLongWx&);
480#endif // wxUSE_STD_IOSTREAM
481
482 void *asArray() const;
483
484private:
485 // long is at least 32 bits, so represent our 64bit number as 2 longs
486
487 long m_hi; // signed bit is in the high part
488 unsigned long m_lo;
489
490#ifdef wxLONGLONG_TEST_MODE
491 void Check()
492 {
493 wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
494 }
495
496 wxLongLong_t m_ll;
497#endif // wxLONGLONG_TEST_MODE
498};
499
500#endif // wxUSE_LONGLONG_WX
501
502// ----------------------------------------------------------------------------
503// binary operators
504// ----------------------------------------------------------------------------
505
506inline bool WXDLLEXPORT operator<(long l, const wxLongLong& ll) { return ll > l; }
507inline bool WXDLLEXPORT operator>(long l, const wxLongLong& ll) { return ll > l; }
508inline bool WXDLLEXPORT operator<=(long l, const wxLongLong& ll) { return ll > l; }
509inline bool WXDLLEXPORT operator>=(long l, const wxLongLong& ll) { return ll > l; }
510inline bool WXDLLEXPORT operator==(long l, const wxLongLong& ll) { return ll > l; }
511inline bool WXDLLEXPORT operator!=(long l, const wxLongLong& ll) { return ll > l; }
512
513inline wxLongLong WXDLLEXPORT operator+(long l, const wxLongLong& ll) { return ll + l; }
514inline wxLongLong WXDLLEXPORT operator-(long l, const wxLongLong& ll) { return ll - l; }
515
516#endif // _WX_LONGLONG_H