mirror of
https://github.com/boostorg/contract.git
synced 2026-02-26 16:42:19 +00:00
141 lines
5.8 KiB
Plaintext
141 lines
5.8 KiB
Plaintext
|
|
[section Examples]
|
|
|
|
This section lists a few non-trivial examples taken from different Contract Programming sources and re-implemented using this library.
|
|
|
|
Some of these examples might be from old sources, containing old or obsolete code practices, not optimized for execution speed, and they be more relevant in the context of other programming languages different from C++ (because the considered sources were also for the Eiffel and D programming languages).
|
|
Nevertheless, programmers are encouraged to review these examples to see a few diverse uses of this library that could program contracts in ways similar to what programmers need in their specific C++ applications.
|
|
|
|
Sources:
|
|
|
|
* __N1962__: Examples from the proposal to add Contract Programming to C++11 (unfortunately, this proposal was never accepted into the standard).
|
|
* __Meyer97__: Examples using the Eiffel programming language and reprogrammed using this library for C++.
|
|
* __Mitchell02__: Additional examples using the Eiffel programming language and reprogrammed using this library for C++.
|
|
* __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).
|
|
* __Stroustrup97__: One example that shows the importance of class invariants and how to configure this library to throw exceptions instead of terminating the program on contract failure.
|
|
|
|
Most interesting examples:
|
|
|
|
* [link contract__.examples.__n1962___vector__comparison_with_c___proposed_syntax \[N1962\] Vector]: The complete contracts of `std::vector` and a comparison with __N1962__ syntax.
|
|
* [link contract__.examples.__n1962___square_root__default_parameters_and_comparison_with_d_syntax \[N1962\] Square Root]: A comparison with D syntax.
|
|
* [link contract__.examples.__mitchell02___counter__subcontracting_and_virtual_specifiers__final__override__new__and_pure_virtual_ \[Mitchell02\] Counter]: Subcontracting.
|
|
* [link contract__.examples.__meyer97___stack4__comparison_with_eiffel_syntax \[Meyer97\] Stack4]: A comparison with Eiffel syntax.
|
|
* [link contract__.examples.__cline90___vector__comparison_with_a___proposed_syntax \[Cline90\] Vector]: A comparison with A++ syntax.
|
|
|
|
[heading \[N1962\] Vector: Comparison with C++ proposal's syntax]
|
|
[import ../example/n1962/vector.cpp]
|
|
[import ../example/n1962/vector_n1962.hpp]
|
|
[table
|
|
[ [This Library] [\[N1962\] Proposal (not part of C++)] ]
|
|
[ [[n1962_vector]] [[n1962_vector_n1962]] ]
|
|
]
|
|
|
|
[heading \[N1962\] Circle: Subcontracting]
|
|
[import ../example/n1962/circle.cpp]
|
|
[n1962_circle]
|
|
|
|
[heading \[N1962\] Factorial: Recursion and assertion computational complexity]
|
|
[import ../example/n1962/factorial.cpp]
|
|
[n1962_factorial]
|
|
|
|
[heading \[N1962\] Equal: Operators]
|
|
[import ../example/n1962/equal.cpp]
|
|
[n1962_equal]
|
|
|
|
[heading \[N1962\] Sum: Array parameter]
|
|
[import ../example/n1962/sum.cpp]
|
|
[n1962_sum]
|
|
|
|
[heading \[N1962\] Square Root: Default parameters and comparison with D syntax]
|
|
[import ../example/n1962/sqrt.cpp]
|
|
[import ../example/n1962/sqrt.d]
|
|
[table
|
|
[ [This Library] [The D Programming Language] ]
|
|
[ [[n1962_sqrt]] [[n1962_sqrt_d]] ]
|
|
]
|
|
|
|
[heading \[Meyer97\] Stack4: Comparison with Eiffel syntax]
|
|
[import ../example/meyer97/stack4.hpp]
|
|
[import ../example/meyer97/stack4_main.cpp]
|
|
[import ../example/meyer97/stack4.e]
|
|
[table
|
|
[ [This Library] [The Eiffel Programming Language] ]
|
|
[ [[meyer97_stack4_header]] [[meyer97_stack4_e]] ]
|
|
[ [[meyer97_stack4_main]] [] ]
|
|
]
|
|
|
|
[heading \[Meyer97\] Stack3: Error codes instead of preconditions]
|
|
[import ../example/meyer97/stack3.cpp]
|
|
[meyer97_stack3]
|
|
|
|
[heading \[Mitchell02\] Name List: Relaxing subcontracts]
|
|
[import ../example/mitchell02/name_list.cpp]
|
|
[mitchell02_name_list]
|
|
|
|
[heading \[Mitchell02\] Dictionary: Key-value map]
|
|
[import ../example/mitchell02/dictionary.cpp]
|
|
[mitchell02_dictionary]
|
|
|
|
[heading \[Mitchell02\] Courier: Subcontracting and static class invariants]
|
|
[import ../example/mitchell02/courier.cpp]
|
|
[mitchell02_courier]
|
|
|
|
[heading \[Mitchell02\] Stack: Stack-like container]
|
|
[import ../example/mitchell02/stack.cpp]
|
|
[mitchell02_stack]
|
|
|
|
[heading \[Mitchell02\] Simple Queue: Queue-like container]
|
|
[import ../example/mitchell02/simple_queue.cpp]
|
|
[mitchell02_simple_queue]
|
|
|
|
[heading \[Mitchell02\] Customer Manager: Contracts instead of Defensive Programming]
|
|
[import ../example/mitchell02/customer_manager.cpp]
|
|
[mitchell02_customer_manager]
|
|
|
|
[heading \[Mitchell02\] Observer: Pure virtual functions]
|
|
[import ../example/mitchell02/observer/observer.hpp]
|
|
[mitchell02_observer]
|
|
[import ../example/mitchell02/observer/subject.hpp]
|
|
[mitchell02_subject]
|
|
[import ../example/mitchell02/observer_main.cpp]
|
|
[mitchell02_observer_main]
|
|
|
|
[heading \[Mitchell02\] Counter: Subcontracting]
|
|
[import ../example/mitchell02/counter/push_button.hpp]
|
|
[mitchell02_push_button]
|
|
[import ../example/mitchell02/counter/decrement_button.hpp]
|
|
[mitchell02_decrement_button]
|
|
[import ../example/mitchell02/counter/counter.hpp]
|
|
[mitchell02_counter]
|
|
[import ../example/mitchell02/counter_main.cpp]
|
|
[mitchell02_counter_main]
|
|
|
|
[heading \[Stroustrup97\] String: Throw when contracts fail]
|
|
[import ../example/stroustrup97/string.cpp]
|
|
[stroustrup97_string]
|
|
|
|
[heading \[Cline90\] Vector: Comparison with A++ proposed syntax]
|
|
[import ../example/cline90/vector.hpp]
|
|
[import ../example/cline90/vector_main.cpp]
|
|
[import ../example/cline90/vector_axx.hpp]
|
|
[table
|
|
[ [This Library] [A++ Proposal (not part of C++)] ]
|
|
[ [[cline90_vector]] [[cline90_vector_axx]] ]
|
|
[ [[cline90_vector_main]] [] ]
|
|
]
|
|
|
|
[heading \[Cline90\] Stack: Stack-like container]
|
|
[import ../example/cline90/stack.cpp]
|
|
[cline90_stack]
|
|
|
|
[heading \[Cline90\] Vector-Stack: Subcontracting]
|
|
[import ../example/cline90/vstack.cpp]
|
|
[cline90_vstack]
|
|
|
|
[heading \[Cline90\] Calendar: A very simple calendar]
|
|
[import ../example/cline90/calendar.cpp]
|
|
[cline90_calendar]
|
|
|
|
[endsect]
|
|
|