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