guidelines.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.3.10: http://docutils.sourceforge.net/" />
  7. <title>Boost Pointer Container Library</title>
  8. <style type="text/css">
  9. /*
  10. :Author: David Goodger
  11. :Contact: goodger@users.sourceforge.net
  12. :Date: $Date$
  13. :Revision: $Revision$
  14. :Copyright: This stylesheet has been placed in the public domain.
  15. Default cascading style sheet for the HTML output of Docutils.
  16. See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
  17. customize this style sheet.
  18. */
  19. /* "! important" is used here to override other ``margin-top`` and
  20. ``margin-bottom`` styles that are later in the stylesheet or
  21. more specific. See http://www.w3.org/TR/CSS1#the-cascade */
  22. .first {
  23. margin-top: 0 ! important }
  24. .last, .with-subtitle {
  25. margin-bottom: 0 ! important }
  26. .hidden {
  27. display: none }
  28. a.toc-backref {
  29. text-decoration: none ;
  30. color: black }
  31. blockquote.epigraph {
  32. margin: 2em 5em ; }
  33. dl.docutils dd {
  34. margin-bottom: 0.5em }
  35. /* Uncomment (and remove this text!) to get bold-faced definition list terms
  36. dl.docutils dt {
  37. font-weight: bold }
  38. */
  39. div.abstract {
  40. margin: 2em 5em }
  41. div.abstract p.topic-title {
  42. font-weight: bold ;
  43. text-align: center }
  44. div.admonition, div.attention, div.caution, div.danger, div.error,
  45. div.hint, div.important, div.note, div.tip, div.warning {
  46. margin: 2em ;
  47. border: medium outset ;
  48. padding: 1em }
  49. div.admonition p.admonition-title, div.hint p.admonition-title,
  50. div.important p.admonition-title, div.note p.admonition-title,
  51. div.tip p.admonition-title {
  52. font-weight: bold ;
  53. font-family: sans-serif }
  54. div.attention p.admonition-title, div.caution p.admonition-title,
  55. div.danger p.admonition-title, div.error p.admonition-title,
  56. div.warning p.admonition-title {
  57. color: red ;
  58. font-weight: bold ;
  59. font-family: sans-serif }
  60. /* Uncomment (and remove this text!) to get reduced vertical space in
  61. compound paragraphs.
  62. div.compound .compound-first, div.compound .compound-middle {
  63. margin-bottom: 0.5em }
  64. div.compound .compound-last, div.compound .compound-middle {
  65. margin-top: 0.5em }
  66. */
  67. div.dedication {
  68. margin: 2em 5em ;
  69. text-align: center ;
  70. font-style: italic }
  71. div.dedication p.topic-title {
  72. font-weight: bold ;
  73. font-style: normal }
  74. div.figure {
  75. margin-left: 2em }
  76. div.footer, div.header {
  77. clear: both;
  78. font-size: smaller }
  79. div.line-block {
  80. display: block ;
  81. margin-top: 1em ;
  82. margin-bottom: 1em }
  83. div.line-block div.line-block {
  84. margin-top: 0 ;
  85. margin-bottom: 0 ;
  86. margin-left: 1.5em }
  87. div.sidebar {
  88. margin-left: 1em ;
  89. border: medium outset ;
  90. padding: 1em ;
  91. background-color: #ffffee ;
  92. width: 40% ;
  93. float: right ;
  94. clear: right }
  95. div.sidebar p.rubric {
  96. font-family: sans-serif ;
  97. font-size: medium }
  98. div.system-messages {
  99. margin: 5em }
  100. div.system-messages h1 {
  101. color: red }
  102. div.system-message {
  103. border: medium outset ;
  104. padding: 1em }
  105. div.system-message p.system-message-title {
  106. color: red ;
  107. font-weight: bold }
  108. div.topic {
  109. margin: 2em }
  110. h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
  111. h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  112. margin-top: 0.4em }
  113. h1.title {
  114. text-align: center }
  115. h2.subtitle {
  116. text-align: center }
  117. hr.docutils {
  118. width: 75% }
  119. img.align-left {
  120. clear: left }
  121. img.align-right {
  122. clear: right }
  123. img.borderless {
  124. border: 0 }
  125. ol.simple, ul.simple {
  126. margin-bottom: 1em }
  127. ol.arabic {
  128. list-style: decimal }
  129. ol.loweralpha {
  130. list-style: lower-alpha }
  131. ol.upperalpha {
  132. list-style: upper-alpha }
  133. ol.lowerroman {
  134. list-style: lower-roman }
  135. ol.upperroman {
  136. list-style: upper-roman }
  137. p.attribution {
  138. text-align: right ;
  139. margin-left: 50% }
  140. p.caption {
  141. font-style: italic }
  142. p.credits {
  143. font-style: italic ;
  144. font-size: smaller }
  145. p.label {
  146. white-space: nowrap }
  147. p.rubric {
  148. font-weight: bold ;
  149. font-size: larger ;
  150. color: maroon ;
  151. text-align: center }
  152. p.sidebar-title {
  153. font-family: sans-serif ;
  154. font-weight: bold ;
  155. font-size: larger }
  156. p.sidebar-subtitle {
  157. font-family: sans-serif ;
  158. font-weight: bold }
  159. p.topic-title {
  160. font-weight: bold }
  161. pre.address {
  162. margin-bottom: 0 ;
  163. margin-top: 0 ;
  164. font-family: serif ;
  165. font-size: 100% }
  166. pre.line-block {
  167. font-family: serif ;
  168. font-size: 100% }
  169. pre.literal-block, pre.doctest-block {
  170. margin-left: 2em ;
  171. margin-right: 2em ;
  172. background-color: #eeeeee }
  173. span.classifier {
  174. font-family: sans-serif ;
  175. font-style: oblique }
  176. span.classifier-delimiter {
  177. font-family: sans-serif ;
  178. font-weight: bold }
  179. span.interpreted {
  180. font-family: sans-serif }
  181. span.option {
  182. white-space: nowrap }
  183. span.pre {
  184. white-space: pre }
  185. span.problematic {
  186. color: red }
  187. span.section-subtitle {
  188. /* font-size relative to parent (h1..h6 element) */
  189. font-size: 80% }
  190. table.citation {
  191. border-left: solid thin gray }
  192. table.docinfo {
  193. margin: 2em 4em }
  194. table.docutils {
  195. margin-top: 0.5em ;
  196. margin-bottom: 0.5em }
  197. table.footnote {
  198. border-left: solid thin black }
  199. table.docutils td, table.docutils th,
  200. table.docinfo td, table.docinfo th {
  201. padding-left: 0.5em ;
  202. padding-right: 0.5em ;
  203. vertical-align: top }
  204. table.docutils th.field-name, table.docinfo th.docinfo-name {
  205. font-weight: bold ;
  206. text-align: left ;
  207. white-space: nowrap ;
  208. padding-left: 0 }
  209. h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
  210. h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  211. font-size: 100% }
  212. tt.docutils {
  213. background-color: #eeeeee }
  214. ul.auto-toc {
  215. list-style-type: none }
  216. </style>
  217. </head>
  218. <body>
  219. <div class="document" id="boost-pointer-container-library">
  220. <h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
  221. <h2 class="subtitle" id="usage-guidelines">Usage Guidelines</h2>
  222. <div class="contents local topic">
  223. <ul class="simple">
  224. <li><a class="reference" href="#choosing-the-right-container" id="id8" name="id8">Choosing the right container</a></li>
  225. <li><a class="reference" href="#recommended-practice-for-object-oriented-programming" id="id9" name="id9">Recommended practice for Object-Oriented Programming</a><ul>
  226. <li><a class="reference" href="#make-base-classes-abstract-and-without-data" id="id10" name="id10">1. Make base classes abstract and without data</a></li>
  227. <li><a class="reference" href="#make-virtual-functions-private-and-provide-a-non-virtual-public-forwarding-function" id="id11" name="id11">2. Make virtual functions private and provide a non-virtual public forwarding function</a></li>
  228. <li><a class="reference" href="#derive-your-base-class-from-boost-noncopyable" id="id12" name="id12">3. Derive your base class from <tt class="docutils literal"><span class="pre">boost::noncopyable</span></tt></a></li>
  229. <li><a class="reference" href="#avoid-null-pointers-in-containers-if-possible" id="id13" name="id13">4. Avoid null-pointers in containers (if possible)</a></li>
  230. </ul>
  231. </li>
  232. </ul>
  233. </div>
  234. <div class="section">
  235. <h1><a class="toc-backref" href="#id8" id="choosing-the-right-container" name="choosing-the-right-container">Choosing the right container</a></h1>
  236. <p>The recommended usage pattern of the container classes is the same as
  237. for normal standard containers.</p>
  238. <p><tt class="docutils literal"><span class="pre">ptr_vector</span></tt>, <tt class="docutils literal"><span class="pre">ptr_list</span></tt> and <tt class="docutils literal"><span class="pre">ptr_deque</span></tt> offer the programmer different
  239. complexity tradeoffs and should be used accordingly. <tt class="docutils literal"><span class="pre">ptr_vector</span></tt> is the
  240. type of sequence that should be used by default. <tt class="docutils literal"><span class="pre">ptr_list</span></tt> should be used
  241. when there are frequent insertions and deletions from the middle of the
  242. sequence and if the container is fairly large (eg. more than 100
  243. elements). <tt class="docutils literal"><span class="pre">ptr_deque</span></tt> is the data structure of choice when most insertions
  244. and deletions take place at the beginning or at the end of the sequence.
  245. The special container <tt class="docutils literal"><span class="pre">ptr_array</span></tt> may be used when the size of the container is invariant
  246. and known at compile time.</p>
  247. <p>An associative container supports unique keys if it may contain at most
  248. one element for each key. Otherwise, it supports equivalent keys.
  249. <tt class="docutils literal"><span class="pre">ptr_set</span></tt> and <tt class="docutils literal"><span class="pre">ptr_map</span></tt> support unique keys.
  250. <tt class="docutils literal"><span class="pre">ptr_multiset</span></tt> and <tt class="docutils literal"><span class="pre">ptr_multimap</span></tt>
  251. support equivalent keys.</p>
  252. </div>
  253. <div class="section">
  254. <h1><a class="toc-backref" href="#id9" id="recommended-practice-for-object-oriented-programming" name="recommended-practice-for-object-oriented-programming">Recommended practice for Object-Oriented Programming</a></h1>
  255. <p>Idiomatic Object-Oriented Programming in C++ looks a bit different from
  256. the way it is done in other languages. This is partly because C++
  257. has both value and reference semantics, and partly because C++ is more flexible
  258. than other languages. Below is a list of recommendations that you are
  259. encouraged to follow:</p>
  260. <div class="section">
  261. <h2><a class="toc-backref" href="#id10" id="make-base-classes-abstract-and-without-data" name="make-base-classes-abstract-and-without-data">1. Make base classes abstract and without data</a></h2>
  262. <p>This has the following advantages:</p>
  263. <blockquote>
  264. <ol class="loweralpha simple">
  265. <li>It reduces <em>coupling</em> because you do not have to maintain or update state</li>
  266. </ol>
  267. <!-- -->
  268. <ol class="loweralpha simple" start="2">
  269. <li>It helps you to avoid <em>slicing</em></li>
  270. </ol>
  271. <!-- -->
  272. <ol class="loweralpha simple" start="3">
  273. <li>It ensures you <em>override</em> the right function</li>
  274. </ol>
  275. </blockquote>
  276. <p>You might also want to read the following articles:</p>
  277. <ul class="simple">
  278. <li>Kevlin Henney's <a class="reference" href="http://www.two-sdg.demon.co.uk/curbralan/papers/SixOfTheBest.pdf">Six of the best</a></li>
  279. </ul>
  280. <ul class="simple">
  281. <li>Jack Reeves' <a class="reference" href="http://www.ddj.com/documents/s=10011/q=1/cuj0602reeves/0602reeves.html">Multiple Inheritance Considered Useful</a></li>
  282. </ul>
  283. </div>
  284. <div class="section">
  285. <h2><a class="toc-backref" href="#id11" id="make-virtual-functions-private-and-provide-a-non-virtual-public-forwarding-function" name="make-virtual-functions-private-and-provide-a-non-virtual-public-forwarding-function">2. Make virtual functions private and provide a non-virtual public forwarding function</a></h2>
  286. <p>In code:</p>
  287. <pre class="literal-block">
  288. class Polymorphic
  289. {
  290. private:
  291. virtual int do_foo() = 0;
  292. public:
  293. int foo()
  294. {
  295. return do_foo();
  296. }
  297. ...
  298. };
  299. </pre>
  300. <p>This has the following advantages:</p>
  301. <blockquote>
  302. <ol class="loweralpha simple">
  303. <li>It makes sure all calls to the virtual function always goes through one place in your code</li>
  304. </ol>
  305. <!-- -->
  306. <ol class="loweralpha simple" start="2">
  307. <li>It enables you to check preconditions and postconditions inside the forwarding function</li>
  308. </ol>
  309. </blockquote>
  310. <p>You might also want to read Herb Sutter's article <a class="reference" href="http://www.gotw.ca/publications/mill18.htm">Virtuality</a>.</p>
  311. </div>
  312. <div class="section">
  313. <h2><a class="toc-backref" href="#id12" id="derive-your-base-class-from-boost-noncopyable" name="derive-your-base-class-from-boost-noncopyable">3. Derive your base class from <tt class="docutils literal docutils literal"><span class="pre">boost::noncopyable</span></tt></a></h2>
  314. <p>Having an abstact base class prevents slicing when the base class is involved, but
  315. it does not prevent it for classes further down the hierarchy. This is where
  316. <a class="reference" href="http://www.boost.org/libs/utility/utility.htm#Class_noncopyable">boost::noncopyable</a> is handy to use:</p>
  317. <pre class="literal-block">
  318. class Polymorphic : boost::noncopyable
  319. {
  320. ...
  321. };
  322. </pre>
  323. </div>
  324. <div class="section">
  325. <h2><a class="toc-backref" href="#id13" id="avoid-null-pointers-in-containers-if-possible" name="avoid-null-pointers-in-containers-if-possible">4. Avoid null-pointers in containers (if possible)</a></h2>
  326. <p>By default the pointer containers do not allow you to store null-pointer in them.
  327. As you might know, this behavior can be changed explicitly with the use
  328. of <a class="reference" href="reference.html#class-nullable">boost::nullable</a>.</p>
  329. <p>The primary reason to avoid null-pointers
  330. is that you have to check for null-pointers every time the container is
  331. used. This extra checking is easy to forget, and it is somewhat contradictory to
  332. the spirit of OO where you replace special cases with dynamic dispatch.</p>
  333. <p>Often, however, you need to place some special object in the container because you
  334. do not have enough information to construct a full object. In that case
  335. you might be able to use the Null Object pattern which simply dictates that
  336. you implement virtual functions from the abstract base-class
  337. as empty functions or with dummy return values. This means that
  338. your OO-code still does not need to worry about null-pointers.</p>
  339. <p>You might want to read</p>
  340. <ul class="simple">
  341. <li>Kevlin Henney's <a class="reference" href="http://www.two-sdg.demon.co.uk/curbralan/papers/europlop/NullObject.pdf">Null Object - Something for Nothing</a></li>
  342. </ul>
  343. <p>Finally you might end up in a situation where not even the Null Object can help
  344. you. That is when you truly need <tt class="docutils literal"><span class="pre">container&lt;</span> <span class="pre">nullable&lt;T&gt;</span> <span class="pre">&gt;</span></tt>.</p>
  345. <hr><p><strong>Navigate:</strong></p>
  346. <ul class="simple">
  347. <li><a class="reference" href="ptr_container.html">home</a></li>
  348. <li><a class="reference" href="reference.html">reference</a></li>
  349. </ul>
  350. <hr><table class="docutils field-list" frame="void" rules="none">
  351. <col class="field-name" />
  352. <col class="field-body" />
  353. <tbody valign="top">
  354. <tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see <a class="reference" href="http://www.boost.org/LICENSE_1_0.txt">LICENSE_1_0.txt</a>).</td>
  355. </tr>
  356. </tbody>
  357. </table>
  358. </div>
  359. </div>
  360. </div>
  361. </body>
  362. </html>