cpp.xml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
  3. "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
  4. <section id="safe_numerics.promotion_policies.cpp">
  5. <title>cpp&lt;int C, int S, int I, int L, int LL&gt;</title>
  6. <section>
  7. <title>Description</title>
  8. <para>This policy is used to promote safe types in arithmetic expressions
  9. according to the rules in the C++ standard. But rather than using the
  10. native C++ standard types supported by the compiler, it uses types whose
  11. length in number of bits is specified by the template parameters.</para>
  12. <para>This policy is useful for running test programs which use C++
  13. portable integer types but which are destined to run on an architecture
  14. which is different than the one on which the test program is being built
  15. and run. This can happen when developing code for embedded systems.
  16. Algorithms developed or borrowed from one architecture but destined for
  17. another can be tested on the desktop.</para>
  18. <para>Note that this policy is only applicable to safe types whose base
  19. type is a type fulfilling the type requirements of <link
  20. linkend="safe_numerics.integer">Integer</link>.</para>
  21. </section>
  22. <section>
  23. <title>Template Parameters</title>
  24. <informaltable>
  25. <tgroup cols="3">
  26. <colspec align="left" colwidth="1*"/>
  27. <colspec align="left" colwidth="1*"/>
  28. <colspec align="left" colwidth="6*"/>
  29. <thead>
  30. <row>
  31. <entry align="left">Parameter</entry>
  32. <entry align="left">Type</entry>
  33. <entry>Description</entry>
  34. </row>
  35. </thead>
  36. <tbody>
  37. <row>
  38. <entry><code>C</code></entry>
  39. <entry>int</entry>
  40. <entry>Number of bits in a char</entry>
  41. </row>
  42. <row>
  43. <entry><code>S</code></entry>
  44. <entry>int</entry>
  45. <entry>Number of bits in a short</entry>
  46. </row>
  47. <row>
  48. <entry><code>I</code></entry>
  49. <entry>int</entry>
  50. <entry>Number of bits in an integer</entry>
  51. </row>
  52. <row>
  53. <entry><code>L</code></entry>
  54. <entry>int</entry>
  55. <entry>Number of bits in a long</entry>
  56. </row>
  57. <row>
  58. <entry><code>LL</code></entry>
  59. <entry>int</entry>
  60. <entry>Number of bits in a long long</entry>
  61. </row>
  62. </tbody>
  63. </tgroup>
  64. </informaltable>
  65. </section>
  66. <section>
  67. <title>Model of</title>
  68. <para><link
  69. linkend="safe_numerics.promotion_policy">PromotionPolicy</link></para>
  70. </section>
  71. <section>
  72. <title>Example of Use</title>
  73. <para>Consider the following problem. One is developing software which
  74. uses a very small microprocessor and a very limited C compiler. The chip
  75. is so small, you can't print anything from the code, log, debug or
  76. anything else. One debugs this code by using the "burn" and "crash" method
  77. - you burn the chip (download the code), run the code, observe the
  78. results, make changes and try again. This is a crude method which is
  79. usually the one used. But it can be quite time consuming.</para>
  80. <para>Consider an alternative. Build and compile your code in testable
  81. modules. For each module write a test which exercises all the code and
  82. makes it work. Finally download your code into the chip and - voilà -
  83. working product. This sounds great, but there's one problem. Our target
  84. processor - in this case a PIC162550 from Microchip Technology is only an
  85. 8 bit CPU. The compiler we use defines INT as 8 bits. This (and a few
  86. other problems), make our algorithm testing environment differ from our
  87. target environment. We can address this by defining INT as a safe integer
  88. with a range of 8 bits. By using a custom promotion policy, we can force
  89. the evaluation of C++ expressions in the test environment to be the same
  90. as that in the target environment. Also in our target environment, we can
  91. trap any overflows or other errors. So we can write and test our code on
  92. our desktop system and download the code to the target knowing that it
  93. just has to work. This is a huge time saver and confidence builder. For an
  94. extended example on how this is done, look at <link
  95. linkend="safe_numerics.safety_critical_embedded_controller">Safety
  96. Critical Embedded Controller</link> .</para>
  97. </section>
  98. <section>
  99. <title>Header</title>
  100. <para><code><ulink
  101. url="../../include/boost/safe_numerics/cpp.hpp"><code>#include
  102. &lt;boost/numeric/safe_numerics/cpp.hpp&gt; </code></ulink></code></para>
  103. </section>
  104. </section>