mirror of
https://github.com/boostorg/contract.git
synced 2026-02-26 16:42:19 +00:00
170 lines
6.0 KiB
Plaintext
170 lines
6.0 KiB
Plaintext
|
|
[/ Copyright (C) 2008-2016 Lorenzo Caminiti]
|
|
[/ Distributed under the Boost Software License, Version 1.0 (see accompanying]
|
|
[/ file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).]
|
|
[/ See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html]
|
|
|
|
[section Examples]
|
|
|
|
This section lists examples taken from different sources discussing Contract Programming and re-implemented using this library.
|
|
|
|
[note
|
|
Some of these examples might be from old sources, containing old or obsolete code practices, not optimized for execution speed, not complete, and they might be more relevant in the context of programming languages different from C++.
|
|
Nevertheless, programmers are encouraged to review these examples to see a few diverse usages of this library that might be relevant for contracts they need to write in their code.
|
|
]
|
|
|
|
Sources of the listed examples:
|
|
|
|
* __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).
|
|
|
|
A few notable examples:
|
|
|
|
* [link N1962_vector_anchor \[N1962\] Vector]: Complete contracts for `std::vector` and a comparison with __N1962__ syntax.
|
|
* [link N1962_square_root_anchor \[N1962\] Square Root]: Comparison with D syntax.
|
|
* [link Mitchell02_counter_anchor \[Mitchell02\] Counter]: Subcontracting.
|
|
* [link Meyer97_stack4_anchor \[Meyer97\] Stack4]: Comparison with Eiffel syntax.
|
|
* [link Cline90_vector_anchor \[Cline90\] Vector]: Comparison with A++ syntax.
|
|
|
|
[#N1962_vector_anchor]
|
|
[section \[N1962\] Vector: STL Vector contracts and comparison with C++ contract proposal syntax]
|
|
[import ../example/n1962/vector.cpp]
|
|
[import ../example/n1962/vector_n1962.hpp]
|
|
[table
|
|
[ [This Library] [Contract Programming Proposal \[N1962\] (not part of C++) plus C++17 [^if constexpr]] ]
|
|
[ [[n1962_vector]] [[n1962_vector_n1962]] ]
|
|
]
|
|
|
|
On compilers that support C++17 `if constexpr`, the above example using this library can be simplified removing [funcref boost::contract::condition_if] and related functor templates `all_of_equal_to`, etc. (making the code to the left more similar to the code to the right).
|
|
[endsect]
|
|
|
|
[section \[N1962\] Circle: Subcontracting]
|
|
[import ../example/n1962/circle.cpp]
|
|
[n1962_circle]
|
|
[endsect]
|
|
|
|
[section \[N1962\] Factorial: Recursion and assertion computational complexity]
|
|
[import ../example/n1962/factorial.cpp]
|
|
[n1962_factorial]
|
|
[endsect]
|
|
|
|
[section \[N1962\] Equal: Operators]
|
|
[import ../example/n1962/equal.cpp]
|
|
[n1962_equal]
|
|
[endsect]
|
|
|
|
[section \[N1962\] Sum: Array parameter]
|
|
[import ../example/n1962/sum.cpp]
|
|
[n1962_sum]
|
|
[endsect]
|
|
|
|
[#N1962_square_root_anchor]
|
|
[section \[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]] ]
|
|
]
|
|
[endsect]
|
|
|
|
[#Meyer97_stack4_anchor]
|
|
[section \[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]] [[meyer97_stack4_e]] ]
|
|
[ [[meyer97_stack4_main]] [] ]
|
|
]
|
|
[endsect]
|
|
|
|
[section \[Meyer97\] Stack3: Error codes instead of preconditions]
|
|
[import ../example/meyer97/stack3.cpp]
|
|
[meyer97_stack3]
|
|
[endsect]
|
|
|
|
[section \[Mitchell02\] Name List: Relaxed subcontracts]
|
|
[import ../example/mitchell02/name_list.cpp]
|
|
[mitchell02_name_list]
|
|
[endsect]
|
|
|
|
[section \[Mitchell02\] Dictionary: Key-value map]
|
|
[import ../example/mitchell02/dictionary.cpp]
|
|
[mitchell02_dictionary]
|
|
[endsect]
|
|
|
|
[section \[Mitchell02\] Courier: Subcontracting and static class invariants]
|
|
[import ../example/mitchell02/courier.cpp]
|
|
[mitchell02_courier]
|
|
[endsect]
|
|
|
|
[section \[Mitchell02\] Stack: Stack-like container]
|
|
[import ../example/mitchell02/stack.cpp]
|
|
[mitchell02_stack]
|
|
[endsect]
|
|
|
|
[section \[Mitchell02\] Simple Queue: Queue-like container and assertion big-O complexity]
|
|
[import ../example/mitchell02/simple_queue.cpp]
|
|
[mitchell02_simple_queue]
|
|
[endsect]
|
|
|
|
[section \[Mitchell02\] Customer Manager: Contracts instead of Defensive Programming]
|
|
[import ../example/mitchell02/customer_manager.cpp]
|
|
[mitchell02_customer_manager]
|
|
[endsect]
|
|
|
|
[section \[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]
|
|
[endsect]
|
|
|
|
[#Mitchell02_counter_anchor]
|
|
[section \[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]
|
|
[endsect]
|
|
|
|
[#Cline90_vector_anchor]
|
|
[section \[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]] [] ]
|
|
]
|
|
[endsect]
|
|
|
|
[section \[Cline90\] Stack: Stack-like container]
|
|
[import ../example/cline90/stack.cpp]
|
|
[cline90_stack]
|
|
[endsect]
|
|
|
|
[section \[Cline90\] Vector-Stack: Subcontracting]
|
|
[import ../example/cline90/vstack.cpp]
|
|
[cline90_vstack]
|
|
[endsect]
|
|
|
|
[section \[Cline90\] Calendar: A very simple calendar]
|
|
[import ../example/cline90/calendar.cpp]
|
|
[cline90_calendar]
|
|
[endsect]
|
|
|
|
[endsect]
|
|
|