Home | Libraries | People | FAQ | More |
BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE — Program contracts that can be completely disabled at compile-time for public function overrides.
// In header: <boost/contract_macro.hpp>
BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(...)
This is used together with BOOST_CONTRACT_PRECONDITION
, BOOST_CONTRACT_POSTCONDITION
, BOOST_CONTRACT_EXCEPT
, and BOOST_CONTRACT_OLD
to specify preconditions, postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for public function overrides (virtual or not):
class u #define BASES private boost::contract::constructor_precondition<u>, \ public b, private w : BASES { friend class boost::contract::access; typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types; #undef BASES BOOST_CONTRACT_INVARIANT({ // Optional (as for static and volatile). BOOST_CONTRACT_ASSERT(...); ... }) BOOST_CONTRACT_OVERRIDES(f, g) public: // Override from `b::f`, and void. void f(t_1 a_1, ..., t_n a_n, boost::contract::virtual_* v = 0) { BOOST_CONTRACT_OLD_PTR(old_type)(old_var); BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_f)( v, &u::f, this, a_1, ..., a_n) BOOST_CONTRACT_PRECONDITION([&] { // Optional. BOOST_CONTRACT_ASSERT(...); ... }) BOOST_CONTRACT_OLD([&] { // Optional. old_var = BOOST_CONTRACT_OLDOF(v, old_expr); ... }) BOOST_CONTRACT_POSTCONDITION([&] { // Optional. BOOST_CONTRACT_ASSERT(...); ... }) BOOST_CONTRACT_EXCEPT([&] { // Optional. BOOST_CONTRACT_ASSERT(...); ... }) ; // Trailing `;` is required. ... // Function body. } // Override from `b::g`, and void. t g(t_1 a_1, ..., t_n a_n, boost::contract::virtual_* v = 0) { t result; BOOST_CONTRACT_OLD_PTR(old_type)(old_var); BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_g)( v, result, &u::g, this, a_1, ..., a_n) ... BOOST_CONTRACT_OLD([&] { // Optional. old_var = BOOST_CONTRACT_OLDOF(v, old_expr); ... }) BOOST_CONTRACT_POSTCONDITION([&] (t const& result) { // Optional BOOST_CONTRACT_ASSERT(...); ... }) ... ; // Trailing `;` is required. ... // Function body (use `return result = return_expr`). } ... };
Public function overrides should always use BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE
otherwise this library will not be able to correctly use it for subcontracting.
This is an overloaded variadic macro and it can be used in the following different ways (note that no code is generated when BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
is defined).
1. BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_type)(v, f, obj, ...)
expands to code equivalent to the following (for public function overrides that return void):
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS boost::contract::check internal_var = boost::contract:: public_function<override_type>(v, f, obj, ...) #endif
2. BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_type)(v, r, f, obj, ...)
expands to code equivalent to the following (for public function overrides that do not return void):
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS boost::contract::check internal_var = boost::contract:: public_function<override_type>(v, r, f, obj, ...) #endif
Where (these are all variadic macro parameters so they can contain commas not protected by round parenthesis):
override_type
is the type override_function-name
declared using the BOOST_CONTRACT_OVERRIDE
or related macros.
v
is the extra parameter of type boost::contract::virtual_
*
and default value 0
from the enclosing virtual public function declaring the contract.
r
is a reference to the return value of the enclosing virtual public function declaring the contract. This is usually a local variable declared by the enclosing virtual public function just before the contract, but programmers must set it to the actual value being returned by the function at each return
statement.
f
is a pointer to the enclosing public function override declaring the contract.
obj
is the object this
from the scope of the enclosing public function declaring the contract. This object might be mutable, const
, volatile
, or const volatile
depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see
Volatile Public Functions).
...
is a variadic macro parameter listing all the arguments passed to the enclosing public function override declaring the contract (by reference and in the order they appear in the enclosing function declaration), but excluding the trailing argument v
.
internal_var
is a variable name internally generated by this library (this name is unique but only on different line numbers so this macro cannot be expanded multiple times on the same line).
See Also:
Disable Contract Compilation, Public Function Overrides