]> git.saurik.com Git - wxWidgets.git/blob - include/wx/longlong.h
02f7ded9ea05345ed4ee5b4e8cce6ccb5f9e343b
[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(__WIN16__)
43 #define wxLongLong_t long
44 #define wxLongLongIsLong
45 #elif defined(__VISUALC__) || defined( __VMS__ )
46 #define wxLongLong_t __int64
47 #elif defined(__GNUG__)
48 #define wxLongLong_t long long
49 #elif defined(__MWERKS__)
50 #if __option(longlong)
51 #define wxLongLong_t long long
52 #else
53 #error "The 64 bit integer support in CodeWarrior has been disabled."
54 #error "See the documentation on the 'longlong' pragma."
55 #endif
56 #else
57 #if !defined(__VISAGECPP__)
58 // Visualage does not support this pragma
59 #warning "Your compiler does not appear to support 64 bit integers, "\
60 "using emulation class instead."
61 #endif
62 #define wxUSE_LONGLONG_WX 1
63 #endif // compiler
64
65 // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
66 // to disable automatic testing (useful for the test program which defines
67 // both classes) but by default we only use one class
68 #ifndef wxLongLong_t
69 #undef wxUSE_LONGLONG_NATIVE
70 #define wxUSE_LONGLONG_NATIVE 0
71 class WXDLLEXPORT wxLongLongWx;
72 typedef wxLongLongWx wxLongLong;
73 #else
74 // if nothing is defined, use native implementation by default, of course
75 #ifndef wxUSE_LONGLONG_NATIVE
76 #define wxUSE_LONGLONG_NATIVE 1
77 #endif
78 #endif
79
80 #ifndef wxUSE_LONGLONG_WX
81 #define wxUSE_LONGLONG_WX 0
82 class WXDLLEXPORT wxLongLongNative;
83 typedef wxLongLongNative wxLongLong;
84 #endif
85
86 // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
87 // typedef wxLongLong as it wants, we don't do it
88
89 // ----------------------------------------------------------------------------
90 // choose the appropriate class
91 // ----------------------------------------------------------------------------
92
93 // we use iostream for wxLongLong output
94 #include "wx/ioswrap.h"
95
96 #if wxUSE_LONGLONG_NATIVE
97
98 class WXDLLEXPORT wxLongLongNative
99 {
100 public:
101 // ctors
102 // default ctor initializes to 0
103 wxLongLongNative() { m_ll = 0; }
104 // from long long
105 wxLongLongNative(wxLongLong_t ll) { m_ll = ll; }
106 // from 2 longs
107 wxLongLongNative(long hi, unsigned long lo)
108 {
109 // assign first to avoid precision loss!
110 m_ll = ((wxLongLong_t) hi) << 32;
111 m_ll |= (wxLongLong_t) lo;
112 }
113
114 // default copy ctor is ok
115
116 // no dtor
117
118 // assignment operators
119 // from native 64 bit integer
120 wxLongLongNative& operator=(wxLongLong_t ll)
121 { m_ll = ll; return *this; }
122
123 // assignment operators from wxLongLongNative is ok
124
125 // accessors
126 // get high part
127 long GetHi() const
128 { return (long)(m_ll >> 32); }
129 // get low part
130 unsigned long GetLo() const
131 { return (unsigned long)m_ll; }
132
133 // get absolute value
134 wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
135
136 // convert to native long long
137 wxLongLong_t GetValue() const { return m_ll; }
138
139 // convert to long with range checking in the debug mode (only!)
140 long ToLong() const
141 {
142 wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
143 _T("wxLongLong to long conversion loss of precision") );
144
145 return (long)m_ll;
146 }
147
148 // don't provide implicit conversion to wxLongLong_t or we will have an
149 // ambiguity for all arithmetic operations
150 //operator wxLongLong_t() const { return m_ll; }
151
152 // operations
153 // addition
154 wxLongLongNative operator+(const wxLongLongNative& ll) const
155 { return wxLongLongNative(m_ll + ll.m_ll); }
156 wxLongLongNative& operator+=(const wxLongLongNative& ll)
157 { m_ll += ll.m_ll; return *this; }
158
159 wxLongLongNative operator+(const wxLongLong_t ll) const
160 { return wxLongLongNative(m_ll + ll); }
161 wxLongLongNative& operator+=(const wxLongLong_t ll)
162 { m_ll += ll; return *this; }
163
164 // pre increment
165 wxLongLongNative& operator++()
166 { m_ll++; return *this; }
167
168 // post increment
169 wxLongLongNative& operator++(int)
170 { m_ll++; return *this; }
171
172 // negation operator
173 wxLongLongNative operator-() const
174 { return wxLongLongNative(-m_ll); }
175
176 // subtraction
177 wxLongLongNative operator-(const wxLongLongNative& ll) const
178 { return wxLongLongNative(m_ll - ll.m_ll); }
179 wxLongLongNative& operator-=(const wxLongLongNative& ll)
180 { m_ll -= ll.m_ll; return *this; }
181
182 wxLongLongNative operator-(const wxLongLong_t ll) const
183 { return wxLongLongNative(m_ll - ll); }
184 wxLongLongNative& operator-=(const wxLongLong_t ll)
185 { m_ll -= ll; return *this; }
186
187 // pre decrement
188 wxLongLongNative& operator--()
189 { m_ll--; return *this; }
190
191 // post decrement
192 wxLongLongNative& operator--(int)
193 { m_ll--; return *this; }
194
195 // shifts
196 // left shift
197 wxLongLongNative operator<<(int shift) const
198 { return wxLongLongNative(m_ll << shift);; }
199 wxLongLongNative& operator<<=(int shift)
200 { m_ll <<= shift; return *this; }
201
202 // right shift
203 wxLongLongNative operator>>(int shift) const
204 { return wxLongLongNative(m_ll >> shift);; }
205 wxLongLongNative& operator>>=(int shift)
206 { m_ll >>= shift; return *this; }
207
208 // bitwise operators
209 wxLongLongNative operator&(const wxLongLongNative& ll) const
210 { return wxLongLongNative(m_ll & ll.m_ll); }
211 wxLongLongNative& operator&=(const wxLongLongNative& ll)
212 { m_ll &= ll.m_ll; return *this; }
213
214 wxLongLongNative operator|(const wxLongLongNative& ll) const
215 { return wxLongLongNative(m_ll | ll.m_ll); }
216 wxLongLongNative& operator|=(const wxLongLongNative& ll)
217 { m_ll |= ll.m_ll; return *this; }
218
219 wxLongLongNative operator^(const wxLongLongNative& ll) const
220 { return wxLongLongNative(m_ll ^ ll.m_ll); }
221 wxLongLongNative& operator^=(const wxLongLongNative& ll)
222 { m_ll ^= ll.m_ll; return *this; }
223
224 // multiplication/division
225 wxLongLongNative operator*(const wxLongLongNative& ll) const
226 { return wxLongLongNative(m_ll * ll.m_ll); }
227 wxLongLongNative operator*(long l) const
228 { return wxLongLongNative(m_ll * l); }
229 wxLongLongNative& operator*=(const wxLongLongNative& ll)
230 { m_ll *= ll.m_ll; return *this; }
231 wxLongLongNative& operator*=(long l)
232 { m_ll *= l; return *this; }
233
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
248 // comparison
249 bool operator==(const wxLongLongNative& ll) const
250 { return m_ll == ll.m_ll; }
251 bool operator==(long l) const
252 { return m_ll == l; }
253 bool operator!=(const wxLongLongNative& ll) const
254 { return m_ll != ll.m_ll; }
255 bool operator!=(long l) const
256 { return m_ll != l; }
257 bool operator<(const wxLongLongNative& ll) const
258 { return m_ll < ll.m_ll; }
259 bool operator<(long l) const
260 { return m_ll < l; }
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
274 // miscellaneous
275 // conversion to byte array: returns a pointer to static buffer!
276 void *asArray() const;
277
278 #if wxUSE_STD_IOSTREAM
279 // input/output
280 friend ostream& operator<<(ostream&, const wxLongLongNative&);
281 #endif
282
283 private:
284 wxLongLong_t m_ll;
285 };
286
287 #endif // wxUSE_LONGLONG_NATIVE
288
289 #if wxUSE_LONGLONG_WX
290
291 class WXDLLEXPORT wxLongLongWx
292 {
293 public:
294 // ctors
295 // default ctor initializes to 0
296 wxLongLongWx() { m_lo = m_hi = 0; }
297 // from long
298 wxLongLongWx(long l)
299 { m_lo = l; m_hi = (l < 0 ? -1l : 0l); }
300 // from 2 longs
301 wxLongLongWx(long hi, unsigned long lo)
302 { m_hi = hi; m_lo = lo; }
303
304 // default copy ctor is ok in both cases
305
306 // no dtor
307
308 // assignment operators
309 // from long
310 wxLongLongWx& operator=(long l)
311 { m_lo = l; m_hi = (l < 0 ? -1l : 0l); return *this; }
312 // can't have assignment operator from 2 longs
313
314 // accessors
315 // get high part
316 long GetHi() const { return m_hi; }
317 // get low part
318 unsigned long GetLo() const { return m_lo; }
319
320 // operations
321 // addition
322 wxLongLongWx operator+(const wxLongLongWx& ll) const;
323 wxLongLongWx& operator+=(const wxLongLongWx& ll);
324 wxLongLongWx operator+(long l) const;
325 wxLongLongWx& operator+=(long l);
326
327 // pre increment operator
328 wxLongLongWx& operator++();
329
330 // post increment operator
331 wxLongLongWx& operator++(int);
332
333 // negation operator
334 wxLongLongWx operator-() const;
335
336 // subraction
337 wxLongLongWx operator-(const wxLongLongWx& ll) const;
338 wxLongLongWx& operator-=(const wxLongLongWx& ll);
339
340 // pre decrement operator
341 wxLongLongWx& operator--();
342
343 // post decrement operator
344 wxLongLongWx& operator--(int);
345
346 // shifts
347 // left shift
348 wxLongLongWx operator<<(int shift) const;
349 wxLongLongWx& operator<<=(int shift);
350
351 // right shift
352 wxLongLongWx operator>>(int shift) const;
353 wxLongLongWx& operator>>=(int shift);
354
355 // bitwise operators
356 wxLongLongWx operator&(const wxLongLongWx& ll) const;
357 wxLongLongWx& operator&=(const wxLongLongWx& ll);
358 wxLongLongWx operator|(const wxLongLongWx& ll) const;
359 wxLongLongWx& operator|=(const wxLongLongWx& ll);
360 wxLongLongWx operator^(const wxLongLongWx& ll) const;
361 wxLongLongWx& operator^=(const wxLongLongWx& ll);
362 wxLongLongWx operator~() const;
363
364 // comparison
365 bool operator==(const wxLongLongWx& ll) const;
366 bool operator!=(const wxLongLongWx& ll) const;
367 bool operator<(const wxLongLongWx& ll) const;
368 bool operator>(const wxLongLongWx& ll) const;
369 bool operator<=(const wxLongLongWx& ll) const;
370 bool operator>=(const wxLongLongWx& ll) const;
371
372 // multiplication
373 wxLongLongWx operator*(const wxLongLongWx& ll) const;
374 wxLongLongWx& operator*=(const wxLongLongWx& ll);
375 void *asArray(void) const;
376
377 // division
378 void Divide(const wxLongLongWx& divisor,
379 wxLongLongWx& quotient,
380 wxLongLongWx& remainder) const;
381
382 #if wxUSE_STD_IOSTREAM
383 // input/output
384 friend ostream& operator<<(ostream&, const wxLongLongWx&);
385 #endif // wxUSE_STD_IOSTREAM
386
387 private:
388 // long is at least 32 bits, so represent our 64bit number as 2 longs
389
390 long m_hi; // signed bit is in the high part
391 unsigned long m_lo;
392 };
393
394 #endif // wxUSE_LONGLONG_WX
395
396 #endif // _WX_LONGLONG_H