index.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0.1 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  5. <title>Boost.Flyweight Documentation - Tutorial</title>
  6. <link rel="stylesheet" href="../style.css" type="text/css">
  7. <link rel="start" href="../index.html">
  8. <link rel="prev" href="../index.html">
  9. <link rel="up" href="../index.html">
  10. <link rel="next" href="basics.html">
  11. </head>
  12. <body>
  13. <h1><img src="../../../../boost.png" alt="Boost logo" align=
  14. "middle" width="277" height="86">Boost.Flyweight Tutorial</h1>
  15. <div class="prev_link"><a href="../index.html"><img src="../prev.gif" alt="index" border="0"><br>
  16. Index
  17. </a></div>
  18. <div class="up_link"><a href="../index.html"><img src="../up.gif" alt="index" border="0"><br>
  19. Index
  20. </a></div>
  21. <div class="next_link"><a href="basics.html"><img src="../next.gif" alt="basics" border="0"><br>
  22. Basics
  23. </a></div><br clear="all" style="clear: all;">
  24. <hr>
  25. <h2>Contents</h2>
  26. <ul>
  27. <li><a href="#rationale">Rationale</a></li>
  28. <li><a href="#namespace">Namespace</a></li>
  29. <li><a href="#guide">Guide to the reader</a></li>
  30. <li><a href="basics.html">Basics</a></li>
  31. <li><a href="key_value.html">Key-value flyweights</a></li>
  32. <li><a href="configuration.html">Configuring Boost.Flyweight</a></li>
  33. <li><a href="extension.html">Extending Boost.Flyweight</a></li>
  34. <li><a href="technical.html">Technical issues</a></li>
  35. <li><a href="lambda_expressions.html">Annex: MPL Lambda expressions</a></li>
  36. </ul>
  37. <h2><a name="rationale">Rationale</a></h2>
  38. <span style="float:left;margin-right:20px;margin-bottom:20px">
  39. <p align="center">
  40. <img src="flyweight_rep.png"
  41. alt="representation of a flyweight scenario"
  42. width="424" height="320"><br>
  43. <b>Fig. 1: Representation of a flyweight scenario.</b>
  44. </p>
  45. </span>
  46. <p>
  47. Consider an application that has to manage large quantities of objects of
  48. moderate size, potentially requiring more memory than reasonably available.
  49. When these objects are <i>immutable</i>, i.e. they do not modify its internal
  50. state except maybe for reattaching to a new set of state data, and some
  51. additional conditions are met, a very convenient optimization technique known
  52. as the <i>flyweight pattern</i> can be introduced.
  53. </p>
  54. <p>
  55. Let us say there are <i>N</i> different objects living at a given time
  56. inside the application, globally taking <i>M</i> different values. If <i>N</i>
  57. is much greater than <i>M</i>, that is, there are many equivalent objects,
  58. we can eliminate the implicit redundancy by replacing the original objects with
  59. handle classes which refer to a common repository of shared value objects,
  60. as depicted in the figure. The handle objects or flyweights, which act as
  61. proxies for the actual values, typically occupy the size of a mere pointer.
  62. The larger the value classes, and the greater the <i>N</i>/<i>M</i> ratio,
  63. the more significant the memory savings achieved by this tecnhique. The
  64. classical example of application of the flyweight idiom is that of a word
  65. processor: each letter in the document carries a large wealth of
  66. information, such as its Unicode identifier, font, size, typesetting effects,
  67. etc., but given that the degree of letter repetition in a document is extremely
  68. high, implementing those letters as flyweight classes allows us to easily
  69. handle documents ranging in the hundreds of thousands of characters.
  70. </p>
  71. <p>
  72. Most presentations of the design pattern found in the literature do make a
  73. distinction between the flyweight <i>intrinsic information</i> (the constant
  74. data factored out into the repository) and <i>extrinsic</i>, mutable
  75. information, which is stored along with the flyweight objects or passed
  76. externally. This separation analysis can have some merit from the point of
  77. view of application design, but when it comes to implementation extrinsic
  78. information has no impact on the overall flyweight scheme. So,
  79. Boost.Flyweight assumes that the type onto which the library operates
  80. entirely consists of intrinsic information: this allows for a particularly
  81. appealing realization of the idiom in C++ in which
  82. <code>flyweight&lt;T&gt;</code> is an opaque type convertible to
  83. <code>const T&amp;</code>.
  84. </p>
  85. <p>
  86. The central repository of shared value objects is known as the <i>flyweight
  87. factory</i>. This component is able to locate and return a reference to an
  88. object with a given value, or insert the value if no copy was previously
  89. stored. Boost.Flyweight controls the interaction of flyweights with
  90. their factory transparently to the programmer, so that a casual user of the
  91. library need not even be concerned about the presence of such factory.
  92. Boost.Flyweight uses by default a factory based on a hashed container which
  93. is expected to be suitable for most situations. When this is not the case, it
  94. is possible to customize the factory or even replace it with another one of
  95. a different type, either provided by Boost.Flyweight or defined by the user.
  96. Other aspects of the implementation are also customizable and extendable.
  97. </p>
  98. <h2 clear="all" style="clear: all;">
  99. <a name="namespace">Namespace</a>
  100. </h2>
  101. <p>
  102. All the public types of Boost.Flyweight reside in namespace <code>::boost::flyweights</code>.
  103. Additionaly, the main class template <code>flyweight</code> is lifted to namespace
  104. <code>::boost</code> by means of a <code>using</code> declaration. For brevity of
  105. exposition, the fragments of code in the documentation are written as if the following
  106. directives were in effect:
  107. </p>
  108. <blockquote><pre>
  109. <span class=keyword>using</span> <span class=keyword>namespace</span> <span class=special>::</span><span class=identifier>boost</span><span class=special>;</span>
  110. <span class=keyword>using</span> <span class=keyword>namespace</span> <span class=special>::</span><span class=identifier>boost</span><span class=special>::</span><span class=identifier>flyweights</span><span class=special>;</span>
  111. </pre></blockquote>
  112. <h2><a name="guide">Guide to the reader</a></h2>
  113. <p>
  114. Although Boost.Flyweight features an extensive customization
  115. framework controlling many internal implementation aspects, the library is designed
  116. in such a way that most users need not be concerned about or
  117. even aware of the underlying complexity. Learning to use Boost.Flyweight
  118. as an off-the-shelf component can be acomplished merely by reading
  119. the <a href="basics.html">basics</a> section and skimming through the
  120. part on <a href="key_value.html">key-value flyweights</a>, the section on flyweight type
  121. <a href="configuration.html#tagging">tagging</a> and the discussion of some
  122. <a href="technical.html">technical issues</a>. The
  123. <a href="configuration.html">configuration</a> section teaches how to fine tune the
  124. different internal components of the library. Only very advanced usage
  125. scenarios will require implementing user-provided pluggable components:
  126. this is covered on the <a href="extension.html">extension</a> section.
  127. </p>
  128. <hr>
  129. <div class="prev_link"><a href="../index.html"><img src="../prev.gif" alt="index" border="0"><br>
  130. Index
  131. </a></div>
  132. <div class="up_link"><a href="../index.html"><img src="../up.gif" alt="index" border="0"><br>
  133. Index
  134. </a></div>
  135. <div class="next_link"><a href="basics.html"><img src="../next.gif" alt="basics" border="0"><br>
  136. Basics
  137. </a></div><br clear="all" style="clear: all;">
  138. <br>
  139. <p>Revised August 13th 2008</p>
  140. <p>&copy; Copyright 2006-2008 Joaqu&iacute;n M L&oacute;pez Mu&ntilde;oz.
  141. Distributed under the Boost Software
  142. License, Version 1.0. (See accompanying file <a href="../../../../LICENSE_1_0.txt">
  143. LICENSE_1_0.txt</a> or copy at <a href="http://www.boost.org/LICENSE_1_0.txt">
  144. http://www.boost.org/LICENSE_1_0.txt</a>)
  145. </p>
  146. </body>
  147. </html>