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