/doc/variant.typecasts.html

http://github.com/foodmike/PTypes · HTML · 75 lines · 71 code · 0 blank · 4 comment · 0 complexity · 4f3ff6454df312a7b9d23ac8af65b338 MD5 · raw file

  1. <html><!-- #BeginTemplate "/Templates/tmpl.dwt" --><!-- DW6 -->
  2. <head>
  3. <!-- #BeginEditable "doctitle" -->
  4. <title>PTypes: variant: assignments and typecasts</title>
  5. <!-- #EndEditable -->
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  7. <link rel="stylesheet" href="styles.css">
  8. </head>
  9. <body bgcolor="#FFFFFF" leftmargin="40" marginwidth="40">
  10. <p><a href="../index.html"><img src="title-21.png" width="253" height="39" alt="C++ Portable Types Library (PTypes) Version 2.1" border="0"></a>
  11. <hr size="1" noshade>
  12. <!-- #BeginEditable "body" -->
  13. <p class="hpath"><a href="index.html">Top</a>: <a href="basic.html">Basic types</a>:
  14. <a href="variant.html">variant</a>: Assignments and typecasts</p>
  15. <p><br>
  16. Variants may hold values of the following types:</p>
  17. <ul>
  18. <li>64-bit integer (<span class="lang">large</span>)</li>
  19. <li>boolean (<span class="lang">bool</span>)</li>
  20. <li>floating point (<span class="lang">double</span>)</li>
  21. <li><span class="lang">string</span></li>
  22. <li>associative array of variants</li>
  23. <li>reference to an object derived from <span class="lang">component</span></li>
  24. <li>unassigned (no type)</li>
  25. </ul>
  26. <p>Variants are compatible with variables and constants of all numeric types,
  27. the string type and a pointer to <span class="lang">component</span>, which means,
  28. you can assign any of these values to a variant object and vice versa, including
  29. passing as parameters to functions. The type of a variant can be changed at run-time
  30. by simply assigning a new value to it. </p>
  31. <p>By default variants are initialized to an unassigned state, which is the same
  32. as calling <span class="lang">clear()</span> for a variant object.</p>
  33. <p>You may store <span class="lang">datetime</span> values in variants, since
  34. <span class="lang">datetime</span> is equivalent to <span class="lang">large</span>
  35. in PTypes. Besides, arbitrary binary data can be stored in a dynamic string and
  36. then assigned to a variant.</p>
  37. <p>The variant class declares (or rather, overloads) all possible typecast operators
  38. to other fundamental data types. These typecast operators always try to return
  39. some value, even when it is not sensible. Below is a list of rules for typecasts
  40. that may make sense in your program, leaving the other less meaningful cases as
  41. 'unspecified'. Although variants store integers as 64-bit values, for simplicity
  42. we use the word <span class="lang">int</span> in this list.</p>
  43. <ul>
  44. <li>integers and floating point variants are compatible and mutually castable
  45. in a manner similar to the C language rules</li>
  46. <li>int to bool: returns true if the value is non-zero</li>
  47. <li>int to string: returns the string representing the value in ASCII form</li>
  48. <li>bool to int: returns 0 or 1</li>
  49. <li>bool to string: returns &quot;0&quot; or &quot;1&quot;</li>
  50. <li>string to int: converts the string to an integer; no spaces are allowed before
  51. or after the number; returns 0 if the string does not represent a number</li>
  52. <li>string to float: similar to string-to-int, converts the string to a floating-point
  53. value</li>
  54. <li>string to bool: returns true if the string is not empty</li>
  55. <li>array to bool: returns true if the array is not empty</li>
  56. <li>(component*) to bool: returns true if the pointer is not NULL</li>
  57. <li>unassigned variants return 0, false, 0.0, empty string, empty array or NULL
  58. when trying to cast to int, boolean, floating point, string, array or reference,
  59. respectively.</li>
  60. </ul>
  61. <p>Typecast operators never affect the actual value of a variant object.</p>
  62. <p>When trying to cast a variant to signed or unsigned 32-bit <span class="lang">int</span>,
  63. the typecast operator first casts the value to 64-bit <span class="lang">large</span>
  64. according to the rules above, and then checks the resulting value. If the value
  65. is out of range, an exception of type (evariant*) is raised. You may always cast
  66. variants to <span class="lang">large</span> to get rid of extra exception handling
  67. code in your program.</p>
  68. <p class="seealso">See also: <a href="string.html">string</a>, <a href="time.html">datetime</a>,
  69. <a href="unknown.html">unknown &amp; component</a>, <a href="variant.arrays.html">Arrays</a>,
  70. <a href="variant.objrefs.html">Object references</a>, <a href="variant.utils.html">Utilities</a></p>
  71. <!-- #EndEditable -->
  72. <hr size="1">
  73. <a href="../index.html" class="ns">PTypes home</a>
  74. </body>
  75. <!-- #EndTemplate --></html>