longlong.h 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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. // Modified by:
  7. // Created: 10.02.99
  8. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_LONGLONG_H
  12. #define _WX_LONGLONG_H
  13. #include "wx/defs.h"
  14. #if wxUSE_LONGLONG
  15. #include "wx/string.h"
  16. #include <limits.h> // for LONG_MAX
  17. // define this to compile wxLongLongWx in "test" mode: the results of all
  18. // calculations will be compared with the real results taken from
  19. // wxLongLongNative -- this is extremely useful to find the bugs in
  20. // wxLongLongWx class!
  21. // #define wxLONGLONG_TEST_MODE
  22. #ifdef wxLONGLONG_TEST_MODE
  23. #define wxUSE_LONGLONG_WX 1
  24. #define wxUSE_LONGLONG_NATIVE 1
  25. #endif // wxLONGLONG_TEST_MODE
  26. // ----------------------------------------------------------------------------
  27. // decide upon which class we will use
  28. // ----------------------------------------------------------------------------
  29. #ifndef wxLongLong_t
  30. // both warning and pragma warning are not portable, but at least an
  31. // unknown pragma should never be an error -- except that, actually, some
  32. // broken compilers don't like it, so we have to disable it in this case
  33. // <sigh>
  34. #ifdef __GNUC__
  35. #warning "Your compiler does not appear to support 64 bit "\
  36. "integers, using emulation class instead.\n" \
  37. "Please report your compiler version to " \
  38. "wx-dev@lists.wxwidgets.org!"
  39. #elif !(defined(__WATCOMC__) || defined(__VISAGECPP__))
  40. #pragma warning "Your compiler does not appear to support 64 bit "\
  41. "integers, using emulation class instead.\n" \
  42. "Please report your compiler version to " \
  43. "wx-dev@lists.wxwidgets.org!"
  44. #endif
  45. #define wxUSE_LONGLONG_WX 1
  46. #endif // compiler
  47. // the user may predefine wxUSE_LONGLONG_NATIVE and/or wxUSE_LONGLONG_NATIVE
  48. // to disable automatic testing (useful for the test program which defines
  49. // both classes) but by default we only use one class
  50. #if (defined(wxUSE_LONGLONG_WX) && wxUSE_LONGLONG_WX) || !defined(wxLongLong_t)
  51. // don't use both classes unless wxUSE_LONGLONG_NATIVE was explicitly set:
  52. // this is useful in test programs and only there
  53. #ifndef wxUSE_LONGLONG_NATIVE
  54. #define wxUSE_LONGLONG_NATIVE 0
  55. #endif
  56. class WXDLLIMPEXP_FWD_BASE wxLongLongWx;
  57. class WXDLLIMPEXP_FWD_BASE wxULongLongWx;
  58. #if defined(__VISUALC__) && !defined(__WIN32__)
  59. #define wxLongLong wxLongLongWx
  60. #define wxULongLong wxULongLongWx
  61. #else
  62. typedef wxLongLongWx wxLongLong;
  63. typedef wxULongLongWx wxULongLong;
  64. #endif
  65. #else
  66. // if nothing is defined, use native implementation by default, of course
  67. #ifndef wxUSE_LONGLONG_NATIVE
  68. #define wxUSE_LONGLONG_NATIVE 1
  69. #endif
  70. #endif
  71. #ifndef wxUSE_LONGLONG_WX
  72. #define wxUSE_LONGLONG_WX 0
  73. class WXDLLIMPEXP_FWD_BASE wxLongLongNative;
  74. class WXDLLIMPEXP_FWD_BASE wxULongLongNative;
  75. typedef wxLongLongNative wxLongLong;
  76. typedef wxULongLongNative wxULongLong;
  77. #endif
  78. // NB: if both wxUSE_LONGLONG_WX and NATIVE are defined, the user code should
  79. // typedef wxLongLong as it wants, we don't do it
  80. // ----------------------------------------------------------------------------
  81. // choose the appropriate class
  82. // ----------------------------------------------------------------------------
  83. // we use iostream for wxLongLong output
  84. #include "wx/iosfwrap.h"
  85. #if wxUSE_LONGLONG_NATIVE
  86. class WXDLLIMPEXP_BASE 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(wxInt32 hi, wxUint32 lo)
  96. {
  97. // cast to wxLongLong_t first to avoid precision loss!
  98. m_ll = ((wxLongLong_t) hi) << 32;
  99. m_ll |= (wxLongLong_t) lo;
  100. }
  101. #if wxUSE_LONGLONG_WX
  102. wxLongLongNative(wxLongLongWx ll);
  103. #endif
  104. // default copy ctor is ok
  105. // no dtor
  106. // assignment operators
  107. // from native 64 bit integer
  108. #ifndef wxLongLongIsLong
  109. wxLongLongNative& operator=(wxLongLong_t ll)
  110. { m_ll = ll; return *this; }
  111. wxLongLongNative& operator=(wxULongLong_t ll)
  112. { m_ll = ll; return *this; }
  113. #endif // !wxLongLongNative
  114. wxLongLongNative& operator=(const wxULongLongNative &ll);
  115. wxLongLongNative& operator=(int l)
  116. { m_ll = l; return *this; }
  117. wxLongLongNative& operator=(long l)
  118. { m_ll = l; return *this; }
  119. wxLongLongNative& operator=(unsigned int l)
  120. { m_ll = l; return *this; }
  121. wxLongLongNative& operator=(unsigned long l)
  122. { m_ll = l; return *this; }
  123. #if wxUSE_LONGLONG_WX
  124. wxLongLongNative& operator=(wxLongLongWx ll);
  125. wxLongLongNative& operator=(const class wxULongLongWx &ll);
  126. #endif
  127. // from double: this one has an explicit name because otherwise we
  128. // would have ambiguity with "ll = int" and also because we don't want
  129. // to have implicit conversions between doubles and wxLongLongs
  130. wxLongLongNative& Assign(double d)
  131. { m_ll = (wxLongLong_t)d; return *this; }
  132. // assignment operators from wxLongLongNative is ok
  133. // accessors
  134. // get high part
  135. wxInt32 GetHi() const
  136. { return wx_truncate_cast(wxInt32, m_ll >> 32); }
  137. // get low part
  138. wxUint32 GetLo() const
  139. { return wx_truncate_cast(wxUint32, m_ll); }
  140. // get absolute value
  141. wxLongLongNative Abs() const { return wxLongLongNative(*this).Abs(); }
  142. wxLongLongNative& Abs() { if ( m_ll < 0 ) m_ll = -m_ll; return *this; }
  143. // convert to native long long
  144. wxLongLong_t GetValue() const { return m_ll; }
  145. // convert to long with range checking in debug mode (only!)
  146. long ToLong() const
  147. {
  148. wxASSERT_MSG( (m_ll >= LONG_MIN) && (m_ll <= LONG_MAX),
  149. wxT("wxLongLong to long conversion loss of precision") );
  150. return wx_truncate_cast(long, m_ll);
  151. }
  152. // convert to double
  153. double ToDouble() const { return wx_truncate_cast(double, m_ll); }
  154. // don't provide implicit conversion to wxLongLong_t or we will have an
  155. // ambiguity for all arithmetic operations
  156. //operator wxLongLong_t() const { return m_ll; }
  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. wxLongLongNative operator+(const wxLongLong_t ll) const
  164. { return wxLongLongNative(m_ll + ll); }
  165. wxLongLongNative& operator+=(const wxLongLong_t ll)
  166. { m_ll += ll; return *this; }
  167. // pre increment
  168. wxLongLongNative& operator++()
  169. { m_ll++; return *this; }
  170. // post increment
  171. wxLongLongNative operator++(int)
  172. { wxLongLongNative value(*this); m_ll++; return value; }
  173. // negation operator
  174. wxLongLongNative operator-() const
  175. { return wxLongLongNative(-m_ll); }
  176. wxLongLongNative& Negate() { m_ll = -m_ll; return *this; }
  177. // subtraction
  178. wxLongLongNative operator-(const wxLongLongNative& ll) const
  179. { return wxLongLongNative(m_ll - ll.m_ll); }
  180. wxLongLongNative& operator-=(const wxLongLongNative& ll)
  181. { m_ll -= ll.m_ll; return *this; }
  182. wxLongLongNative operator-(const wxLongLong_t ll) const
  183. { return wxLongLongNative(m_ll - ll); }
  184. wxLongLongNative& operator-=(const wxLongLong_t ll)
  185. { m_ll -= ll; return *this; }
  186. // pre decrement
  187. wxLongLongNative& operator--()
  188. { m_ll--; return *this; }
  189. // post decrement
  190. wxLongLongNative operator--(int)
  191. { wxLongLongNative value(*this); m_ll--; return value; }
  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. // right shift
  199. wxLongLongNative operator>>(int shift) const
  200. { return wxLongLongNative(m_ll >> shift); }
  201. wxLongLongNative& operator>>=(int shift)
  202. { m_ll >>= shift; return *this; }
  203. // bitwise operators
  204. wxLongLongNative operator&(const wxLongLongNative& ll) const
  205. { return wxLongLongNative(m_ll & ll.m_ll); }
  206. wxLongLongNative& operator&=(const wxLongLongNative& ll)
  207. { m_ll &= ll.m_ll; return *this; }
  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. wxLongLongNative operator^(const wxLongLongNative& ll) const
  213. { return wxLongLongNative(m_ll ^ ll.m_ll); }
  214. wxLongLongNative& operator^=(const wxLongLongNative& ll)
  215. { m_ll ^= ll.m_ll; return *this; }
  216. // multiplication/division
  217. wxLongLongNative operator*(const wxLongLongNative& ll) const
  218. { return wxLongLongNative(m_ll * ll.m_ll); }
  219. wxLongLongNative operator*(long l) const
  220. { return wxLongLongNative(m_ll * l); }
  221. wxLongLongNative& operator*=(const wxLongLongNative& ll)
  222. { m_ll *= ll.m_ll; return *this; }
  223. wxLongLongNative& operator*=(long l)
  224. { m_ll *= l; return *this; }
  225. wxLongLongNative operator/(const wxLongLongNative& ll) const
  226. { return wxLongLongNative(m_ll / ll.m_ll); }
  227. wxLongLongNative operator/(long l) const
  228. { return wxLongLongNative(m_ll / l); }
  229. wxLongLongNative& operator/=(const wxLongLongNative& ll)
  230. { m_ll /= ll.m_ll; return *this; }
  231. wxLongLongNative& operator/=(long l)
  232. { m_ll /= l; return *this; }
  233. wxLongLongNative operator%(const wxLongLongNative& ll) const
  234. { return wxLongLongNative(m_ll % ll.m_ll); }
  235. wxLongLongNative operator%(long l) const
  236. { return wxLongLongNative(m_ll % l); }
  237. // comparison
  238. bool operator==(const wxLongLongNative& ll) const
  239. { return m_ll == ll.m_ll; }
  240. bool operator==(long l) const
  241. { return m_ll == l; }
  242. bool operator!=(const wxLongLongNative& ll) const
  243. { return m_ll != ll.m_ll; }
  244. bool operator!=(long l) const
  245. { return m_ll != l; }
  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. // miscellaneous
  263. // return the string representation of this number
  264. wxString ToString() const;
  265. // conversion to byte array: returns a pointer to static buffer!
  266. void *asArray() const;
  267. #if wxUSE_STD_IOSTREAM
  268. // input/output
  269. friend WXDLLIMPEXP_BASE
  270. wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongNative&);
  271. #endif
  272. friend WXDLLIMPEXP_BASE
  273. wxString& operator<<(wxString&, const wxLongLongNative&);
  274. #if wxUSE_STREAMS
  275. friend WXDLLIMPEXP_BASE
  276. class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongNative&);
  277. friend WXDLLIMPEXP_BASE
  278. class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongNative&);
  279. #endif
  280. private:
  281. wxLongLong_t m_ll;
  282. };
  283. class WXDLLIMPEXP_BASE wxULongLongNative
  284. {
  285. public:
  286. // ctors
  287. // default ctor initializes to 0
  288. wxULongLongNative() : m_ll(0) { }
  289. // from long long
  290. wxULongLongNative(wxULongLong_t ll) : m_ll(ll) { }
  291. // from 2 longs
  292. wxULongLongNative(wxUint32 hi, wxUint32 lo) : m_ll(0)
  293. {
  294. // cast to wxLongLong_t first to avoid precision loss!
  295. m_ll = ((wxULongLong_t) hi) << 32;
  296. m_ll |= (wxULongLong_t) lo;
  297. }
  298. #if wxUSE_LONGLONG_WX
  299. wxULongLongNative(const class wxULongLongWx &ll);
  300. #endif
  301. // default copy ctor is ok
  302. // no dtor
  303. // assignment operators
  304. // from native 64 bit integer
  305. #ifndef wxLongLongIsLong
  306. wxULongLongNative& operator=(wxULongLong_t ll)
  307. { m_ll = ll; return *this; }
  308. wxULongLongNative& operator=(wxLongLong_t ll)
  309. { m_ll = ll; return *this; }
  310. #endif // !wxLongLongNative
  311. wxULongLongNative& operator=(int l)
  312. { m_ll = l; return *this; }
  313. wxULongLongNative& operator=(long l)
  314. { m_ll = l; return *this; }
  315. wxULongLongNative& operator=(unsigned int l)
  316. { m_ll = l; return *this; }
  317. wxULongLongNative& operator=(unsigned long l)
  318. { m_ll = l; return *this; }
  319. wxULongLongNative& operator=(const wxLongLongNative &ll)
  320. { m_ll = ll.GetValue(); return *this; }
  321. #if wxUSE_LONGLONG_WX
  322. wxULongLongNative& operator=(wxLongLongWx ll);
  323. wxULongLongNative& operator=(const class wxULongLongWx &ll);
  324. #endif
  325. // assignment operators from wxULongLongNative is ok
  326. // accessors
  327. // get high part
  328. wxUint32 GetHi() const
  329. { return wx_truncate_cast(wxUint32, m_ll >> 32); }
  330. // get low part
  331. wxUint32 GetLo() const
  332. { return wx_truncate_cast(wxUint32, m_ll); }
  333. // convert to native ulong long
  334. wxULongLong_t GetValue() const { return m_ll; }
  335. // convert to ulong with range checking in debug mode (only!)
  336. unsigned long ToULong() const
  337. {
  338. wxASSERT_MSG( m_ll <= ULONG_MAX,
  339. wxT("wxULongLong to long conversion loss of precision") );
  340. return wx_truncate_cast(unsigned long, m_ll);
  341. }
  342. // convert to double
  343. //
  344. // For some completely obscure reasons compiling the cast below with
  345. // VC6 in DLL builds only (!) results in "error C2520: conversion from
  346. // unsigned __int64 to double not implemented, use signed __int64" so
  347. // we must use a different version for that compiler.
  348. #ifdef __VISUALC6__
  349. double ToDouble() const;
  350. #else
  351. double ToDouble() const { return wx_truncate_cast(double, m_ll); }
  352. #endif
  353. // operations
  354. // addition
  355. wxULongLongNative operator+(const wxULongLongNative& ll) const
  356. { return wxULongLongNative(m_ll + ll.m_ll); }
  357. wxULongLongNative& operator+=(const wxULongLongNative& ll)
  358. { m_ll += ll.m_ll; return *this; }
  359. wxULongLongNative operator+(const wxULongLong_t ll) const
  360. { return wxULongLongNative(m_ll + ll); }
  361. wxULongLongNative& operator+=(const wxULongLong_t ll)
  362. { m_ll += ll; return *this; }
  363. // pre increment
  364. wxULongLongNative& operator++()
  365. { m_ll++; return *this; }
  366. // post increment
  367. wxULongLongNative operator++(int)
  368. { wxULongLongNative value(*this); m_ll++; return value; }
  369. // subtraction
  370. wxULongLongNative operator-(const wxULongLongNative& ll) const
  371. { return wxULongLongNative(m_ll - ll.m_ll); }
  372. wxULongLongNative& operator-=(const wxULongLongNative& ll)
  373. { m_ll -= ll.m_ll; return *this; }
  374. wxULongLongNative operator-(const wxULongLong_t ll) const
  375. { return wxULongLongNative(m_ll - ll); }
  376. wxULongLongNative& operator-=(const wxULongLong_t ll)
  377. { m_ll -= ll; return *this; }
  378. // pre decrement
  379. wxULongLongNative& operator--()
  380. { m_ll--; return *this; }
  381. // post decrement
  382. wxULongLongNative operator--(int)
  383. { wxULongLongNative value(*this); m_ll--; return value; }
  384. // shifts
  385. // left shift
  386. wxULongLongNative operator<<(int shift) const
  387. { return wxULongLongNative(m_ll << shift); }
  388. wxULongLongNative& operator<<=(int shift)
  389. { m_ll <<= shift; return *this; }
  390. // right shift
  391. wxULongLongNative operator>>(int shift) const
  392. { return wxULongLongNative(m_ll >> shift); }
  393. wxULongLongNative& operator>>=(int shift)
  394. { m_ll >>= shift; return *this; }
  395. // bitwise operators
  396. wxULongLongNative operator&(const wxULongLongNative& ll) const
  397. { return wxULongLongNative(m_ll & ll.m_ll); }
  398. wxULongLongNative& operator&=(const wxULongLongNative& ll)
  399. { m_ll &= ll.m_ll; return *this; }
  400. wxULongLongNative operator|(const wxULongLongNative& ll) const
  401. { return wxULongLongNative(m_ll | ll.m_ll); }
  402. wxULongLongNative& operator|=(const wxULongLongNative& ll)
  403. { m_ll |= ll.m_ll; return *this; }
  404. wxULongLongNative operator^(const wxULongLongNative& ll) const
  405. { return wxULongLongNative(m_ll ^ ll.m_ll); }
  406. wxULongLongNative& operator^=(const wxULongLongNative& ll)
  407. { m_ll ^= ll.m_ll; return *this; }
  408. // multiplication/division
  409. wxULongLongNative operator*(const wxULongLongNative& ll) const
  410. { return wxULongLongNative(m_ll * ll.m_ll); }
  411. wxULongLongNative operator*(unsigned long l) const
  412. { return wxULongLongNative(m_ll * l); }
  413. wxULongLongNative& operator*=(const wxULongLongNative& ll)
  414. { m_ll *= ll.m_ll; return *this; }
  415. wxULongLongNative& operator*=(unsigned long l)
  416. { m_ll *= l; return *this; }
  417. wxULongLongNative operator/(const wxULongLongNative& ll) const
  418. { return wxULongLongNative(m_ll / ll.m_ll); }
  419. wxULongLongNative operator/(unsigned long l) const
  420. { return wxULongLongNative(m_ll / l); }
  421. wxULongLongNative& operator/=(const wxULongLongNative& ll)
  422. { m_ll /= ll.m_ll; return *this; }
  423. wxULongLongNative& operator/=(unsigned long l)
  424. { m_ll /= l; return *this; }
  425. wxULongLongNative operator%(const wxULongLongNative& ll) const
  426. { return wxULongLongNative(m_ll % ll.m_ll); }
  427. wxULongLongNative operator%(unsigned long l) const
  428. { return wxULongLongNative(m_ll % l); }
  429. // comparison
  430. bool operator==(const wxULongLongNative& ll) const
  431. { return m_ll == ll.m_ll; }
  432. bool operator==(unsigned long l) const
  433. { return m_ll == l; }
  434. bool operator!=(const wxULongLongNative& ll) const
  435. { return m_ll != ll.m_ll; }
  436. bool operator!=(unsigned long l) const
  437. { return m_ll != l; }
  438. bool operator<(const wxULongLongNative& ll) const
  439. { return m_ll < ll.m_ll; }
  440. bool operator<(unsigned long l) const
  441. { return m_ll < l; }
  442. bool operator>(const wxULongLongNative& ll) const
  443. { return m_ll > ll.m_ll; }
  444. bool operator>(unsigned long l) const
  445. { return m_ll > l; }
  446. bool operator<=(const wxULongLongNative& ll) const
  447. { return m_ll <= ll.m_ll; }
  448. bool operator<=(unsigned long l) const
  449. { return m_ll <= l; }
  450. bool operator>=(const wxULongLongNative& ll) const
  451. { return m_ll >= ll.m_ll; }
  452. bool operator>=(unsigned long l) const
  453. { return m_ll >= l; }
  454. // miscellaneous
  455. // return the string representation of this number
  456. wxString ToString() const;
  457. // conversion to byte array: returns a pointer to static buffer!
  458. void *asArray() const;
  459. #if wxUSE_STD_IOSTREAM
  460. // input/output
  461. friend WXDLLIMPEXP_BASE
  462. wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongNative&);
  463. #endif
  464. friend WXDLLIMPEXP_BASE
  465. wxString& operator<<(wxString&, const wxULongLongNative&);
  466. #if wxUSE_STREAMS
  467. friend WXDLLIMPEXP_BASE
  468. class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongNative&);
  469. friend WXDLLIMPEXP_BASE
  470. class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongNative&);
  471. #endif
  472. private:
  473. wxULongLong_t m_ll;
  474. };
  475. inline
  476. wxLongLongNative& wxLongLongNative::operator=(const wxULongLongNative &ll)
  477. {
  478. m_ll = ll.GetValue();
  479. return *this;
  480. }
  481. #endif // wxUSE_LONGLONG_NATIVE
  482. #if wxUSE_LONGLONG_WX
  483. class WXDLLIMPEXP_BASE wxLongLongWx
  484. {
  485. public:
  486. // ctors
  487. // default ctor initializes to 0
  488. wxLongLongWx()
  489. {
  490. m_lo = m_hi = 0;
  491. #ifdef wxLONGLONG_TEST_MODE
  492. m_ll = 0;
  493. Check();
  494. #endif // wxLONGLONG_TEST_MODE
  495. }
  496. // from long
  497. wxLongLongWx(long l) { *this = l; }
  498. // from 2 longs
  499. wxLongLongWx(long hi, unsigned long lo)
  500. {
  501. m_hi = hi;
  502. m_lo = lo;
  503. #ifdef wxLONGLONG_TEST_MODE
  504. m_ll = hi;
  505. m_ll <<= 32;
  506. m_ll |= lo;
  507. Check();
  508. #endif // wxLONGLONG_TEST_MODE
  509. }
  510. // default copy ctor is ok in both cases
  511. // no dtor
  512. // assignment operators
  513. // from long
  514. wxLongLongWx& operator=(long l)
  515. {
  516. m_lo = l;
  517. m_hi = (l < 0 ? -1l : 0l);
  518. #ifdef wxLONGLONG_TEST_MODE
  519. m_ll = l;
  520. Check();
  521. #endif // wxLONGLONG_TEST_MODE
  522. return *this;
  523. }
  524. // from int
  525. wxLongLongWx& operator=(int l)
  526. {
  527. return operator=((long)l);
  528. }
  529. wxLongLongWx& operator=(unsigned long l)
  530. {
  531. m_lo = l;
  532. m_hi = 0;
  533. #ifdef wxLONGLONG_TEST_MODE
  534. m_ll = l;
  535. Check();
  536. #endif // wxLONGLONG_TEST_MODE
  537. return *this;
  538. }
  539. wxLongLongWx& operator=(unsigned int l)
  540. {
  541. return operator=((unsigned long)l);
  542. }
  543. wxLongLongWx& operator=(const class wxULongLongWx &ll);
  544. // from double
  545. wxLongLongWx& Assign(double d);
  546. // can't have assignment operator from 2 longs
  547. // accessors
  548. // get high part
  549. long GetHi() const { return m_hi; }
  550. // get low part
  551. unsigned long GetLo() const { return m_lo; }
  552. // get absolute value
  553. wxLongLongWx Abs() const { return wxLongLongWx(*this).Abs(); }
  554. wxLongLongWx& Abs()
  555. {
  556. if ( m_hi < 0 )
  557. m_hi = -m_hi;
  558. #ifdef wxLONGLONG_TEST_MODE
  559. if ( m_ll < 0 )
  560. m_ll = -m_ll;
  561. Check();
  562. #endif // wxLONGLONG_TEST_MODE
  563. return *this;
  564. }
  565. // convert to long with range checking in debug mode (only!)
  566. long ToLong() const
  567. {
  568. wxASSERT_MSG( (m_hi == 0l) || (m_hi == -1l),
  569. wxT("wxLongLong to long conversion loss of precision") );
  570. return (long)m_lo;
  571. }
  572. // convert to double
  573. double ToDouble() const;
  574. // operations
  575. // addition
  576. wxLongLongWx operator+(const wxLongLongWx& ll) const;
  577. wxLongLongWx& operator+=(const wxLongLongWx& ll);
  578. wxLongLongWx operator+(long l) const;
  579. wxLongLongWx& operator+=(long l);
  580. // pre increment operator
  581. wxLongLongWx& operator++();
  582. // post increment operator
  583. wxLongLongWx& operator++(int) { return ++(*this); }
  584. // negation operator
  585. wxLongLongWx operator-() const;
  586. wxLongLongWx& Negate();
  587. // subraction
  588. wxLongLongWx operator-(const wxLongLongWx& ll) const;
  589. wxLongLongWx& operator-=(const wxLongLongWx& ll);
  590. // pre decrement operator
  591. wxLongLongWx& operator--();
  592. // post decrement operator
  593. wxLongLongWx& operator--(int) { return --(*this); }
  594. // shifts
  595. // left shift
  596. wxLongLongWx operator<<(int shift) const;
  597. wxLongLongWx& operator<<=(int shift);
  598. // right shift
  599. wxLongLongWx operator>>(int shift) const;
  600. wxLongLongWx& operator>>=(int shift);
  601. // bitwise operators
  602. wxLongLongWx operator&(const wxLongLongWx& ll) const;
  603. wxLongLongWx& operator&=(const wxLongLongWx& ll);
  604. wxLongLongWx operator|(const wxLongLongWx& ll) const;
  605. wxLongLongWx& operator|=(const wxLongLongWx& ll);
  606. wxLongLongWx operator^(const wxLongLongWx& ll) const;
  607. wxLongLongWx& operator^=(const wxLongLongWx& ll);
  608. wxLongLongWx operator~() const;
  609. // comparison
  610. bool operator==(const wxLongLongWx& ll) const
  611. { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
  612. #if wxUSE_LONGLONG_NATIVE
  613. bool operator==(const wxLongLongNative& ll) const
  614. { return m_lo == ll.GetLo() && m_hi == ll.GetHi(); }
  615. #endif
  616. bool operator!=(const wxLongLongWx& ll) const
  617. { return !(*this == ll); }
  618. bool operator<(const wxLongLongWx& ll) const;
  619. bool operator>(const wxLongLongWx& ll) const;
  620. bool operator<=(const wxLongLongWx& ll) const
  621. { return *this < ll || *this == ll; }
  622. bool operator>=(const wxLongLongWx& ll) const
  623. { return *this > ll || *this == ll; }
  624. bool operator<(long l) const { return *this < wxLongLongWx(l); }
  625. bool operator>(long l) const { return *this > wxLongLongWx(l); }
  626. bool operator==(long l) const
  627. {
  628. return l >= 0 ? (m_hi == 0 && m_lo == (unsigned long)l)
  629. : (m_hi == -1 && m_lo == (unsigned long)l);
  630. }
  631. bool operator<=(long l) const { return *this < l || *this == l; }
  632. bool operator>=(long l) const { return *this > l || *this == l; }
  633. // multiplication
  634. wxLongLongWx operator*(const wxLongLongWx& ll) const;
  635. wxLongLongWx& operator*=(const wxLongLongWx& ll);
  636. // division
  637. wxLongLongWx operator/(const wxLongLongWx& ll) const;
  638. wxLongLongWx& operator/=(const wxLongLongWx& ll);
  639. wxLongLongWx operator%(const wxLongLongWx& ll) const;
  640. void Divide(const wxLongLongWx& divisor,
  641. wxLongLongWx& quotient,
  642. wxLongLongWx& remainder) const;
  643. // input/output
  644. // return the string representation of this number
  645. wxString ToString() const;
  646. void *asArray() const;
  647. #if wxUSE_STD_IOSTREAM
  648. friend WXDLLIMPEXP_BASE
  649. wxSTD ostream& operator<<(wxSTD ostream&, const wxLongLongWx&);
  650. #endif // wxUSE_STD_IOSTREAM
  651. friend WXDLLIMPEXP_BASE
  652. wxString& operator<<(wxString&, const wxLongLongWx&);
  653. #if wxUSE_STREAMS
  654. friend WXDLLIMPEXP_BASE
  655. class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxLongLongWx&);
  656. friend WXDLLIMPEXP_BASE
  657. class wxTextInputStream& operator>>(class wxTextInputStream&, wxLongLongWx&);
  658. #endif
  659. private:
  660. // long is at least 32 bits, so represent our 64bit number as 2 longs
  661. long m_hi; // signed bit is in the high part
  662. unsigned long m_lo;
  663. #ifdef wxLONGLONG_TEST_MODE
  664. void Check()
  665. {
  666. wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
  667. }
  668. wxLongLong_t m_ll;
  669. #endif // wxLONGLONG_TEST_MODE
  670. };
  671. class WXDLLIMPEXP_BASE wxULongLongWx
  672. {
  673. public:
  674. // ctors
  675. // default ctor initializes to 0
  676. wxULongLongWx()
  677. {
  678. m_lo = m_hi = 0;
  679. #ifdef wxLONGLONG_TEST_MODE
  680. m_ll = 0;
  681. Check();
  682. #endif // wxLONGLONG_TEST_MODE
  683. }
  684. // from ulong
  685. wxULongLongWx(unsigned long l) { *this = l; }
  686. // from 2 ulongs
  687. wxULongLongWx(unsigned long hi, unsigned long lo)
  688. {
  689. m_hi = hi;
  690. m_lo = lo;
  691. #ifdef wxLONGLONG_TEST_MODE
  692. m_ll = hi;
  693. m_ll <<= 32;
  694. m_ll |= lo;
  695. Check();
  696. #endif // wxLONGLONG_TEST_MODE
  697. }
  698. // from signed to unsigned
  699. wxULongLongWx(wxLongLongWx ll)
  700. {
  701. wxASSERT(ll.GetHi() >= 0);
  702. m_hi = (unsigned long)ll.GetHi();
  703. m_lo = ll.GetLo();
  704. }
  705. // default copy ctor is ok in both cases
  706. // no dtor
  707. // assignment operators
  708. // from long
  709. wxULongLongWx& operator=(unsigned long l)
  710. {
  711. m_lo = l;
  712. m_hi = 0;
  713. #ifdef wxLONGLONG_TEST_MODE
  714. m_ll = l;
  715. Check();
  716. #endif // wxLONGLONG_TEST_MODE
  717. return *this;
  718. }
  719. wxULongLongWx& operator=(long l)
  720. {
  721. m_lo = l;
  722. m_hi = (unsigned long) ((l<0) ? -1l : 0);
  723. #ifdef wxLONGLONG_TEST_MODE
  724. m_ll = (wxULongLong_t) (wxLongLong_t) l;
  725. Check();
  726. #endif // wxLONGLONG_TEST_MODE
  727. return *this;
  728. }
  729. wxULongLongWx& operator=(const class wxLongLongWx &ll) {
  730. // Should we use an assert like it was before in the constructor?
  731. // wxASSERT(ll.GetHi() >= 0);
  732. m_hi = (unsigned long)ll.GetHi();
  733. m_lo = ll.GetLo();
  734. return *this;
  735. }
  736. // can't have assignment operator from 2 longs
  737. // accessors
  738. // get high part
  739. unsigned long GetHi() const { return m_hi; }
  740. // get low part
  741. unsigned long GetLo() const { return m_lo; }
  742. // convert to long with range checking in debug mode (only!)
  743. unsigned long ToULong() const
  744. {
  745. wxASSERT_MSG( m_hi == 0ul,
  746. wxT("wxULongLong to long conversion loss of precision") );
  747. return (unsigned long)m_lo;
  748. }
  749. // convert to double
  750. double ToDouble() const;
  751. // operations
  752. // addition
  753. wxULongLongWx operator+(const wxULongLongWx& ll) const;
  754. wxULongLongWx& operator+=(const wxULongLongWx& ll);
  755. wxULongLongWx operator+(unsigned long l) const;
  756. wxULongLongWx& operator+=(unsigned long l);
  757. // pre increment operator
  758. wxULongLongWx& operator++();
  759. // post increment operator
  760. wxULongLongWx& operator++(int) { return ++(*this); }
  761. // subtraction
  762. wxLongLongWx operator-(const wxULongLongWx& ll) const;
  763. wxULongLongWx& operator-=(const wxULongLongWx& ll);
  764. // pre decrement operator
  765. wxULongLongWx& operator--();
  766. // post decrement operator
  767. wxULongLongWx& operator--(int) { return --(*this); }
  768. // shifts
  769. // left shift
  770. wxULongLongWx operator<<(int shift) const;
  771. wxULongLongWx& operator<<=(int shift);
  772. // right shift
  773. wxULongLongWx operator>>(int shift) const;
  774. wxULongLongWx& operator>>=(int shift);
  775. // bitwise operators
  776. wxULongLongWx operator&(const wxULongLongWx& ll) const;
  777. wxULongLongWx& operator&=(const wxULongLongWx& ll);
  778. wxULongLongWx operator|(const wxULongLongWx& ll) const;
  779. wxULongLongWx& operator|=(const wxULongLongWx& ll);
  780. wxULongLongWx operator^(const wxULongLongWx& ll) const;
  781. wxULongLongWx& operator^=(const wxULongLongWx& ll);
  782. wxULongLongWx operator~() const;
  783. // comparison
  784. bool operator==(const wxULongLongWx& ll) const
  785. { return m_lo == ll.m_lo && m_hi == ll.m_hi; }
  786. bool operator!=(const wxULongLongWx& ll) const
  787. { return !(*this == ll); }
  788. bool operator<(const wxULongLongWx& ll) const;
  789. bool operator>(const wxULongLongWx& ll) const;
  790. bool operator<=(const wxULongLongWx& ll) const
  791. { return *this < ll || *this == ll; }
  792. bool operator>=(const wxULongLongWx& ll) const
  793. { return *this > ll || *this == ll; }
  794. bool operator<(unsigned long l) const { return *this < wxULongLongWx(l); }
  795. bool operator>(unsigned long l) const { return *this > wxULongLongWx(l); }
  796. bool operator==(unsigned long l) const
  797. {
  798. return (m_hi == 0 && m_lo == (unsigned long)l);
  799. }
  800. bool operator<=(unsigned long l) const { return *this < l || *this == l; }
  801. bool operator>=(unsigned long l) const { return *this > l || *this == l; }
  802. // multiplication
  803. wxULongLongWx operator*(const wxULongLongWx& ll) const;
  804. wxULongLongWx& operator*=(const wxULongLongWx& ll);
  805. // division
  806. wxULongLongWx operator/(const wxULongLongWx& ll) const;
  807. wxULongLongWx& operator/=(const wxULongLongWx& ll);
  808. wxULongLongWx operator%(const wxULongLongWx& ll) const;
  809. void Divide(const wxULongLongWx& divisor,
  810. wxULongLongWx& quotient,
  811. wxULongLongWx& remainder) const;
  812. // input/output
  813. // return the string representation of this number
  814. wxString ToString() const;
  815. void *asArray() const;
  816. #if wxUSE_STD_IOSTREAM
  817. friend WXDLLIMPEXP_BASE
  818. wxSTD ostream& operator<<(wxSTD ostream&, const wxULongLongWx&);
  819. #endif // wxUSE_STD_IOSTREAM
  820. friend WXDLLIMPEXP_BASE
  821. wxString& operator<<(wxString&, const wxULongLongWx&);
  822. #if wxUSE_STREAMS
  823. friend WXDLLIMPEXP_BASE
  824. class wxTextOutputStream& operator<<(class wxTextOutputStream&, const wxULongLongWx&);
  825. friend WXDLLIMPEXP_BASE
  826. class wxTextInputStream& operator>>(class wxTextInputStream&, wxULongLongWx&);
  827. #endif
  828. private:
  829. // long is at least 32 bits, so represent our 64bit number as 2 longs
  830. unsigned long m_hi;
  831. unsigned long m_lo;
  832. #ifdef wxLONGLONG_TEST_MODE
  833. void Check()
  834. {
  835. wxASSERT( (m_ll >> 32) == m_hi && (unsigned long)m_ll == m_lo );
  836. }
  837. wxULongLong_t m_ll;
  838. #endif // wxLONGLONG_TEST_MODE
  839. };
  840. #endif // wxUSE_LONGLONG_WX
  841. // ----------------------------------------------------------------------------
  842. // binary operators
  843. // ----------------------------------------------------------------------------
  844. inline bool operator<(long l, const wxLongLong& ll) { return ll > l; }
  845. inline bool operator>(long l, const wxLongLong& ll) { return ll < l; }
  846. inline bool operator<=(long l, const wxLongLong& ll) { return ll >= l; }
  847. inline bool operator>=(long l, const wxLongLong& ll) { return ll <= l; }
  848. inline bool operator==(long l, const wxLongLong& ll) { return ll == l; }
  849. inline bool operator!=(long l, const wxLongLong& ll) { return ll != l; }
  850. inline wxLongLong operator+(long l, const wxLongLong& ll) { return ll + l; }
  851. inline wxLongLong operator-(long l, const wxLongLong& ll)
  852. {
  853. return wxLongLong(l) - ll;
  854. }
  855. inline bool operator<(unsigned long l, const wxULongLong& ull) { return ull > l; }
  856. inline bool operator>(unsigned long l, const wxULongLong& ull) { return ull < l; }
  857. inline bool operator<=(unsigned long l, const wxULongLong& ull) { return ull >= l; }
  858. inline bool operator>=(unsigned long l, const wxULongLong& ull) { return ull <= l; }
  859. inline bool operator==(unsigned long l, const wxULongLong& ull) { return ull == l; }
  860. inline bool operator!=(unsigned long l, const wxULongLong& ull) { return ull != l; }
  861. inline wxULongLong operator+(unsigned long l, const wxULongLong& ull) { return ull + l; }
  862. inline wxLongLong operator-(unsigned long l, const wxULongLong& ull)
  863. {
  864. wxULongLong ret = wxULongLong(l) - ull;
  865. return wxLongLong((wxInt32)ret.GetHi(),ret.GetLo());
  866. }
  867. #if wxUSE_LONGLONG_NATIVE && wxUSE_STREAMS
  868. WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxULongLong_t value);
  869. WXDLLIMPEXP_BASE class wxTextOutputStream &operator<<(class wxTextOutputStream &stream, wxLongLong_t value);
  870. WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxULongLong_t &value);
  871. WXDLLIMPEXP_BASE class wxTextInputStream &operator>>(class wxTextInputStream &stream, wxLongLong_t &value);
  872. #endif
  873. // ----------------------------------------------------------------------------
  874. // Specialize numeric_limits<> for our long long wrapper classes.
  875. // ----------------------------------------------------------------------------
  876. #if wxUSE_LONGLONG_NATIVE
  877. // VC6 is known to not have __int64 specializations of numeric_limits<> in its
  878. // <limits> anyhow so don't bother including it, especially as it results in
  879. // tons of warnings because the standard header itself uses obsolete template
  880. // specialization syntax.
  881. #ifndef __VISUALC6__
  882. #include <limits>
  883. namespace std
  884. {
  885. #ifdef __clang__
  886. // libstdc++ (used by Clang) uses struct for numeric_limits; unlike gcc, clang
  887. // warns about this
  888. template<> struct numeric_limits<wxLongLong> : public numeric_limits<wxLongLong_t> {};
  889. template<> struct numeric_limits<wxULongLong> : public numeric_limits<wxULongLong_t> {};
  890. #else
  891. template<> class numeric_limits<wxLongLong> : public numeric_limits<wxLongLong_t> {};
  892. template<> class numeric_limits<wxULongLong> : public numeric_limits<wxULongLong_t> {};
  893. #endif
  894. } // namespace std
  895. #endif // !VC6
  896. #endif // wxUSE_LONGLONG_NATIVE
  897. // ----------------------------------------------------------------------------
  898. // Specialize wxArgNormalizer to allow using wxLongLong directly with wx pseudo
  899. // vararg functions.
  900. // ----------------------------------------------------------------------------
  901. // Notice that this must be done here and not in wx/strvararg.h itself because
  902. // we can't include wx/longlong.h from there as this header itself includes
  903. // wx/string.h which includes wx/strvararg.h too, so to avoid the circular
  904. // dependencies we can only do it here (or add another header just for this but
  905. // it doesn't seem necessary).
  906. #include "wx/strvararg.h"
  907. template<>
  908. struct WXDLLIMPEXP_BASE wxArgNormalizer<wxLongLong>
  909. {
  910. wxArgNormalizer(wxLongLong value,
  911. const wxFormatString *fmt, unsigned index)
  912. : m_value(value)
  913. {
  914. wxASSERT_ARG_TYPE( fmt, index, wxFormatString::Arg_LongLongInt );
  915. }
  916. wxLongLong_t get() const { return m_value.GetValue(); }
  917. wxLongLong m_value;
  918. };
  919. #endif // wxUSE_LONGLONG
  920. #endif // _WX_LONGLONG_H