guidelines.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ++++++++++++++++++++++++++++++++++
  2. |Boost| Pointer Container Library
  3. ++++++++++++++++++++++++++++++++++
  4. .. |Boost| image:: boost.png
  5. ================
  6. Usage Guidelines
  7. ================
  8. .. contents:: :local:
  9. Choosing the right container
  10. ----------------------------
  11. The recommended usage pattern of the container classes is the same as
  12. for normal standard containers.
  13. ``ptr_vector``, ``ptr_list`` and ``ptr_deque`` offer the programmer different
  14. complexity tradeoffs and should be used accordingly. ``ptr_vector`` is the
  15. type of sequence that should be used by default. ``ptr_list`` should be used
  16. when there are frequent insertions and deletions from the middle of the
  17. sequence and if the container is fairly large (eg. more than 100
  18. elements). ``ptr_deque`` is the data structure of choice when most insertions
  19. and deletions take place at the beginning or at the end of the sequence.
  20. The special container ``ptr_array`` may be used when the size of the container is invariant
  21. and known at compile time.
  22. An associative container supports unique keys if it may contain at most
  23. one element for each key. Otherwise, it supports equivalent keys.
  24. ``ptr_set`` and ``ptr_map`` support unique keys.
  25. ``ptr_multiset`` and ``ptr_multimap``
  26. support equivalent keys.
  27. Recommended practice for Object-Oriented Programming
  28. ----------------------------------------------------
  29. Idiomatic Object-Oriented Programming in C++ looks a bit different from
  30. the way it is done in other languages. This is partly because C++
  31. has both value and reference semantics, and partly because C++ is more flexible
  32. than other languages. Below is a list of recommendations that you are
  33. encouraged to follow:
  34. 1. Make base classes abstract and without data
  35. ++++++++++++++++++++++++++++++++++++++++++++++
  36. This has the following advantages:
  37. a. It reduces *coupling* because you do not have to maintain or update state
  38. ..
  39. b. It helps you to avoid *slicing*
  40. ..
  41. c. It ensures you *override* the right function
  42. You might also want to read the following articles:
  43. - Kevlin Henney's `Six of the best`__
  44. .. __: http://www.two-sdg.demon.co.uk/curbralan/papers/SixOfTheBest.pdf
  45. - Jack Reeves' `Multiple Inheritance Considered Useful`__
  46. .. __: http://www.ddj.com/documents/s=10011/q=1/cuj0602reeves/0602reeves.html
  47. 2. Make virtual functions private and provide a non-virtual public forwarding function
  48. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  49. In code::
  50. class Polymorphic
  51. {
  52. private:
  53. virtual int do_foo() = 0;
  54. public:
  55. int foo()
  56. {
  57. return do_foo();
  58. }
  59. ...
  60. };
  61. This has the following advantages:
  62. a. It makes sure all calls to the virtual function always goes through one place in your code
  63. ..
  64. b. It enables you to check preconditions and postconditions inside the forwarding function
  65. You might also want to read Herb Sutter's article `Virtuality`__.
  66. .. __: http://www.gotw.ca/publications/mill18.htm
  67. 3. Derive your base class from ``boost::noncopyable``
  68. +++++++++++++++++++++++++++++++++++++++++++++++++++++
  69. Having an abstact base class prevents slicing when the base class is involved, but
  70. it does not prevent it for classes further down the hierarchy. This is where
  71. `boost::noncopyable`__ is handy to use::
  72. class Polymorphic : boost::noncopyable
  73. {
  74. ...
  75. };
  76. .. __ : http://www.boost.org/libs/utility/utility.htm#Class_noncopyable
  77. 4. Avoid null-pointers in containers (if possible)
  78. ++++++++++++++++++++++++++++++++++++++++++++++++++
  79. By default the pointer containers do not allow you to store null-pointer in them.
  80. As you might know, this behavior can be changed explicitly with the use
  81. of `boost::nullable`__.
  82. The primary reason to avoid null-pointers
  83. is that you have to check for null-pointers every time the container is
  84. used. This extra checking is easy to forget, and it is somewhat contradictory to
  85. the spirit of OO where you replace special cases with dynamic dispatch.
  86. .. __: reference.html#class-nullable
  87. Often, however, you need to place some special object in the container because you
  88. do not have enough information to construct a full object. In that case
  89. you might be able to use the Null Object pattern which simply dictates that
  90. you implement virtual functions from the abstract base-class
  91. as empty functions or with dummy return values. This means that
  92. your OO-code still does not need to worry about null-pointers.
  93. You might want to read
  94. - Kevlin Henney's `Null Object - Something for Nothing`__
  95. .. __: http://www.two-sdg.demon.co.uk/curbralan/papers/europlop/NullObject.pdf
  96. Finally you might end up in a situation where not even the Null Object can help
  97. you. That is when you truly need ``container< nullable<T> >``.
  98. .. raw:: html
  99. <hr>
  100. **Navigate:**
  101. - `home <ptr_container.html>`_
  102. - `reference <reference.html>`_
  103. .. raw:: html
  104. <hr>
  105. :Copyright: Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see LICENSE_1_0.txt__).
  106. __ http://www.boost.org/LICENSE_1_0.txt