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