examples.qbk 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. [/ Copyright (C) 2008-2018 Lorenzo Caminiti]
  2. [/ Distributed under the Boost Software License, Version 1.0 (see accompanying]
  3. [/ file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).]
  4. [/ See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html]
  5. [section Examples]
  6. This section lists some examples taken from different sources discussing contract programming and implemented here using this library.
  7. [note
  8. Some of these examples might be from old code, containing obsolete coding practices, not optimized for execution speed, not complete, and they might be more relevant in the context of programming languages different from C++.
  9. Nevertheless, programmers are encouraged to review these examples to see a few diverse uses of this library that might be relevant to their needs.
  10. ]
  11. The examples in this sections are taken from the following sources:
  12. * __N1962__: Examples from a detailed and complete proposal to add contract programming to C++11 (unfortunately, this proposal was never accepted into the standard).
  13. * __Meyer97__: Examples from the Eiffel programming language (reprogrammed here in C++ using this library).
  14. * __Mitchell02__: Additional examples from the Eiffel programming language (reprogrammed here in C++ using this library).
  15. * __Cline90__: Examples from a very early proposal called Annotated C++ (A++) to add contract programming to C++ (A++ was never implemented or proposed for addition to the standard).
  16. The following are some examples of particular interest:
  17. * [link N1962_vector_anchor \[N1962\] Vector]: Complete set of contracts for `std::vector`, plus a comparison with __N1962__ syntax.
  18. * [link N1962_square_root_anchor \[N1962\] Square Root]: Comparison with D syntax.
  19. * [link Mitchell02_counter_anchor \[Mitchell02\] Counter]: Subcontracting.
  20. * [link Meyer97_stack4_anchor \[Meyer97\] Stack4]: Comparison with Eiffel syntax.
  21. * [link Cline90_vector_anchor \[Cline90\] Vector]: Comparison with A++ syntax.
  22. Most of the examples listed here use old values and class invariants which are instead not supported by __P0380__.
  23. Therefore, there is not meaningful example here that can be directly implemented and compared using __P0380__ syntax.
  24. [#N1962_vector_anchor]
  25. [section \[N1962\] Vector: Contracts for STL vector and comparison with __N1962__ proposed syntax]
  26. On compilers that support C++17 `if constexpr`, the following example using this library can be simplified removing [funcref boost::contract::condition_if] and related functor templates such as `all_of_equal_to`, etc., making it more similar to the pseudo-code on the right-hand side (see __Assertion_Requirements__).
  27. [import ../example/n1962/vector.cpp]
  28. [import ../example/n1962/vector_n1962.hpp]
  29. [table
  30. [ [This library] [\[N1962\] proposal (not accepted in C++) plus C++17 [^if constexpr]] ]
  31. [ [[n1962_vector]] [[n1962_vector_n1962]] ]
  32. ]
  33. [endsect]
  34. [section \[N1962\] Circle: Subcontracting]
  35. [import ../example/n1962/circle.cpp]
  36. [n1962_circle]
  37. [endsect]
  38. [#N1962_factorial_anchor]
  39. [section \[N1962\] Factorial: Recursion]
  40. [import ../example/n1962/factorial.cpp]
  41. [n1962_factorial]
  42. [endsect]
  43. [section \[N1962\] Equal: Operators]
  44. [import ../example/n1962/equal.cpp]
  45. [n1962_equal]
  46. [endsect]
  47. [section \[N1962\] Sum: Array parameter]
  48. [import ../example/n1962/sum.cpp]
  49. [n1962_sum]
  50. [endsect]
  51. [#N1962_square_root_anchor]
  52. [section \[N1962\] Square Root: Default parameters and comparison with D syntax]
  53. [import ../example/n1962/sqrt.cpp]
  54. [import ../example/n1962/sqrt.d]
  55. [table
  56. [ [This Library] [The D Programming Language] ]
  57. [ [[n1962_sqrt]] [[n1962_sqrt_d]] ]
  58. ]
  59. [endsect]
  60. [#Meyer97_stack4_anchor]
  61. [section \[Meyer97\] Stack4: Comparison with Eiffel syntax]
  62. [import ../example/meyer97/stack4.hpp]
  63. [import ../example/meyer97/stack4_main.cpp]
  64. [import ../example/meyer97/stack4.e]
  65. [table
  66. [ [This Library] [The Eiffel Programming Language] ]
  67. [ [[meyer97_stack4]] [[meyer97_stack4_e]] ]
  68. [ [[meyer97_stack4_main]] [] ]
  69. ]
  70. [endsect]
  71. [section \[Meyer97\] Stack3: Error codes instead of preconditions]
  72. [import ../example/meyer97/stack3.cpp]
  73. [meyer97_stack3]
  74. [endsect]
  75. [section \[Mitchell02\] Name List: Relaxed subcontracts]
  76. [import ../example/mitchell02/name_list.cpp]
  77. [mitchell02_name_list]
  78. [endsect]
  79. [section \[Mitchell02\] Dictionary: Key-value map]
  80. [import ../example/mitchell02/dictionary.cpp]
  81. [mitchell02_dictionary]
  82. [endsect]
  83. [section \[Mitchell02\] Courier: Subcontracting and static class invariants]
  84. [import ../example/mitchell02/courier.cpp]
  85. [mitchell02_courier]
  86. [endsect]
  87. [section \[Mitchell02\] Stack: Stack-like container]
  88. [import ../example/mitchell02/stack.cpp]
  89. [mitchell02_stack]
  90. [endsect]
  91. [section \[Mitchell02\] Simple Queue: Queue-like container and disable old value copies for audit assertions]
  92. [import ../example/mitchell02/simple_queue.cpp]
  93. [mitchell02_simple_queue]
  94. [endsect]
  95. [section \[Mitchell02\] Customer Manager: Contracts instead of defensive programming]
  96. [import ../example/mitchell02/customer_manager.cpp]
  97. [mitchell02_customer_manager]
  98. [endsect]
  99. [section \[Mitchell02\] Observer: Pure virtual functions]
  100. [import ../example/mitchell02/observer/observer.hpp]
  101. [mitchell02_observer]
  102. [import ../example/mitchell02/observer/subject.hpp]
  103. [mitchell02_subject]
  104. [import ../example/mitchell02/observer_main.cpp]
  105. [mitchell02_observer_main]
  106. [endsect]
  107. [#Mitchell02_counter_anchor]
  108. [section \[Mitchell02\] Counter: Subcontracting]
  109. [import ../example/mitchell02/counter/push_button.hpp]
  110. [mitchell02_push_button]
  111. [import ../example/mitchell02/counter/decrement_button.hpp]
  112. [mitchell02_decrement_button]
  113. [import ../example/mitchell02/counter/counter.hpp]
  114. [mitchell02_counter]
  115. [import ../example/mitchell02/counter_main.cpp]
  116. [mitchell02_counter_main]
  117. [endsect]
  118. [#Cline90_vector_anchor]
  119. [section \[Cline90\] Vector: Comparison with A++ proposal syntax]
  120. [import ../example/cline90/vector.hpp]
  121. [import ../example/cline90/vector_main.cpp]
  122. [import ../example/cline90/vector_axx.hpp]
  123. [table
  124. [ [This Library] [A++ Proposal (never actually implemented)] ]
  125. [ [[cline90_vector]] [[cline90_vector_axx]] ]
  126. [ [[cline90_vector_main]] [] ]
  127. ]
  128. [endsect]
  129. [section \[Cline90\] Stack: Stack-like container]
  130. [import ../example/cline90/stack.cpp]
  131. [cline90_stack]
  132. [endsect]
  133. [section \[Cline90\] Vector-Stack: Subcontracting]
  134. [import ../example/cline90/vstack.cpp]
  135. [cline90_vstack]
  136. [endsect]
  137. [section \[Cline90\] Calendar: A very simple calendar]
  138. [import ../example/cline90/calendar.cpp]
  139. [cline90_calendar]
  140. [endsect]
  141. [endsect]