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