imp_vars.htm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  5. <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
  6. <meta name="ProgId" content="FrontPage.Editor.Document">
  7. <title>Boost Implementation Variations</title>
  8. </head>
  9. <body link="#0000ff" vlink="#800080" bgcolor="#FFFFFF" text="#000000">
  10. <table summary="Navigational header"
  11. border="1" bgcolor="#007F7F" cellpadding="2">
  12. <tr>
  13. <td bgcolor="#FFFFFF"><img src="../boost.png" alt="boost.png (6897 bytes)" width="277" height="86"></td>
  14. <td><a href="../index.htm"><font face="Arial" color="#FFFFFF"><big>Home</big></font></a></td>
  15. <td><a href="../libs/libraries.htm"><font face="Arial" color="#FFFFFF"><big>Libraries</big></font></a></td>
  16. <td><a href="../people/people.htm"><font face="Arial" color="#FFFFFF"><big>People</big></font></a></td>
  17. <td><a href="faq.htm"><font face="Arial" color="#FFFFFF"><big>FAQ</big></font></a></td>
  18. <td><a href="index.htm"><font face="Arial" color="#FFFFFF"><big>More</big></font></a></td>
  19. </tr>
  20. </table>
  21. <h1>Boost Implementation Variations</h1>
  22. <h2>Separation of interface and implementation</h2>
  23. <p>The interface specifications for boost.org library components (as well as for
  24. quality software in general) are conceptually separate from implementations of
  25. those interfaces. This may not be obvious, particularly when a component is
  26. implemented entirely within a header, but this separation of interface and
  27. implementation is always assumed. From the perspective of those concerned with
  28. software design, portability, and standardization, the interface is what is
  29. important, while the implementation is just a detail.</p>
  30. <p>Dietmar Kühl, one of the original boost.org contributors, comments &quot;The
  31. main contribution is the interface, which is augmented with an implementation,
  32. proving that it is possible to implement the corresponding class and providing a
  33. free implementation.&quot;</p>
  34. <h2>Implementation variations</h2>
  35. <p>There may be a need for multiple implementations of an interface, to
  36. accommodate either platform dependencies or performance tradeoffs. Examples of
  37. platform dependencies include compiler shortcomings, file systems, thread
  38. mechanisms, and graphical user interfaces. The classic example of a performance
  39. tradeoff is a fast implementation which uses a lot of memory versus a slower
  40. implementation which uses less memory.</p>
  41. <p>Boost libraries generally use a <a href="../libs/config/config.htm">configuration
  42. header</a>, boost/config.hpp, to capture compiler and platform
  43. dependencies.&nbsp; Although the use of boost/config.hpp is not required, it is
  44. the preferred approach for simple configuration problems.&nbsp;&nbsp;</p>
  45. <h2>Boost policy</h2>
  46. <p>The Boost policy is to avoid platform dependent variations in interface
  47. specifications, but supply implementations which are usable over a wide range of
  48. platforms and applications.&nbsp; That means boost libraries will use the
  49. techniques below described as appropriate for dealing with platform
  50. dependencies.</p>
  51. <p>The Boost policy toward implementation variations designed to enhance
  52. performance is to avoid them unless the benefits greatly exceed the full
  53. costs.&nbsp; The term &quot;full costs&quot; is intended to include both
  54. tangible costs like extra maintenance, and intangible cost like increased
  55. difficulty in user understanding.</p>
  56. <h2>Techniques for providing implementation variations</h2>
  57. <p>Several techniques may be used to provide implementation variations. Each is
  58. appropriate in some situations, and not appropriate in other situations.</p>
  59. <h3>Single general purpose implementation</h3>
  60. <p>The first technique is to simply not provide implementation variation at
  61. all.&nbsp; Instead, provide a single general purpose implementation, and forgo
  62. the increased complexity implied by all other techniques.</p>
  63. <p><b>Appropriate:</b>&nbsp; When it is possible to write a single portable
  64. implementation which has reasonable performance across a wide range of
  65. platforms. Particularly appropriate when alternative implementations differ only
  66. in esoteric ways.</p>
  67. <p><b>Not appropriate:</b> When implementation requires platform specific
  68. features, or when there are multiple implementation possible with widely
  69. differing performance characteristics.</p>
  70. <p>Beman Dawes comments &quot;In design discussions some implementation is often
  71. alleged to be much faster than another, yet&nbsp; a timing test discovers no
  72. significant difference. The lesson is that while algorithmic differences may
  73. affect speed dramatically, coding differences such as changing a class from
  74. virtual to non-virtual members or removing a level of indirection are unlikely
  75. to make any measurable difference unless deep in an inner loop. And even in an
  76. inner loop, modern CPUs often execute such competing code sequences in the
  77. same number of clock cycles!&nbsp; A single general purpose implementation is
  78. often just fine.&quot;</p>
  79. <p>Or as Donald Knuth said, &quot;Premature optimization is the root of all
  80. evil.&quot; (Computing Surveys, vol 6, #4, p 268).</p>
  81. <h3>Macros</h3>
  82. <p>While the evils of macros are well known, there remain a few cases where
  83. macros are the preferred solution:</p>
  84. <blockquote>
  85. <ul>
  86. <li>&nbsp;Preventing multiple inclusion of headers via #include guards.</li>
  87. <li>&nbsp;Passing minor configuration information from a configuration
  88. header to other files.</li>
  89. </ul>
  90. </blockquote>
  91. <p><b>Appropriate:</b>&nbsp; For small compile-time variations which would
  92. otherwise be costly or confusing to install, use, or maintain. More appropriate
  93. to communicate within and between library components than to communicate with
  94. library users.</p>
  95. <p><b>Not appropriate:&nbsp;</b> If other techniques will do.</p>
  96. <p>To minimize the negative aspects of macros:</p>
  97. <blockquote>
  98. <ul>
  99. <li>Only use macros when they are clearly superior to other
  100. techniques.&nbsp; They should be viewed as a last resort.</li>
  101. <li>Names should be all uppercase, and begin with the namespace name. This
  102. will minimize the chance of name collisions. For example, the #include
  103. guard for a boost header called foobar.h might be named BOOST_FOOBAR_H.</li>
  104. </ul>
  105. </blockquote>
  106. <h3>Separate files</h3>
  107. <p>A library component can have multiple variations, each contained in its own
  108. separate file or files.&nbsp; The files for the most appropriate variation are
  109. copied to the appropriate include or implementation directories at installation
  110. time.</p>
  111. <p>The way to provide this approach in boost libraries is to include specialized
  112. implementations as separate files in separate sub-directories in the .ZIP
  113. distribution file. For example, the structure within the .ZIP distribution file
  114. for a library named foobar which has both default and specialized variations
  115. might look something like:</p>
  116. <blockquote>
  117. <pre>foobar.h // The default header file
  118. foobar.cpp // The default implementation file
  119. readme.txt // Readme explains when to use which files
  120. self_contained/foobar.h // A variation with everything in the header
  121. linux/foobar.cpp // Implementation file to replace the default
  122. win32/foobar.h // Header file to replace the default
  123. win32/foobar.cpp // Implementation file to replace the default</pre>
  124. </blockquote>
  125. <p><b>Appropriate:</b>&nbsp; When different platforms require different
  126. implementations, or when there are major performance differences between
  127. possible implementations.&nbsp;</p>
  128. <p><b>Not appropriate:</b>&nbsp; When it makes sense to use more that one of the
  129. variations in the same installation.</p>
  130. <h3>Separate components</h3>
  131. <p>Rather than have several implementation variations of a single component,
  132. supply several separate components. For example, the Boost library currently
  133. supplies <code>scoped_ptr</code> and <code>shared_ptr</code> classes rather than
  134. a single <code>smart_ptr</code> class parameterized to distinguish between the
  135. two cases.&nbsp; There are several ways to make the component choice:</p>
  136. <blockquote>
  137. <ul>
  138. <li>Hardwired by the programmer during coding.</li>
  139. <li>Chosen by programmer written runtime logic (trading off some extra
  140. space, time, and program complexity for the ability to select the
  141. implementation at run-time.)</li>
  142. </ul>
  143. </blockquote>
  144. <p><b>Appropriate: </b>When the interfaces for the variations diverge, and when
  145. it is reasonably to use more than one of the variations. When run-time selection
  146. of implementation is called for.</p>
  147. <p><b>Not appropriate:</b> When the variations are data type, traits, or
  148. specialization variations which can be better handled by making the component a
  149. template. Also not appropriate when choice of variation is best done by some
  150. setup or installation mechanism outside of the program itself.&nbsp; Thus
  151. usually not appropriate to cope with platform differences.</p>
  152. <p><b>Note:</b> There is a related technique where the interface is specified as
  153. an abstract (pure virtual) base class (or an interface definition language), and
  154. the implementation choice is passed off to some third-party, such as a
  155. dynamic-link library or object-request broker. While that is a powerful
  156. technique, it is way beyond the scope of this discussion.</p>
  157. <h3>Template-based approaches</h3>
  158. <p>Turning a class or function into a template is often an elegant way to cope
  159. with variations.&nbsp; Template-based approaches provide optimal space and time
  160. efficiency in return for constraining the implementation selection to compile
  161. time.&nbsp;</p>
  162. <p>Important template techniques include:</p>
  163. <blockquote>
  164. <ul>
  165. <li>Data type parameterization.&nbsp; This allows a single component to
  166. operate on a variety of data types, and is why templates were originally
  167. invented.</li>
  168. <li>Traits parameterization.&nbsp; If parameterization is complex, bundling
  169. up aspects into a single traits helper class can allow great variation
  170. while hiding messy details.&nbsp; The C++ Standard Library provides
  171. several examples of this idiom, such as <code>iterator_traits&lt;&gt;</code>
  172. (24.3.1 lib.iterator.traits) and <tt>char_traits&lt;&gt;</tt> (21.2
  173. lib.char.traits).</li>
  174. <li>Specialization.&nbsp; A template parameter can be used purely for the
  175. purpose of selecting a specialization. For example:</li>
  176. </ul>
  177. <blockquote>
  178. <blockquote>
  179. <pre>SomeClass&lt;fast&gt; my_fast_object; // fast and small are empty classes
  180. SomeClass&lt;small&gt; my_small_object; // used just to select specialization</pre>
  181. </blockquote>
  182. </blockquote>
  183. </blockquote>
  184. <p><b>Appropriate: </b>When the need for variation is due to data type or
  185. traits, or is performance related like selecting among several algorithms, and
  186. when a program might reasonably use more than one of the variations.</p>
  187. <p><b>Not appropriate:</b>&nbsp; When the interfaces for variations are
  188. different, or when choice of variation is best done by some mechanism outside of
  189. the program itself.&nbsp; Thus usually not appropriate to cope with platform
  190. differences.</p>
  191. <hr>
  192. <p>Revised <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->02 October, 2003<!--webbot bot="Timestamp" endspan i-checksum="38549" --></p>
  193. <p>© Copyright Beman Dawes 2001</p>
  194. <p>Distributed under the Boost Software License, Version 1.0. (See
  195. accompanying file <a href="../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
  196. at <a href=
  197. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)
  198. </p>
  199. </body>
  200. </html>
粤ICP备19079148号