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