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