17642 lines
2.3 MiB
17642 lines
2.3 MiB
<?xml version="1.0"?>
|
|
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
|
|
<chapter xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" id="boost_contract" rev:last-revision="$Date: 2021/04/13 16:29:48 $">
|
|
<chapterinfo><author>
|
|
<firstname>Lorenzo</firstname> <surname>Caminiti <email>lorcaminiti@gmail.com</email></surname>
|
|
</author><copyright>
|
|
<year>2008</year> <year>2009</year> <year>2010</year> <year>2011</year> <year>2012</year>
|
|
<year>2013</year> <year>2014</year> <year>2015</year> <year>2016</year> <year>2017</year>
|
|
<year>2018</year> <year>2019</year> <holder>Lorenzo Caminiti</holder>
|
|
</copyright><legalnotice id="boost_contract.legal">
|
|
<para>
|
|
Distributed under the Boost Software License, Version 1.0 (see accompanying
|
|
file LICENSE_1_0.txt or a copy at <ulink url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)
|
|
</para>
|
|
</legalnotice></chapterinfo>
|
|
<title>Boost.Contract 1.0.0</title>
|
|
<blockquote>
|
|
<para>
|
|
<emphasis><quote>Our field needs more formality, but the profession has not
|
|
realized it yet.</quote></emphasis>
|
|
</para>
|
|
</blockquote>
|
|
<blockquote>
|
|
<para>
|
|
<emphasis>-- Bertrand Meyer (see <link linkend="Meyer97_anchor">[Meyer97]</link>
|
|
page 400)</emphasis>
|
|
</para>
|
|
</blockquote>
|
|
<para>
|
|
This library implements <ulink url="http://en.wikipedia.org/wiki/Design_by_contract">contract
|
|
programming</ulink> (a.k.a., Design by Contract or DbC) <footnote id="boost_contract.f0">
|
|
<para>
|
|
Design by Contract (DbC) is a registered trademark of the Eiffel Software company
|
|
and it was first introduced by the Eiffel programming language (see <link linkend="Meyer97_anchor">[Meyer97]</link>).
|
|
</para>
|
|
</footnote> for the C++ programming language. All contract programming features
|
|
are supported by this library: Subcontracting, class invariants (also for static
|
|
and volatile member functions), postconditions (with old and return values),
|
|
preconditions, customizable actions on assertion failure (e.g., terminate the
|
|
program or throw exceptions), optional compilation of assertions, disable assertions
|
|
while already checking other assertions (to avoid infinite recursion), and more
|
|
(see <link linkend="boost_contract.contract_programming_overview.feature_summary">Feature
|
|
Summary</link>).
|
|
</para>
|
|
<section id="boost_contract.introduction">
|
|
<title><link linkend="boost_contract.introduction">Introduction</link></title>
|
|
<para>
|
|
Contract programming allows to specify preconditions, postconditions, and class
|
|
invariants that are automatically checked when functions are executed at run-time.
|
|
These conditions assert program specifications within the source code itself
|
|
allowing to find bugs more quickly during testing, making the code self-documenting,
|
|
and increasing overall software quality (see <link linkend="boost_contract.contract_programming_overview">Contract
|
|
Programming Overview</link>).
|
|
</para>
|
|
<para>
|
|
For example, consider the following function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput>
|
|
that increments its argument <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput>
|
|
by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">1</phrase></computeroutput> and let's write its contract
|
|
using code comments (see <ulink url="../../example/features/introduction_comments.cpp"><literal moreinfo="none">introduction_comments.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Precondition: x < std::numeric_limit<int>::max()</phrase>
|
|
<phrase role="comment">// Postcondition: x == oldof(x) + 1</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="special">++</phrase><phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The precondition states that at function entry the argument <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput>
|
|
must be strictly smaller than the maximum allowable value of its type (so it
|
|
can be incremented by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">1</phrase></computeroutput> without
|
|
overflowing). The postcondition states that at function exit the argument
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput> must be incremented by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">1</phrase></computeroutput> with respect to the <emphasis>old value</emphasis>
|
|
that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput> had before executing
|
|
the function (indicated here by <literal moreinfo="none"><emphasis>oldof</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">)</phrase></computeroutput>).
|
|
Note that postconditions shall be checked only when the execution of the function
|
|
body does not throw an exception.
|
|
</para>
|
|
<para>
|
|
Now let's program this function and its contract using this library (see <ulink url="../../example/features/introduction.cpp"><literal moreinfo="none">introduction.cpp</literal></ulink>
|
|
and <link linkend="boost_contract.tutorial.non_member_functions">Non-Member
|
|
Functions</link>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="comment">// Old value.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">());</phrase> <phrase role="comment">// Line 17.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Line 20.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">++</phrase><phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
When the above function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput>
|
|
is called, this library will:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
First, execute the functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
that asserts <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput> precondition.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Then, execute <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput> body
|
|
(i.e., all the code that follows the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="special">...</phrase></computeroutput> declaration).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Last, execute the functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
that asserts <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput> postcondition
|
|
(unless <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput> body threw
|
|
an exception).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
For example, if there is a bug in the code calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput>
|
|
so that the function is called with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput>
|
|
equal to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">()</phrase></computeroutput> then the program will terminate with an error
|
|
message similar to the following (and it will be evident that the bug is in
|
|
the calling code):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">precondition assertion "x < std::numeric_limits<int>::max()" failed: file "introduction.cpp", line 17
|
|
</programlisting>
|
|
<para>
|
|
Instead, if there is a bug in the implementation of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput>
|
|
so that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput> is not incremented
|
|
by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">1</phrase></computeroutput> after the execution of the
|
|
function body then the program will terminate with an error message similar
|
|
to the following (and it will be evident that the bug is in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">inc</phrase></computeroutput>
|
|
body): <footnote id="boost_contract.introduction.f0">
|
|
<para>
|
|
In this example the function body is composed of a single trivial instruction
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">++</phrase><phrase role="identifier">x</phrase></computeroutput>
|
|
so it easy to check by visual inspection that it does not contain any bug
|
|
and it will always increment <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput>
|
|
by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">1</phrase></computeroutput> thus the function postcondition
|
|
will never fail. In real production code, function bodies are rarely this
|
|
simple and can hide bugs which make checking postconditions useful.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">postcondition assertion "x == *old_x + 1" failed: file "introduction.cpp", line 20
|
|
</programlisting>
|
|
<para>
|
|
By default, when an assertion fails this library prints an error message such
|
|
the ones above to the standard error <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> and
|
|
terminates the program calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>
|
|
(this behaviour can be customized to take any user-specified action including
|
|
throwing exceptions, see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link>). Note that the error messages printed by this library contain
|
|
all the information necessary to easily and uniquely identify the point in
|
|
the code at which contract assertions fail. <footnote id="boost_contract.introduction.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The assertion failure message
|
|
printed by this library follows a format similar to the message printed by
|
|
Clang when the C-style <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>
|
|
macro fails.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
C++11 lambda functions are necessary to use this library without manually
|
|
writing a significant amount of boiler-plate code to program functors that
|
|
assert the contracts (see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link>). That said, this library implementation does not
|
|
use C++11 features and should work on most modern C++ compilers (see <link linkend="boost_contract.getting_started">Getting Started</link>).
|
|
</para>
|
|
</note>
|
|
<para>
|
|
In addition to contracts for non-member functions as shown the in the example
|
|
above, this library allows to program contracts for constructors, destructors,
|
|
and member functions. These can check class invariants and can also <emphasis>subcontract</emphasis>
|
|
inheriting and extending contracts from base classes (see <ulink url="../../example/features/introduction_public.cpp"><literal moreinfo="none">introduction_public.cpp</literal></ulink>
|
|
and <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>): <footnote id="boost_contract.introduction.f2">
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">pushable</phrase></computeroutput> base class is
|
|
used in this example just to show subcontracting, it is somewhat arbitrary
|
|
and it will likely not appear in real production code.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// For subcontracting.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Checked in AND with base class invariants.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase> <phrase role="comment">// For virtuals.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase> <phrase role="comment">// Old values for virtuals.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase> <phrase role="comment">// For overrides.</phrase>
|
|
<phrase role="identifier">override_push_back</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vector</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Checked in OR with base preconditions.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Checked in AND with base postconditions.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">push_back</phrase><phrase role="special">)</phrase> <phrase role="comment">// Define `override_push_back` above.</phrase>
|
|
|
|
<phrase role="comment">// Could program contracts for those as well.</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">max_size</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<bridgehead renderas="sect3" id="boost_contract.introduction.h0">
|
|
<phrase id="boost_contract.introduction.language_support"/><link linkend="boost_contract.introduction.language_support">Language
|
|
Support</link>
|
|
</bridgehead>
|
|
<para>
|
|
The authors of this library advocate for contracts to be added to the core
|
|
language. Adding contract programming to the C++ standard has a number of advantages
|
|
over any library implementation (shorter and more concise syntax to program
|
|
contracts, specify contracts in declarations instead of definitions, enforce
|
|
contract constant-correctness, expected faster compile- and run-time, vendors
|
|
could develop static analysis tools to recognize and check contracts statically
|
|
when possible, compiler optimizations could be improved based on contract conditions,
|
|
etc.).
|
|
</para>
|
|
<para>
|
|
The <link linkend="P0380_anchor">[P0380]</link> proposal supports basic contract
|
|
programming, it was accepted and it will be included in C++20. This is undoubtedly
|
|
a step in the right direction, but unfortunately <link linkend="P0380_anchor">[P0380]</link>
|
|
only supports pre- and postconditions while missing important features such
|
|
as class invariants and old values in postconditions, not to mention the lack
|
|
of more advanced features like subcontracting (more complete proposals like
|
|
<link linkend="N1962_anchor">[N1962]</link> were rejected by the C++ standard
|
|
committee). All contracting programming features are instead supported by this
|
|
library (see <link linkend="boost_contract.contract_programming_overview.feature_summary">Feature
|
|
Summary</link> for a detailed comparison between the features supported by
|
|
this library and the ones listed in different contract programming proposals,
|
|
see <link linkend="boost_contract.bibliography">Bibliography</link> for a list
|
|
of references considered during the design and implementation of this library,
|
|
including the vast majority of contract programming proposals submitted to
|
|
the C++ standard committee).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.full_table_of_contents">
|
|
<title><link linkend="boost_contract.full_table_of_contents">Full Table of Contents</link></title>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost_contract.introduction">Introduction</link>
|
|
<link linkend="boost_contract.full_table_of_contents">Full Table of Contents</link>
|
|
<link linkend="boost_contract.getting_started">Getting Started</link>
|
|
<link linkend="boost_contract.getting_started.this_documentation">This Documentation</link>
|
|
<link linkend="boost_contract.getting_started.compilers_and_platforms">Compilers and Platforms</link>
|
|
<link linkend="boost_contract.getting_started.code_organization">Code Organization</link>
|
|
<link linkend="boost_contract.getting_started.build">Build</link>
|
|
<link linkend="boost_contract.contract_programming_overview">Contract Programming Overview</link>
|
|
<link linkend="boost_contract.contract_programming_overview.assertions">Assertions</link>
|
|
<link linkend="boost_contract.contract_programming_overview.benefits_and_costs">Benefits and Costs</link>
|
|
<link linkend="boost_contract.contract_programming_overview.function_calls">Function Calls</link>
|
|
<link linkend="boost_contract.contract_programming_overview.public_function_calls">Public Function Calls</link>
|
|
<link linkend="boost_contract.contract_programming_overview.constructor_calls">Constructor Calls</link>
|
|
<link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor Calls</link>
|
|
<link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>
|
|
<link linkend="boost_contract.contract_programming_overview.specifications_vs__implementation">Specifications vs. Implementation</link>
|
|
<link linkend="boost_contract.contract_programming_overview.on_contract_failures">On Contract Failures</link>
|
|
<link linkend="boost_contract.contract_programming_overview.feature_summary">Feature Summary</link>
|
|
<link linkend="boost_contract.tutorial">Tutorial</link>
|
|
<link linkend="boost_contract.tutorial.non_member_functions">Non-Member Functions</link>
|
|
<link linkend="boost_contract.tutorial.preconditions">Preconditions</link>
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>
|
|
<link linkend="boost_contract.tutorial.return_values">Return Values</link>
|
|
<link linkend="boost_contract.tutorial.old_values">Old Values</link>
|
|
<link linkend="boost_contract.tutorial.exception_guarantees">Exception Guarantees</link>
|
|
<link linkend="boost_contract.tutorial.class_invariants">Class Invariants</link>
|
|
<link linkend="boost_contract.tutorial.constructors">Constructors</link>
|
|
<link linkend="boost_contract.tutorial.destructors">Destructors</link>
|
|
<link linkend="boost_contract.tutorial.public_functions">Public Functions</link>
|
|
<link linkend="boost_contract.tutorial.virtual_public_functions">Virtual Public Functions</link>
|
|
<link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public Function Overrides (Subcontracting)</link>
|
|
<link linkend="boost_contract.tutorial.base_classes__subcontracting_">Base Classes (Subcontracting)</link>
|
|
<link linkend="boost_contract.tutorial.static_public_functions">Static Public Functions</link>
|
|
<link linkend="boost_contract.advanced">Advanced</link>
|
|
<link linkend="boost_contract.advanced.pure_virtual_public_functions">Pure Virtual Public Functions</link>
|
|
<link linkend="boost_contract.advanced.optional_return_values">Optional Return Values</link>
|
|
<link linkend="boost_contract.advanced.private_and_protected_functions">Private and Protected Functions</link>
|
|
<link linkend="boost_contract.advanced.friend_functions">Friend Functions</link>
|
|
<link linkend="boost_contract.advanced.function_overloads">Function Overloads</link>
|
|
<link linkend="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__">Lambdas, Loops, Code Blocks (and <computeroutput><phrase role="keyword">constexpr</phrase></computeroutput>)</link>
|
|
<link linkend="boost_contract.advanced.implementation_checks">Implementation Checks</link>
|
|
<link linkend="boost_contract.advanced.old_values_copied_at_body">Old Values Copied at Body</link>
|
|
<link linkend="boost_contract.advanced.named_overrides">Named Overrides</link>
|
|
<link linkend="boost_contract.advanced.access_specifiers">Access Specifiers</link>
|
|
<link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw on Failures (and <computeroutput><phrase role="keyword">noexcept</phrase></computeroutput>)</link>
|
|
<link linkend="boost_contract.extras">Extras</link>
|
|
<link linkend="boost_contract.extras.old_value_requirements__templates_">Old Value Requirements (Templates)</link>
|
|
<link linkend="boost_contract.extras.assertion_requirements__templates_">Assertion Requirements (Templates)</link>
|
|
<link linkend="boost_contract.extras.volatile_public_functions">Volatile Public Functions</link>
|
|
<link linkend="boost_contract.extras.move_operations">Move Operations</link>
|
|
<link linkend="boost_contract.extras.unions">Unions</link>
|
|
<link linkend="boost_contract.extras.assertion_levels">Assertion Levels</link>
|
|
<link linkend="boost_contract.extras.disable_contract_checking">Disable Contract Checking</link>
|
|
<link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable Contract Compilation (Macro Interface)</link>
|
|
<link linkend="boost_contract.extras.separate_body_implementation">Separate Body Implementation</link>
|
|
<link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No Lambda Functions (No C++11)</link>
|
|
<link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No Macros (and No Variadic Macros)</link>
|
|
<link linkend="boost_contract.examples">Examples</link>
|
|
<ulink url="reference.html">Reference</ulink>
|
|
<link linkend="boost_contract.release_notes">Release Notes</link>
|
|
<link linkend="boost_contract.bibliography">Bibliography</link>
|
|
<link linkend="boost_contract.acknowledgments">Acknowledgments</link>
|
|
</programlisting>
|
|
</section>
|
|
<section id="boost_contract.getting_started">
|
|
<title><link linkend="boost_contract.getting_started">Getting Started</link></title>
|
|
<para>
|
|
This section shows how to setup and start using this library.
|
|
</para>
|
|
<section id="boost_contract.getting_started.this_documentation">
|
|
<title><link linkend="boost_contract.getting_started.this_documentation">This
|
|
Documentation</link></title>
|
|
<para>
|
|
Programmers should be able to start using this library after reading the
|
|
<link linkend="boost_contract.introduction">Introduction</link>, <link linkend="boost_contract.getting_started">Getting
|
|
Started</link>, and <link linkend="boost_contract.tutorial">Tutorial</link>.
|
|
Other sections of this documentation (e.g., <link linkend="boost_contract.advanced">Advanced</link>
|
|
and <link linkend="boost_contract.extras">Extras</link>) can be consulted
|
|
at a later point to gain a more in-depth knowledge of the library. <link linkend="boost_contract.contract_programming_overview">Contract Programming
|
|
Overview</link> can be skipped by programmers that are already familiar with
|
|
the contract programming methodology.
|
|
</para>
|
|
<para>
|
|
Some of the source code listed in this documentation contains special code
|
|
comments of the form <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">//[...</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">//]</phrase></computeroutput>. These mark sections
|
|
of the code that are automatically extracted from the source code and presented
|
|
as part of this documentation. <footnote id="boost_contract.getting_started.this_documentation.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> This allows to make sure that
|
|
most of the example code presented in this documentation is always up-to-date,
|
|
builds and runs with the latest implementation of the library.
|
|
</para>
|
|
</footnote> It should be noted that the purpose of all examples of this documentation
|
|
is to illustrate how to use this library and not to show real production
|
|
code.
|
|
</para>
|
|
<para>
|
|
Some footnotes are marked by the word "<emphasis role="bold">Rationale</emphasis>".
|
|
These explain some of the decisions made during the design and implementation
|
|
of this library.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.getting_started.compilers_and_platforms">
|
|
<title><link linkend="boost_contract.getting_started.compilers_and_platforms">Compilers
|
|
and Platforms</link></title>
|
|
<para>
|
|
In general, this library requires C++ compilers with a sound implementation
|
|
of SFINAE and other template meta-programming techniques supported by the
|
|
C++03 standard. It is possible to use this library without C++11 lambda functions
|
|
but a large amount of boiler-plate code is required to manually program separate
|
|
functors to specify preconditions, postconditions, etc. (so using this library
|
|
without C++11 lambda functions is possible but not recommended, see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No Lambda
|
|
Functions</link>). It is also possible to use this library without variadic
|
|
macros by manually programming a small amount of boiler-plate code (but most
|
|
if not all modern C++ compilers support variadic macros even before C++99
|
|
and C++11 so this should never be needed in practice, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>).
|
|
</para>
|
|
<para>
|
|
Some parts of this documentation use the syntax <literal moreinfo="none"><emphasis>type-of</emphasis>(...)</literal>
|
|
to indicate an operator logically equivalent to C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">decltype</phrase><phrase role="special">(...)</phrase></computeroutput>. However, this library implementation
|
|
does not actually use type deduction in these cases (because the library
|
|
internally already knows the types in question) so support for C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">decltype</phrase></computeroutput> and other type-of implementations
|
|
are not actually required (that is why <literal moreinfo="none"><emphasis>type-of</emphasis></literal>
|
|
and not the real <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">decltype</phrase></computeroutput> operator
|
|
is used in this documentation).
|
|
</para>
|
|
<para>
|
|
This library has been developed and tested using:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Visual Studio 2015 on Windows (MSVC <literal moreinfo="none">cl</literal> version 19.00.24215.1).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
GCC version 5.4.0 on Cygwin (with C++11 features enabled <literal moreinfo="none">-std=c++11</literal>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Clang version 3.8.1 on Cygwin (with C++11 features enabled <literal moreinfo="none">-std=c++11</literal>).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
For information on other compilers and platforms see the library <ulink url="http://www.boost.org/development/tests/master/developer/contract.html">regression
|
|
tests</ulink>. The development and maintenance of this library is hosted
|
|
on <ulink url="https://github.com/boostorg/contract">GitHub</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.getting_started.code_organization">
|
|
<title><link linkend="boost_contract.getting_started.code_organization">Code
|
|
Organization</link></title>
|
|
<para>
|
|
Let <literal moreinfo="none"><emphasis>boost-root</emphasis></literal> be the directory where
|
|
Boost source files were installed. This library flies are organized as follows:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><emphasis>boost-root</emphasis>/libs/contract # Directory where this library files are.
|
|
build/ # Build files (using BJam).
|
|
doc/ # Documentation (using Boost.QuickBook).
|
|
example/ # Examples (also those listed in this documentation).
|
|
include/ # DO NOT USE: Use copies of these files from
|
|
boost/ # <emphasis>boost-root</emphasis>/boost/ instead:
|
|
contract.hpp # - Include all headers at once.
|
|
contract_macro.hpp # - Include library macro interface.
|
|
contract/ # - Header files that can be included one-by-one.
|
|
core/ # - Fundamental headers (usually indirectly included by other headers).
|
|
detail/ # - Implementation code (should never be included or used directly).
|
|
src/ # Library source code to be compiled.
|
|
test/ # Tests.
|
|
</programlisting>
|
|
<para>
|
|
All headers required by this library can be included at once by:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
</programlisting>
|
|
<para>
|
|
Or, by the following when using the library macro interface (see <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable
|
|
Contract Compilation</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract_macro</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
</programlisting>
|
|
<para>
|
|
Alternatively, all <literal moreinfo="none">boost/contract/*.hpp</literal> headers are independent
|
|
from one another and they can be selectively included one-by-one based on
|
|
the specific functionality of this library being used (but this was measured
|
|
to not make an appreciable difference in compile-time so <literal moreinfo="none">boost/contract.hpp</literal>
|
|
can be included directly in most cases). The <literal moreinfo="none">boost/contract/core/*.hpp</literal>
|
|
headers are not independent from other headers and they do not need to be
|
|
directly included in user code when <literal moreinfo="none">boost/contract.hpp</literal>
|
|
or <literal moreinfo="none">boost/contract/*.hpp</literal> headers are included already.
|
|
</para>
|
|
<para>
|
|
All files under <literal moreinfo="none">boost/contract/detail/</literal>, names in the
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">detail</phrase></computeroutput> namespace, macros starting with
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_DETAIL</phrase><phrase role="special">...</phrase></computeroutput>,
|
|
and all names starting with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost_contract_detail</phrase><phrase role="special">...</phrase></computeroutput> (in any namespace, including user-defined
|
|
namespaces) are part of this library implementation and should never be used
|
|
directly in user code. Names starting with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ERROR</phrase><phrase role="special">...</phrase></computeroutput> are used by this library to report some
|
|
compile-time errors (so spotting these names in compiler error messages might
|
|
help troubleshooting).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.getting_started.build">
|
|
<title><link linkend="boost_contract.getting_started.build">Build</link></title>
|
|
<para>
|
|
Let <literal moreinfo="none"><emphasis>boost-root</emphasis></literal> be the directory where
|
|
Boost source files were installed. This library is installed and compiled
|
|
as part of Boost using BJam.
|
|
</para>
|
|
<warning>
|
|
<para>
|
|
It is strongly recommended to compile and use this library as a shared
|
|
library (a.k.a., Dynamically Linked Library or DLL) by defining the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_ALL_DYN_LINK</phrase></computeroutput> macro (or at least
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_DYN_LINK">BOOST_CONTRACT_DYN_LINK</link></computeroutput>)
|
|
when building Boost. When using BJam to build Boost, this can be achieved
|
|
by the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">link</phrase><phrase role="special">=</phrase><phrase role="identifier">shared</phrase></computeroutput> parameter (which is already the
|
|
default so no extra parameter is actually needed for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">bjam</phrase></computeroutput>).
|
|
</para>
|
|
<para>
|
|
It is also possible to compile and use this library as a static library
|
|
(by defining the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_STATIC_LINK">BOOST_CONTRACT_STATIC_LINK</link></computeroutput>
|
|
macro) or as a header-only library (by leaving both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_DYN_LINK">BOOST_CONTRACT_DYN_LINK</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_STATIC_LINK">BOOST_CONTRACT_STATIC_LINK</link></computeroutput>
|
|
undefined). However, this library is not guaranteed to always work correctly
|
|
in these cases. Specifically, this library might not correctly disable
|
|
contracts while checking other contracts and call the correct user-defined
|
|
contract failure handlers unless it is compiled as a shared library when
|
|
it is used across different program units (different programs, different
|
|
shared libraries in the same program, etc.).
|
|
</para>
|
|
</warning>
|
|
<bridgehead renderas="sect4" id="boost_contract.getting_started.build.h0">
|
|
<phrase id="boost_contract.getting_started.build.linux_based_systems"/><link linkend="boost_contract.getting_started.build.linux_based_systems">Linux-Based
|
|
Systems</link>
|
|
</bridgehead>
|
|
<para>
|
|
For example, to build all Boost libraries including this one (as shared libraries,
|
|
see also <ulink url="https://www.boost.org/doc/libs/1_70_0/more/getting_started">Boost
|
|
documentation</ulink>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">$ cd <emphasis>boost-root</emphasis>
|
|
$ ./bootstrap.sh
|
|
$ ./bjam
|
|
</programlisting>
|
|
<para>
|
|
To compile and run the <ulink url="../../example/features/introduction.cpp"><literal moreinfo="none"><emphasis>boost-root</emphasis>/libs/contract/example/features/introduction.cpp</literal></ulink>
|
|
example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">$ cd <emphasis>boost-root</emphasis>/libs/contract/example
|
|
$ ../../../bjam features-introduction
|
|
</programlisting>
|
|
<para>
|
|
To compile and run all this library's tests (this might take while):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">$ cd <emphasis>boost-root</emphasis>/libs/contract/test
|
|
$ ../../../bjam
|
|
</programlisting>
|
|
<para>
|
|
To compile and run code that uses this library but without BJam (similarly
|
|
for Clang):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">$ cd /tmp
|
|
$ g++ -std=c++11 -D BOOST_CONTRACT_DYN_LINK -I <emphasis>boost-root</emphasis> <emphasis>boost-root</emphasis>/stage/lib/<emphasis>system-prefix</emphasis>boost_contract.dll <emphasis>boost-root</emphasis>/libs/contract/example/features/introduction.cpp -o introduction
|
|
$ export PATH=$PATH:<emphasis>boost-root</emphasis>/stage/lib
|
|
$ ./introduction
|
|
</programlisting>
|
|
<bridgehead renderas="sect4" id="boost_contract.getting_started.build.h1">
|
|
<phrase id="boost_contract.getting_started.build.windows_based_systems"/><link linkend="boost_contract.getting_started.build.windows_based_systems">Windows-Based
|
|
Systems</link>
|
|
</bridgehead>
|
|
<para>
|
|
For example, to build all Boost libraries including this one (as DLLs, see
|
|
also <ulink url="https://www.boost.org/doc/libs/1_70_0/more/getting_started">Boost
|
|
documentation</ulink>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">>cd <emphasis>boost-root</emphasis>
|
|
>bootstrap.bat
|
|
>bjam
|
|
</programlisting>
|
|
<para>
|
|
To compile and run the <ulink url="../../example/features/introduction.cpp"><literal moreinfo="none"><emphasis>boost-root</emphasis>/libs/contract/example/features/introduction.cpp</literal></ulink>
|
|
example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">>cd <emphasis>boost-root</emphasis>\libs\contract\example
|
|
>..\..\..\bjam features-introduction
|
|
</programlisting>
|
|
<para>
|
|
To compile and run all this library's tests (this might take while):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">>cd <emphasis>boost-root</emphasis>\libs\contract\test
|
|
>..\..\..\bjam
|
|
</programlisting>
|
|
<para>
|
|
To compile and run code that uses this library but without BJam:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude">>cd C:\Temp
|
|
>cl /MDd /EHs /std:c++11 /D BOOST_CONTRACT_DYN_LINK /I <emphasis>boost-root</emphasis> /link /DLL /LIBPATH:<emphasis>boost-root</emphasis>\stage\lib <emphasis>boost-root</emphasis>\libs\contract\example\features\introduction.cpp /out:introduction
|
|
>set PATH=%PATH%;<emphasis>boost-root</emphasis>/stage/lib
|
|
>introduction
|
|
</programlisting>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview">
|
|
<title><link linkend="boost_contract.contract_programming_overview">Contract
|
|
Programming Overview</link></title>
|
|
<blockquote>
|
|
<para>
|
|
<emphasis><quote>It is absurd to make elaborate security checks on debugging
|
|
runs, when no trust is put in the results, and then remove them in production
|
|
runs, when an erroneous result could be expensive or disastrous. What would
|
|
we think of a sailing enthusiast who wears his life-jacket when training
|
|
on dry land but takes it off as soon as he goes to sea?</quote></emphasis>
|
|
</para>
|
|
</blockquote>
|
|
<blockquote>
|
|
<para>
|
|
<emphasis>-- Charles Antony Richard Hoare (see <link linkend="Hoare73_anchor">[Hoare73]</link>)</emphasis>
|
|
</para>
|
|
</blockquote>
|
|
<para>
|
|
This section gives an overview of contract programming (see <link linkend="Meyer97_anchor">[Meyer97]</link>,
|
|
<link linkend="Mitchell02_anchor">[Mitchell02]</link>, and <link linkend="N1613_anchor">[N1613]</link>
|
|
for more extensive introductions to contract programming). Readers that already
|
|
have a basic understanding of contract programming can skip this section and
|
|
maybe come back to it after reading the <link linkend="boost_contract.tutorial">Tutorial</link>.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
The objective of this library is not to convince programmers to use contract
|
|
programming. It is assumed that programmes understand the benefits and trade-offs
|
|
associated with contract programming and they have already decided to use
|
|
this methodology in their code. Then, this library aims to be the best and
|
|
more complete contract programming library for C++ (without using programs
|
|
and tools external to the C++ language and its preprocessor).
|
|
</para>
|
|
</note>
|
|
<section id="boost_contract.contract_programming_overview.assertions">
|
|
<title><link linkend="boost_contract.contract_programming_overview.assertions">Assertions</link></title>
|
|
<para>
|
|
Contract programming is characterized by the following assertion mechanisms:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
<emphasis>Preconditions</emphasis>: These are logical conditions that
|
|
programmers expect to be true when a function is called (e.g., to check
|
|
constraints on function arguments). Operations that logically have no
|
|
preconditions (i.e., that are always well-defined for the entire domain
|
|
of their inputs) are also referred to as having a <emphasis>wide contract</emphasis>.
|
|
This is in contrast to operations that have preconditions which are also
|
|
referred to as having a <emphasis>narrow contract</emphasis> (note that
|
|
operations with truly narrow contracts are also expected to never throw
|
|
exceptions because the implementation body of these operations is always
|
|
expected to succeed after its preconditions are checked to be true).
|
|
<footnote id="boost_contract.contract_programming_overview.assertions.f0">
|
|
<para>
|
|
The nomenclature of wide and narrow contracts has gained some popularity
|
|
in recent years in the C++ community (appearing in a number of more
|
|
recent proposals to add contract programming to the C++ standard, see
|
|
<link linkend="boost_contract.bibliography">Bibliography</link>). This
|
|
nomenclature is perfectly reasonable but it is not often used in this
|
|
document just because the authors usually prefer to explicitly say
|
|
"this operation has no preconditions..." or "this operation
|
|
has preconditions..." (this is just a matter of taste).
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<emphasis>Postconditions</emphasis>: These are logical conditions that
|
|
programmers expect to be true when a function exits without throwing
|
|
an exception (e.g., to check the result and any side effect that a function
|
|
might have). Postconditions can access the function return value (for
|
|
non-void functions) and also <emphasis>old values</emphasis> (which are
|
|
the values that expressions had before the function implementation was
|
|
executed).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<emphasis>Exception guarantees</emphasis>: These are logical conditions
|
|
that programmers except to be true when a function exits throwing an
|
|
exception. Exception guarantees can access old values (but not the function
|
|
return value). <footnote id="boost_contract.contract_programming_overview.assertions.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Contract assertions for
|
|
exception guarantees were first introduced by this library, they are
|
|
not part of <link linkend="N1962_anchor">[N1962]</link> or other references
|
|
listed in the <link linkend="boost_contract.bibliography">Bibliography</link>
|
|
(even if exception safety guarantees have long been part of C++ STL
|
|
documentation).
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<emphasis>Class invariants</emphasis>: These are logical conditions that
|
|
programmers expect to be true after a constructor exits without throwing
|
|
an exception, before and after the execution of every non-static public
|
|
function (even if they throw exceptions), before the destructor is executed
|
|
(and also after the destructor is executed but only when the destructor
|
|
throws an exception). Class invariants define valid states for all objects
|
|
of a given class. It is possible to specify a different set of class
|
|
invariants for volatile public functions, namely <emphasis>volatile class
|
|
invariants</emphasis>. It is also possible to specify <emphasis>static
|
|
class invariants</emphasis> which are excepted to be true before and
|
|
after the execution of any constructor, destructor (even if it does not
|
|
throw an exception), and public function (even if static). <footnote id="boost_contract.contract_programming_overview.assertions.f2">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Static and volatile class
|
|
invariants were first introduced by this library (simply to reflect
|
|
the fact that C++ supports also static and volatile public functions),
|
|
they are not part of <link linkend="N1962_anchor">[N1962]</link> or
|
|
other references listed in the <link linkend="boost_contract.bibliography">Bibliography</link>.
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<emphasis>Subcontracting</emphasis>: This indicates that preconditions
|
|
cannot be strengthen, while postconditions and class invariants cannot
|
|
be weaken when a public function in a derived class overrides public
|
|
functions in one or more of its base classes (this is formally defined
|
|
according to the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink>).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
The actual function implementation code, that remains outside of these contract
|
|
assertions, is often referred to as the <emphasis>function body</emphasis>
|
|
in contract programming.
|
|
</para>
|
|
<para>
|
|
Class invariants can also be used to specify <emphasis>basic</emphasis> exception
|
|
safety guarantees for an object (because they are checked at exit of public
|
|
functions even when those throw exceptions). Contract assertions for exception
|
|
guarantees can be used to specify <emphasis>strong</emphasis> exception safety
|
|
guarantees for a given operation on the same object.
|
|
</para>
|
|
<para>
|
|
It is also a common requirement for contract programming to automatically
|
|
disable contract checking while already checking assertions from another
|
|
contract (in order to avoid infinite recursion while checking contract assertions).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
This library implements this requirement but in order to globally disable
|
|
assertions while checking another assertion some kind of global arbitrating
|
|
variable needs to be used by this library implementation. This library
|
|
will automatically protect such a global variable from race conditions
|
|
in multi-threated programs, but this will effectively introduce a global
|
|
lock in the program (the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039741520">BOOST_CONTRACT_DISABLE_THREADS</link></computeroutput>
|
|
macro can be defined to disable this global lock but at the risk of incurring
|
|
in race conditions). <footnote id="boost_contract.contract_programming_overview.assertions.f3">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039741520">BOOST_CONTRACT_DISABLE_THREADS</link></computeroutput>
|
|
is named after <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_DISABLE_THREADS</phrase></computeroutput>.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</note>
|
|
<para>
|
|
In general, it is recommended to specify different contract conditions using
|
|
separate assertion statements and not to group them together into a single
|
|
condition using logical operators (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">&&</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">||</phrase></computeroutput>, etc.). This is because when
|
|
contract conditions are programmed together in a single assertion using logical
|
|
operators, it might not be clear which condition actually failed in case
|
|
the entire assertion fails at run-time.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.assertions.h0">
|
|
<phrase id="boost_contract.contract_programming_overview.assertions.c_style_assertions"/><link linkend="boost_contract.contract_programming_overview.assertions.c_style_assertions">C-Style
|
|
Assertions</link>
|
|
</bridgehead>
|
|
<para>
|
|
A limited form of contract programming (typically some form of precondition
|
|
and basic postcondition checking) can be achieved using the C-style <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> macro. Using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>
|
|
is common practice for many programmers but it suffers of the following limitations:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> does not distinguish
|
|
between preconditions and postconditions. In well-tested production code,
|
|
postconditions can usually be disabled trusting the correctness of the
|
|
implementation while preconditions might still need to remain enabled
|
|
because of possible changes in the calling code (e.g., postconditions
|
|
of a given library could be disabled after testing while keeping the
|
|
library preconditions enabled given that future changes in the user code
|
|
that calls the library cannot be anticipated). Using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>
|
|
it is not possible to selectively disable only postconditions and all
|
|
assertions must be disabled at once.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> requires to manually
|
|
program extra code to correctly check postconditions (specifically to
|
|
handle functions with multiple return statements, to not check postconditions
|
|
when functions throw exceptions, and to implement old values).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> requires to manually
|
|
program extra code to check class invariants (extra member functions,
|
|
try blocks, etc.).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> does not support
|
|
subcontracting.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> calls are usually
|
|
scattered within function implementations thus the asserted conditions
|
|
are not immediately visible in their entirety by programmers (as they
|
|
are instead when the assertions appear in the function declaration or
|
|
at least at the very top of the function definition).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
Contract programming does not suffer of these limitations.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.benefits_and_costs">
|
|
<title><link linkend="boost_contract.contract_programming_overview.benefits_and_costs">Benefits
|
|
and Costs</link></title>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.benefits_and_costs.h0">
|
|
<phrase id="boost_contract.contract_programming_overview.benefits_and_costs.benefits"/><link linkend="boost_contract.contract_programming_overview.benefits_and_costs.benefits">Benefits</link>
|
|
</bridgehead>
|
|
<para>
|
|
The main use of contract programming is to improve software quality. <link linkend="Meyer97_anchor">[Meyer97]</link> discusses how contract programming
|
|
can be used as the basic tool to write <quote>correct</quote> software.
|
|
<link linkend="Stroustrup94_anchor">[Stroustrup94]</link> discusses the key
|
|
importance of class invariants plus advantages and disadvantages of preconditions
|
|
and postconditions.
|
|
</para>
|
|
<para>
|
|
The following is a short summary of benefits associated with contract programming
|
|
inspired mainly by <link linkend="N1613_anchor">[N1613]</link>:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Preconditions and postconditions: Using function preconditions and postconditions,
|
|
programmers can give a precise semantic description of what a function
|
|
requires at its entry and what it ensures at its exit (if it does not
|
|
throw an exception). In particular, using postcondition old values, contract
|
|
programming provides a mechanism that allows programmers to compare values
|
|
of an expression before and after the function body execution. This mechanism
|
|
is powerful enough to enable programmers to express many correctness
|
|
constraints within the code itself, constraints that would otherwise
|
|
have to be captured at best only informally by documentation.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Class invariants: Using class invariants, programmers can describe what
|
|
to expect from a class and the logic dependencies between the class members.
|
|
It is the job of the constructor to ensure that the class invariants
|
|
are satisfied when the object is first created. Then the implementation
|
|
of the member functions can be largely simplified as they can be written
|
|
knowing that the class invariants are satisfied because contract programming
|
|
checks them before and after the execution of every public function.
|
|
Finally, the destructor makes sure that the class invariants held for
|
|
the entire life of the object checking the class invariants one last
|
|
time before the object is destructed. Class invariants can also be used
|
|
as a criteria for good abstractions: If it is not possible to specify
|
|
an invariant, it might be an indication that the design abstraction maybe
|
|
be poor and it should not have been made into a class (maybe a namespace
|
|
would have sufficed instead).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Self-documenting code: Contracts are part of the source code, they are
|
|
checked at run-time so they are always up-to-date with the code itself.
|
|
Therefore program specifications, as documented by the contracts, can
|
|
be trusted to always be up-to-date with the implementation.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Easier debugging: Contract programming can provide a powerful debugging
|
|
facility because, if contracts are well-written, bugs will cause contract
|
|
assertions to fail exactly where the problem first occurs instead than
|
|
at some later stage of the program execution in an apparently unrelated
|
|
(and often hard to debug) manner. Note that a precondition failure points
|
|
to a bug in the function caller, a postcondition failure points instead
|
|
to a bug in the function implementation. <footnote id="boost_contract.contract_programming_overview.benefits_and_costs.f0">
|
|
<para>
|
|
Of course, if contracts are ill-written then contract programming is
|
|
of little use. However, it is less likely to have a bug in both the
|
|
function body and the contract than in the function body alone. For
|
|
example, consider the validation of a result in postconditions. Validating
|
|
the return value might seem redundant, but in this case we actually
|
|
want that redundancy. When programmers write a function, there is a
|
|
certain probability that they make a mistake in implementing the function
|
|
body. When programmers specify the result of the function in the postconditions,
|
|
there is also a certain probability that they make a mistake in writing
|
|
the contract. However, the probability that programmers make a mistake
|
|
twice (in both the body <emphasis>and</emphasis> the contract) is in
|
|
general lower than the probability that the mistake is made only once
|
|
(in either the body <emphasis>or</emphasis> the contract).
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Easier testing: Contract programming facilitates testing because a contract
|
|
naturally specifies what a test should check. For example, preconditions
|
|
of a function state which inputs cause the function to fail and postconditions
|
|
state which outputs are produced by the function on successful exit (contract
|
|
programming should be seen as a tool to complement and guide, but obviously
|
|
not to replace, testing).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Formal design: Contract programming can serve to reduce the gap between
|
|
designers and programmers by providing a precise and unambiguous specification
|
|
language in terms of contract assertions. Moreover, contracts can make
|
|
code reviews easier by clarifying some of the semantics and usage of
|
|
the code.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Formalize inheritance: Contract programming formalizes the virtual function
|
|
overriding mechanism using subcontracting as justified by the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink>. This keeps the base class programmers in control as
|
|
overriding functions always have to fully satisfy the contracts of their
|
|
base classes.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Replace defensive programming: Contract programming assertions can replace
|
|
<ulink url="http://en.wikipedia.org/wiki/Defensive_programming">defensive
|
|
programming</ulink> checks localizing these checks within the contracts
|
|
and making the code more readable.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
Of course, not all formal contract specifications can be asserted in C++.
|
|
For example, in C++ is it not possible to assert the validity of an iterator
|
|
range in the general case (because the only way to check if two iterators
|
|
form a valid range is to keep incrementing the first iterator until it reaches
|
|
the second iterator, but if the iterator range is invalid then such a code
|
|
would render undefined behaviour or run forever instead of failing an assertion).
|
|
Nevertheless, a large amount of contract assertions can be successfully programmed
|
|
in C++ as illustrated by the numerous examples in this documentation and
|
|
from the literature (for example see how much of STL <link linkend="N1962_vector_anchor"><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">vector</phrase></computeroutput></link> contract assertions can actually
|
|
be programmed in C++ using this library).
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.benefits_and_costs.h1">
|
|
<phrase id="boost_contract.contract_programming_overview.benefits_and_costs.costs"/><link linkend="boost_contract.contract_programming_overview.benefits_and_costs.costs">Costs</link>
|
|
</bridgehead>
|
|
<para>
|
|
In general, contract programming benefits come at the cost of performance
|
|
as discussed in detail by both <link linkend="Stroustrup94_anchor">[Stroustrup94]</link>
|
|
and <link linkend="Meyer97_anchor">[Meyer97]</link>. While performance trade-offs
|
|
should be carefully considered depending on the specific application domain,
|
|
software quality cannot be sacrificed: It is difficult to see value in software
|
|
that quickly and efficiently provides incorrect results.
|
|
</para>
|
|
<para>
|
|
The run-time performances are negatively impacted by contract programming
|
|
mainly because of extra time require to:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Check the asserted conditions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Copy old values when these are used in postconditions or exception guarantees.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Call additional functors that check preconditions, postconditions, exception
|
|
guarantees, class invariants, etc. (these can add up to many extra calls
|
|
especially when using subcontracting).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<note>
|
|
<para>
|
|
In general, contracts introduce at least three extra functor calls to check
|
|
preconditions, postconditions, and exception guarantees for any given non-member
|
|
function call. Public functions introduce also two more function calls
|
|
to check class invariants (at entry and at exit). For subcontracting, these
|
|
extra calls (some of which become virtual calls) are repeated for the number
|
|
of functions being overridden from the base classes (possibly deep in the
|
|
inheritance tree). In addition to that, this library introduces a number
|
|
of function calls internal to its implementation in order to properly check
|
|
the contracts.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
To mitigate the run-time performance impact, programmers can selectively
|
|
disable run-time checking of some of the contract assertions. Programmers
|
|
will have to decide based on the performance trade-offs required by their
|
|
specific applications, but a reasonable approach often is to (see <link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link>):
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Always write contracts to clarify the semantics of the design embedding
|
|
the specifications directly in the code and making the code self-documenting.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check preconditions, postconditions, class invariants, and maybe even
|
|
exception guarantees during initial testing.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check only preconditions (and maybe class invariants, but not postconditions
|
|
and exception guarantees) during release testing and for the final release.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
This approach is usually reasonable because in well-tested production code,
|
|
validating the function body implementation using postconditions is rarely
|
|
needed since the function has shown itself to be <quote>correct</quote> during
|
|
testing. On the other hand, checking function arguments using preconditions
|
|
is always needed because of changes that can be made to the calling code
|
|
(without having to necessarily re-test and re-release the called code). Furthermore,
|
|
postconditions and also exception guarantees, with related old value copies,
|
|
are often computationally more expensive to check than preconditions and
|
|
class invariants.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.function_calls">
|
|
<title><link linkend="boost_contract.contract_programming_overview.function_calls">Function
|
|
Calls</link></title>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.function_calls.h0">
|
|
<phrase id="boost_contract.contract_programming_overview.function_calls.non_member_functions"/><link linkend="boost_contract.contract_programming_overview.function_calls.non_member_functions">Non-Member
|
|
Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
A call to a non-member function with a contract executes the following steps
|
|
(see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check function preconditions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the function body.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the body did not throw an exception, check function postconditions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else, check function exception guarantees.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.function_calls.h1">
|
|
<phrase id="boost_contract.contract_programming_overview.function_calls.private_and_protected_functions"/><link linkend="boost_contract.contract_programming_overview.function_calls.private_and_protected_functions">Private
|
|
and Protected Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
Private and protected functions do not have to satisfy class invariants because
|
|
these functions are part of the class implementation and not of the class
|
|
public interface. Furthermore, the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> does not apply to private and protected functions because
|
|
these functions are not accessible to the user at the calling site where
|
|
the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> applies.
|
|
</para>
|
|
<para>
|
|
Therefore, calls to private and protected functions with contracts execute
|
|
the same steps as the ones indicated above for non-member functions (checking
|
|
only preconditions and postconditions, without checking class invariants
|
|
and without subcontracting).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.public_function_calls">
|
|
<title><link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link></title>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.public_function_calls.h0">
|
|
<phrase id="boost_contract.contract_programming_overview.public_function_calls.overriding_public_functions"/><link linkend="boost_contract.contract_programming_overview.public_function_calls.overriding_public_functions">Overriding
|
|
Public Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
Let's consider a public function in a derived class that overrides public
|
|
virtual functions declared by its public base classes (because of C++ multiple
|
|
inheritance, the function could override from more than one of its base classes).
|
|
We refer to the function in the derived class as the <emphasis>overriding
|
|
function</emphasis>, and to the set of base classes containing all the <emphasis>overridden
|
|
functions</emphasis> as <emphasis>overridden bases</emphasis>.
|
|
</para>
|
|
<para>
|
|
When subcontracting, overridden functions are searched (at compile-time)
|
|
deeply in all public branches of the inheritance tree (i.e., not just the
|
|
derived class' direct public parents are inspected, but also all its public
|
|
grandparents, etc.). In case of multiple inheritance, this search also extends
|
|
(at compile-time) widely to all public trees of the multiple inheritance
|
|
forest (multiple public base classes are searched following their order of
|
|
declaration in the derived class' inheritance list). As usual with C++ multiple
|
|
inheritance, this search could result in multiple overridden functions and
|
|
therefore in subcontracting from multiple public base classes. Note that
|
|
only public base classes are considered for subcontracting because private
|
|
and protected base classes are not accessible to the user at the calling
|
|
site where the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> applies.
|
|
</para>
|
|
<para>
|
|
A call to the overriding public function with a contract executes the following
|
|
steps (see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static class invariants for all overridden bases, <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
then check the derived class static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static invariants.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check preconditions of overridden public functions from all overridden
|
|
bases in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
with each other, <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
else check the overriding function preconditions in the derived class.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the overriding function body.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static class invariants for all overridden bases, <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
then check the derived class static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static invariants (even if the body threw an exception).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the body did not throw an exception, check postconditions of overridden
|
|
public functions from all overridden bases in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with each other, <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
then check the overriding function postconditions in the derived class.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else, check exception guarantees of overridden public functions from
|
|
all overridden bases in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with each other, <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
then check the overriding function exception guarantees in the derived
|
|
class.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Volatile public functions check static class invariants <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
volatile class invariants instead. Preconditions and postconditions of volatile
|
|
public functions and volatile class invariants access the object as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>.
|
|
</para>
|
|
<para>
|
|
Class invariants are checked before preconditions and postconditions so programming
|
|
precondition and postcondition assertions can be simplified assuming that
|
|
class invariants are satisfied already (e.g., if class invariants assert
|
|
that a pointer cannot be null then preconditions and postconditions can safety
|
|
dereference that pointer without additional checking). Similarly, static
|
|
class invariants are checked before non-static class invariants so programming
|
|
non-static class invariant (volatile and non) can be simplified assuming
|
|
that static class invariants are satisfied already. Furthermore, subcontracting
|
|
checks contracts of public base classes before checking the derived class
|
|
contracts so programming derived class contract assertions can be simplified
|
|
by assuming that public base class contracts are satisfied already.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
<anchor id="and_anchor"/><anchor id="or_anchor"/>In this documentation
|
|
<link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
and <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
indicate the logic <emphasis>and</emphasis> and <emphasis>or</emphasis>
|
|
operations evaluated in <emphasis>short-circuit</emphasis>. For example:
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">q</phrase></computeroutput> is true if and only if
|
|
both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">q</phrase></computeroutput> are true, but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">q</phrase></computeroutput>
|
|
is never evaluated when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput>
|
|
is false; <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput> <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">q</phrase></computeroutput> is true if and only if
|
|
either <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput> or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">q</phrase></computeroutput> are true, but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">q</phrase></computeroutput>
|
|
is never evaluated when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput>
|
|
is true.
|
|
</para>
|
|
<para>
|
|
As indicated by the steps above and in accordance with the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink>, subcontracting checks preconditions in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
while class invariants, postconditions, and exceptions guarantees are checked
|
|
in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with preconditions, class invariants, postconditions, and exceptions guarantees
|
|
of base classes respectively.
|
|
</para>
|
|
</note>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.public_function_calls.h1">
|
|
<phrase id="boost_contract.contract_programming_overview.public_function_calls.non_overriding_public_functions"/><link linkend="boost_contract.contract_programming_overview.public_function_calls.non_overriding_public_functions">Non-Overriding
|
|
Public Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
A call to a non-static public function with a contract (that does not override
|
|
functions from any of its public base classes) executes the following steps
|
|
(see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check class static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static invariants (but none of the invariants from base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check function preconditions (but none of the preconditions from functions
|
|
in base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the function body.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check the class static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static invariants (even if the body threw an exception, but none
|
|
of the invariants from base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the body did not throw an exception, check function postconditions
|
|
(but none of the postconditions from functions in base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else, check function exception guarantees (but none of the exception
|
|
guarantees from functions in base classes).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Volatile public functions check static class invariants <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
volatile class invariants instead. Preconditions and postconditions of volatile
|
|
functions and volatile class invariants access the object as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>.
|
|
</para>
|
|
<para>
|
|
Class invariants are checked because this function is part of the class public
|
|
interface. However, none of the contracts of the base classes are checked
|
|
because this function does not override any functions from any of the public
|
|
base classes (so the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> does not require to subcontract in this case).
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.contract_programming_overview.public_function_calls.h2">
|
|
<phrase id="boost_contract.contract_programming_overview.public_function_calls.static_public_functions"/><link linkend="boost_contract.contract_programming_overview.public_function_calls.static_public_functions">Static
|
|
Public Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
A call to a static public function with a contract executes the following
|
|
steps (see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants (but not the non-static invariants and
|
|
none of the invariants from base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check function preconditions (but none of the preconditions from functions
|
|
in base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the function body.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants (even if the body threw an exception, but
|
|
not the non-static invariants and none of the invariants from base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the body did not throw an exception, check function postconditions
|
|
(but none of the postconditions from functions in base classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else, check function exception guarantees (but none of the exception
|
|
guarantees from functions in base classes).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Class invariants are checked because this function is part of the class public
|
|
interface, but only static class invariants can be checked (because this
|
|
is a static function so it cannot access the object that would instead be
|
|
required to check non-static class invariants, volatile or not). Furthermore,
|
|
static functions cannot override any function so the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> does not apply and they do not subcontract.
|
|
</para>
|
|
<para>
|
|
Preconditions and postconditions of static functions and static class invariants
|
|
cannot access the object (because they are checked from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
member functions).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.constructor_calls">
|
|
<title><link linkend="boost_contract.contract_programming_overview.constructor_calls">Constructor
|
|
Calls</link></title>
|
|
<para>
|
|
A call to a constructor with a contract executes the following steps (see
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check constructor preconditions (but these cannot access the object because
|
|
the object is not constructed yet).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the constructor member initialization list (if present).
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Construct any base class (public or not) according with C++ construction
|
|
mechanism and also check the contracts of these base constructors
|
|
(according with steps similar to the ones listed here).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants (but not the non-static or volatile class
|
|
invariants, because the object is not constructed yet).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the constructor body.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants (even if the body threw an exception).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check non-static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
volatile class invariants (because the object is now successfully
|
|
constructed).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check constructor postconditions (but these cannot access the object
|
|
old value <literal moreinfo="none"><emphasis>oldof</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase></computeroutput> because the object was not constructed
|
|
before the execution of the constructor body).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else, check constructor exception guarantees (but these cannot access
|
|
the object old value <literal moreinfo="none"><emphasis>oldof</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase></computeroutput> because the object was not constructed
|
|
before the execution of the constructor body, plus they can only access
|
|
class' static members because the object has not been successfully constructed
|
|
given the constructor body threw an exception in this case).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Constructor preconditions are checked before executing the member initialization
|
|
list so programming these initializations can be simplified assuming the
|
|
constructor preconditions are satisfied (e.g., constructor arguments can
|
|
be validated by the constructor preconditions before they are used to initialize
|
|
base classes and data members).
|
|
</para>
|
|
<para>
|
|
As indicated in step 2.a. above, C++ object construction mechanism will automatically
|
|
check base class contracts when these bases are initialized (no explicit
|
|
subcontracting behaviour is required here).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.destructor_calls">
|
|
<title><link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor
|
|
Calls</link></title>
|
|
<para>
|
|
A call to a destructor with a contract executes the following steps (see
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
non-static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
volatile class invariants.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Execute the destructor body (destructors have no parameters and they
|
|
can be called at any time after object construction so they have no preconditions).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants (even if the body threw an exception).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check destructor postconditions (but these can only access class'
|
|
static members and the object old value <literal moreinfo="none"><emphasis>oldof</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase></computeroutput> because the object has been destroyed
|
|
after successful execution of the destructor body). <footnote id="boost_contract.contract_programming_overview.destructor_calls.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Postconditions for
|
|
destructors are not part of <link linkend="N1962_anchor">[N1962]</link>
|
|
or other references listed in the <link linkend="boost_contract.bibliography">Bibliography</link>
|
|
(but with respect to <link linkend="Meyer97_anchor">[Meyer97]</link>
|
|
it should be noted that Eiffel does not support static data members
|
|
and that might by why destructors do not have postconditions
|
|
in Eiffel). However, in principle there could be uses for destructor
|
|
postconditions so this library supports postconditions for destructors
|
|
(e.g., a class that counts object instances could use destructor
|
|
postconditions to assert that an instance counter stored in a
|
|
static data member is decreased by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">1</phrase></computeroutput>
|
|
because the object has been destructed).
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Destroy any base class (public or not) according with C++ destruction
|
|
mechanism and also check the contracts of these base destructors
|
|
(according with steps similar to the ones listed here).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else (even if destructors should rarely, if ever, be allowed to throw
|
|
exceptions in C++):
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check non-static <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
volatile class invariants (because the object was not successfully
|
|
destructed so it still exists and should satisfy its invariants).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check destructor exception guarantees.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
As indicated in step 4.b. above, C++ object destruction mechanism will automatically
|
|
check base class contracts when the destructor exits without throwing an
|
|
exception (no explicit subcontracting behaviour is required here).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Given that C++ allows destructors to throw, this library handles the case
|
|
when the destructor body throws an exception as indicated above. However,
|
|
in order to comply with STL exception safety guarantees and good C++ programming
|
|
practices, programmers should implement destructor bodies to rarely, if
|
|
ever, throw exceptions (in fact destructors are implicitly declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput> in C++11).
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.constant_correctness">
|
|
<title><link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link></title>
|
|
<para>
|
|
Contracts should not be allowed to modify the program state because they
|
|
are only responsible to check (and not to change) the program state in order
|
|
to verify its compliance with the specifications. Therefore, contracts should
|
|
only access objects, function arguments, function return values, old values,
|
|
and all other program variables in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
context (via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">*</phrase>
|
|
<phrase role="keyword">const</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput>, etc.).
|
|
</para>
|
|
<para>
|
|
Whenever possible (e.g., class invariants and postcondition old values),
|
|
this library automatically enforces this <emphasis>constant-correctness constraint</emphasis>
|
|
at compile-time using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>.
|
|
However, this library cannot automatically enforce this constraint in all
|
|
cases (for preconditions and postconditions of mutable member functions,
|
|
for global variables, etc.). See <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link> for ways of using this library that enforce the constant-correctness
|
|
constraint at compile-time (but at the cost of significant boiler-plate code
|
|
to be programmed manually so not recommended in general).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
In general, it is the responsibility of the programmers to code assertions
|
|
that only check, and do not change, program variables. <footnote id="boost_contract.contract_programming_overview.constant_correctness.f0">
|
|
<para>
|
|
Note that this is true when using C-style <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>
|
|
as well.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.specifications_vs__implementation">
|
|
<title><link linkend="boost_contract.contract_programming_overview.specifications_vs__implementation">Specifications
|
|
vs. Implementation</link></title>
|
|
<para>
|
|
Contracts are part of the program specification and not of its implementation.
|
|
Therefore, contracts should ideally be programmed within C++ declarations,
|
|
and not within definitions.
|
|
</para>
|
|
<para>
|
|
In general, this library cannot satisfy this requirement. However, even when
|
|
contracts are programmed together with the body in the function definition,
|
|
it is still fairly easy for users to identify and read just the contract
|
|
portion of the function definition (because the contract code must always
|
|
be programmed at the very top of the function definition). See <link linkend="boost_contract.extras.separate_body_implementation">Separate
|
|
Body Implementation</link> for ways of using this library to program contract
|
|
specifications outside of the body implementation (but at the cost of writing
|
|
one extra function for any given function so not recommended in general).
|
|
</para>
|
|
<para>
|
|
Furthermore, contracts are most useful when they assert conditions only using
|
|
public members (in most cases, the need for using non-public members to check
|
|
contracts, especially in preconditions, indicates an error in the class design).
|
|
For example, the caller of a public function cannot in general make sure
|
|
that the function preconditions are satisfied if the precondition assertions
|
|
use private members that are not callable by the caller (therefore, a failure
|
|
in the preconditions will not necessarily indicate a bug in the caller given
|
|
that the caller was made unable to fully check the preconditions in the first
|
|
place). However, given that C++ provides programmers ways around access level
|
|
restrictions (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">friend</phrase></computeroutput>, function
|
|
pointers, etc.), this library leaves it up to programmers to make sure that
|
|
only public members are used in contract assertions (especially in preconditions).
|
|
(<link linkend="N1962_anchor">[N1962]</link> follows the same approach not
|
|
restricting contracts to only use public members, Eiffel instead generates
|
|
a compile-time error if preconditions are asserted using non-public members.)
|
|
<footnote id="boost_contract.contract_programming_overview.specifications_vs__implementation.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Out of curiosity, if C++ <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#45">defect
|
|
45</ulink> had not been fixed, this library could have been implemented
|
|
to generate a compile-time error when precondition assertions use non-public
|
|
members more similarly to Eiffel's implementation (but still, not necessary
|
|
the best approach for C++).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.on_contract_failures">
|
|
<title><link linkend="boost_contract.contract_programming_overview.on_contract_failures">On
|
|
Contract Failures</link></title>
|
|
<para>
|
|
If preconditions, postconditions, exception guarantees, or class invariants
|
|
are either checked to be false or their evaluation throws an exception at
|
|
run-time then this library will call specific <emphasis>failure handler functions</emphasis>.
|
|
<footnote id="boost_contract.contract_programming_overview.on_contract_failures.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> If the evaluation of a contract
|
|
assertion throws an exception, the assertion cannot be checked to be true
|
|
so the only safe thing to assume is that the assertion failed (indeed the
|
|
contract assertion checking failed) and call the contract failure handler
|
|
in this case also.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
By default, these failure handler functions print a message to the standard
|
|
error <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> (with detailed information about the
|
|
failure) and then terminate the program calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>.
|
|
However, using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_precondition_failure">boost::contract::set_precondition_failure</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_postcondition_failure">boost::contract::set_postcondition_failure</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_except_failure">boost::contract::set_except_failure</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_invariant_failure">boost::contract::set_invariant_failure</link></computeroutput>,
|
|
etc. programmers can define their own failure handler functions that can
|
|
take any user-specified action (throw an exception, exit the program with
|
|
an error code, etc., see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link>). <footnote id="boost_contract.contract_programming_overview.on_contract_failures.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> This customizable failure handling
|
|
mechanism is similar to the one used by C++ <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>
|
|
and also to the one proposed in <link linkend="N1962_anchor">[N1962]</link>.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
In C++ there are a number of issues with programming contract failure handlers
|
|
that throw exceptions instead of terminating the program. Specifically,
|
|
destructors check class invariants so they will throw if programmers change
|
|
class invariant failure handlers to throw instead of terminating the program,
|
|
but in general destructors should not throw in C++ (to comply with STL
|
|
exception safety, C++11 implicit <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput>
|
|
declarations for destructors, etc.). Furthermore, programming a failure
|
|
handler that throws on exception guarantee failures results in throwing
|
|
an exception (the one reporting the contract failure) while there is already
|
|
an active exception (the one that caused the exception guarantees to be
|
|
checked in the first place), and this will force C++ to terminate the program
|
|
anyway.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
Therefore, it is recommended to terminate the program at least for contract
|
|
failures from destructors and exception guarantees (if not in all other cases
|
|
of contract failures as it is done by default by this library). The contract
|
|
failure handler functions programmed using this library have information
|
|
about the failed contract (preconditions, postconditions, etc.) and the operation
|
|
that was checking the contract (constructor, destructor, etc.) so programmers
|
|
can granularly distinguish all cases and decide when it is appropriate to
|
|
terminate, throw, or take some other user-specific action.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.contract_programming_overview.feature_summary">
|
|
<title><link linkend="boost_contract.contract_programming_overview.feature_summary">Feature
|
|
Summary</link></title>
|
|
<para>
|
|
The contract programming features supported by this library are largely based
|
|
on <link linkend="N1962_anchor">[N1962]</link> and on the Eiffel programming
|
|
language.
|
|
</para>
|
|
<para>
|
|
The following table compares contract programming features among this library,
|
|
<link linkend="N1962_anchor">[N1962]</link> (unfortunately the C++ standard
|
|
committee rejected this proposal commenting on a lack of interest in adding
|
|
contract programming to C++ at that time, even if <link linkend="N1962_anchor">[N1962]</link>
|
|
itself is sound), a more recent proposal <link linkend="P0380_anchor">[P0380]</link>
|
|
(which was accepted in the C++20 standard but unfortunately only supports
|
|
preconditions and postconditions, while does not support class invariants,
|
|
old values, and subcontracting), the Eiffel and D programming languages.
|
|
Some of the items listed in this summary table will become clear in detail
|
|
after reading the remaining sections of this documentation.
|
|
</para>
|
|
<informaltable frame="all">
|
|
<tgroup cols="6">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
Feature
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This Library
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<link linkend="N1962_anchor">[N1962]</link> Proposal (not accepted
|
|
in C++)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
C++20 (see <link linkend="P0380_anchor">[P0380]</link>)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
ISE Eiffel 5.4 (see <link linkend="Meyer97_anchor">[Meyer97]</link>)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
D (see <link linkend="Bright04_anchor">[Bright04]</link>)
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Keywords and specifiers</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Specifiers: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">precondition</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">postcondition</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>. The last three specifiers
|
|
appear in user code so their names can be referred to or changed
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_INVARIANT">BOOST_CONTRACT_INVARIANT</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038619104">BOOST_CONTRACT_STATIC_INVARIANT</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039730032">BOOST_CONTRACT_BASES_TYPEDEF</link></computeroutput>
|
|
macros respectively to avoid name clashes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Keywords: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">precondition</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">postcondition</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">oldof</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Attributes: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[[</phrase><phrase role="identifier">expects</phrase><phrase role="special">]]</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[[</phrase><phrase role="identifier">ensures</phrase><phrase role="special">]]</phrase></computeroutput>.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Keywords: <literal moreinfo="none">require</literal>, <literal moreinfo="none">require else</literal>,
|
|
<literal moreinfo="none">ensure</literal>, <literal moreinfo="none">ensure then</literal>, <literal moreinfo="none">old</literal>,
|
|
<literal moreinfo="none">result</literal>, <literal moreinfo="none">do</literal>, and <literal moreinfo="none">invariant</literal>.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Keywords: <literal moreinfo="none">in</literal>, <literal moreinfo="none">out</literal>, <literal moreinfo="none">do</literal>,
|
|
<literal moreinfo="none">assert</literal>, and <literal moreinfo="none">invariant</literal>.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>On contract failures</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Print an error to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput>
|
|
and call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput> (but can be customized
|
|
to throw exceptions, exit with an error code, etc.).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput> (but can be customized
|
|
to throw exceptions, exit with an error code, etc.).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">abort</phrase></computeroutput> (but can be customized
|
|
to throw exceptions, exit with an error code, etc.).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Throw exceptions.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Throw exceptions.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Return values in postconditions</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, captured by or passed as a parameter to (for virtual functions)
|
|
the postcondition functor.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>result-variable-name</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[[</phrase><phrase role="identifier">ensures</phrase>
|
|
</computeroutput><literal moreinfo="none"><emphasis>result-variable-name</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">:</phrase> <phrase role="special">...]]</phrase></computeroutput>.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <literal moreinfo="none">result</literal> keyword.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">out</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>result-variable-name</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Old values in postconditions</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
macro and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
(but copied before preconditions unless <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
is used as shown in <link linkend="boost_contract.advanced.old_values_copied_at_body">Old
|
|
Values Copied at Body</link>). For templates, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>
|
|
skips old value copies for non-copyable types and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
skips old value copies selectively based on old expression type
|
|
requirements (on compilers that do not support <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput>).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">oldof</phrase></computeroutput> keyword
|
|
(copied right after preconditions). (Never skipped, not even in
|
|
templates for non-copyable types.)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, <literal moreinfo="none">old</literal> keyword (copied right after preconditions).
|
|
(Never skipped, but all types are copyable in Eiffel.)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Class invariants</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, checked at constructor exit, at destructor entry and throw,
|
|
and at public function entry, exit, and throw. Same for volatile
|
|
class invariants. Static class invariants checked at entry, exit,
|
|
and throw for constructors, destructors, and any (also <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>) public function.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, checked at constructor exit, at destructor entry and throw,
|
|
and at public function entry, exit, and throw. (Volatile and static
|
|
class invariants not supported.)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, checked at constructor exit, and around public functions.
|
|
(Volatile and static class invariants do not apply to Eiffel.)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, checked at constructor exit, at destructor entry, and around
|
|
public functions. However, invariants cannot call public functions
|
|
(to avoid infinite recursion because D does not disable contracts
|
|
while checking other contracts). (Volatile and static class invariants
|
|
not supported, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>
|
|
was deprecated all together in D.)
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Subcontracting</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, also supports subcontracting for multiple inheritance (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput>
|
|
are used to declare base classes, overrides and virtual public
|
|
functions respectively).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, also supports subcontracting for multiple inheritance, but
|
|
preconditions cannot be subcontracted. <footnote id="boost_contract.contract_programming_overview.feature_summary.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The authors of <link linkend="N1962_anchor">[N1962]</link> decided to forbid derived
|
|
classes from subcontracting preconditions because they found
|
|
that such a feature was rarely, if ever, used (see <ulink url="http://lists.boost.org/Archives/boost/2010/04/164862.php">Re:
|
|
[boost] [contract] diff n1962</ulink>). Still, it should be noted
|
|
that even in <link linkend="N1962_anchor">[N1962]</link> if a
|
|
derived class overrides two functions with preconditions coming
|
|
from two different base classes via multiple inheritance, the
|
|
overriding function contract will check preconditions from its
|
|
two base class functions in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
(so even in <link linkend="N1962_anchor">[N1962]</link> preconditions
|
|
can indirectly subcontract when multiple inheritance is used).
|
|
Furthermore, subcontracting preconditions is soundly defined
|
|
by the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> so this library allows to subcontract preconditions
|
|
as Eiffel does (users can always avoid using this feature if
|
|
they have no need for it). (This is essentially the only feature
|
|
on which this library deliberately differs from <link linkend="N1962_anchor">[N1962]</link>.)
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Contracts for pure virtual functions</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (programmed via out-of-line functions as always in C++ with
|
|
pure virtual function definitions).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No (because no subcontracting).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (contracts for abstract functions).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Arbitrary code in contracts</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (but users are generally recommended to only program assertions
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
and if-guard statements within contracts to avoid introducing bugs
|
|
and expensive code in contracts, and also to only use public functions
|
|
to program preconditions).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No, assertions only (use of only public functions to program preconditions
|
|
is recommended but not prescribed).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No, assertions only (in addition contracts of public, protected,
|
|
and private members can only use other public, public/protected,
|
|
and public/protected/private members respectively).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No, assertions only (in addition only public members can be used
|
|
in preconditions).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Constant-correctness</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No, enforced only for class invariants and old values (making also
|
|
preconditions and postconditions constant-correct is possible but
|
|
requires users to program a fare amount of boiler-plate code).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (side effects in contracts lead to undefined behaviour).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No, enforced only for class invariants.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Contracts in specifications</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No, in function definitions instead (unless programmers manually
|
|
write an extra function for any given function).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (in function declarations).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (in function declarations).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Function code ordering</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Preconditions, postconditions, exception guarantees, body.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Preconditions, postconditions, body.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Preconditions, postconditions, body.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Preconditions, body, postconditions.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Preconditions, postconditions, body.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Disable assertion checking within assertions checking
|
|
(to avoid infinite recursion when checking contracts)</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, but use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039682816">BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</link></computeroutput>
|
|
to disable no assertion while checking preconditions (see also
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039677008">BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION</link></computeroutput>).
|
|
<footnote id="boost_contract.contract_programming_overview.feature_summary.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Technically, it can
|
|
be shown that an invalid argument can reach the function body
|
|
when assertion checking is disabled while checking preconditions
|
|
(that is why <link linkend="N1962_anchor">[N1962]</link> does
|
|
not disable any assertion while checking preconditions, see
|
|
<ulink url="http://lists.boost.org/Archives/boost/2010/04/164862.php">Re:
|
|
[boost] [contract] diff n1962</ulink>). However, this can only
|
|
happen while checking contracts when an invalid argument passed
|
|
to the body, which should results in the body either throwing
|
|
an exception or returning an incorrect result, will in turn fail
|
|
the contract assertion being checked by the caller of the body
|
|
and invoke the related contract failure handler as desired in
|
|
the first place. Furthermore, not disabling assertions while
|
|
checking preconditions (like <link linkend="N1962_anchor">[N1962]</link>
|
|
does) makes it possible to have infinite recursion while checking
|
|
preconditions. Therefore, this library by default disables assertion
|
|
checking also while checking preconditions (like Eiffel does),
|
|
but it also provides the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039682816">BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</link></computeroutput>
|
|
configuration macro so users can change this behaviour to match
|
|
<link linkend="N1962_anchor">[N1962]</link> if needed.
|
|
</para>
|
|
</footnote> (In multi-threaded programs this introduces a global
|
|
lock, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039741520">BOOST_CONTRACT_DISABLE_THREADS</link></computeroutput>.)
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes for class invariants and postconditions, but preconditions
|
|
disable no assertion.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Nested member function calls</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Disable nothing. <footnote id="boost_contract.contract_programming_overview.feature_summary.f2">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Older versions of
|
|
this library defined a data member in the user class that was
|
|
automatically used to disable checking of class invariants within
|
|
nested member function calls (similarly to Eiffel). This feature
|
|
was required by older revisions of <link linkend="N1962_anchor">[N1962]</link>
|
|
but it is no longer required by <link linkend="N1962_anchor">[N1962]</link>
|
|
(because it seems to be motivated purely by optimization reasons
|
|
while similar performances can be achieved by disabling invariants
|
|
for release builds). Furthermore, in multi-threaded programs
|
|
this feature would introduce a lock that synchronizes all member
|
|
functions calls for a given object. Therefore, this feature was
|
|
removed in the current revision of this library.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Disable nothing.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Disable nothing.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Disable all contract assertions.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Disable nothing.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Disable contract checking</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, contract checking can be skipped at run-time by defining combinations
|
|
of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039634896">BOOST_CONTRACT_NO_ENTRY_INVARIANTS</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>
|
|
macros (completely removing contract code from compiled object
|
|
code is also possible but requires using macros as shown in <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable
|
|
Contract Compilation</link>).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (contract code also removed from compiled object code, but
|
|
details are compiler-implementation specific).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes (contract code also removed from compiled object code, but
|
|
details are compiler-implementation specific).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, but only predefined combinations of preconditions, postconditions,
|
|
and class invariants can be disabled (contract code also removed
|
|
from compiled object code).
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<emphasis>Assertion levels</emphasis>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, predefined default, audit, and axiom, in addition programmers
|
|
can also define their own levels.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No (but a previous revision of this proposal considered adding
|
|
assertion levels under the name of "assertion ordering").
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Yes, predefined default, audit, and axiom.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
No.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<para>
|
|
The authors of this library consulted the following references that implement
|
|
contract programming for C++ (but usually for only a limited set of features,
|
|
or using preprocessing tools other than the C++ preprocessor and external
|
|
to the language itself) and for other languages (see <link linkend="boost_contract.bibliography">Bibliography</link>
|
|
for a complete list of all references consulted during the design and development
|
|
of this library):
|
|
</para>
|
|
<informaltable frame="all">
|
|
<tgroup cols="3">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
Reference
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Language
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Notes
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Bright04b_anchor">[Bright04b]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Digital Mars C++
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
The Digital Mars C++ compiler extends C++ adding contract programming
|
|
language support (among many other features).
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Maley99_anchor">[Maley99]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
C++
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This supports contract programming including subcontracting but
|
|
with limitations (e.g., programmers need to manually build an inheritance
|
|
tree using artificial template parameters), it does not use macros
|
|
but programmers are required to write by hand a significant amount
|
|
of boiler-plate code. (The authors have found this work very inspiring
|
|
when developing initial revisions of this library especially for
|
|
its attempt to support subcontracting.)
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Lindrud04_anchor">[Lindrud04]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
C++
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This supports class invariants and old values but it does not support
|
|
subcontracting (contracts are specified within definitions instead
|
|
of declarations and assertions are not constant-correct).
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Tandin04_anchor">[Tandin04]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
C++
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Interestingly, these contract macros automatically generate Doxygen
|
|
documentation <footnote id="boost_contract.contract_programming_overview.feature_summary.f3">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Older versions of
|
|
this library also automatically generated Doxygen documentation
|
|
from contract definition macros. This functionality was abandoned
|
|
for a number of reasons: This library no longer uses macros to
|
|
program contracts; even before that, the implementation of this
|
|
library macros became too complex and the Doxygen preprocessor
|
|
was no longer able to expand them; the Doxygen documentation
|
|
was just a repeat of the contract code (so programmers could
|
|
directly look at contracts in the source code); Doxygen might
|
|
not necessarily be the documentation tool used by all C++ programmers.
|
|
</para>
|
|
</footnote> but old values, class invariants, and subcontracting
|
|
are not supported (plus contracts are specified within definitions
|
|
instead of declarations and assertions are not constant-correct).
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Nana_anchor">[Nana]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
GCC C++
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This uses macros but it only works on GCC (and maybe Clang, but
|
|
it does not work on MSVC, etc.). It does not support subcontracting.
|
|
It requires extra care to program postconditions for functions
|
|
with multiple return statements. It seems that it might not check
|
|
class invariants when functions throw exceptions (unless the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">END</phrase></computeroutput> macro does that...). (In
|
|
addition, it provides tools for logging and integration with GDB.)
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="C2_anchor">[C2]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
C++
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This uses an external preprocessing tool (the authors could no
|
|
longer find this project's code to evaluate it).
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="iContract_anchor">[iContract]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Java
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This uses an external preprocessing tool.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Jcontract_anchor">[Jcontract]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Java
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This uses an external preprocessing tool.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="CodeContracts_anchor">[CodeContracts]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
.NET
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Microsoft contract programming for .NET programming languages.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="SpecSharp_anchor">[SpecSharp]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
C#
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This is a C# extension with contract programming language support.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="Chrome_anchor">[Chrome]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Object Pascal
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This is the .NET version of Object Pascal and it has language support
|
|
for contract programming.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<link linkend="SPARKAda_anchor">[SPARKAda]</link>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
Ada
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
This is an Ada-like programming language with support for contract
|
|
programming.
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<para>
|
|
To the best knowledge of the authors, this the only library that fully supports
|
|
all contract programming features for C++ (without using preprocessing tools
|
|
external to the language itself). In general:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Implementing preconditions and postconditions in C++ is not difficult
|
|
(e.g., using some kind of RAII object).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementing postcondition old values is also not too difficult (usually
|
|
requiring programmers to copy old values into local variables), but it
|
|
is already somewhat more difficult to ensure such copies are not performed
|
|
when postconditions are disabled. <footnote id="boost_contract.contract_programming_overview.feature_summary.f4">
|
|
<para>
|
|
For example, the following pseudocode attempts to emulate old values
|
|
in <link linkend="P0380_anchor">[P0380]</link>:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">struct</phrase> <phrase role="identifier">scope_exit</phrase> <phrase role="special">{</phrase> <phrase role="comment">// RAII.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">F</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">scope_exit</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">f_</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase> <phrase role="special">{}</phrase>
|
|
<phrase role="special">~</phrase><phrase role="identifier">scope_exit</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="identifier">f_</phrase><phrase role="special">();</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">scope_exit</phrase><phrase role="special">(</phrase><phrase role="identifier">scope_exit</phrase> <phrase role="keyword">const</phrase><phrase role="special">&)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">scope_exit</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">scope_exit</phrase> <phrase role="keyword">const</phrase><phrase role="special">&)</phrase> <phrase role="special">=</phrase> <phrase role="keyword">delete</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">()></phrase> <phrase role="identifier">f_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">fswap</phrase><phrase role="special">(</phrase><phrase role="identifier">file</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">file</phrase><phrase role="special">&</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">expects</phrase><phrase role="special">:</phrase> <phrase role="identifier">x</phrase><phrase role="special">.</phrase><phrase role="identifier">closed</phrase><phrase role="special">()]]</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">expects</phrase><phrase role="special">:</phrase> <phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">closed</phrase><phrase role="special">()]]</phrase>
|
|
<phrase role="comment">// Cannot use [[ensures]] for postconditions so to emulate old values.</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">file</phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="comment">// Emulate old values with local copies (not disabled).</phrase>
|
|
<phrase role="identifier">file</phrase> <phrase role="identifier">old_y</phrase> <phrase role="special">=</phrase> <phrase role="identifier">y</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">scope_exit</phrase> <phrase role="identifier">ensures</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check after local objects destroyed.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uncaught_exceptions</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check only if no throw.</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">assert</phrase><phrase role="special">:</phrase> <phrase role="identifier">x</phrase><phrase role="special">.</phrase><phrase role="identifier">closed</phrase><phrase role="special">()]]</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">assert</phrase><phrase role="special">:</phrase> <phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">closed</phrase><phrase role="special">()]]</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">assert</phrase><phrase role="special">:</phrase> <phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="identifier">old_y</phrase><phrase role="special">]]</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">assert</phrase><phrase role="special">:</phrase> <phrase role="identifier">y</phrase> <phrase role="special">==</phrase> <phrase role="identifier">old_x</phrase><phrase role="special">]]</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">});</phrase>
|
|
|
|
<phrase role="identifier">x</phrase><phrase role="special">.</phrase><phrase role="identifier">open</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">scope_exit</phrase> <phrase role="identifier">close_x</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="identifier">x</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase>
|
|
<phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">open</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">scope_exit</phrase> <phrase role="identifier">close_y</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase>
|
|
<phrase role="identifier">file</phrase> <phrase role="identifier">z</phrase> <phrase role="special">=</phrase> <phrase role="identifier">file</phrase><phrase role="special">::</phrase><phrase role="identifier">temp</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">z</phrase><phrase role="special">.</phrase><phrase role="identifier">open</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">scope_exit</phrase> <phrase role="identifier">close_z</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="identifier">z</phrase><phrase role="special">.</phrase><phrase role="identifier">close</phrase><phrase role="special">();</phrase> <phrase role="special">});</phrase>
|
|
|
|
<phrase role="identifier">x</phrase><phrase role="special">.</phrase><phrase role="identifier">mv</phrase><phrase role="special">(</phrase><phrase role="identifier">z</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">mv</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">z</phrase><phrase role="special">.</phrase><phrase role="identifier">mv</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
This requires boiler-plate code to make sure postconditions are correctly
|
|
checked only if the function did not throw an exception and in a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">scope_exit</phrase></computeroutput> RAII object after all
|
|
other local objects have been destroyed (because some of these destructors
|
|
contribute to establishing the postconditions). Still, it never disables
|
|
old value copies (not even if postconditions are disabled in release
|
|
builds, this would require adding even more boiler-plate code using
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifdef</phrase></computeroutput>, etc.).
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementing class invariants is more involved (especially if done automatically,
|
|
without requiring programmers to manually invoke extra functions to check
|
|
the invariants). <footnote id="boost_contract.contract_programming_overview.feature_summary.f5">
|
|
<para>
|
|
For example, the following pseudocode attempts to emulation of class
|
|
invariants in <link linkend="P0380_anchor">[P0380]</link>:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check invariants at...</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">assert</phrase><phrase role="special">:</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)]]</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">assert</phrase><phrase role="special">:</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()]]</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">ensures</phrase><phrase role="special">:</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()]]</phrase> <phrase role="comment">// ...constructor exit (only if no throw).</phrase>
|
|
<phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">~</phrase><phrase role="identifier">vector</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">expects</phrase><phrase role="special">:</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()]]</phrase> <phrase role="comment">// ...destructor entry.</phrase>
|
|
<phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">expects</phrase><phrase role="special">:</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()]]</phrase> <phrase role="comment">// ...public function entry.</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">ensures</phrase><phrase role="special">:</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()]]</phrase> <phrase role="comment">// ...public function exit (if no throw).</phrase>
|
|
<phrase role="keyword">try</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">catch</phrase><phrase role="special">(...)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">invariant</phrase><phrase role="special">();</phrase> <phrase role="comment">// ...public function exit (if throw).</phrase>
|
|
<phrase role="keyword">throw</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
This requires boiler-plate code to manually invoke the function that
|
|
checks the invariants (note that invariants are checked at public function
|
|
exit regardless of exceptions being thrown while postconditions are
|
|
not). In case the destructor can throw (e.g., it is declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">)</phrase></computeroutput>),
|
|
the destructor also requires a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">try</phrase><phrase role="special">-</phrase><phrase role="keyword">catch</phrase></computeroutput>
|
|
statement similar to the one programmed for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">push_back</phrase></computeroutput>
|
|
to check class invariants at destructor exit when it throws exceptions.
|
|
Still, an outstanding issue remains to avoid infinite recursion if
|
|
also <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">empty</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">size</phrase></computeroutput> are public functions programmed
|
|
to check class invariants (because <link linkend="P0380_anchor">[P0380]</link>
|
|
does not automatically disable assertions while checking other assertions).
|
|
</para>
|
|
</footnote> In addition, all references reviewed by the authors seem
|
|
to not consider static and volatile functions not supporting static and
|
|
volatile invariants respectively.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementing subcontracting involves a significant amount of complexity
|
|
and it seems to not be properly supported by any C++ library other than
|
|
this one (especially when handling multiple inheritance, correctly copying
|
|
postcondition old values across all overridden contracts deep in the
|
|
inheritance tree, and correctly reporting the return value to the postconditions
|
|
of overridden virtual functions in base classes). <footnote id="boost_contract.contract_programming_overview.feature_summary.f6">
|
|
<para>
|
|
For example, it is not really possible to sketch pseudocode based on
|
|
<link linkend="P0380_anchor">[P0380]</link> that emulates subcontracting
|
|
in the general case.
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.tutorial">
|
|
<title><link linkend="boost_contract.tutorial">Tutorial</link></title>
|
|
<para>
|
|
This section is a guide to basic usage of this library.
|
|
</para>
|
|
<section id="boost_contract.tutorial.non_member_functions">
|
|
<title><link linkend="boost_contract.tutorial.non_member_functions">Non-Member
|
|
Functions</link></title>
|
|
<para>
|
|
Contracts for non-member functions are programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>.
|
|
For example (see <ulink url="../../example/features/non_member.cpp"><literal moreinfo="none">non_member.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Contract for a non-member function.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">++;</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
All necessary header files of this library are included by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase></computeroutput>.
|
|
Alternatively, programmers can selectively include only the header files
|
|
they actually need among <literal moreinfo="none">boost/contract/*.hpp</literal> (see <link linkend="boost_contract.getting_started">Getting Started</link>).
|
|
</para>
|
|
<para>
|
|
It is possible to specify preconditions, postconditions, and exception guarantees
|
|
for non-member functions (see <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>,
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>,
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>).
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
function returns an RAII object that must always be assigned to a local variable
|
|
of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
(otherwise this library will generate a run-time error, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>).
|
|
<footnote id="boost_contract.tutorial.non_member_functions.f0">
|
|
<para>
|
|
The name of this local variable is arbitrary, but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">c</phrase></computeroutput>
|
|
is often used in this documentation for <quote>c</quote>heck or <quote>c</quote>aminiti
|
|
<literal moreinfo="none">;-)</literal> .
|
|
</para>
|
|
</footnote> Furthermore, C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput>
|
|
declarations cannot be used here and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
type must be explicitly specified (otherwise this library will generate a
|
|
compile-time error prior C++17 and a run-time error post C++17). <footnote id="boost_contract.tutorial.non_member_functions.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> C++17 guaranteed copy elision
|
|
on function return value voids the trick this library uses to force a compile-time
|
|
error when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> is incorrectly
|
|
used instead of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>.
|
|
The library still generates a run-time error in this case (also on C++17).
|
|
In any case, after reading this documentation it should be evident to programmers
|
|
that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> should not be used
|
|
in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
declarations so this misuse of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput>
|
|
should not be an issue in practice.
|
|
</para>
|
|
</footnote> The function body is programmed right after the declaration of
|
|
this RAII object.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
In some cases, it might be necessary to program some code before the contract.
|
|
For example for acquiring resources that will be used while checking the
|
|
contract like old values, but also to lock mutexes (or other synchronization
|
|
mechanisms) in multi-threaded programs.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
At construction, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
RAII object for non-member functions does the following (enclosing function
|
|
entry):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check preconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
At destruction instead (enclosing function exit):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
If the function body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check postconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check exception guarantees, by calling the nullary functor <literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
This ensures that non-member function contracts are correctly checked at
|
|
run-time (see <link linkend="boost_contract.contract_programming_overview.function_calls">Function
|
|
Calls</link>). (Also note that functions will correctly check their contracts
|
|
even when they are called via function pointers, function objects, etc.)
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A non-member function can avoid calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
for efficiency but only when it has no preconditions, no postconditions,
|
|
and no exception guarantees.
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.preconditions">
|
|
<title><link linkend="boost_contract.tutorial.preconditions">Preconditions</link></title>
|
|
<para>
|
|
When preconditions are specified, they are programmed using a functor <literal moreinfo="none"><emphasis>r</emphasis></literal>
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> that can be called with no parameters as
|
|
in <literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>.
|
|
Contracts that do not have preconditions simply do not call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>. Preconditions must appear before postconditions
|
|
and exception guarantees when these are all present (see <link linkend="boost_contract.tutorial.postconditions">Postconditions</link>
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>).
|
|
</para>
|
|
<para>
|
|
C++11 lambda functions are convenient to program preconditions, but any other
|
|
nullary functor can be used (see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link>). <footnote id="boost_contract.tutorial.preconditions.f0">
|
|
<para>
|
|
Lambda functions with no parameters can be programmed in C++11 as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[...]</phrase> <phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase></computeroutput>
|
|
but also equivalently as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[...]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase></computeroutput>.
|
|
This second from is often used in this documentation omitting the empty
|
|
parameter list <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> for brevity.
|
|
</para>
|
|
</footnote> For example, for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(similarly for public functions, instead destructors do not have preconditions
|
|
and constructors use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>,
|
|
see <link linkend="boost_contract.tutorial.public_functions">Public Functions</link>,
|
|
<link linkend="boost_contract.tutorial.destructors">Destructors</link>, and
|
|
<link linkend="boost_contract.tutorial.constructors">Constructors</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(...)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// Same for all other contracts.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Capture by reference or value...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase> <phrase role="comment">// ...and should not modify captures.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
The precondition functor should capture all the variables that it needs to
|
|
assert the preconditions. These variables can be captured by value when the
|
|
overhead of copying such variables is acceptable. <footnote id="boost_contract.tutorial.preconditions.f1">
|
|
<para>
|
|
In this documentation preconditions often capture variables by reference
|
|
to avoid extra copies.
|
|
</para>
|
|
</footnote> In any case, programmers should not write precondition assertions
|
|
that modify the value of the captured variables, even when those are captured
|
|
by reference (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
</para>
|
|
<para>
|
|
Any code can be programmed in the precondition functor, but it is recommended
|
|
to keep this code simple using mainly assertions and if-statements (to avoid
|
|
programming complex preconditions that might be buggy and also slow to check
|
|
at run-time). It is also recommended to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
to program precondition assertions because that enables this library to print
|
|
informative error messages when the asserted conditions are evaluated to
|
|
be false (note that this is not a variadic macro, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` contains commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will automatically call the failure handler <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>
|
|
if any of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
conditions are false or, more in general, if calling the functor specified
|
|
via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput> throws any exception. By default, this
|
|
failure handler prints an error message to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput>
|
|
and terminates the program calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>
|
|
(see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to change the failure handler to throw exceptions, exit
|
|
the program with an error code, etc.).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Contracts are most useful when their assertions only use public members
|
|
that are accessible to the caller so the caller can properly check and
|
|
use the contract. In particular, preconditions of a public function or
|
|
constructor that use non-public members are essentially incorrect because
|
|
they cannot be fully checked by the caller (in fact, Eiffel generates a
|
|
compile-time error in this case). However, this library does not enforce
|
|
such a constraint and it leaves it up to programmers to only use public
|
|
members when programming contracts, especially when asserting preconditions
|
|
(see <link linkend="boost_contract.contract_programming_overview.specifications_vs__implementation">Specifications
|
|
vs. Implementation</link>).
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.postconditions">
|
|
<title><link linkend="boost_contract.tutorial.postconditions">Postconditions</link></title>
|
|
<para>
|
|
When postconditions are specified, they are programmed using a functor <literal moreinfo="none"><emphasis>s</emphasis></literal>
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> that can be called with no parameters as
|
|
in <literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>.
|
|
Contracts that do not have postconditions simply do not call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput>. Postconditions must appear after preconditions
|
|
but before exception guarantees when these are all present (see <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>).
|
|
</para>
|
|
<para>
|
|
C++11 lambda functions are convenient to program postconditions, but any
|
|
other nullary functor can be used (see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link>). For example, for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(similarly for all other contracts):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(...)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// Same for all other contracts.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Capture by reference...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase> <phrase role="comment">// ...but should not modify captures.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
The postcondition functor should capture all the variables that it needs
|
|
to assert the postconditions. In general, these variables should be captured
|
|
by reference and not by value (because postconditions need to access the
|
|
value that these variables will have at function exit, and not the value
|
|
these variables had when the postcondition functor was first declared). Postconditions
|
|
can also capture return and old values (see <link linkend="boost_contract.tutorial.return_values">Return
|
|
Values</link> and <link linkend="boost_contract.tutorial.old_values">Old
|
|
Values</link>). In any case, programmers should not write postcondition assertions
|
|
that modify the value of the captured variables, even when those are captured
|
|
by reference (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
</para>
|
|
<para>
|
|
Any code can be programmed in the postcondition functor, but it is recommended
|
|
to keep this code simple using mainly assertions and if-statements (to avoid
|
|
programming complex postconditions that might be buggy and slow to check
|
|
at run-time). It is also recommended to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
to program postcondition assertions because that enables this library to
|
|
print informative error messages when the asserted conditions are evaluated
|
|
to be false (note that this is not a variadic macro, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` has commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will automatically call the failure handler <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput>
|
|
if any of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
conditions are false or, more in general, if calling the functor specified
|
|
via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput> throws any exception. By default, this
|
|
failure handler prints an error message to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput>
|
|
and terminates the program calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>
|
|
(see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to change the failure handler to throw exceptions, exit
|
|
the program with an error code, etc.).
|
|
</para>
|
|
<para>
|
|
For non-void virtual public functions and non-void public function overrides,
|
|
the functor <literal moreinfo="none"><emphasis>s</emphasis></literal> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> is not a nullary functor, instead it is
|
|
a unary functor taking a variable holding the return value as its one parameter
|
|
<literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>result</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> (this is to properly support subcontracting,
|
|
see <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link> and <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.tutorial.return_values">
|
|
<title><link linkend="boost_contract.tutorial.return_values">Return Values</link></title>
|
|
<para>
|
|
In non-void functions, postconditions might need to access the function return
|
|
value to program assertions. In these cases, programmers are responsible
|
|
to declare a local variable before the contract and to assign it to the return
|
|
value at function exit (when the function does not throw an exception).
|
|
<footnote id="boost_contract.tutorial.return_values.f0">
|
|
<para>
|
|
The name of the local variable that holds the return value is arbitrary,
|
|
but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> is often used
|
|
in this documentation.
|
|
</para>
|
|
</footnote> For example, for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(similarly for all other contracts):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">return_type</phrase> <phrase role="identifier">f</phrase><phrase role="special">(...)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">return_type</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase> <phrase role="comment">// Must be later assigned to return value.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// Same for all other contracts.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Also capture `result` reference...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">...);</phrase> <phrase role="comment">// ...but should not modify captures.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">...;</phrase> <phrase role="comment">// Assign `result` at each return.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
At any point where the enclosing function returns, programmers are responsible
|
|
to assign the result variable to the expression being returned. This can
|
|
be done ensuring that <emphasis>all</emphasis> <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">return</phrase></computeroutput>
|
|
statements in the function are of the form:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><literal moreinfo="none"><emphasis>return-type</emphasis></literal> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <literal moreinfo="none"><emphasis>return-expression</emphasis></literal><phrase role="special">;</phrase> <phrase role="comment">// Assign `result` at each return.</phrase>
|
|
</programlisting>
|
|
<para>
|
|
The functor used to program postconditions should capture the result variable
|
|
by reference and not by value (because postconditions must access the value
|
|
the result variable will have at function exit, and not the value the result
|
|
variable had when the postcondition functor was first declared). The return
|
|
value should never be used in preconditions, old value copies, or exception
|
|
guarantees (because the return value is not yet correctly evaluated and set
|
|
when preconditions are checked, old values are copied, or if the function
|
|
throws an exception). In any case, programmers should not modify the result
|
|
variable in the contract assertions (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
</para>
|
|
<para>
|
|
It is also possible to declared the result variable using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput>
|
|
when the function return type does not have a default constructor, or if
|
|
the default constructor is too expensive or undesirable to execute when first
|
|
declaring the result variable (see <link linkend="boost_contract.advanced.optional_return_values">Optional
|
|
Return Values</link>).
|
|
</para>
|
|
<para>
|
|
Non-void virtual public functions and non-void public function overrides
|
|
must always declare and use a result variable even when postconditions do
|
|
not directly use the function return value (this is to properly support subcontracting,
|
|
see <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link> and <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.tutorial.old_values">
|
|
<title><link linkend="boost_contract.tutorial.old_values">Old Values</link></title>
|
|
<para>
|
|
When old values are used in postconditions or in exception guarantees, programmes
|
|
are responsible to declare local variables before the contract and to assign
|
|
them to related old value expressions using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>.
|
|
<footnote id="boost_contract.tutorial.old_values.f0">
|
|
<para>
|
|
The name of a local variable that holds an old value is arbitrary, but
|
|
<literal moreinfo="none">old_<emphasis>variable-name</emphasis></literal> is often used
|
|
in this documentation.
|
|
</para>
|
|
</footnote> For example, for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(similarly for all other contracts):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(...)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// More old value declarations here if needed.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// Same for all other contracts.</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Preconditions shall not use old values.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Capture by reference...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_var</phrase> <phrase role="special">==</phrase> <phrase role="special">...);</phrase> <phrase role="comment">// ...but should not modify captures.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Capture by reference...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">->...);</phrase> <phrase role="comment">// ...but should not modify captures.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
Old values are handled by this library using the smart pointer class template
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
(so programmers do not directly manage allocation and deallocation of the
|
|
pointed memory). <footnote id="boost_contract.tutorial.old_values.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Old values have to be optional
|
|
values because they need to be left uninitialized when they are not used
|
|
because both postconditions and exception guarantees are disabled (defining
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>).
|
|
That is to avoid old value copies when old values are not used, either
|
|
a pointer or (better) a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput>
|
|
could have been used to achieve that. In addition, old values need to be
|
|
pointers internally allocated by this library so that they are never copied
|
|
twice even when calling an overridden function multiple times to check
|
|
preconditions, postconditions, etc. to implement subcontracting, so a smart
|
|
pointer class template was used.
|
|
</para>
|
|
</footnote> The pointed old value type is automatically qualified as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> (so old values cannot be mistakenly
|
|
changed by contract assertions, see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
This library ensures that old value pointers are always not null by the time
|
|
postconditions and exception guarantees are checked (so programmers can safely
|
|
dereference and use these pointers in postcondition and exception guarantee
|
|
assertions using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">operator</phrase><phrase role="special">*</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">operator</phrase><phrase role="special">-></phrase></computeroutput>
|
|
without having to check if old value pointers are not null first).
|
|
</para>
|
|
<para>
|
|
Old values should not be used in preconditions and this library does not
|
|
guarantee that old value pointers are always not null when preconditions
|
|
are checked. <footnote id="boost_contract.tutorial.old_values.f2">
|
|
<para>
|
|
For example, old value pointers might be null in preconditions when postconditions
|
|
and exception guarantees are disabled defining <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>
|
|
(but also when checking an overridden virtual public function contract
|
|
via subcontracting, etc.).
|
|
</para>
|
|
</footnote> See <link linkend="boost_contract.advanced.old_values_copied_at_body">Old
|
|
Values Copied at Body</link> for delaying the copy of old values until after
|
|
class invariants (for constructors, destructors, and public functions) and
|
|
preconditions are checked (when necessary, this allows to program old value
|
|
expressions under the simplifying assumption that class invariant and precondition
|
|
assertions are satisfied already).
|
|
</para>
|
|
<para>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput> is
|
|
a variadic macro and it takes an extra parameter when used in virtual public
|
|
functions or public function overrides (see <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link> and <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>). C++11 auto declarations can be used with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput> for brevity
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase> </computeroutput><literal moreinfo="none">old_<emphasis>variable-name</emphasis>
|
|
= BOOST_CONTRACT_OLDOF(<emphasis>expression</emphasis>)</literal> (but see
|
|
also <link linkend="boost_contract.extras.old_value_requirements__templates_">Old
|
|
Value Requirements</link>). See <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link> to program old values without using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
(e.g., on compilers that do not support variadic macros).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
This library ensures that old values are copied only once. This library
|
|
also ensures that old values are never copied when postconditions and exception
|
|
guarantees are disabled defining both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>
|
|
(note that both these two macros must be defined, defining only <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>
|
|
or only <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>
|
|
is not sufficient to prevent the run-time cost of old value copies).
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.exception_guarantees">
|
|
<title><link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link></title>
|
|
<para>
|
|
When exception guarantees are specified, they are programmed using a functor
|
|
<literal moreinfo="none"><emphasis>e</emphasis></literal> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> that can be called with no parameters as
|
|
in <literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>.
|
|
Contracts that do not have exception guarantees simply do not call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(...)</phrase></computeroutput>. Exception guarantees must appear after
|
|
both preconditions and postconditions when these are all present (see <link linkend="boost_contract.tutorial.preconditions">Preconditions</link> and
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>).
|
|
</para>
|
|
<para>
|
|
C++11 lambda functions are convenient to program exception guarantees, but
|
|
any other nullary functor can be used (see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link>). For example, for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(similarly for all other contracts):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(...)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// Same for all other contracts.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Capture by reference...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase> <phrase role="comment">// ...but should not modify captures.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
The exception guarantee functor should capture all the variables that it
|
|
needs to assert the exception guarantees. In general, these variables should
|
|
be captured by reference and not by value (because exception guarantees need
|
|
to access the value that these variables will have when the function throws,
|
|
and not the value these variables had when the exception guarantee functor
|
|
was first declared). Exception guarantees can also capture old values (see
|
|
<link linkend="boost_contract.tutorial.old_values">Old Values</link>) but
|
|
they should not access the function return value instead (because the return
|
|
value is not be properly set when the function throws an exception). In any
|
|
case, programmers should not write exception guarantee assertions that modify
|
|
the value of the captured variables, even when those are captured by reference
|
|
(see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
In real production code, it might be difficult to program meaningful exception
|
|
guarantees without resorting to expensive old value copies that will slow
|
|
down execution. Therefore, the authors recognize that exception guarantees,
|
|
even if supported by this library, might not be used often in practice
|
|
(and they are not used in most of the examples listed in the rest of this
|
|
documentation). In any case, these performance considerations are ultimately
|
|
left to programmers and their specific application domains.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
Any code can be programmed in the exception guarantee functor, but it is
|
|
recommended to keep this code simple using mainly assertions and if-statements
|
|
(to avoid programming complex exception guarantees that might be buggy and
|
|
slow to check at run-time). It is also recommended to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
to program exception guarantee assertions because that enables this library
|
|
to print informative error messages when the asserted conditions are evaluated
|
|
to be false (note that this is not a variadic macro, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` has commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will automatically call the failure handler <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput>
|
|
if any of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
conditions are false or, more in general, if calling the functor specified
|
|
via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(...)</phrase></computeroutput> throws any exception. By default, this
|
|
failure handler prints an error message to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput>
|
|
and terminates the program calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>
|
|
(see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to change the failure handler to exit the program with
|
|
an error code or to take some other custom action).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
While it is technically possible for programmers to specify an exception
|
|
guarantee handler that throws an exception in case of an exception guarantee
|
|
failure, this will force C++ to terminate the program. That is because
|
|
the handler will throw an exception while there is already an active exception
|
|
on the stack (the exception thrown by the function body that caused the
|
|
exception guarantees to be checked in the first place). Therefore, programmers
|
|
should not change the exception guarantee failure handler to throw exceptions.
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.class_invariants">
|
|
<title><link linkend="boost_contract.tutorial.class_invariants">Class Invariants</link></title>
|
|
<para>
|
|
Public member functions, constructors, and destructors can be programmed
|
|
to also check class invariants. When class invariants are specified, they
|
|
are programmed in a public <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
function named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
taking no argument and returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>.
|
|
Classes that do not have invariants, simply do not declare the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput> function. <footnote id="boost_contract.tutorial.class_invariants.f0">
|
|
<para>
|
|
This library uses template meta-programming (SFINAE-based introspection
|
|
techniques) to check invariants only for classes that declare a member
|
|
function named by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039722128">BOOST_CONTRACT_INVARIANT_FUNC</link></computeroutput>.
|
|
</para>
|
|
</footnote> For example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// Must be public.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Must be const.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This member function must be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
because contracts should not modify the object state (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
This library will generate a compile-time error if the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
qualifier is missing (unless <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_PERMISSIVE">BOOST_CONTRACT_PERMISSIVE</link></computeroutput>
|
|
is defined).
|
|
</para>
|
|
<para>
|
|
Any code can be programmed in the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
function, but it is recommended to keep this code simple using mainly assertions
|
|
and if-statements (to avoid programming complex invariants that might be
|
|
buggy and slow to check at run-time). It is also recommended to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput> to program
|
|
class invariant assertions because that enables this library to print informative
|
|
error messages when the asserted conditions are evaluated to be false (note
|
|
that this is not a variadic macro, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` has commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will automatically call failure handlers <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>
|
|
if any of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
conditions are false or, more in general, if the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
function throws an exception when invariants are checked at function entry
|
|
or exit respectively. By default, these handlers print an error message to
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> and terminate the program calling
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput> (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to change these failure handlers to throw exceptions,
|
|
exit the program with an error code, etc.).
|
|
</para>
|
|
<para>
|
|
See <link linkend="boost_contract.advanced.access_specifiers">Access Specifiers</link>
|
|
to avoid making the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
member function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>. <footnote id="boost_contract.tutorial.class_invariants.f1">
|
|
<para>
|
|
In this documentation the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
member function is often declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>
|
|
for simplicity. However, in production code it might not be acceptable
|
|
to augment the public members of a class adding the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
function (and that can be avoided using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
as explained in <link linkend="boost_contract.advanced.access_specifiers">Access
|
|
Specifiers</link>).
|
|
</para>
|
|
</footnote> See <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039722128">BOOST_CONTRACT_INVARIANT_FUNC</link></computeroutput>
|
|
to use a name different from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
(e.g., because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
clashes with other names in user-defined classes).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Contract assertions are not checked (not even class invariants) when data
|
|
members are accessed directly (this is different from Eiffel where even
|
|
accessing public data members checks class invariants). Therefore, it might
|
|
be best for both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase></computeroutput>es and
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">struct</phrase></computeroutput>s (and also <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">union</phrase></computeroutput>s, see <link linkend="boost_contract.extras.unions">Unions</link>)
|
|
that have invariants to have no mutable public data members and to access
|
|
data members publicly only via appropriate public functions (e.g., setters
|
|
and getters) that can be programmed to check the class invariants using
|
|
this library.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
See <link linkend="boost_contract.extras.volatile_public_functions">Volatile
|
|
Public Functions</link> to program invariants for classes with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput> public functions.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.tutorial.class_invariants.h0">
|
|
<phrase id="boost_contract.tutorial.class_invariants.static_class_invariants"/><link linkend="boost_contract.tutorial.class_invariants.static_class_invariants">Static
|
|
Class Invariants</link>
|
|
</bridgehead>
|
|
<para>
|
|
Static public functions can be programmed to check static class invariants.
|
|
When static class invariants are specified, they are programmed in a public
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput> function named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput> taking no argument and
|
|
returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>. Classes that
|
|
do not have static class invariants, simply do not declare the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput> function. <footnote id="boost_contract.tutorial.class_invariants.f2">
|
|
<para>
|
|
This library uses template meta-programming (SFINAE-based introspection
|
|
techniques) to check static invariants only for classes that declare a
|
|
member function named by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039712400">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</link></computeroutput>.
|
|
</para>
|
|
</footnote> For example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// Must be public.</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Must be static.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This member function must be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
(and it correctly cannot access the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>).
|
|
This library will generate a compile-time error if the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
classifier is missing (unless the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_PERMISSIVE">BOOST_CONTRACT_PERMISSIVE</link></computeroutput>
|
|
macro is defined).
|
|
</para>
|
|
<para>
|
|
Any code can be programmed in the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
function, but it is recommended to keep this code simple using mainly assertions
|
|
and if-statements (to avoid programming complex static invariants that might
|
|
be buggy and slow to check at run-time). It is also recommended to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput> to program
|
|
the assertions because that enables this library to print informative error
|
|
messages when the asserted conditions are evaluated to be false (note that
|
|
this is not a variadic macro, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` has commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will automatically call failure handlers <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>
|
|
if any of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
conditions are false or, more in general, if the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
function throws an exception when invariants are checked at function entry
|
|
or exit respectively. By default, these handlers print an error message to
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> and terminate the program calling
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput> (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to change these failure handlers to throw exceptions,
|
|
exit the program with an error code, etc.).
|
|
</para>
|
|
<para>
|
|
See <link linkend="boost_contract.advanced.access_specifiers">Access Specifiers</link>
|
|
to avoid making <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
member function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>. <footnote id="boost_contract.tutorial.class_invariants.f3">
|
|
<para>
|
|
In this documentation the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
member function is often declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>
|
|
for simplicity. However, in production code it might not be acceptable
|
|
to augment the public members of a class adding the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
function (and that can be avoided using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
as explained in <link linkend="boost_contract.advanced.access_specifiers">Access
|
|
Specifiers</link>).
|
|
</para>
|
|
</footnote> See <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039712400">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</link></computeroutput>
|
|
to use a name different from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
(e.g., because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput>
|
|
clashes with other names in user-defined classes). <footnote id="boost_contract.tutorial.class_invariants.f4">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> In C++, it is not possible
|
|
to overload a member function based on the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
classifier. Therefore, this library has to use different names for the
|
|
member functions checking non-static and static class invariants (namely
|
|
for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039722128">BOOST_CONTRACT_INVARIANT_FUNC</link></computeroutput>
|
|
and for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039712400">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</link></computeroutput>).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.tutorial.constructors">
|
|
<title><link linkend="boost_contract.tutorial.constructors">Constructors</link></title>
|
|
<para>
|
|
Contracts for constructors are programmed using the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
function and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
base class. For example (see <ulink url="../../example/features/public.cpp"><literal moreinfo="none">public.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">unique_identifiers</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">unique_identifiers</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a constructor.</phrase>
|
|
<phrase role="identifier">unique_identifiers</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">from</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">to</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">unique_identifiers</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">to</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">from</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">to</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Constructor body.</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">id</phrase> <phrase role="special">=</phrase> <phrase role="identifier">from</phrase><phrase role="special">;</phrase> <phrase role="identifier">id</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">to</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
It is not possible to specify preconditions using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
for constructors (this library will generate a compile-time error if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput> is used on the object returned by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>).
|
|
Constructor preconditions are specified using the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
base class instead (same considerations as the ones made in <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>
|
|
apply also to the precondition functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>).
|
|
Programmes should not access the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="keyword">this</phrase></computeroutput> from constructor preconditions (because
|
|
the object does not exists yet before the constructor body is executed).
|
|
<footnote id="boost_contract.tutorial.constructors.f0">
|
|
<para>
|
|
See <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link> to enforce this constraint at compile-time (but
|
|
not recommended because of extra boiler-plate code).
|
|
</para>
|
|
</footnote> Constructors without preconditions simply do not explicitly initialize
|
|
the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
base (because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
default constructor checks no contract). When the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
base class is used: <footnote id="boost_contract.tutorial.constructors.f1">
|
|
<para>
|
|
There is a MSVC bug that was fixed in MSVC 2013 for which lambdas cannot
|
|
be used in constructor member initialization lists for templates. On MSVC
|
|
compilers with that bug, an extra (static) member function can be used
|
|
(together with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">bind</phrase></computeroutput> and
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">cref</phrase></computeroutput> as needed) to program
|
|
constructor preconditions instead of using lambdas (see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link>).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
It should be specified as the <emphasis>first</emphasis> class in the
|
|
inheritance list (so constructor preconditions are checked before initializing
|
|
any other base class or data member).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Its inheritance access specifier should always be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput>
|
|
(so this extra base class does not alter the public inheritance tree
|
|
of its derived classes).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
It should never be declared as a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">virtual</phrase></computeroutput>
|
|
base (because virtual bases are initialized only once across the entire
|
|
inheritance hierarchy preventing preconditions of other base classes
|
|
from being checked).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
It takes the derived class as template parameter. <footnote id="boost_contract.tutorial.constructors.f2">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
takes the derived class as its template parameter (using the Curiously
|
|
Recursive Template Pattern, CRTP) so the instantiated template type
|
|
is unique for each derived class. This always avoids base class ambiguity
|
|
resolution errors even when multiple inheritance is used. Note that,
|
|
as already mentioned, virtual inheritance could not be used instead
|
|
of the template parameter here to resolve ambiguities (because virtual
|
|
bases are initialized only once by the outer-most derived class, and
|
|
that would not allow to properly check preconditions of all base classes).
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<note>
|
|
<para>
|
|
A class can avoid inheriting from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
for efficiency but only when all its constructors have no preconditions.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
It is possible to specify postconditions for constructors (see <link linkend="boost_contract.tutorial.postconditions">Postconditions</link>),
|
|
but programmers should not access the old value of the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="keyword">this</phrase></computeroutput> in constructor
|
|
postconditions (because the object did not exist yet before the constructor
|
|
body was executed). <footnote id="boost_contract.tutorial.constructors.f3">
|
|
<para>
|
|
See <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link> to enforce this constraint at compile-time (but
|
|
not recommended because of extra boiler-plate code).
|
|
</para>
|
|
</footnote> It is also possible to specify exceptions guarantees for constructors
|
|
(see <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>), but programmers should not access the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="keyword">this</phrase></computeroutput> or its
|
|
old value in constructor exception guarantees (because the object did not
|
|
exist before executing the constructor body and it was not properly constructed
|
|
given the constructor body threw an exception). <footnote id="boost_contract.tutorial.constructors.f4">
|
|
<para>
|
|
See <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link> to enforce these constraints at compile-time (but
|
|
not recommended because of extra boiler-plate code).
|
|
</para>
|
|
</footnote> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
function takes <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> as a parameter
|
|
(because constructors check class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>).
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
function returns an RAII object that must always be assigned to a local variable
|
|
of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
(otherwise this library will generate a run-time error, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>).
|
|
Furthermore, C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> declarations
|
|
cannot be used here and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
type must be explicitly specified (otherwise this library will generate a
|
|
compile-time error prior C++17 and a run-time error post C++17). The constructor
|
|
body is programmed right after the declaration of this RAII object.
|
|
</para>
|
|
<para>
|
|
At construction, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
RAII object for constructors does the following (enclosing constructor entry):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants, by calling <literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> (but not non-static class invariants
|
|
because the object does not exist yet).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
At destruction instead (enclosing constructor exit):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants, by calling <literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the constructor body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check non-static class invariants, by calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check postconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check exception guarantees, by calling the nullary functor <literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
This together with C++ object construction mechanism of base classes and
|
|
the use of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
ensures that the constructor contracts are correctly checked at run-time
|
|
(see <link linkend="boost_contract.contract_programming_overview.constructor_calls">Constructor
|
|
Calls</link>).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A constructor can avoid calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
for efficiency but only when it has no postconditions, no exception guarantees,
|
|
and its class has no invariants (even if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
is not used by a derived class, contracts of base class constructors will
|
|
still be correctly checked by C++ object construction mechanism).
|
|
</para>
|
|
<para>
|
|
The default constructor and copy constructor automatically generated by
|
|
C++ will not check contracts. Therefore, unless these constructors are
|
|
not public or they have no preconditions, no postconditions, no exception
|
|
guarantees, and their class has no invariants, programmers should manually
|
|
define them using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>.
|
|
Similar considerations apply to all other constructors automatically generated
|
|
by C++ (e.g., the move constructor).
|
|
</para>
|
|
</note>
|
|
<bridgehead renderas="sect4" id="boost_contract.tutorial.constructors.h0">
|
|
<phrase id="boost_contract.tutorial.constructors.private_and_protected_constructors"/><link linkend="boost_contract.tutorial.constructors.private_and_protected_constructors">Private
|
|
and Protected Constructors</link>
|
|
</bridgehead>
|
|
<para>
|
|
Private and protected constructors can omit <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
(because they are not part of the public interface of the class so they are
|
|
not required to check class invariants, see <link linkend="boost_contract.contract_programming_overview.constructor_calls">Constructor
|
|
Calls</link>). They could still use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
to check preconditions before member initializations, and even use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(but not <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>)
|
|
to only check postconditions and exception guarantees without checking class
|
|
invariants and without calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
(see <link linkend="boost_contract.advanced.private_and_protected_functions">Private
|
|
and Protected Functions</link>). For example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">:</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">protected</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a protected constructor (same for private constructors).</phrase>
|
|
<phrase role="identifier">u</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="comment">// Still use this base class to check constructor preconditions.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="comment">// Following will correctly not check class invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="comment">// Do not use `.precondition(...)` here.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Constructor body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</section>
|
|
<section id="boost_contract.tutorial.destructors">
|
|
<title><link linkend="boost_contract.tutorial.destructors">Destructors</link></title>
|
|
<para>
|
|
Contracts for destructors are programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>.
|
|
For example (see <ulink url="../../example/features/public.cpp"><literal moreinfo="none">public.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">unique_identifiers</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">unique_identifiers</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a destructor.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">unique_identifiers</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Following contract checks class invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="comment">// Destructor body here... (do nothing in this example).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
It is not possible to specify preconditions for destructors (this library
|
|
will generate a compile-time error if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
is used here and that is because destructors can be called at any time after
|
|
construction so they have no precondition). It is possible to specify postconditions
|
|
for destructors (see <link linkend="boost_contract.tutorial.postconditions">Postconditions</link>,
|
|
and also <link linkend="boost_contract.tutorial.static_public_functions">Static
|
|
Public Functions</link> for an example), but programmers should not access
|
|
the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="keyword">this</phrase></computeroutput>
|
|
in destructor postconditions (because the object no longer exists after the
|
|
destructor body has been executed). <footnote id="boost_contract.tutorial.destructors.f0">
|
|
<para>
|
|
See <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link> to enforce this constraint at compile-time (but
|
|
not recommended because of extra boiler-plate code).
|
|
</para>
|
|
</footnote> It is also possible to specify exceptions guarantees for destructors
|
|
(see <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>, even if destructors should usually be programmed to not
|
|
throw exceptions in C++, in fact destructors are implicitly declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput> since C++11). <footnote id="boost_contract.tutorial.destructors.f1">
|
|
<para>
|
|
Exceptions guarantees in destructors can access both the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="keyword">this</phrase></computeroutput> and
|
|
its old value because the object existed before executing the destructor
|
|
body and it still exists given the destructor body failed throwing an exception
|
|
so technically the object should still be properly constructed and satisfy
|
|
its class invariants.
|
|
</para>
|
|
</footnote> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>
|
|
function takes <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> as a parameter
|
|
(because destructors check class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>).
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>
|
|
function returns an RAII object that must always be assigned to a local variable
|
|
of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
(otherwise this library will generate a run-time error, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>).
|
|
Furthermore, C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> declarations
|
|
cannot be used here and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
type must be explicitly specified (otherwise this library will generate a
|
|
compile-time error prior C++17 and a run-time error post C++17). The destructor
|
|
body is programmed right after the declaration of this RAII object.
|
|
</para>
|
|
<para>
|
|
At construction, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
RAII object for destructors does the following (enclosing destructor entry):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static and non-static class invariants, by calling <emphasis><literal moreinfo="none">type-of</literal></emphasis><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
At destruction instead (enclosing destructor exit):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants, by calling <literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the destructor body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check postconditions, by calling the nullay functor <literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else (even if destructors should generally be programmed not to throw
|
|
in C++):
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check non-static class invariants, by calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput> (because the object was not successfully
|
|
destructed).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check exception guarantees, by calling the nullary functor <literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
This together with C++ object destruction mechanism of base classes ensures
|
|
that destructor contracts are correctly checked at run-time (see <link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor
|
|
Calls</link>).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A destructor can avoid calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>
|
|
for efficiency but only when it has no postconditions, no exception guarantees,
|
|
and its class has no invariants (even if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>
|
|
is not used by a derived class, contracts of base class destructors will
|
|
still be correctly checked by C++ object destruction mechanism).
|
|
</para>
|
|
<para>
|
|
The default destructor automatically generated by C++ will not check contracts.
|
|
Therefore, unless the destructor is not public or it has no postconditions,
|
|
no exception guarantees, and its class has no invariants, programmers should
|
|
manually define it using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>.
|
|
</para>
|
|
</note>
|
|
<bridgehead renderas="sect4" id="boost_contract.tutorial.destructors.h0">
|
|
<phrase id="boost_contract.tutorial.destructors.private_and_protected_destructors"/><link linkend="boost_contract.tutorial.destructors.private_and_protected_destructors">Private
|
|
and Protected Destructors</link>
|
|
</bridgehead>
|
|
<para>
|
|
Private and protected destructors can omit <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>
|
|
(because they are not part of the public interface of the class so they are
|
|
not required to check class invariants, see <link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor
|
|
Calls</link>). They could use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(but not <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>)
|
|
to only check postconditions and exception guarantees without checking class
|
|
invariants and without calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
(see <link linkend="boost_contract.advanced.private_and_protected_functions">Private
|
|
and Protected Functions</link>). For example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">protected</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a protected destructor (same for private destructors).</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">u</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Following will correctly not check class invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="comment">// Do not use `.precondition(...)` here.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(...);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="comment">// Could use `.except(...)` here in rare cases of destructors declared to throw.</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Destructor body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</section>
|
|
<section id="boost_contract.tutorial.public_functions">
|
|
<title><link linkend="boost_contract.tutorial.public_functions">Public Functions</link></title>
|
|
<para>
|
|
Contracts for public functions are programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
In this section, let's consider public functions that are not static, not
|
|
virtual, and do not override any function from base classes. For example
|
|
(see <ulink url="../../example/features/public.cpp"><literal moreinfo="none">public.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">unique_identifiers</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">unique_identifiers</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a public function (but no static, virtual, or override).</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">result</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Function body.</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="special">!=</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
It is possible to specify preconditions, postconditions, and exception guarantees
|
|
for public functions (see <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>,
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>,
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>). When called from non-static public functions, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function takes <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> as a parameter
|
|
(because public functions check class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>).
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function returns an RAII object that must always be assigned to a local variable
|
|
of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
(otherwise this library will generate a run-time error, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>).
|
|
Furthermore, C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> declarations
|
|
cannot be used here and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
type must be explicitly specified (otherwise this library will generate a
|
|
compile-time error prior C++17 and a run-time error post C++17). The public
|
|
function body is programmed right after the declaration of this RAII object.
|
|
</para>
|
|
<para>
|
|
At construction, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
RAII object for public functions does the following (enclosing public function
|
|
entry):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static and non-static class invariants, by calling <literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check preconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
At destruction instead (enclosing public function exit):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static and non-static class invariants, by calling <literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput>
|
|
(even if the function body threw an exception).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the function body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check postconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check exception guarantees, by calling the nullary functor <literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
This ensures that public function contracts are correctly checked at run-time
|
|
(see <link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link>).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A public function can avoid calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
for efficiency but only when it has no preconditions, no postconditions,
|
|
no exception guarantees, it is not virtual, it does not override any virtual
|
|
function, and its class has no invariants.
|
|
</para>
|
|
<para>
|
|
The default copy assignment operator automatically generated by C++ will
|
|
not check contracts. Therefore, unless this operator is not public or it
|
|
has no preconditions, no postconditions, no exception guarantees, and its
|
|
class has no invariants, programmers should manually define it using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
Similar considerations apply to all other operators automatically generated
|
|
by C++ (e.g., the move operator).
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.virtual_public_functions">
|
|
<title><link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link></title>
|
|
<para>
|
|
Contracts for public functions are programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
In this section, let's consider public functions that are virtual but that
|
|
do not override any function from base classes. For example (see <ulink url="../../example/features/public.cpp"><literal moreinfo="none">public.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">unique_identifiers</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">unique_identifiers</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a public virtual function (but no override).</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">id</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Extra `v`.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase><phrase role="special">></phrase> <phrase role="identifier">old_find</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase> <phrase role="comment">// Pass `v`.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase> <phrase role="comment">// Pass `v`.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase> <phrase role="comment">// Pass `v` and `result`.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase> <phrase role="comment">// ID cannot be already present.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!*</phrase><phrase role="identifier">old_find</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">id</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Function body.</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Virtual public functions must declare an extra trailing parameter of type
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput> with default value <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">0</phrase></computeroutput>
|
|
(i.e., <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">nullptr</phrase></computeroutput>). <footnote id="boost_contract.tutorial.virtual_public_functions.f0">
|
|
<para>
|
|
The name of this extra parameter is arbitrary, but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput>
|
|
is often used in this documentation.
|
|
</para>
|
|
</footnote> This extra parameter is the last parameter and it has a default
|
|
value so it does not alter the calling interface of the virtual function
|
|
(callers will rarely, if ever, have to explicitly deal with this extra parameter
|
|
a part from when manipulating the virtual function type directly for function
|
|
pointer type-casting, etc.). Programmers must pass the extra virtual parameter
|
|
as the very first argument to all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
calls in the virtual public function definition. <footnote id="boost_contract.tutorial.virtual_public_functions.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput> parameter is used by this library to determine
|
|
that a function is virtual (in C++ it is not possible to introspect if
|
|
a function is declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">virtual</phrase></computeroutput>).
|
|
Furthermore, this parameter is internally used by this library to implement
|
|
subcontracting (specifically to pass result and old values that are evaluated
|
|
by the overriding function to the contracts of overridden virtual functions
|
|
in base classes, and also to check preconditions, postconditions, and exception
|
|
guarantees of overridden virtual functions in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
and <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with contracts of the overriding virtual function).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
When called from virtual public functions, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function takes <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> as a parameter
|
|
(because public functions check class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>). For virtual public functions returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// A void virtual public function (that does not override).</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase> <phrase role="comment">// No result parameter...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase> <phrase role="comment">// ...so nullary functor.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
For virtual public functions not returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>,
|
|
programmers must also pass a reference to the function return value as the
|
|
second argument to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
In this case, the library will pass this return value reference to the postcondition
|
|
functor that must therefore take one single argument matching the return
|
|
type, otherwise this library will generate a compile-time error (the functor
|
|
parameter can be a constant reference <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput> to avoid extra copies of the return
|
|
value): <footnote id="boost_contract.tutorial.virtual_public_functions.f2">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The extra function result parameter
|
|
taken by the functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
is used by this library to pass the return value evaluated by the overriding
|
|
function to all its overridden virtual functions to support subcontracting.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// A void virtual public function (that does not override).</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">t</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase> <phrase role="comment">// Result parameter...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="identifier">t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase> <phrase role="comment">// ...so unary functor.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Assign `result` at each return.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<important>
|
|
<para>
|
|
It is the responsibility of the programmers to pass the extra virtual parameter
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> to all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
calls within virtual public functions, and also to pass the return value
|
|
reference after <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
for non-void virtual public functions. This library cannot automatically
|
|
generate compile-time errors if programmers fail to do so (but in general
|
|
this will prevent the library from correctly checking contracts at run-time).
|
|
<footnote id="boost_contract.tutorial.virtual_public_functions.f3">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> This library does not require
|
|
programmers to specify the function type when using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
for non-overriding virtual public functions. Therefore, this library
|
|
does not know if the enclosing function has a non-void return type so
|
|
it cannot check if the return value reference is passed as required for
|
|
non-overriding virtual public functions. Instead the function type is
|
|
passed to this library for virtual public function overrides and that
|
|
also allows this library to give a compile-time error if the return value
|
|
reference is missing in those cases.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
<emphasis role="bold">Mnemonics:</emphasis>
|
|
</para>
|
|
<blockquote>
|
|
<para>
|
|
When <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> is present, always
|
|
pass it as the first argument to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>.
|
|
</para>
|
|
</blockquote>
|
|
<blockquote>
|
|
<para>
|
|
Always pass <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> to
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
right after <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> for non-void
|
|
functions.
|
|
</para>
|
|
</blockquote>
|
|
</important>
|
|
<para>
|
|
For the rest, considerations made in <link linkend="boost_contract.tutorial.public_functions">Public
|
|
Functions</link> apply to virtual public functions as well.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A virtual public function should always call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(even if it has no preconditions, no postconditions, no exception guarantees,
|
|
and its class has no invariants), otherwise this library will not be able
|
|
to correctly use it for subcontracting.
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
<title><link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides (Subcontracting)</link></title>
|
|
<para>
|
|
Contracts for public functions are programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
In this section, let's consider public functions (virtual or not) that override
|
|
virtual public functions from one or more of their public base classes. For
|
|
example (see <ulink url="../../example/features/public.cpp"><literal moreinfo="none">public.cpp</literal></ulink>):
|
|
<footnote id="boost_contract.tutorial.public_function_overrides__subcontracting_.f0">
|
|
<para>
|
|
In this documentation, function overrides are often marked with the code
|
|
comment <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">/* override */</phrase></computeroutput>. On
|
|
compilers that support C++11 virtual specifiers, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override</phrase></computeroutput>
|
|
identifier can be used instead (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override</phrase></computeroutput>
|
|
is not used in the documentation simply because virtual specifiers are
|
|
not widely supported yet, even by compilers that support C++11 lambda functions).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">identifiers</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">unique_identifiers</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Bases typedef.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check in AND with bases.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract for a public function override.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">id</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase><phrase role="special">></phrase> <phrase role="identifier">old_find</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_push_back</phrase> <phrase role="comment">// Pass override type plus below function pointer...</phrase>
|
|
<phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">identifiers</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="comment">// ...and arguments.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check in OR with bases.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase> <phrase role="comment">// ID can be already present.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check in AND with bases.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_find</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Function body.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">))</phrase> <phrase role="identifier">unique_identifiers</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">);</phrase> <phrase role="comment">// Else, do nothing.</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">push_back</phrase><phrase role="special">)</phrase> <phrase role="comment">// Define `override_push_back`.</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typedef</phrase></computeroutput> declared using
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
is required by this library for derived classes and it is internally used
|
|
to detect base classes for subcontracting (see <link linkend="boost_contract.tutorial.base_classes__subcontracting_">Base
|
|
Classes</link>). This library will generate a compile-time error if there
|
|
is no suitable virtual function to override in any of the public base classes
|
|
for subcontracting. <footnote id="boost_contract.tutorial.public_function_overrides__subcontracting_.f1">
|
|
<para>
|
|
The compile-time error generated by the library in this case is similar
|
|
in principle to the error generated by the C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override</phrase></computeroutput>
|
|
specifier, but it is limited to functions with the extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput> parameter and searched recursively only
|
|
in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput> base classes passed
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
because only those are considered for subcontracting.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
When called from public function overrides, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function template takes an explicit template argument <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase></computeroutput><literal moreinfo="none"><emphasis>function-name</emphasis></literal>
|
|
that must be defined using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>function-name</emphasis></literal><phrase role="special">)</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This can be declared at any point in the public section of the enclosing
|
|
class (see <link linkend="boost_contract.advanced.access_specifiers">Access
|
|
Specifiers</link> to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
also in a non-public section of the class). <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
is used only once in a class for a given function name and overloaded functions
|
|
can reuse the same <literal moreinfo="none">override_<emphasis>function-name</emphasis></literal>
|
|
definition (see <link linkend="boost_contract.advanced.function_overloads">Function
|
|
Overloads</link>). <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
can be used to generate a name different than <literal moreinfo="none">override_<emphasis>function-name</emphasis></literal>
|
|
(e.g., to avoid generating C++ reserved names containing double underscores
|
|
"<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">__</phrase></computeroutput>" for function
|
|
names that already start with an underscore "<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">_</phrase></computeroutput>",
|
|
see <link linkend="boost_contract.advanced.named_overrides">Named Overrides</link>).
|
|
For convenience <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link></computeroutput>
|
|
can be used with multiple function names instead of repeating <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput> for each
|
|
function name (on compilers that support variadic macros). For example, for
|
|
three functions named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">f</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">g</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">h</phrase></computeroutput>
|
|
(but same for any other number of functions), the following:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="identifier">g</phrase><phrase role="special">,</phrase> <phrase role="identifier">h</phrase><phrase role="special">)</phrase>
|
|
</programlisting>
|
|
<para>
|
|
Is equivalent to: <footnote id="boost_contract.tutorial.public_function_overrides__subcontracting_.f2">
|
|
<para>
|
|
There is no equivalent of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
that operates on multiple function names at once (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
is not expected to be used often so it can simply be repeated multiple
|
|
times when needed).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">g</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">h</phrase><phrase role="special">)</phrase>
|
|
</programlisting>
|
|
<para>
|
|
Public function overrides must always list the extra trailing parameter of
|
|
type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput> with default value <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">0</phrase></computeroutput>
|
|
(i.e., <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">nullptr</phrase></computeroutput>), even when they
|
|
are not declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">virtual</phrase></computeroutput>, if
|
|
this parameter is present in the signature of the virtual function being
|
|
overridden from base classes. Programmers must pass the extra virtual parameter
|
|
as the very first argument to all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
calls in the public function override definition (see <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link>).
|
|
</para>
|
|
<para>
|
|
When called from public function overrides, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function takes a pointer to the enclosing function, the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="keyword">this</phrase></computeroutput> (because
|
|
public function overrides check class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>), and references to each function argument in the order
|
|
they appear in the function declaration. <footnote id="boost_contract.tutorial.public_function_overrides__subcontracting_.f3">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>
|
|
is passed after the function pointer to follow <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase></computeroutput>'s
|
|
syntax. The function pointer and references to all function arguments are
|
|
needed for public function overrides because this library has to internally
|
|
call overridden virtual public functions to check their contracts for subcontracting
|
|
(even if this library will not actually execute the bodies of the overridden
|
|
functions).
|
|
</para>
|
|
</footnote> For public function overrides returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// A void public function override.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">override_f</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">u</phrase><phrase role="special">::</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...,</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">)</phrase> <phrase role="comment">// No result parameter...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase> <phrase role="comment">// ...so nullary functor.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
For public function overrides not returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>,
|
|
programmers must also pass a reference to the function return value as the
|
|
second argument to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(this library will generate a compile-time error otherwise). <footnote id="boost_contract.tutorial.public_function_overrides__subcontracting_.f4">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> As for non-overriding virtual
|
|
public functions, also public function overrides use the extra return value
|
|
parameter to pass it to the overridden functions when subcontracting. In
|
|
the case of public function overrides, this library has the function pointer
|
|
so it will generate a compile-time error if the function is non-void and
|
|
programmers forget to specify the extra return value parameter (this extra
|
|
error checking is not possible instead for non-overriding virtual public
|
|
functions because their contracts do not take the function pointer as a
|
|
parameter, see <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link>).
|
|
</para>
|
|
</footnote> In this case, the library will pass this return value reference
|
|
to the postcondition functor that must therefore take one single argument
|
|
matching the return type, otherwise this library will generate a compile-time
|
|
error (the functor parameter can be a constant reference <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput> to avoid extra copies of the return
|
|
value, similarly to non-overriding non-void <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// A non-void public function override.</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">override_f</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">u</phrase><phrase role="special">::</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...,</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">)</phrase> <phrase role="comment">// Result parameter...</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="identifier">t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase> <phrase role="comment">// ...so unary functor.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Assign `result` at each return.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will throw <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.bad_virtual_result_cast">boost::contract::bad_virtual_result_cast</link></computeroutput>
|
|
if programmers specify return values for public function overrides in derived
|
|
classes that are not consistent with the return types of the virtual public
|
|
functions being overridden in the base classes. <footnote id="boost_contract.tutorial.public_function_overrides__subcontracting_.f5">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bad_any_cast</phrase></computeroutput>
|
|
exception was not used here because it does not print the from- and to-
|
|
type names (so it is not descriptive enough).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<important>
|
|
<para>
|
|
It is the responsibility of the programmers to pass the extra virtual parameter
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> to all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
calls within public function overrides, and also to pass the return value
|
|
reference after <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
for non-void public function overrides. This library cannot always generate
|
|
compile-time errors if programmers fail to do so (but in general this will
|
|
prevent the library from correctly checking contracts at run-time).
|
|
</para>
|
|
<para>
|
|
<emphasis role="bold">Mnemonics:</emphasis>
|
|
</para>
|
|
<blockquote>
|
|
<para>
|
|
When <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
is present, always pass it as template parameter to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
</para>
|
|
</blockquote>
|
|
<blockquote>
|
|
<para>
|
|
When <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> is present, always
|
|
pass it as the first argument to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>.
|
|
</para>
|
|
</blockquote>
|
|
<blockquote>
|
|
<para>
|
|
Always pass <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> to
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
right after <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> for non-void
|
|
functions.
|
|
</para>
|
|
</blockquote>
|
|
</important>
|
|
<para>
|
|
At construction, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
RAII object for public function overrides does the following (enclosing public
|
|
function override entry):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static and non-static class invariants for all overridden bases
|
|
and for the derived class in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with each other, by calling <literal moreinfo="none"><emphasis>type-of</emphasis>(<emphasis>overridden-base_1</emphasis>)</literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>overridden-base_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>...
|
|
<literal moreinfo="none"><emphasis>type-of</emphasis>(<emphasis>overridden-base_n</emphasis>)</literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>overridden-base_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput>
|
|
<link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check preconditions for all overridden base functions and for the overriding
|
|
derived function in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
with each other, by calling the nullary functors <literal moreinfo="none"><emphasis>r_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>...
|
|
<literal moreinfo="none"><emphasis>r_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
<link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>, ... <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> for all of the overridden and overriding
|
|
functions respectively.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
At destruction instead (enclosing public function override exit):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static and non-static class invariants for all overridden bases
|
|
and for the derived class in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with each other, by calling <literal moreinfo="none"><emphasis>type-of</emphasis>(<emphasis>overridden-base_1</emphasis>)</literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>overridden-base_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>...
|
|
<literal moreinfo="none"><emphasis>type-of</emphasis>(<emphasis>overridden-base_n</emphasis>)</literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>overridden-base_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>type-of</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput>
|
|
<link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput>
|
|
(even if the function body threw an exception).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the function body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check postconditions for all overridden base functions and for
|
|
the overriding derived function in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with each other, by calling the nullary functors <literal moreinfo="none"><emphasis>s_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>...
|
|
<literal moreinfo="none"><emphasis>s_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
<link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>, ... <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> for all of the overridden and
|
|
overriding functions respectively (or the unary functors <literal moreinfo="none"><emphasis>s_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>result</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>...
|
|
<literal moreinfo="none"><emphasis>s_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>result</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>result</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> for non-void public function overrides).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check exception guarantees for all overridden base functions and
|
|
for the overriding derived function in <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
with each other, by calling the nullary functors <literal moreinfo="none"><emphasis>e_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> <link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>...
|
|
<literal moreinfo="none"><emphasis>e_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
<link linkend="and_anchor"><literal moreinfo="none"><emphasis>AND</emphasis></literal></link>
|
|
<literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e_1</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>, ... <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e_n</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> for all of the overridden and
|
|
overriding functions respectively.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
This ensures that contracts and subcontracts of public function overrides
|
|
are correctly checked at run-time in accordance with the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> (see <link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link>).
|
|
</para>
|
|
<para>
|
|
For the rest, considerations made in <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link> apply to public function overrides as well.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A public function override should always call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(even if it has no preconditions, no postconditions, no exception guarantees,
|
|
and its class has no invariants), otherwise this library will not be able
|
|
to correctly use it for subcontracting.
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.tutorial.base_classes__subcontracting_">
|
|
<title><link linkend="boost_contract.tutorial.base_classes__subcontracting_">Base
|
|
Classes (Subcontracting)</link></title>
|
|
<para>
|
|
In order for this library to support subcontracting, programmers must specify
|
|
the bases of a derived class declaring a public member type named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput> via a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typedef</phrase></computeroutput>
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>.
|
|
For example (see <ulink url="../../example/features/base_types.cpp"><literal moreinfo="none">base_types.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">chars</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="comment">/* local macro (for convenience) */</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">chars</phrase><phrase role="special">>,</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">unique_chars</phrase><phrase role="special">,</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="keyword">public</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">>,</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">protected</phrase> <phrase role="identifier">has_size</phrase><phrase role="special">,</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">has_empty</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase> <phrase role="comment">// Bases of this class.</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Bases typedef.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase> <phrase role="comment">// Undefine local macro.</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
For convenience, a <emphasis>local macro</emphasis> named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BASES</phrase></computeroutput>
|
|
can be used to avoid repeating the base list twice (first in the derived
|
|
class declaration <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> </computeroutput><literal moreinfo="none"><emphasis>class-name</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
<phrase role="special">:</phrase> </computeroutput><literal moreinfo="none"><emphasis>base-list</emphasis></literal>
|
|
and then again when invoking <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>base-list</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>). Being a local macro, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BASES</phrase></computeroutput>
|
|
must be undefined using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#undef</phrase>
|
|
<phrase role="identifier">BASES</phrase></computeroutput> after it is used to declare
|
|
the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput> <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typedef</phrase></computeroutput> (to avoid name clashes and macro redefinition
|
|
errors). <footnote id="boost_contract.tutorial.base_classes__subcontracting_.f0">
|
|
<para>
|
|
The name of this local macro is arbitrary, but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BASES</phrase></computeroutput>
|
|
is often used in this documentation.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
is a variadic macro and accepts a list of bases separated by commas (see
|
|
<link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link> to program <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
without using macros). As already noted in <link linkend="boost_contract.tutorial.constructors">Constructors</link>,
|
|
when the extra base <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
is used to program constructor preconditions, its inheritance access level
|
|
must always be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput> and it
|
|
must be specified as the very first base.
|
|
</para>
|
|
<important>
|
|
<para>
|
|
Each base passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
must <emphasis>explicitly</emphasis> specify its inheritance access level
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">protected</phrase></computeroutput>,
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput> (but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">virtual</phrase></computeroutput> is optional and can be specified
|
|
either before or after the access level as usual in C++). This library
|
|
will generate a compile-time error if the first base is missing its inheritance
|
|
access level, but this library will not be able to always generate an error
|
|
if the access level is missing for bases after the first one. <footnote id="boost_contract.tutorial.base_classes__subcontracting_.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> This library explicitly requires
|
|
the inheritance access level because derived classes must subcontract
|
|
only from public bases, but not from protected or private bases (see
|
|
<link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link>). <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
inspects each inheritance access level using preprocessor meta-programming
|
|
and removes non-public bases from the list of bases internally used for
|
|
subcontracting. However, this library cannot always detect when programmers
|
|
forget to specify the inheritance access level because, when commas are
|
|
used to separate template parameters passed to base classes, the preprocessor
|
|
will not be able to correctly use commas to identify the next base class
|
|
token in the inheritance list (the preprocessor cannot distinguish between
|
|
commas that are not protected by round parenthesis, like the ones used
|
|
in templates). Therefore, this library uses the inheritance access level
|
|
keyword <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">protected</phrase></computeroutput>, or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput>
|
|
instead of commas <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">,</phrase></computeroutput> for
|
|
the preprocessor to correctly find the next base class token in the inheritance
|
|
list (thus inheritance access levels must always be explicit specified
|
|
by programmers for each base).
|
|
</para>
|
|
</footnote> It is the responsibility of the programmers to make sure that
|
|
all bases passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
explicitly specify their inheritance access level (inheritance access levels
|
|
are instead optional in C++ because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput>
|
|
is implicitly assumed for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase></computeroutput>
|
|
types and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput> for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">struct</phrase></computeroutput> types).
|
|
</para>
|
|
<para>
|
|
<emphasis role="bold">Mnemonics:</emphasis>
|
|
</para>
|
|
<blockquote>
|
|
<para>
|
|
Always explicitly specify the inheritance access level <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">protected</phrase></computeroutput>,
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput> for base classes
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>.
|
|
</para>
|
|
</blockquote>
|
|
</important>
|
|
<para>
|
|
See <link linkend="boost_contract.advanced.access_specifiers">Access Specifiers</link>
|
|
to avoid making the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
member type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>. <footnote id="boost_contract.tutorial.base_classes__subcontracting_.f2">
|
|
<para>
|
|
In this documentation the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_type</phrase></computeroutput>
|
|
member type is often declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>
|
|
for simplicity. However, in production code it might not be acceptable
|
|
to augment the public members of a class adding the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
type (and that can be avoided using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
as explained in <link linkend="boost_contract.advanced.access_specifiers">Access
|
|
Specifiers</link>).
|
|
</para>
|
|
</footnote> See <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039730032">BOOST_CONTRACT_BASES_TYPEDEF</link></computeroutput>
|
|
to use a name different from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
(e.g., because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
clashes with other names in user-defined classes).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.tutorial.static_public_functions">
|
|
<title><link linkend="boost_contract.tutorial.static_public_functions">Static
|
|
Public Functions</link></title>
|
|
<para>
|
|
Contracts for public functions are programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
In this section, let's consider static public functions. For example (see
|
|
<ulink url="../../example/features/static_public.cpp"><literal moreinfo="none">static_public.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">class</phrase> <phrase role="identifier">C</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">make</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Static class invariants.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Contract for a static public function.</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Explicit template parameter `make` (check static invariants).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">make</phrase><phrase role="special">>();</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">instances_</phrase><phrase role="special">;</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
It is possible to specify preconditions, postconditions, and exception guarantees
|
|
for static public functions (see <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>,
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>,
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>). When called from static public functions, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
cannot take the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>
|
|
as a parameter (because there is no object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>
|
|
in static member functions) so the enclosing class type is specified via
|
|
an explicit template parameter as in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput><literal moreinfo="none"><<emphasis>class-type</emphasis>></literal>
|
|
(the class type is required to check static class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// A static public function.</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">>()</phrase> <phrase role="comment">// Class type `u` as explicit template parameter.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function returns an RAII object that must be assigned to a local variable
|
|
of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
(otherwise this library will generate a run-time error, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>).
|
|
Furthermore, C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> declarations
|
|
cannot be used here and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
type must be explicitly specified (otherwise this library will generate a
|
|
compile-time error prior C++17 and a run-time error post C++17). The static
|
|
public functions body is programmed right after the declaration of this RAII
|
|
object.
|
|
</para>
|
|
<para>
|
|
At construction, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
RAII object for static public functions does the following (enclosing static
|
|
public function entry):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants, by calling <literal moreinfo="none"><emphasis>class-type</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> (but never non-static class invariants).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Check preconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>r</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
At destruction instead (enclosing static public function exit):
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check static class invariants, by calling <literal moreinfo="none"><emphasis>class-type</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">::</phrase><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> (even if the function body threw an
|
|
exception, but never non-static class invariants).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the function body did not throw an exception:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check postconditions, by calling the nullary functor <literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>s</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Else:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Check exception guarantees, by calling the nullary functor <literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>e</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
This ensures that static public function contracts are correctly checked
|
|
at run-time (static public functions do not subcontract because they have
|
|
no object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> and therefore
|
|
there is no inheritance, see <link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link>).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
A static public function can avoid calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
for efficiency but only when it has no preconditions, no postconditions,
|
|
no exception guarantees, and its class has no static invariants (the class
|
|
can still have non-static invariants or base classes instead).
|
|
</para>
|
|
</note>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.advanced">
|
|
<title><link linkend="boost_contract.advanced">Advanced</link></title>
|
|
<para>
|
|
This section is a guide to advanced usage of this library.
|
|
</para>
|
|
<section id="boost_contract.advanced.pure_virtual_public_functions">
|
|
<title><link linkend="boost_contract.advanced.pure_virtual_public_functions">Pure
|
|
Virtual Public Functions</link></title>
|
|
<para>
|
|
In C++, pure virtual functions are allowed to have a <ulink url="http://en.cppreference.com/w/cpp/language/abstract_class">default
|
|
implementation</ulink> as long as such implementation is programmed out-of-line
|
|
so defined outside the class declaring the pure virtual function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">virtual</phrase> <phrase role="special">...</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase></computeroutput>.
|
|
</para>
|
|
<para>
|
|
Contracts for pure virtual public functions are programmed using the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
function like for (non-pure) virtual public functions (all consideration
|
|
made in <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link> apply). However, contracts have to be programmed
|
|
out-of-line, in the default implementation of the pure virtual function.
|
|
For example (see <ulink url="../../example/features/pure_virtual_public.cpp"><literal moreinfo="none">pure_virtual_public.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iterator</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">range</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Pure virtual function declaration (contract in definition below).</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">Iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Pure virtual function default implementation (out-of-line in C++).</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iterator</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">Iterator</phrase> <phrase role="identifier">range</phrase><phrase role="special"><</phrase><phrase role="identifier">Iterator</phrase><phrase role="special">>::</phrase><phrase role="identifier">begin</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">Iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase> <phrase role="comment">// As usual, virtual pass `result` right after `v`...</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="identifier">Iterator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Pure function body (never executed by this library).</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
This library will never actually execute the pure virtual function body while
|
|
it is calling the pure virtual function default implementation to check contracts
|
|
for subcontracting. Therefore, programmers can safely <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">)</phrase></computeroutput>
|
|
at the beginning of the body if they intend for that body to never be executed
|
|
(or they can program a working body in case they need to use pure virtual
|
|
function default implementations as usual in C++).
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.advanced.pure_virtual_public_functions.h0">
|
|
<phrase id="boost_contract.advanced.pure_virtual_public_functions.subcontracting_preconditions_always_true_or_false"/><link linkend="boost_contract.advanced.pure_virtual_public_functions.subcontracting_preconditions_always_true_or_false">Subcontracting
|
|
Preconditions Always True or False</link>
|
|
</bridgehead>
|
|
<para>
|
|
As seen in <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>, preconditions of overriding public functions are
|
|
checked in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
with preconditions of overridden virtual public functions. Therefore, if
|
|
a virtual public function in a base class specifies no precondition then
|
|
preconditions specified by all its overriding functions in derived classes
|
|
will have no effect (because when checked in <link linkend="or_anchor"><literal moreinfo="none"><emphasis>OR</emphasis></literal></link>
|
|
with the overridden function from the base class that has no preconditions,
|
|
they will always pass):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Some base class.</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// No preconditions, same as `ASSERT(true)`.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This correctly reflects the fact that the overridden function in the base
|
|
class can be called from any context (because it has no precondition) and
|
|
so must all its overriding functions in all derived classes in accordance
|
|
to the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink>. <footnote id="boost_contract.advanced.pure_virtual_public_functions.f0">
|
|
<para>
|
|
This consequence of the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> <quote>that if any function in an inheritance hierarchy
|
|
has no preconditions, then preconditions on functions overriding it have
|
|
no useful effect</quote> is also explicitly mentioned in the contract documentation
|
|
of the D Programming Language (see <link linkend="Bright04_anchor">[Bright04]</link>).
|
|
</para>
|
|
</footnote> In other words, the code above has the same effect as declaring
|
|
the virtual public function in the base class with a single precondition
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase><phrase role="special">)</phrase></computeroutput> that
|
|
will always trivially pass:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Some base class.</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase><phrase role="special">);</phrase> <phrase role="comment">// Same as no preconditions.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
<para>
|
|
On the flip side, programmers might sometimes consider to declare a pure
|
|
virtual public function in a base class with a single precondition <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">)</phrase></computeroutput> that
|
|
will always fail. This indicates that the pure virtual public function can
|
|
never be called unless it is redefined by a derived class (which is already
|
|
the case with C++ pure virtual functions) and also that the base class designers
|
|
have intentionally left it up to derived classes to specify preconditions
|
|
for the pure virtual function in question. This technique might make sense
|
|
only for preconditions of pure virtual public functions (otherwise <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">)</phrase></computeroutput> will
|
|
prevent calling virtual public functions in concrete bases). For example
|
|
(see <ulink url="../../example/features/named_override.cpp"><literal moreinfo="none">named_override.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">generic_unary_pack</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">_1</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">T</phrase> <phrase role="identifier">_1</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">generic_unary_pack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">_1</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase> <phrase role="comment">// Defer preconditions to overrides.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
That said, the need to declare such a precondition <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">)</phrase></computeroutput>
|
|
that will always fail might also be an indication that the base class interface
|
|
is not correctly designed. In general, the base class interface should still
|
|
contain all functions (eventually as pure virtual) that are necessary to
|
|
program its contracts.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.advanced.optional_return_values">
|
|
<title><link linkend="boost_contract.advanced.optional_return_values">Optional
|
|
Return Values</link></title>
|
|
<para>
|
|
It is possible to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput>
|
|
to handle return values when programmers cannot construct the result variable
|
|
at its point of declaration before the contract (e.g., because an appropriate
|
|
constructor for the return type is not available at that point, or just because
|
|
it would be too expensive to execute an extra initialization of the return
|
|
value at run-time). <footnote id="boost_contract.advanced.optional_return_values.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> This library uses <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput> instead of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput>
|
|
to support a larger number of compilers and their versions (because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput> was not available before C++17).
|
|
</para>
|
|
</footnote> For example (see <ulink url="../../example/features/optional_result.cpp"><literal moreinfo="none">optional_result.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">Index</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">get</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>&</phrase> <phrase role="identifier">vect</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase> <phrase role="comment">// Result not initialized here...</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">Index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">vect</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">vect</phrase><phrase role="special">[</phrase><phrase role="identifier">Index</phrase><phrase role="special">]);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Function body (executed after preconditions checked).</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect</phrase><phrase role="special">[</phrase><phrase role="identifier">Index</phrase><phrase role="special">]);</phrase> <phrase role="comment">// ...result initialized here instead.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
In this example the return type is a reference so it does not have default
|
|
constructor that can be used to initialize <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput>
|
|
when it is declared before the contract declaration. In addition, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Index</phrase></computeroutput> needs to be validated to be smaller
|
|
than <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">size</phrase><phrase role="special">()</phrase></computeroutput>
|
|
by the precondition before it can be used to retrieve the reference to assign
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> so <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">vect</phrase><phrase role="special">[</phrase><phrase role="identifier">Index</phrase><phrase role="special">]</phrase></computeroutput> cannot be used to initialize <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> when it is declared before the contract
|
|
declaration. Therefore, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput>
|
|
is used to defer <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> real
|
|
initialization until the execution of the function body, after the contract
|
|
declaration, where <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Index</phrase></computeroutput>
|
|
has been validated by the precondition and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">vect</phrase><phrase role="special">[</phrase><phrase role="identifier">Index</phrase><phrase role="special">]</phrase></computeroutput> can be safely evaluated to initialize <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput>.
|
|
</para>
|
|
<para>
|
|
As seen in <link linkend="boost_contract.tutorial.return_values">Return Values</link>,
|
|
it is the responsibility of the programmers to ensure that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> is always set to the return value
|
|
(when the function exits without trowing an exception). This also ensures
|
|
that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> is always set
|
|
before the postconditions are checked so programmers can always dereference
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> in postconditions
|
|
to access the return value (using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">operator</phrase><phrase role="special">*</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">operator</phrase><phrase role="special">-></phrase></computeroutput> as usual with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput>,
|
|
and without having to explicitly check if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput>
|
|
is an empty <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput> object or not). This can be done
|
|
ensuring that <emphasis>all</emphasis> <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">return</phrase></computeroutput>
|
|
statements in the function are of the form:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><literal moreinfo="none"><emphasis>return-type</emphasis></literal><phrase role="special">></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <literal moreinfo="none"><emphasis>return-expression</emphasis></literal><phrase role="special">);</phrase> <phrase role="comment">// Assign `result` at each return.</phrase>
|
|
</programlisting>
|
|
<bridgehead renderas="sect4" id="boost_contract.advanced.optional_return_values.h0">
|
|
<phrase id="boost_contract.advanced.optional_return_values.optional_results_in_virtual_public_functions"/><link linkend="boost_contract.advanced.optional_return_values.optional_results_in_virtual_public_functions">Optional
|
|
Results in Virtual Public Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
Similarly, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput> can be used to handle the return
|
|
value passed to contracts of virtual public functions (pure or not) and of
|
|
public function overrides. As seen in <link linkend="boost_contract.advanced.pure_virtual_public_functions">Pure
|
|
Virtual Public Functions</link>, <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link>, and <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>, in these cases the return value <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">result</phrase></computeroutput> must be passed as a parameter to
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
right after the parameter <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput>
|
|
of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput>. Then the functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput> takes one single parameter of type
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase></computeroutput><literal moreinfo="none"><emphasis>return-type</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
<phrase role="keyword">const</phrase><phrase role="special">&></phrase>
|
|
<phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>.
|
|
For example (see <ulink url="../../example/features/optional_result_virtual.cpp"><literal moreinfo="none">optional_result_virtual.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">accessible</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">index</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="comment">// Pass `result` right after `v`...</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="comment">// ...plus postconditions take `result` as a parameter (not capture).</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="identifier">index</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*</phrase><phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The inner <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>
|
|
in the postcondition functor parameter type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><...</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="special">...</phrase></computeroutput>
|
|
is mandatory (while the outer <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput> in the postcondition functor parameter
|
|
type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><...></phrase>
|
|
<phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>
|
|
is not). <footnote id="boost_contract.advanced.optional_return_values.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> This library requires the postcondition
|
|
functor parameter to be of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><...</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase></computeroutput> so the return value does not have
|
|
to be copied (because of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">&</phrase></computeroutput>)
|
|
while postconditions are still not allowed to change its value (because
|
|
of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>, see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
In addition, programmers are encouraged to declare the postcondition functor
|
|
to take its argument also as a constant reference <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><...</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput> to avoid possibly expensive copies
|
|
of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase></computeroutput> type itself.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.advanced.private_and_protected_functions">
|
|
<title><link linkend="boost_contract.advanced.private_and_protected_functions">Private
|
|
and Protected Functions</link></title>
|
|
<para>
|
|
Private and protected functions do not check class invariants (because they
|
|
are not part of the public class interface) and they do not subcontract (because
|
|
they are not accessible at the calling site where the <ulink url="http://en.wikipedia.org/wiki/Liskov_substitution_principle">substitution
|
|
principle</ulink> applies, see <link linkend="boost_contract.contract_programming_overview.function_calls">Function
|
|
Calls</link>). However, programmers may still want to specify preconditions
|
|
and postconditions for private and protected functions when they want to
|
|
check correctness of their implementation and use (from within the class,
|
|
base classes, friend classes or functions, etc.). When programmers decide
|
|
to specify contracts for private and protected functions, they can use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
(because, like for non-member functions, this does not check class invariants
|
|
and does not subcontract). For example (see <ulink url="../../example/features/private_protected.cpp"><literal moreinfo="none">private_protected.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">counter</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">protected</phrase><phrase role="special">:</phrase> <phrase role="comment">// Protected functions use `function()` (like non-members).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special"><=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">n_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">n</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase> <phrase role="comment">// Private functions use `function()` (like non-members).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">dec</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_get</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">min</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_get</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">n_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Considerations made in <link linkend="boost_contract.tutorial.non_member_functions">Non-Member
|
|
Functions</link> apply to private and protected functions as well. See <link linkend="boost_contract.tutorial.constructors">Constructors</link> and <link linkend="boost_contract.tutorial.destructors">Destructors</link> on how to
|
|
program contracts for private and protected constructors and destructors
|
|
instead.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.advanced.private_and_protected_functions.h0">
|
|
<phrase id="boost_contract.advanced.private_and_protected_functions.virtual_private_and_protected_functions"/><link linkend="boost_contract.advanced.private_and_protected_functions.virtual_private_and_protected_functions">Virtual
|
|
Private and Protected Functions</link>
|
|
</bridgehead>
|
|
<para>
|
|
When private and protected functions are virtual they should still declare
|
|
the extra virtual parameter of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput> with default value <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="number">0</phrase></computeroutput>
|
|
(see <link linkend="boost_contract.tutorial.virtual_public_functions">Virtual
|
|
Public Functions</link>) even if that parameter does not have to be passed
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
takes no such an argument (so the extra virtual parameter will remain unused
|
|
and it does not need a name). <footnote id="boost_contract.advanced.private_and_protected_functions.f0">
|
|
<para>
|
|
Technically, the extra virtual parameter can still be passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput> but that is
|
|
not necessary and it has no effect so it is not done in this documentation.
|
|
</para>
|
|
</footnote> That is necessary otherwise the private and protected virtual
|
|
functions cannot be overridden by public functions in derived classes that
|
|
specify contracts (because the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase></computeroutput>
|
|
parameter has to be part of signatures for public function overrides). For
|
|
example (see <ulink url="../../example/features/private_protected_virtual.cpp"><literal moreinfo="none">private_protected_virtual.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">counter</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Virtual private and protected functions still declare extra</phrase>
|
|
<phrase role="comment">// `virtual_* = 0` parameter (otherwise they cannot be overridden), but...</phrase>
|
|
<phrase role="keyword">protected</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// ...no `v`.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special"><=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">n_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">n</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">dec</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_get</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">());</phrase> <phrase role="comment">// ...no `v`.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase> <phrase role="comment">// ...no `v`.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">min</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_get</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">n_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
However, public functions in derived classes overriding private or protected
|
|
virtual functions from base classes shall not specify the extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
template parameter to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
because the overridden functions are private or protected and, not being
|
|
public, they do not participate to subcontracting (this library will generate
|
|
a compile-time error if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput> is specified because there will be no
|
|
virtual <emphasis>public</emphasis> function to override from the base class).
|
|
For example (see <ulink url="../../example/features/private_protected_virtual.cpp"><literal moreinfo="none">private_protected_virtual.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">counter10</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">counter</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="comment">// Overriding from non-public members so no subcontracting, no override_...</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">%</phrase> <phrase role="number">10</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">counter</phrase><phrase role="special">::</phrase><phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">dec</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_get</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">get</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="number">10</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">min</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_get</phrase> <phrase role="special">-</phrase> <phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Furthermore, using multiple inheritance it is possible to override functions
|
|
that are private or protected from one base but public from another base.
|
|
In this case, public function overrides in derived classes will specify the
|
|
extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
template parameter to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(because the overridden functions are private or protected in one base and
|
|
those do not participate to subcontracting, but public in another base and
|
|
these participate to subcontracting instead). For example (see <ulink url="../../example/features/private_protected_virtual_multi.cpp"><literal moreinfo="none">private_protected_virtual_multi.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">countable</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">dec</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">get</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">counter10</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">countable</phrase><phrase role="special">,</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">counter</phrase> <phrase role="comment">// Multiple inheritance.</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="comment">// Overriding from public members from `countable` so use `override_...`.</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_set</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">counter10</phrase><phrase role="special">::</phrase><phrase role="identifier">set</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">%</phrase> <phrase role="number">10</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">counter</phrase><phrase role="special">::</phrase><phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">dec</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_get</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">get</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_dec</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">counter10</phrase><phrase role="special">::</phrase><phrase role="identifier">dec</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="number">10</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">min</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_get</phrase> <phrase role="special">-</phrase> <phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">set</phrase><phrase role="special">(</phrase><phrase role="identifier">get</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">set</phrase><phrase role="special">,</phrase> <phrase role="identifier">dec</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<warning>
|
|
<para>
|
|
Unfortunately, the code above does not compile on MSVC (at least up to
|
|
Visual Studio 2015) because MSVC incorrectly gives a compile-time error
|
|
when SFINAE fails due to private or protected access levels. Instead, GCC
|
|
and Clang correctly implement SFINAE failures due to private and protected
|
|
functions so the code above correctly complies on GCC and Clang. Therefore,
|
|
currently it is not possible to override a function that is public in one
|
|
base but private or protected in other base using this library on MSVC
|
|
(at least up to Visual Studio 2015), but that can correctly be done on
|
|
GCC or Clang instead.
|
|
</para>
|
|
</warning>
|
|
</section>
|
|
<section id="boost_contract.advanced.friend_functions">
|
|
<title><link linkend="boost_contract.advanced.friend_functions">Friend Functions</link></title>
|
|
<para>
|
|
In general, friend functions are not member functions so <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
is used to program their contracts and all considerations made in <link linkend="boost_contract.tutorial.non_member_functions">Non-Member
|
|
Functions</link> apply. For example (see <ulink url="../../example/features/friend.cpp"><literal moreinfo="none">friend.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">buffer</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">byte</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">buffer</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">byte</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="identifier">value_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">buffer</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Friend functions are not member functions...</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">buffer</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">byte</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// ...so check contracts via `function` (which won't check invariants).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase><phrase role="special">*</phrase> <phrase role="identifier">x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">values_</phrase><phrase role="special">.</phrase><phrase role="identifier">c_str</phrase><phrase role="special">();</phrase> <phrase role="special">*</phrase><phrase role="identifier">x</phrase> <phrase role="special">!=</phrase> <phrase role="char">'\0'</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(*</phrase><phrase role="identifier">x</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">value_</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">values_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
However, in some cases a friend function might take an object as parameter
|
|
and it can be logically considered an extension of that object's public interface
|
|
(essentially at the same level as the object's public functions). In these
|
|
cases, programmers might chose to program the friend function contracts using
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(instead of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>)
|
|
so to also check the class invariants of the object passed as parameter (and
|
|
not just pre- and postconditions). For example (see <ulink url="../../example/features/friend_invariant.cpp"><literal moreinfo="none">friend_invariant.cpp</literal></ulink>):
|
|
<footnote id="boost_contract.advanced.friend_functions.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Contract programming proposals
|
|
for C++ like <link linkend="N1962_anchor">[N1962]</link> do not provide
|
|
a mechanism for friend functions to check class invariants of objects passed
|
|
as parameters. In other words, these proposals do not enable contracts
|
|
to recognize that in C++ some friend functions logically act as if they
|
|
were part of the public interface of the objects they take as parameters.
|
|
This is reasonable for proposals that add contracts to the core language
|
|
because friend functions are not always meant to extend an object public
|
|
interface and C++ does not provide a mechanism to programmatically specify
|
|
when they do and when they do not. However, this library provides the flexibility
|
|
to let programmers manually specify when friend functions should also check
|
|
class invariants of the objects they take as parameters (using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>)
|
|
and when they should not (using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
instead).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">positive</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Can be considered an extension of enclosing class' public interface...</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">positive</phrase><phrase role="special">&</phrase> <phrase role="identifier">object</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_object_value</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">object</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_value</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="comment">// ...so it can be made to check invariants via `public_function`.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(&</phrase><phrase role="identifier">object</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">object</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_object_value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">T</phrase> <phrase role="identifier">saved</phrase> <phrase role="special">=</phrase> <phrase role="identifier">object</phrase><phrase role="special">.</phrase><phrase role="identifier">value_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">object</phrase><phrase role="special">.</phrase><phrase role="identifier">value_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">value</phrase> <phrase role="special">=</phrase> <phrase role="identifier">saved</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="identifier">value_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
This technique can also be extended to friend functions that take multiple
|
|
objects as parameters and can be logically considered extensions to the public
|
|
interfaces of each of these objects. For example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Can be considered an extension of multiple objects' public interfaces.</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">class1</phrase><phrase role="special">&</phrase> <phrase role="identifier">object1</phrase><phrase role="special">,</phrase> <phrase role="identifier">class2</phrase><phrase role="special">*</phrase> <phrase role="identifier">object2</phrase><phrase role="special">,</phrase> <phrase role="identifier">type3</phrase><phrase role="special">&</phrase> <phrase role="identifier">value3</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check preconditions.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">pre</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">object2</phrase> <phrase role="special">!=</phrase> <phrase role="keyword">nullptr</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="comment">// Check class invariants for each object (programmers chose the order).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">inv1</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(&</phrase><phrase role="identifier">object1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">inv2</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">object2</phrase><phrase role="special">);</phrase>
|
|
<phrase role="comment">// Check postconditions and exception guarantees.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">postex</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(...)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
Changing the order of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
declarations above, programmers can chose the order for checking class invariants
|
|
among the different objects passed to the friend function and also whether
|
|
to check these invariants before or after preconditions, postconditions,
|
|
and exception guarantees of the friend function (see <link linkend="boost_contract.tutorial.non_member_functions">Non-Member
|
|
Functions</link> and <link linkend="boost_contract.tutorial.public_functions">Public
|
|
Functions</link> for information on how the RAII objects returned by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
check contract conditions). The example above is programmed to check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">class1</phrase></computeroutput> invariants before <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">class2</phrase></computeroutput> invariants (but that order could
|
|
have been inverted if programmers so chose).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
In the example above, preconditions are intentionally programmed to be
|
|
checked before class invariants so the objects passed to the friend function
|
|
can be validated by the preconditions before they are passed as pointers
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(e.g., check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">object2</phrase></computeroutput> is
|
|
not null). (Within member functions instead, the object pointer <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> is always well-formed, its validation
|
|
is never needed, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
checks class invariants before checking preconditions so programming preconditions
|
|
can be simplified assuming the class invariants are satisfied already,
|
|
see <link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link>.)
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.advanced.function_overloads">
|
|
<title><link linkend="boost_contract.advanced.function_overloads">Function
|
|
Overloads</link></title>
|
|
<para>
|
|
No special attention is required when using this library with overloaded
|
|
functions or constructors. The only exception is for the function pointer
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
from public function overrides (see <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>). When the name of public function override are
|
|
also overloaded, the related function pointer cannot be automatically deduced
|
|
by the compiler so programmers have to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static_cast</phrase></computeroutput>
|
|
to resolve ambiguities (as usual with pointers to overloaded functions in
|
|
C++). <footnote id="boost_contract.advanced.function_overloads.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> In order to avoid copies, this
|
|
library takes all function arguments and the return value passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
as references when used within public function overrides. Therefore, the
|
|
library cannot differentiate when the actual function argument and return
|
|
types are passed by reference and when they are not. As a result, the library
|
|
cannot automatically reconstruct the type of the enclosing public function
|
|
so this type must be deduced from the function pointer passed by programmers
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>.
|
|
When this automatic deduction is not possible due to overloaded function
|
|
names, programmers must explicitly use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static_cast</phrase></computeroutput>
|
|
to resolve ambiguities as usual in C++ with pointers to overloaded functions.
|
|
</para>
|
|
</footnote> For example, note how <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static_cast</phrase></computeroutput>
|
|
is used in the following calls to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
(see <ulink url="../../example/features/overload.cpp"><literal moreinfo="none">overload.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">string_lines</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">lines</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">str</phrase><phrase role="special">)</phrase> <phrase role="comment">// Invoked only once for all `str` overloads.</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">str</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_str</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase>
|
|
<phrase role="comment">// `static_cast` resolves overloaded function pointer ambiguities.</phrase>
|
|
<phrase role="keyword">static_cast</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="special">(</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::*)(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*)</phrase> <phrase role="keyword">const</phrase><phrase role="special">>(&</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::</phrase><phrase role="identifier">str</phrase><phrase role="special">),</phrase>
|
|
<phrase role="keyword">this</phrase>
|
|
<phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">str_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Overload on (absence of) `const` qualifier.</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">&</phrase> <phrase role="identifier">str</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_str</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">str_</phrase><phrase role="special">,</phrase>
|
|
<phrase role="comment">// `static_cast` resolves overloaded function pointer ambiguities.</phrase>
|
|
<phrase role="keyword">static_cast</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">&</phrase> <phrase role="special">(</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::*)(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*)>(&</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::</phrase><phrase role="identifier">str</phrase><phrase role="special">),</phrase>
|
|
<phrase role="keyword">this</phrase>
|
|
<phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">str_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">put</phrase><phrase role="special">)</phrase> <phrase role="comment">// Invoked only once for all `put` overloads.</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase> <phrase role="identifier">old_str</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">str</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_put</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase>
|
|
<phrase role="comment">// `static_cast` resolves overloaded function pointer ambiguities.</phrase>
|
|
<phrase role="keyword">static_cast</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">(</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::*)(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*)>(&</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::</phrase><phrase role="identifier">put</phrase><phrase role="special">),</phrase>
|
|
<phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_str</phrase> <phrase role="special">+</phrase> <phrase role="identifier">x</phrase> <phrase role="special">+</phrase> <phrase role="char">'\n'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">str_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">str_</phrase> <phrase role="special">+</phrase> <phrase role="identifier">x</phrase> <phrase role="special">+</phrase> <phrase role="char">'\n'</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Overload on argument type.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase> <phrase role="identifier">old_str</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">str</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_put</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase>
|
|
<phrase role="comment">// `static_cast` resolves overloaded function pointer ambiguities.</phrase>
|
|
<phrase role="keyword">static_cast</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">(</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::*)(</phrase><phrase role="keyword">char</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*)>(&</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::</phrase><phrase role="identifier">put</phrase><phrase role="special">),</phrase>
|
|
<phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_str</phrase> <phrase role="special">+</phrase> <phrase role="identifier">x</phrase> <phrase role="special">+</phrase> <phrase role="char">'\n'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">str_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">str_</phrase> <phrase role="special">+</phrase> <phrase role="identifier">x</phrase> <phrase role="special">+</phrase> <phrase role="char">'\n'</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Overload on argument type and arity (also with default parameter).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">tab</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase> <phrase role="identifier">old_str</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">str</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_put</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase>
|
|
<phrase role="comment">// `static_cast` resolves overloaded function pointer ambiguities.</phrase>
|
|
<phrase role="keyword">static_cast</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">(</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::*)(</phrase><phrase role="keyword">int</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*)>(&</phrase><phrase role="identifier">string_lines</phrase><phrase role="special">::</phrase><phrase role="identifier">put</phrase><phrase role="special">),</phrase>
|
|
<phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">tab</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">s</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">s</phrase> <phrase role="special"><<</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_str</phrase> <phrase role="special">+</phrase> <phrase role="special">(</phrase><phrase role="identifier">tab</phrase> <phrase role="special">?</phrase> <phrase role="string">"\t"</phrase> <phrase role="special">:</phrase> <phrase role="string">""</phrase><phrase role="special">)</phrase> <phrase role="special">+</phrase> <phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="char">'\n'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">ostringstream</phrase> <phrase role="identifier">s</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">s</phrase> <phrase role="special"><<</phrase> <phrase role="identifier">str_</phrase> <phrase role="special"><<</phrase> <phrase role="special">(</phrase><phrase role="identifier">tab</phrase> <phrase role="special">?</phrase> <phrase role="string">"\t"</phrase> <phrase role="special">:</phrase> <phrase role="string">""</phrase><phrase role="special">)</phrase> <phrase role="special"><<</phrase> <phrase role="identifier">x</phrase> <phrase role="special"><<</phrase> <phrase role="char">'\n'</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">str_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">str</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">str_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Overloaded functions have the same function name so the same <literal moreinfo="none">override_<emphasis>function-name</emphasis></literal>
|
|
type can be reused as template parameter for all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
calls in a given class. Therefore, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
only needs to be invoked once for a function name in a given class, even
|
|
when that function name is overloaded.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__">
|
|
<title><link linkend="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__">Lambdas,
|
|
Loops, Code Blocks (and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>)</link></title>
|
|
<para>
|
|
While contracts are usually most useful to program specifications of functions
|
|
and class interfaces, this library also allows to check contract conditions
|
|
for implementation code (lambda functions, loops, code blocks, etc.).
|
|
</para>
|
|
<para>
|
|
Lambda functions are not member functions, they are not part of class public
|
|
interfaces so they do not check class invariants and they do not subcontract.
|
|
They can use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
to specify preconditions, postconditions, and exception guarantees (considerations
|
|
made in <link linkend="boost_contract.tutorial.non_member_functions">Non-Member
|
|
Functions</link> apply). For example (see <ulink url="../../example/features/lambda.cpp"><literal moreinfo="none">lambda.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">int</phrase> <phrase role="identifier">total</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">for_each</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">cbegin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">(),</phrase>
|
|
<phrase role="comment">// Contract for a lambda function.</phrase>
|
|
<phrase role="special">[&</phrase><phrase role="identifier">total</phrase><phrase role="special">]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_total</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">total</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">total</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">total</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_total</phrase> <phrase role="special">+</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">total</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase> <phrase role="comment">// Lambda function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">);</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Similarly, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
can be used to program preconditions, postconditions, and exception guarantees
|
|
for loops. For example, for a for-loop but same for while- and all other
|
|
loops (see <ulink url="../../example/features/loop.cpp"><literal moreinfo="none">loop.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">int</phrase> <phrase role="identifier">total</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="comment">// Contract for a for-loop (same for while- and all other loops).</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase> <phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_total</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">total</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">total</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="special">*</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">total</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_total</phrase> <phrase role="special">+</phrase> <phrase role="special">*</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">total</phrase> <phrase role="special">+=</phrase> <phrase role="special">*</phrase><phrase role="identifier">i</phrase><phrase role="special">;</phrase> <phrase role="comment">// For-loop body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
More in general, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>
|
|
can be used to program preconditions, postconditions, and exception guarantees
|
|
of any block of code in a given function. For example (see <ulink url="../../example/features/code_block.cpp"><literal moreinfo="none">code_block.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">/* ... */</phrase>
|
|
|
|
<phrase role="comment">// Contract for a code block.</phrase>
|
|
<phrase role="special">{</phrase> <phrase role="comment">// Code block entry (check preconditions).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_total</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">total</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">total</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_total</phrase> <phrase role="special">+</phrase> <phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">0</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">1</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">2</phrase><phrase role="special">]);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">total</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">0</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">1</phrase><phrase role="special">]</phrase> <phrase role="special">+</phrase> <phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">2</phrase><phrase role="special">];</phrase> <phrase role="comment">// Code block body.</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="comment">// Code block exit (check postconditions and exceptions guarantees).</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The library does not support contracts for functions and classes declared
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>. <footnote id="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> In general, it might be useful
|
|
to specify contracts for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>
|
|
functions and literal classes. However, the current implementation of this
|
|
library cannot support contracts for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>
|
|
functions and classes because C++ does not currently allow <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput> functions to do the following:
|
|
Declare local variables of (literal) types with non-trivial <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput> destructors (this RAII technique
|
|
is used by this library to check invariants, postconditions, and exceptions
|
|
guarantees at exit); Call other <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>
|
|
functions using try-catch statements (used by this library to report contract
|
|
assertion failures and catch any other exception that might be thrown when
|
|
evaluating the asserted conditions); Use lambda functions (used by this
|
|
library for convenience to program functors that that check preconditions,
|
|
postconditions, and exception guarantees). Also note that even if supported,
|
|
contracts for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput> functions
|
|
probably would not use old values (because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>
|
|
prevents functions from having any side effect visible to the caller and
|
|
variables recording such side-effects are usually the candidates for old
|
|
value copies) and subcontracting (because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">constexpr</phrase></computeroutput>
|
|
functions cannot be virtual).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.advanced.implementation_checks">
|
|
<title><link linkend="boost_contract.advanced.implementation_checks">Implementation
|
|
Checks</link></title>
|
|
<para>
|
|
This library provides also a mechanism to check assertions within implementation
|
|
code (differently from preconditions, postconditions, exceptions guarantees,
|
|
and class invariants that are instead checked before or after code that implements
|
|
a function body). These <emphasis>implementation checks</emphasis> are programmed
|
|
using a nullary functor that is directly assigned to a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
object declaration right at the place within the code where the checks need
|
|
to be performed (without calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>,
|
|
etc. in this case). For example (see <ulink url="../../example/features/check.cpp"><literal moreinfo="none">check.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Implementation checks (via nullary functor).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="special">[]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">gcd</phrase><phrase role="special">(</phrase><phrase role="number">12</phrase><phrase role="special">,</phrase> <phrase role="number">28</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">4</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">gcd</phrase><phrase role="special">(</phrase><phrase role="number">4</phrase><phrase role="special">,</phrase> <phrase role="number">14</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The implementation check functor should capture all the variables that it
|
|
needs for its assertions. These variables can be captured by value when the
|
|
overhead of copying such variables is acceptable. In any case, programmers
|
|
should not write implementation checks that modify the value of the captured
|
|
variables, even when those are captured by reference (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
</para>
|
|
<para>
|
|
Any code can be programmed in the implementation check functor, but it is
|
|
recommended to keep this code simple using mainly assertions and if-statements
|
|
(to avoid programming complex checks that might be buggy and also slow to
|
|
check at run-time). It is also recommended to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
to program the assertions because that enables this library to print informative
|
|
error messages when the asserted conditions are evaluated to be false (note
|
|
that this is not a variadic macro, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` contains commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
This library will automatically call the failure handler <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check_failure">boost::contract::check_failure</link></computeroutput>
|
|
if any of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
conditions are false or, more in general, if calling the implementation check
|
|
functor throws any exception. By default, this failure handler prints an
|
|
error message to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> and terminates the program calling
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput> (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to change the failure handler to throw exceptions, exit
|
|
the program with an error code, etc.).
|
|
</para>
|
|
<para>
|
|
Similarly to the C-style <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>
|
|
macro that is disabled when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">NDEBUG</phrase></computeroutput>
|
|
is defined, implementation checks are disabled when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>
|
|
is defined (see <link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link>). That will skip all implementation checks at run-time
|
|
but it will not eliminate some of the overhead of executing and compiling
|
|
the related <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
declarations. Alternatively, this library provides the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput>
|
|
macro that allows to completely remove run- and compile-time overheads of
|
|
implementation checks when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>
|
|
is defined (note that this is not a variadic macro):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Or, if `boolean-condition` contains commas `,` not already within parenthesis `()`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">((</phrase><literal moreinfo="none"><emphasis>boolean-condition</emphasis></literal><phrase role="special">))</phrase> <phrase role="comment">// ...use extra parenthesis (not a variadic macro).</phrase>
|
|
</programlisting>
|
|
<para>
|
|
For example (see <ulink url="../../example/features/check_macro.cpp"><literal moreinfo="none">check_macro.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Implementation checks (via macro, disable run-/compile-time overhead).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><phrase role="identifier">gcd</phrase><phrase role="special">(</phrase><phrase role="number">12</phrase><phrase role="special">,</phrase> <phrase role="number">28</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">4</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><phrase role="identifier">gcd</phrase><phrase role="special">(</phrase><phrase role="number">4</phrase><phrase role="special">,</phrase> <phrase role="number">14</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput>
|
|
macro is similar to the C-style assert macro as it accepts a boolean condition
|
|
(instead of a nullary functor like <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
does). <footnote id="boost_contract.advanced.implementation_checks.f0">
|
|
<para>
|
|
Of course, nothing prevents programmers from calling functors within <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput> to specify
|
|
boolean conditions when if-guards and other statements are required to
|
|
assert the implementation checks. For example, programmers can use C++11
|
|
lambda functions to define and call such functors in place where the implementation
|
|
checks are specified:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">([&]</phrase> <phrase role="special">-></phrase> <phrase role="keyword">bool</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">even_numbers</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">gcd</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">2</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">gcd</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="special">());</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</footnote> Using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput>
|
|
is essentially equivalent to using the C-style <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>
|
|
macro a part from the following:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Implementation checks are disabled defining <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>
|
|
(instead of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">NDEBUG</phrase></computeroutput> for
|
|
disabling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
If the asserted boolean condition is either false or it throws an exception
|
|
then this library will call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check_failure">boost::contract::check_failure</link></computeroutput>
|
|
(instead <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> calls
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">abort</phrase></computeroutput> if the asserted condition is
|
|
false and it unwinds the stack if evaluating the condition throws an
|
|
exception).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementation checks are automatically disabled when other contract
|
|
conditions specified using this library are already being checked (to
|
|
avoid infinite recursion, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039677008">BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION</link></computeroutput>).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
</section>
|
|
<section id="boost_contract.advanced.old_values_copied_at_body">
|
|
<title><link linkend="boost_contract.advanced.old_values_copied_at_body">Old
|
|
Values Copied at Body</link></title>
|
|
<para>
|
|
In the examples seen so far, old value variables of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
are initialized to a copy of the expression passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
as soon as they are declared. That correctly happens before the function
|
|
body is executed but also before the contract is declared, therefore even
|
|
before class invariants (for public functions) and preconditions are checked
|
|
at function entry. This might work well in most practical cases however,
|
|
technically speaking, old values should be copied before executing the function
|
|
body but <emphasis>after</emphasis> checking class invariants and preconditions
|
|
at function entry (see <link linkend="boost_contract.contract_programming_overview.assertions">Assertions</link>).
|
|
Specifically, there could be cases in which it makes sense to evaluate the
|
|
expressions passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
only under the assumption that assertions programmed in class invariants
|
|
and preconditions are true.
|
|
</para>
|
|
<para>
|
|
This library allows to construct <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
variables using their default constructor (equivalent to a null pointer)
|
|
and then to later assign them to a copy of the expression specified by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput> in a nullary
|
|
functor <literal moreinfo="none"><emphasis>d</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>
|
|
passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>d</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput>. The functor <literal moreinfo="none"><emphasis>d</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput> is called by this library before the function
|
|
body is executed but only after class invariants and preconditions are checked.
|
|
Old value assignments via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
must appear after preconditions but before postconditions and exception guarantees
|
|
wen these are all present (see <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>,
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>,
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>). <footnote id="boost_contract.advanced.old_values_copied_at_body.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Functors for preconditions,
|
|
old value assignments, postconditions, and exception guarantees are all
|
|
optional but when specified, they must be specified in that order. Such
|
|
order is enforced by the fact that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.specify__idm45028039176672">boost::contract::specify_precondition_old_postcondition_except</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.specify__idm45028039243488">boost::contract::specify_old_postcondition_except</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.specify__idm45028039205424">boost::contract::specify_postcondition_except</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.specify_except">boost::contract::specify_except</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.specify_nothing">boost::contract::specify_nothing</link></computeroutput>
|
|
provide a progressively smaller subset of their <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(...)</phrase></computeroutput> member functions. The enforced order
|
|
for specifying preconditions, old value assignments, postconditions, and
|
|
exception guarantees makes logical sense because it follows the order at
|
|
which these are executed at run-time. Other contract programming frameworks
|
|
allow to mix this order, that could have been implemented for this library
|
|
as well but it would have complicated somewhat the library implementation
|
|
while adding no real value (arguably creating confusion in user code by
|
|
not enforcing a consistent order for specifying contract conditions).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
For example, the following old value expression <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">s</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">]</phrase></computeroutput> passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
is valid only after the precondition has checked that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">index</phrase></computeroutput>
|
|
is within the valid range <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">index</phrase>
|
|
<phrase role="special"><</phrase> <phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase></computeroutput>.
|
|
Therefore, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">old_char</phrase></computeroutput> is first
|
|
declared using its default constructor (i.e., initialized to a null pointer)
|
|
and later assigned to a copy of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">s</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">]</phrase></computeroutput> in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
after the precondition has checked <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">index</phrase></computeroutput>
|
|
(see <ulink url="../../example/features/old.cpp"><literal moreinfo="none">old.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">char</phrase> <phrase role="identifier">replace</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">&</phrase> <phrase role="identifier">s</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">index</phrase><phrase role="special">,</phrase> <phrase role="keyword">char</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">></phrase> <phrase role="identifier">old_char</phrase><phrase role="special">;</phrase> <phrase role="comment">// Null, old value copied later...</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// ...after preconditions (and invariants) checked.</phrase>
|
|
<phrase role="identifier">old_char</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">]);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">]</phrase> <phrase role="special">==</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_char</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">s</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="identifier">s</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput> should capture all the variables that
|
|
it needs to evaluate the old value expressions passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>.
|
|
In general, these variables should be captured by reference and not by value
|
|
(because old values need to copy the values the captured variables will have
|
|
just before executing the function body, and not the values these variables
|
|
had when the functor passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
was first declared). In any case, programmers should write the functor passed
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput> so that it modifies only old values
|
|
and not the values of other captured variables, even when those are captured
|
|
by reference (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
</para>
|
|
<para>
|
|
This library will automatically call the failure handler <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput>
|
|
if calling the functor specified via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
throws an exception (by default, this handler prints an error message to
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> and terminates the program calling
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>, but see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link> to throw exceptions, exit the program with an error code,
|
|
etc.).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
If old value pointers are initialized at the point of their construction
|
|
instead of using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput> then an exception thrown by the old
|
|
value expression passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>,
|
|
or more in general any exception thrown by the old value pointer initialization,
|
|
will result in that exception being thrown up the stack by the enclosing
|
|
function. This is arguably less correct than calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput>
|
|
because an exception thrown by an old value copy causes the program to
|
|
fail to check its postconditions and exception guarantees but should not
|
|
automatically causes the enclosing function to thrown an exception (this
|
|
might not be a significant difference in practice, but it could be an additional
|
|
reason to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput> instead of assigning old values when
|
|
they are declared before the contract). <footnote id="boost_contract.advanced.old_values_copied_at_body.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> It would be possible for
|
|
this library to internally wrap all old value operations (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput> copy
|
|
constructor, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.make_old_idm45028038966352">boost::contract::make_old</link></computeroutput>,
|
|
etc.) with try-catch statements so to call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput>
|
|
also when old values are copied when they are constructed outside <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput>. However, that will prevent this
|
|
library from knowing the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.from">boost::contract::from</link></computeroutput>
|
|
parameter and that would be problematic (specifically because destructors
|
|
can have postconditions so that parameter is necessary to make sure user-defined
|
|
failure handlers can be programmed to never throw from destructors as
|
|
C++ usually requires).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.advanced.named_overrides">
|
|
<title><link linkend="boost_contract.advanced.named_overrides">Named Overrides</link></title>
|
|
<para>
|
|
As seen in <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
macro has to be used to declare the type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput> that is passed as an explicit template
|
|
parameter to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
for public function overrides. The function names passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
(and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link></computeroutput>)
|
|
should never start with an underscore to avoid generating names containing
|
|
double underscores <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override__</phrase><phrase role="special">...</phrase></computeroutput> (because all symbols containing double
|
|
underscores <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">__</phrase><phrase role="special">...</phrase></computeroutput> are reserved symbols in the C++ standard).
|
|
There is a separate macro <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
that can be used to explicitly specify the name of the type being declared:
|
|
<footnote id="boost_contract.advanced.named_overrides.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> A different macro <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
is used instead of overloading <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
using variadic macros because the override macro cannot be programmed manually
|
|
by users so making it a variadic would prevent to use this library on compilers
|
|
that do not support variadic macros (see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>function-name</emphasis></literal><phrase role="special">)</phrase> <phrase role="comment">// Generate `override_...`.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDE</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>type-name</emphasis></literal><phrase role="special">,</phrase> <literal moreinfo="none"><emphasis>function-name</emphasis></literal><phrase role="special">)</phrase> <phrase role="comment">// Generate `type-name`.</phrase>
|
|
</programlisting>
|
|
<para>
|
|
For example, the following public function override is named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">_1</phrase></computeroutput> so <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">_1</phrase><phrase role="special">)</phrase></computeroutput>
|
|
would declare a type named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override__1</phrase></computeroutput>
|
|
(which is reserved symbol in C++ because it contains a double underscore
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">__</phrase></computeroutput>), thus <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">override1</phrase><phrase role="special">,</phrase> <phrase role="identifier">_1</phrase><phrase role="special">)</phrase></computeroutput>
|
|
is used to name the type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override1</phrase></computeroutput>
|
|
instead (see <ulink url="../../example/features/named_override.cpp"><literal moreinfo="none">named_override.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">positive_unary_pack</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">generic_unary_pack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="comment">// BOOST_CONTRACT_OVERRIDE(_1) would generate reserved name `override__1`.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">override1</phrase><phrase role="special">,</phrase> <phrase role="identifier">_1</phrase><phrase role="special">)</phrase> <phrase role="comment">// Generate `override1`.</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">_1</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Use `override1` generated by BOOST_CONTRACT_NAMED_OVERRIDE above.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">override1</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">static_cast</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">(</phrase><phrase role="identifier">positive_unary_pack</phrase><phrase role="special">::*)(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*)>(&</phrase><phrase role="identifier">positive_unary_pack</phrase><phrase role="special">::</phrase><phrase role="identifier">_1</phrase><phrase role="special">),</phrase>
|
|
<phrase role="keyword">this</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">value</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">value1_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
macro can also be used when the name <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput> generated by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
would clash with other names in user code, to generate names in CamelCase
|
|
or in any other preferred style, and in any other case when programmers need
|
|
or prefer to generate names different from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput>.
|
|
</para>
|
|
<para>
|
|
Note that there is not a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDES</phrase></computeroutput>
|
|
macro so <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
needs to be invoked separately on each function name (there is instead a
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link></computeroutput>
|
|
macro as seen in <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>). <footnote id="boost_contract.advanced.named_overrides.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The syntax for invoking a possible
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDES</phrase></computeroutput>
|
|
macro would need to be something like <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">type_name1</phrase><phrase role="special">,</phrase> <phrase role="identifier">func_name1</phrase><phrase role="special">,</phrase> <phrase role="identifier">type_name2</phrase><phrase role="special">,</phrase> <phrase role="identifier">func_name2</phrase><phrase role="special">,</phrase> <phrase role="special">...)</phrase></computeroutput>.
|
|
The authors found such a syntax less readable than repeating single <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
invocations as in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">type_name1</phrase><phrase role="special">,</phrase> <phrase role="identifier">func_name1</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">type_name2</phrase><phrase role="special">,</phrase> <phrase role="identifier">func_name2</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase></computeroutput> so
|
|
decided not to provide the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NAMED_OVERRIDES</phrase></computeroutput>
|
|
macro.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.advanced.access_specifiers">
|
|
<title><link linkend="boost_contract.advanced.access_specifiers">Access Specifiers</link></title>
|
|
<para>
|
|
As seen thus far, this library requires programmers to decorate their classes
|
|
declaring the following extra members:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput> member functions (used
|
|
to check class invariants, see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput> member
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typedef</phrase></computeroutput> declared via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
(used to implement subcontracting, see <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
member types declared via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link></computeroutput>
|
|
(used to implement subcontracting for overriding functions, see <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>). <footnote id="boost_contract.advanced.access_specifiers.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Note that the internals
|
|
of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
type generated by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
use names reserved by this library so programmers should not actually
|
|
use such a type even when it is declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>.
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
In general, these members must be declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>
|
|
in the user class in order for this library to be able to access them. <footnote id="boost_contract.advanced.access_specifiers.f1">
|
|
<para>
|
|
There is some variability among compiler implementations: The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput> member type needs to be declared
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput> on MSVC, GCC, and Clang;
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase></computeroutput> member functions need
|
|
to be declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput> on MSVC,
|
|
but not on GCC and Clang; The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override_</phrase><phrase role="special">...</phrase></computeroutput> member types do not have to be declared
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput> on any compiler. In
|
|
any case, declaring these extra members all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>
|
|
or all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput> when the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput> class
|
|
is also declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">friend</phrase></computeroutput> always
|
|
works on all compilers.
|
|
</para>
|
|
</footnote> However, programmers might need to more precisely control the
|
|
public members of their classes to prevent incorrect access of encapsulated
|
|
members. All these members can be declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput>
|
|
as long as the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
class is declared as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">friend</phrase></computeroutput>
|
|
of the user class. For example (see <ulink url="../../example/features/access.cpp"><literal moreinfo="none">access.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase> <phrase role="comment">// Private section of the class.</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase> <phrase role="comment">// Friend `access` class so...</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// ...private bases.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// ...private invariants.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">push_back</phrase><phrase role="special">)</phrase> <phrase role="comment">// ...private overrides.</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase> <phrase role="comment">// Public section of the class.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_push_back</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vector</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
This technique is not used in most examples of this documentation only for
|
|
brevity. Programmers are encouraged to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
in real production code freely as they see fit.
|
|
</para>
|
|
<warning>
|
|
<para>
|
|
Not declaring <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
friend of user classes might cause compiler errors on some compilers (e.g.,
|
|
MSVC) because the private members needed to check the contracts will not
|
|
be accessible. On other compilers (e.g., GCC and Clang), the private access
|
|
level will instead fail SFINAE and no compiler error will be reported while
|
|
invariants and subcontracting will be silently skipped at run-time. Therefore,
|
|
programmers should always make sure to either declare invariant functions
|
|
and base types <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typedef</phrase></computeroutput> as
|
|
public members or to declare <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
as friend.
|
|
</para>
|
|
</warning>
|
|
</section>
|
|
<section id="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
<title><link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures (and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput>)</link></title>
|
|
<para>
|
|
If a condition checked using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
is evaluated to be false or, more in general, if any of the specified contract
|
|
code throws an exception (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
simply expands to code that throws a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.assertion_failure">boost::contract::assertion_failure</link></computeroutput>
|
|
exception, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>), this library will call an appropriate <emphasis>contract
|
|
failure handler</emphasis> function as follow:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Preconditions: False <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
assertions and exceptions thrown from within <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Postconditions: False <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
assertions and exceptions thrown from within <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Exceptions guarantees: False <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
assertions and exceptions thrown from within <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Class invariants: False <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
assertions and exceptions thrown from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase><phrase role="special">()</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase></computeroutput> call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput>
|
|
when checked at function entry and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>
|
|
when checked at function exit.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Old values copied at body: Exceptions thrown from old values copied at
|
|
body within <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(...)</phrase></computeroutput> call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementation checks: False <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
assertions and exceptions thrown from implementation checks <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase>
|
|
<phrase role="special">=</phrase> </computeroutput><literal moreinfo="none"><emphasis>nullary-functor</emphasis></literal>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(...)</phrase></computeroutput> call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check_failure">boost::contract::check_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
By default, these contract failure handlers print a message to the standard
|
|
error <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cerr</phrase></computeroutput> and then terminate the program calling
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase></computeroutput>. <footnote id="boost_contract.advanced.throw_on_failures__and__noexcept__.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> In general, when a contract
|
|
fails the only safe thing to do is to terminate program execution (because
|
|
the contract failure indicates a bug in the program, and in general a buggy
|
|
program will be in a state for which no operation can be successfully and
|
|
safely performed, so the program should be stopped as soon as possible).
|
|
Therefore, this library terminates the program by default. However, for
|
|
specific applications, programmers could implement some fail-safe mechanism
|
|
for which some mission-critical operations could always be performed upon
|
|
handling failures so this library allows programmers to override the default
|
|
contract failure handlers to fully customize how to handle contract failures.
|
|
</para>
|
|
</footnote> However, programmers can override the default contract failure
|
|
handlers to perform any custom action on contract failure using the following
|
|
functions respectively:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Preconditions: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_precondition_failure">boost::contract::set_precondition_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Postconditions: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_postcondition_failure">boost::contract::set_postcondition_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Exception guarantees: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_except_failure">boost::contract::set_except_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Class invariants: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_entr_idm45028039331504">boost::contract::set_entry_invariant_failure</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_exit_invariant_failure">boost::contract::set_exit_invariant_failure</link></computeroutput>,
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_invariant_failure">boost::contract::set_invariant_failure</link></computeroutput>
|
|
(to set both entry and exit invariant failure handlers at once for convenience).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Old values copied at body: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_old_failure">boost::contract::set_old_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementation checks: <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.set_check_failure">boost::contract::set_check_failure</link></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
These <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">set_</phrase><phrase role="special">...</phrase><phrase role="identifier">_failure</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>f</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> function calls return a reference to the
|
|
contract failure handler functor <literal moreinfo="none"><emphasis>f</emphasis></literal>
|
|
that they take as input parameter (so they can be concatenated). <footnote id="boost_contract.advanced.throw_on_failures__and__noexcept__.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">set_</phrase><phrase role="special">...</phrase><phrase role="identifier">_failure</phrase></computeroutput>
|
|
functions take a functor as parameter (to accept not just function pointers
|
|
but also lambdas, binds, etc.) and they return this same functor as result
|
|
so they can be concatenated (this interface is a bit different from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">set_terminate</phrase></computeroutput>). The related <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">get_</phrase><phrase role="special">...</phrase><phrase role="identifier">_failure</phrase></computeroutput> functions can be used to query
|
|
the functors currently set as failure handlers (this interface is similar
|
|
to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">get_terminate</phrase></computeroutput>).
|
|
</para>
|
|
</footnote> For example (see <ulink url="../../example/features/throw_on_failure.cpp"><literal moreinfo="none">throw_on_failure.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_precondition_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_postcondition_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_invariant_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_old_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">[]</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">from</phrase> <phrase role="identifier">where</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase> <phrase role="special">==</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">from_destructor</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Shall not throw from C++ destructors.</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">clog</phrase> <phrase role="special"><<</phrase> <phrase role="string">"ignored destructor contract failure"</phrase> <phrase role="special"><<</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="keyword">throw</phrase><phrase role="special">;</phrase> <phrase role="comment">// Re-throw (assertion_failure, user-defined, etc.).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">))));</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_except_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">[]</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Already an active exception so shall not throw another...</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">clog</phrase> <phrase role="special"><<</phrase> <phrase role="string">"ignored exception guarantee failure"</phrase> <phrase role="special"><<</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">endl</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_check_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">[]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// But now CHECK shall not be used in destructor implementations.</phrase>
|
|
<phrase role="keyword">throw</phrase><phrase role="special">;</phrase> <phrase role="comment">// Re-throw (assertion_failure, user-defined, etc.).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">);</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
When programming custom failure handlers that trow exceptions instead of
|
|
terminating the program, programmers should be wary of the following:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
In order to comply with C++ and STL exception safety, destructors should
|
|
never throw (in fact destructors are implicitly declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput> since C++11). This library passes
|
|
a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.from">boost::contract::from</link></computeroutput>
|
|
parameter to the contract failure handlers for preconditions, postconditions,
|
|
class invariants, and old values copied at body (see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput>
|
|
respectively). This <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.from">boost::contract::from</link></computeroutput>
|
|
parameter indicates if the contract failure occurred in a destructor,
|
|
constructor, or function call so programmers can use it to code custom
|
|
contract failure hander functions that never throw from destructors.
|
|
(In the example above, contract failures from destructors are simply
|
|
ignored even if that is probably never a safe thing to do in real production
|
|
code.)
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
C++ stack-unwinding will execute base class destructors even when the
|
|
derived class destructor trows an exception. Therefore, the contracts
|
|
of base class destructors will continue to be checked when contract failure
|
|
handlers are programmed to throw exceptions on contract failures from
|
|
destructors (yet another reason not to throw exceptions from destructors,
|
|
not even because of contract failures).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
The contract failure handler for exception guarantees <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput>
|
|
should never throw (regardless of the value of its <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.from">boost::contract::from</link></computeroutput>
|
|
parameter) because when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput>
|
|
is called there is already an active exception on the stack, the exception
|
|
that triggered the exception guarantees to be checked in the first place
|
|
(throwing an exception while there is already an active exception will
|
|
force program termination or lead to undefined behaviour in C++).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Implementation checks can appear in any code, including destructor implementation
|
|
code, so <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check_failure">boost::contract::check_failure</link></computeroutput>
|
|
should also never throw, or implementation checks should never be used
|
|
in destructors otherwise these destructors will throw (note that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check_failure">boost::contract::check_failure</link></computeroutput>
|
|
does not provide the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.from">boost::contract::from</link></computeroutput>
|
|
parameter so it is not possible to differentiate from implementation
|
|
checks failing from destructors instead than from other parts of the
|
|
code).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<note>
|
|
<para>
|
|
Programmers need to decide how to handle contract failures from destructors
|
|
when they write custom contract failure handlers that throw exceptions
|
|
instead of terminating the program (given that C++ and STL exception safety
|
|
rules requires destructors to never throw). This is not a simple dilemma
|
|
and it might be a good reason to terminate the program instead of throwing
|
|
exceptions when assertions fail in C++ (as this library and also C-style
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">assert</phrase></computeroutput> do by default).
|
|
</para>
|
|
</note>
|
|
<bridgehead renderas="sect4" id="boost_contract.advanced.throw_on_failures__and__noexcept__.h0">
|
|
<phrase id="boost_contract.advanced.throw_on_failures__and__noexcept__.throw_user_defined_exceptions"/><link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__.throw_user_defined_exceptions">Throw
|
|
User-Defined Exceptions</link>
|
|
</bridgehead>
|
|
<para>
|
|
Contract assertions can be programmed to throw <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.assertion_failure">boost::contract::assertion_failure</link></computeroutput>
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>condition</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> as we have seen so far (see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link>). Alternatively, contract assertions can be programmed to throw
|
|
any other exception (including user-defined exceptions) using code similar
|
|
to the following:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase><phrase role="special">(!</phrase><literal moreinfo="none"><emphasis>condition</emphasis></literal><phrase role="special">)</phrase> <phrase role="keyword">throw</phrase> <literal moreinfo="none"><emphasis>exception-object</emphasis></literal><phrase role="special">;</phrase>
|
|
</programlisting>
|
|
<para>
|
|
For example, the following precondition functor throws <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.assertion_failure">boost::contract::assertion_failure</link></computeroutput>
|
|
(via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>)
|
|
on its first assertion and the user-defined exception <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">too_large_error</phrase></computeroutput>
|
|
on its second assertion (both exceptions will cause this library to call
|
|
the customized <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>
|
|
listed above which will in turn re-throw the exceptions up the stack, see
|
|
<ulink url="../../example/features/throw_on_failure.cpp"><literal moreinfo="none">throw_on_failure.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">struct</phrase> <phrase role="identifier">too_large_error</phrase> <phrase role="special">{};</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">MaxSize</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">cstring</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">cstring</phrase><phrase role="special"><</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">MaxSize</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* implicit */</phrase> <phrase role="identifier">cstring</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase><phrase role="special">*</phrase> <phrase role="identifier">chars</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">cstring</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">chars</phrase><phrase role="special">);</phrase> <phrase role="comment">// Throw `assertion_failure`.</phrase>
|
|
<phrase role="comment">// Or, throw user-defined exception.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">strlen</phrase><phrase role="special">(</phrase><phrase role="identifier">chars</phrase><phrase role="special">)</phrase> <phrase role="special">></phrase> <phrase role="identifier">MaxSize</phrase><phrase role="special">)</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">too_large_error</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">{</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.advanced.throw_on_failures__and__noexcept__.h1">
|
|
<phrase id="boost_contract.advanced.throw_on_failures__and__noexcept__.exception_specifiers___code__phrase_role__keyword__noexcept__phrase___code__and__code__phrase_role__keyword__throw__phrase___code___"/><link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__.exception_specifiers___code__phrase_role__keyword__noexcept__phrase___code__and__code__phrase_role__keyword__throw__phrase___code___">Exception
|
|
Specifiers (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">throw</phrase></computeroutput>`)</link>
|
|
</bridgehead>
|
|
<para>
|
|
Exception specifiers <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput>
|
|
(since C++11) and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">throw</phrase></computeroutput> (deprecated
|
|
in C++11) of the enclosing function, constructor, or destructor declaring
|
|
the contract correctly apply to the contract code as well. Therefore, even
|
|
if the contract failure handlers are reprogrammed to throw exceptions in
|
|
case of contract failures, those exceptions will never be thrown outside
|
|
the context of the enclosing operation if that is not in accordance with
|
|
the exception specifiers of that operation (specifically, note that all destructors
|
|
are implicitly declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput>
|
|
in C++11).
|
|
</para>
|
|
<para>
|
|
For example, the following code will correctly never throw from the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">noexcept</phrase></computeroutput> destructor, not even if the class
|
|
invariants checked at destructor entry throw <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">too_large_error</phrase></computeroutput>
|
|
and the contract failure handlers for invariants are programmed to throw
|
|
from destructors (the program will always terminate in this case instead,
|
|
see <ulink url="../../example/features/throw_on_failure.cpp"><literal moreinfo="none">throw_on_failure.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">struct</phrase> <phrase role="identifier">too_large_error</phrase> <phrase role="special">{};</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">MaxSize</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">cstring</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">cstring</phrase><phrase role="special"><</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">MaxSize</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="identifier">MaxSize</phrase><phrase role="special">)</phrase> <phrase role="keyword">throw</phrase> <phrase role="identifier">too_large_error</phrase><phrase role="special">();</phrase> <phrase role="comment">// Throw user-defined ex.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">chars_</phrase><phrase role="special">);</phrase> <phrase role="comment">// Or, throw `assertion_failure`.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">chars_</phrase><phrase role="special">[</phrase><phrase role="identifier">size</phrase><phrase role="special">()]</phrase> <phrase role="special">==</phrase> <phrase role="char">'\0'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">~</phrase><phrase role="identifier">cstring</phrase><phrase role="special">()</phrase> <phrase role="keyword">noexcept</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Exception specifiers apply to contract code.</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">/* ... */</phrase>
|
|
|
|
<phrase role="comment">// Warning... might cause destructors to throw (unless declared noexcept).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">set_invariant_failure</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">[]</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">throw</phrase><phrase role="special">;</phrase> <phrase role="comment">// Throw no matter if from destructor, etc.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">);</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.extras">
|
|
<title><link linkend="boost_contract.extras">Extras</link></title>
|
|
<para>
|
|
This section can be consulted selectively for specific topics of interest.
|
|
</para>
|
|
<section id="boost_contract.extras.old_value_requirements__templates_">
|
|
<title><link linkend="boost_contract.extras.old_value_requirements__templates_">Old
|
|
Value Requirements (Templates)</link></title>
|
|
<para>
|
|
Old values require to copy the expression passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
thus the type of that expression needs to be copyable. More precisely, dereferencing
|
|
an old value pointer of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
requires <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase></computeroutput> to be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput>,
|
|
otherwise this library will generate a compile-time error.
|
|
</para>
|
|
<para>
|
|
In some cases it might be acceptable, or even desirable, to cause a compile-time
|
|
error when a program uses old value types that are not copyable (because
|
|
it is not possible to fully check the correctness of the program as stated
|
|
by the contract assertions that use these old values). In these cases, programmers
|
|
can declare old values using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
as seen so far.
|
|
</para>
|
|
<para>
|
|
However, in some other cases it might be desirable to simply skip assertions
|
|
that use old values when the respective old value types are not copyable,
|
|
without causing compile-time errors. Programmers can do this using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>
|
|
instead of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
and checking if the old value pointer is not null before dereferencing it
|
|
in postconditions. For example, consider the following function template
|
|
that could in general be instantiated for types <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput>
|
|
that are not copy constructible (that is for which <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase></computeroutput> is <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">false</phrase></computeroutput>,
|
|
see <ulink url="../../example/features/old_if_copyable.cpp"><literal moreinfo="none">old_if_copyable.cpp</literal></ulink>):
|
|
<footnote id="boost_contract.extras.old_value_requirements__templates_.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> <link linkend="N1962_anchor">[N1962]</link>
|
|
and other proposals to add contracts to C++ do not provide a mechanism
|
|
to selectively disable copies only for old value types that are not copy
|
|
constructible. However, this library provides such a mechanism to allow
|
|
to program contracts for template code without necessarily adding extra
|
|
copy constructible type requirements that would not be present if it were
|
|
not for copying old values (so compiling the code with and without contracts
|
|
will not necessarily alter the type requirements of the program). Something
|
|
similar could be achieved combing C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> with <link linkend="N1962_anchor">[N1962]</link>
|
|
or <link linkend="P0380_anchor">[P0380]</link> so that old value expressions
|
|
within template code can be guarded by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> statements checking if
|
|
the old value types are copyable or not. For example, assuming old values
|
|
are added to <link linkend="P0380_anchor">[P0380]</link> (using some kind
|
|
of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">oldof</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
syntax) and that C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput> can be used within <link linkend="P0380_anchor">[P0380]</link>
|
|
contracts:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">offset</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">ensures</phrase><phrase role="special">:</phrase> <phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">is_copy_constructible</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">]]</phrase>
|
|
<phrase role="special">...</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="comment">// T might or might not be copyable.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">offset</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// No compiler error if T has no copy constructor...</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// ...but old value null if T has no copy constructor.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_x</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">x</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The old value pointer <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">old_x</phrase></computeroutput>
|
|
is programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>.
|
|
When <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> is copyable, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
behaves like <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>.
|
|
When <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> is not copyable instead,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
will simply not copy <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">x</phrase></computeroutput> at
|
|
run-time and leave <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">old_x</phrase></computeroutput>
|
|
initialized to a null pointer. Therefore, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>
|
|
objects like <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">old_x</phrase></computeroutput> must be
|
|
checked to be not null as in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_x</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase></computeroutput> before
|
|
they are dereferenced in postconditions and exception guarantees (to avoid
|
|
run-time errors of dereferencing null pointers).
|
|
</para>
|
|
<para>
|
|
If the above example used <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
instead then this library would have generated a compile-time error when
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">offset</phrase></computeroutput> is instantiated with
|
|
types <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> that are not copy
|
|
constructible (but only if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">old_x</phrase></computeroutput>
|
|
is actually dereferenced somewhere, for example in the contract assertions
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase><phrase role="identifier">old_x</phrase>
|
|
<phrase role="special">...</phrase></computeroutput> or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">old_x</phrase><phrase role="special">->...</phrase></computeroutput>). <footnote id="boost_contract.extras.old_value_requirements__templates_.f1">
|
|
<para>
|
|
Technically, on C++17 it is possible to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
together with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
instead of using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>,
|
|
for example:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">offset</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">x</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
However, the authors find this code less readable and more verbose than
|
|
its equivalent that uses <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>.
|
|
Guarding old value copies and related assertions with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> is useful instead when
|
|
the guard condition checks type requirements more complex than just <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput>
|
|
(as shown later in this documentation).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
When C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> declarations
|
|
are used with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>,
|
|
this library always defaults to using the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
type (because its type requirements are more stringent than <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>).
|
|
For example, the following statements are equivalent:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="comment">// C++11 auto declarations always use `old_ptr` (never `old_ptr_if_copyable`).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">decltype</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">)></phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
</programlisting>
|
|
<para>
|
|
If programmers want to relax the copyable type requirement, they must do
|
|
so explicitly by using the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>
|
|
type instead of using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput> declarations.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.old_value_requirements__templates_.h0">
|
|
<phrase id="boost_contract.extras.old_value_requirements__templates_.old_value_type_traits"/><link linkend="boost_contract.extras.old_value_requirements__templates_.old_value_type_traits">Old
|
|
Value Type Traits</link>
|
|
</bridgehead>
|
|
<para>
|
|
This library uses <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput>
|
|
to determine if an old value type is copyable or not, and then <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_value_copy">boost::contract::old_value_copy</link></computeroutput>
|
|
to actually copy the old value.
|
|
</para>
|
|
<para>
|
|
By default, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
is equivalent to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">is_copy_constructible</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput> and
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_value_copy">boost::contract::old_value_copy</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
is implemented using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput>'s
|
|
copy constructor. However, these type traits can be specialized by programmers
|
|
for example to avoid making old value copies of types even when they have
|
|
a copy constructor (maybe because these copy constructors are too expensive),
|
|
or to make old value copies for types that do not have a copy constructor,
|
|
or for any other specific need programmers might have for the types in question.
|
|
</para>
|
|
<para>
|
|
For example, the following specialization of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput>
|
|
intentionally avoids making old value copies for all expressions of type
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">w</phrase></computeroutput> even if that type has a
|
|
copy constructor (see <ulink url="../../example/features/old_if_copyable.cpp"><literal moreinfo="none">old_if_copyable.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Copyable type but...</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">w</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">w</phrase><phrase role="special">(</phrase><phrase role="identifier">w</phrase> <phrase role="keyword">const</phrase><phrase role="special">&)</phrase> <phrase role="special">{</phrase> <phrase role="comment">/* Some very expensive copy operation here... */</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// ...never copy old values for type `w` (because its copy is too expensive).</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><></phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">w</phrase><phrase role="special">></phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">false_type</phrase> <phrase role="special">{};</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
On the flip side, the following specializations of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_value_copy">boost::contract::old_value_copy</link></computeroutput>
|
|
make old value copies of expressions of type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">p</phrase></computeroutput>
|
|
even if that type does not actually have a copy constructor (see <ulink url="../../example/features/old_if_copyable.cpp"><literal moreinfo="none">old_if_copyable.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Non-copyable type but...</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">p</phrase> <phrase role="special">:</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">noncopyable</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase><phrase role="special">*</phrase> <phrase role="identifier">num_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">struct</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_value_copy</phrase><phrase role="special"><</phrase><phrase role="identifier">p</phrase><phrase role="special">>;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// ...still copy old values for type `p` (using a deep copy).</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><></phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">old_value_copy</phrase><phrase role="special"><</phrase><phrase role="identifier">p</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">old_value_copy</phrase><phrase role="special">(</phrase><phrase role="identifier">p</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">old</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">*</phrase><phrase role="identifier">old_</phrase><phrase role="special">.</phrase><phrase role="identifier">num_</phrase> <phrase role="special">=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old</phrase><phrase role="special">.</phrase><phrase role="identifier">num_</phrase><phrase role="special">;</phrase> <phrase role="comment">// Deep copy pointed value.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">p</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">old</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">old_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">p</phrase> <phrase role="identifier">old_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><></phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">p</phrase><phrase role="special">></phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">true_type</phrase> <phrase role="special">{};</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
In general, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">is_copy_constructible</phrase></computeroutput> and therefore <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput>
|
|
require C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">decltype</phrase></computeroutput> and SFINAE
|
|
to automatically detect if a given type is copyable or not. On non-C++11
|
|
compilers, it is possible to inherit the old value type from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">noncopyable</phrase></computeroutput>, or use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_MOVABLE_BUT_NOT_COPYABLE</phrase></computeroutput>,
|
|
or specialize <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">is_copy_constructible</phrase></computeroutput> (see <ulink url="http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_typetraits/reference/is_copy_constructible.html"><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">is_copy_constructible</phrase></computeroutput></ulink> documentation
|
|
for more information), or just specialize <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput>.
|
|
For example, for a non-copyable type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">n</phrase></computeroutput>
|
|
the following code will work also on non-C++11 compilers (see <ulink url="../../example/features/old_if_copyable.cpp"><literal moreinfo="none">old_if_copyable.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">n</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Do not want to use boost::noncopyable but...</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">num_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">n</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="keyword">const</phrase><phrase role="special">&);</phrase> <phrase role="comment">// ...unimplemented private copy constructor (so non-copyable).</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Specialize `boost::is_copy_constructible` (no need for this on C++11).</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><></phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">n</phrase><phrase role="special">></phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">false_type</phrase> <phrase role="special">{};</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.assertion_requirements__templates_">
|
|
<title><link linkend="boost_contract.extras.assertion_requirements__templates_">Assertion
|
|
Requirements (Templates)</link></title>
|
|
<para>
|
|
In general, assertions can introduce a new set of requirements on the types
|
|
used by the program. Some of these type requirements might be necessary only
|
|
to check the assertions and they would not be required by the program otherwise.
|
|
</para>
|
|
<para>
|
|
In some cases it might be acceptable, or even desirable, to cause a compile-time
|
|
error when a program uses types that do not provide all the operations needed
|
|
to check contract assertions (because it is not possible to fully check the
|
|
correctness of the program as specified by its contracts). In these cases,
|
|
programmers can specify contract assertions as we have seen so far, compilation
|
|
will fail if user types do not provide all operations necessary to check
|
|
the contracts.
|
|
</para>
|
|
<para>
|
|
However, in some other cases it might be desirable to not augment the type
|
|
requirements of a program just because of contract assertions and to simply
|
|
skip assertions when user types do not provide all the operations necessary
|
|
to check them, without causing compile-time errors. Programmers can do this
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
on C++17 compilers, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
(or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if_c">boost::contract::condition_if_c</link></computeroutput>)
|
|
on non-C++17 compilers.
|
|
</para>
|
|
<para>
|
|
For example, let's consider the following <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
class template equivalent to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>.
|
|
C++ <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput> does
|
|
not require that its value type parameter <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput>
|
|
has an equality operator <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">==</phrase></computeroutput>
|
|
(it only requires <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> to be
|
|
copy constructible, see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase></computeroutput>
|
|
documentation). However, the contracts for the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase></computeroutput>
|
|
public function include a postcondition <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase></computeroutput>
|
|
that introduces the new requirement that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput>
|
|
must also have an equality operator <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">==</phrase></computeroutput>.
|
|
Programmers can specify this postcondition as usual with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">back</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase></computeroutput> an let the program fail to compile when
|
|
users instantiate <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase></computeroutput>
|
|
with a type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> that does not
|
|
provide an equality operator <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">==</phrase></computeroutput>.
|
|
Otherwise, programmers can guard this postcondition using C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
to evaluate the asserted condition only for types <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput>
|
|
that have an equality operator <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">==</phrase></computeroutput>
|
|
and skip it otherwise. <footnote id="boost_contract.extras.assertion_requirements__templates_.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> <link linkend="N1962_anchor">[N1962]</link>
|
|
and other proposals to add contracts to C++ do not provide a mechanism
|
|
to selectively disable assertions based on their type requirements. However,
|
|
this library provides such a mechanism to allow to program contracts for
|
|
template code without necessarily adding extra type requirements that would
|
|
not be present if it was not for the contracts (so compiling the code with
|
|
and without contracts will not alter the type requirements of the program).
|
|
Something similar could be achieved combing C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> with <link linkend="N1962_anchor">[N1962]</link>
|
|
or <link linkend="P0380_anchor">[P0380]</link> so that contract assertions
|
|
within template code could be guarded by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> statements checking the
|
|
related type requirements (<link linkend="N1962_anchor">[N1962]</link>
|
|
already allows of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase></computeroutput> statements
|
|
in contracts under the name of <emphasis>select assertions</emphasis>,
|
|
<link linkend="P0380_anchor">[P0380]</link> does not so probably <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase></computeroutput> statements should be added to <link linkend="P0380_anchor">[P0380]</link>
|
|
as well). For example, assuming C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> can be used within <link linkend="P0380_anchor">[P0380]</link> contracts:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">[[</phrase><phrase role="identifier">ensures</phrase><phrase role="special">:</phrase> <phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">]]</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</footnote> For example:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boot</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Guard with `if constexpr` for T without `==`.</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
<para>
|
|
More in general, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
can be used to guard a mix of both old value copies and contract assertions
|
|
that introduce specific extra type requirements. For example, the following
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">swap</phrase></computeroutput> function can be called
|
|
on any type <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> that is movable
|
|
but its old value copies and postcondition assertions are evaluated only
|
|
for types <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">T</phrase></computeroutput> that are also
|
|
copyable and have an equality operator <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">==</phrase></computeroutput>
|
|
(see <ulink url="../../example/features/if_constexpr.cpp"><literal moreinfo="none">if_constexpr.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="identifier">y</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">constexpr</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">b</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase> <phrase role="special">&&</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_y</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">b</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Contract requires copyable T...</phrase>
|
|
<phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">old_y</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">b</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// ... and T with `==`...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_y</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">T</phrase> <phrase role="identifier">t</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="comment">// ...but body only requires movable T.</phrase>
|
|
<phrase role="identifier">x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">y</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">t</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.assertion_requirements__templates_.h0">
|
|
<phrase id="boost_contract.extras.assertion_requirements__templates_.no__code__phrase_role__keyword__if__phrase___phrase_role__keyword__constexpr__phrase___code___no_c__17_"/><link linkend="boost_contract.extras.assertion_requirements__templates_.no__code__phrase_role__keyword__if__phrase___phrase_role__keyword__constexpr__phrase___code___no_c__17_">No
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
(no C++17)</link>
|
|
</bridgehead>
|
|
<para>
|
|
On non-C++17 compilers where <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput> is not available, it is possible
|
|
to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
to skip assertions based on type requirements (even if this code is less
|
|
readable and more verbose than using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput>). For example (see <ulink url="../../example/features/condition_if.cpp"><literal moreinfo="none">condition_if.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Instead of `ASSERT(back() == value)` for T without `==`.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">back</phrase><phrase role="special">()),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
More in general, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
is used as follow:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">cond</phrase>
|
|
<phrase role="special">)</phrase>
|
|
</programlisting>
|
|
<para>
|
|
Where <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred</phrase></computeroutput> is a nullary boolean
|
|
meta-function and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">cond</phrase></computeroutput> is
|
|
a nullary boolean functor. If <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput>
|
|
is statically evaluated to be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput>
|
|
at compile-time then <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">cond</phrase><phrase role="special">()</phrase></computeroutput> is called at run-time and its boolean result
|
|
is returned by the enclosing <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
call. Otherwise, if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput>
|
|
is statically evaluated to be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">false</phrase></computeroutput>
|
|
at compile-time then <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
trivially returns <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput>. Therefore,
|
|
if <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">cond</phrase></computeroutput> is a functor template
|
|
instantiation (not just a functor) then its call that contains the assertion
|
|
operations with the extra type requirements (e.g., the equality operator
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">==</phrase></computeroutput>) will not be actually instantiated
|
|
and compiled unless the compiler determines at compile-time that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput> is <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput>
|
|
(when used this way, functor templates like <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase></computeroutput>
|
|
and C++14 generic lambdas can be used to program <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">cond</phrase></computeroutput>,
|
|
but C++11 lambdas cannot).
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
function template is a special case of the more general <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput>,
|
|
specifically <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">Pred</phrase><phrase role="special">>(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase></computeroutput> is logically equivalent to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special"><</phrase><phrase role="identifier">Pred</phrase><phrase role="special">>(</phrase><phrase role="identifier">cond</phrase><phrase role="special">).</phrase><phrase role="identifier">else_</phrase><phrase role="special">([]</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase>
|
|
<phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="special">})</phrase></computeroutput>. <footnote id="boost_contract.extras.assertion_requirements__templates_.f1">
|
|
<para>
|
|
The internal implementation of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
is optimized and it does not actually use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput>.
|
|
</para>
|
|
</footnote> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput>
|
|
function template also accepts a number of optional <emphasis>else-if</emphasis>
|
|
statements and one optional <emphasis>else</emphasis> statement:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">call_if</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred1</phrase><phrase role="special">>(</phrase>
|
|
<phrase role="identifier">t1</phrase>
|
|
<phrase role="special">).</phrase><phrase role="keyword">template</phrase> <phrase role="identifier">else_if</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred2</phrase><phrase role="special">>(</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">t2</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Optionally, other `else_if` statements.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">else_</phrase><phrase role="special">(</phrase> <phrase role="comment">// Optional for `void` functors, otherwise required.</phrase>
|
|
<phrase role="identifier">e</phrase>
|
|
<phrase role="special">)</phrase>
|
|
</programlisting>
|
|
<para>
|
|
Where <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred1</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred2</phrase></computeroutput>, ... are nullary boolean meta-functions
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t1</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t2</phrase></computeroutput>,
|
|
..., <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">e</phrase></computeroutput> are nullary functors.
|
|
The return types of the functor calls <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t1</phrase><phrase role="special">()</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t2</phrase><phrase role="special">()</phrase></computeroutput>, ..., <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">e</phrase><phrase role="special">()</phrase></computeroutput> must either be all the same (including
|
|
possibly all <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>) or be of
|
|
types implicitly convertible into one another. At run-time <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput> will
|
|
call the functor <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t1</phrase><phrase role="special">()</phrase></computeroutput>,
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t2</phrase><phrase role="special">()</phrase></computeroutput>,
|
|
..., or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">e</phrase><phrase role="special">()</phrase></computeroutput>
|
|
depending on which meta-function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred1</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred2</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput>, ... is statically evaluated to be
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput> or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">false</phrase></computeroutput>
|
|
at compile-time, and it will return the value returned by the functor being
|
|
called. If <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t1</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">t2</phrase></computeroutput>, ..., <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">e</phrase></computeroutput>
|
|
are functor template instantiations (not just functors) then their code will
|
|
only be compiled if the compiler determines they need to be actually called
|
|
at run-time (so only if the related <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred1</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">Pred2</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase></computeroutput>, ... are respectively evaluated to
|
|
be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput> or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">false</phrase></computeroutput>
|
|
at compile-time). All the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">else_if</phrase><phrase role="special"><...>(...)</phrase></computeroutput> statements are optional.
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">else_</phrase><phrase role="special">(...)</phrase></computeroutput>
|
|
statement is optional if the functor calls return <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>,
|
|
otherwise it is required.
|
|
</para>
|
|
<para>
|
|
In general, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput>
|
|
can be used to program contract assertions that compile and check different
|
|
functor templates depending on related predicates being statically evaluated
|
|
to be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">true</phrase></computeroutput> or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">false</phrase></computeroutput> at compile-time (but in most cases
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
should be sufficient and less verbose to use). The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if_c">boost::contract::condition_if_c</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if_c">boost::contract::call_if_c</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">else_if_c</phrase></computeroutput>
|
|
function templates work similarly to their counterparts without the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_c</phrase></computeroutput> postfix
|
|
seen so far, but they take their predicate template parameters as static
|
|
boolean values instead of nullary boolean meta-functions.
|
|
</para>
|
|
<para>
|
|
On compilers that support C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> there should be no need
|
|
to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput>
|
|
because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
can be used instead (making the code more readable and easier to program).
|
|
<footnote id="boost_contract.extras.assertion_requirements__templates_.f2">
|
|
<para>
|
|
<para>
|
|
A part from its use within contracts, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput>
|
|
can be used together with C++14 generic lambdas to emulate C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
(<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">hana</phrase><phrase role="special">::</phrase><phrase role="identifier">if_</phrase></computeroutput> and probably other approaches can
|
|
also be used together with generic lambdas to emulate C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase></computeroutput>
|
|
on C++14 compilers). For example, the following implementation of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">myadvance</phrase></computeroutput> will compile since C++14
|
|
and it is more concise, easier to read and maintain than the usual implementation
|
|
of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">advance</phrase></computeroutput> that uses tag dispatching (see
|
|
<ulink url="../../example/features/call_if_cxx14.cpp"><literal moreinfo="none">call_if_cxx14.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iter</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Dist</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">myadvance</phrase><phrase role="special">(</phrase><phrase role="identifier">Iter</phrase><phrase role="special">&</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">Dist</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">Iter</phrase><phrase role="special">*</phrase> <phrase role="identifier">p</phrase> <phrase role="special">=</phrase> <phrase role="special">&</phrase><phrase role="identifier">i</phrase><phrase role="special">;</phrase> <phrase role="comment">// So captures change actual pointed iterator value.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">call_if</phrase><phrase role="special"><</phrase><phrase role="identifier">is_random_access_iterator</phrase><phrase role="special"><</phrase><phrase role="identifier">Iter</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([]</phrase> <phrase role="special">(</phrase><phrase role="keyword">auto</phrase> <phrase role="identifier">p</phrase><phrase role="special">,</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// C++14 generic lambda.</phrase>
|
|
<phrase role="special">*</phrase><phrase role="identifier">p</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">n</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">},</phrase> <phrase role="identifier">p</phrase><phrase role="special">,</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">).</phrase><phrase role="keyword">template</phrase> <phrase role="identifier">else_if</phrase><phrase role="special"><</phrase><phrase role="identifier">is_bidirectional_iterator</phrase><phrase role="special"><</phrase><phrase role="identifier">Iter</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([]</phrase> <phrase role="special">(</phrase><phrase role="keyword">auto</phrase> <phrase role="identifier">p</phrase><phrase role="special">,</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">--)</phrase> <phrase role="special">++*</phrase><phrase role="identifier">p</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">++)</phrase> <phrase role="special">--*</phrase><phrase role="identifier">p</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">},</phrase> <phrase role="identifier">p</phrase><phrase role="special">,</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">).</phrase><phrase role="keyword">template</phrase> <phrase role="identifier">else_if</phrase><phrase role="special"><</phrase><phrase role="identifier">is_input_iterator</phrase><phrase role="special"><</phrase><phrase role="identifier">Iter</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([]</phrase> <phrase role="special">(</phrase><phrase role="keyword">auto</phrase> <phrase role="identifier">p</phrase><phrase role="special">,</phrase> <phrase role="keyword">auto</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">--)</phrase> <phrase role="special">++*</phrase><phrase role="identifier">p</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">},</phrase> <phrase role="identifier">p</phrase><phrase role="special">,</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">).</phrase><phrase role="identifier">else_</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">([]</phrase> <phrase role="special">(</phrase><phrase role="keyword">auto</phrase> <phrase role="identifier">false_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">static_assert</phrase><phrase role="special">(</phrase><phrase role="identifier">false_</phrase><phrase role="special">,</phrase> <phrase role="string">"requires at least input iterator"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">},</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">false_type</phrase><phrase role="special">())</phrase> <phrase role="comment">// Use constexpr value.</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Of course, since C++17 the implementation that uses <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput> is even more readable
|
|
and concise:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iter</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">Dist</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">myadvance</phrase><phrase role="special">(</phrase><phrase role="identifier">Iter</phrase><phrase role="special">&</phrase> <phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="identifier">Dist</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">is_random_access_iterator</phrase><phrase role="special"><</phrase><phrase role="identifier">Iter</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">i</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">n</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">is_bidirectional_iterator</phrase><phrase role="special"><</phrase><phrase role="identifier">Iter</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">--)</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">++)</phrase> <phrase role="special">--</phrase><phrase role="identifier">i</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">is_input_iterator</phrase><phrase role="special"><</phrase><phrase role="identifier">Iter</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">--)</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">static_assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">,</phrase> <phrase role="string">"requires at least input iterator"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.volatile_public_functions">
|
|
<title><link linkend="boost_contract.extras.volatile_public_functions">Volatile
|
|
Public Functions</link></title>
|
|
<para>
|
|
This library allows to specify a different set of class invariants to check
|
|
for volatile public functions. These <emphasis>volatile class invariants</emphasis>
|
|
are programmed in a public <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput> function, named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>,
|
|
taking no argument, and returning <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase></computeroutput>
|
|
(see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039722128">BOOST_CONTRACT_INVARIANT_FUNC</link></computeroutput>
|
|
to name the invariant function differently from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">invariant</phrase></computeroutput>
|
|
and <link linkend="boost_contract.advanced.access_specifiers">Access Specifiers</link>
|
|
to not have to declare it <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>).
|
|
Classes that do no have invariants for their volatile public functions, simply
|
|
do not declare the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput> function.
|
|
</para>
|
|
<para>
|
|
In general, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
invariants work the same as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
invariants (see <link linkend="boost_contract.tutorial.class_invariants">Class
|
|
Invariants</link>) with the only difference that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
invariants while non-<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> (i.e.,
|
|
neither <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> nor <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>) and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariants.
|
|
A given class can specify any combination of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariant functions
|
|
(see <link linkend="boost_contract.tutorial.class_invariants">Class Invariants</link>):
|
|
<footnote id="boost_contract.extras.volatile_public_functions.f0">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Constructors and destructors
|
|
check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariants in that
|
|
order because the qualifier that can be applied to more calls is checked
|
|
first (note that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
calls can be made on any object while <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
calls cannot be made on <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>
|
|
objects, in that sense the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> qualifier can be applied
|
|
to more calls than <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> alone
|
|
can). This is consistent with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
class invariants that are checked even before <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariants (the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput> classifier can be applied to even
|
|
more calls than <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>,
|
|
in fact an object is not even needed to make static calls).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Constructors check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
invariants at entry and exit (even if an exception is thrown), plus
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariants in
|
|
that order at exit but only if no exception is thrown.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Destructors check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
invariants at entry and exit (even if an exception is thrown), plus
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariants in
|
|
that order at entry (and at exit but only if an exception is thrown,
|
|
even is destructors should in general never throw in C++).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Both non-<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> public functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
invariants at entry and at exit (even if an exception is thrown).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
public functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
invariants at entry and at exit (even if an exception is thrown).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
These rules ensure that volatile class invariants are correctly checked (see
|
|
<link linkend="boost_contract.contract_programming_overview.constructor_calls">Constructor
|
|
Calls</link>, <link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor
|
|
Calls</link>, and <link linkend="boost_contract.contract_programming_overview.public_function_calls">Public
|
|
Function Calls</link>). For example (see <ulink url="../../example/features/volatile.cpp"><literal moreinfo="none">volatile.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">();</phrase> <phrase role="comment">// Static invariants.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase><phrase role="special">;</phrase> <phrase role="comment">// Volatile invariants.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="comment">// Const invariants.</phrase>
|
|
|
|
<phrase role="identifier">u</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static, volatile, and const invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase><phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">~</phrase><phrase role="identifier">u</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static, volatile, and const invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">nc</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static and const invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">c</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static and const invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">v</phrase><phrase role="special">()</phrase> <phrase role="keyword">volatile</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static and volatile invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">cv</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static and volatile invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">s</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Check static invariants only.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">>();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
This library does not automatically check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariants for non-<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput> functions. However, if the contract
|
|
specifications require it, programmers can explicitly call the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
invariant function from the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
invariant function (preferably in that order to be consistent with the order
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariants are checked
|
|
for constructors and destructors). That way all public functions, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput> or not, will check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
invariants (while only <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
and non-<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> public functions
|
|
will check only <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariants,
|
|
correctly so because the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>
|
|
qualifier shall not be stripped away): <footnote id="boost_contract.extras.volatile_public_functions.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> Note that while all public
|
|
functions can be made to check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariants, it is never
|
|
possible to make volatile public functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
non-volatile invariants. That is because both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput> can always be
|
|
added but never stripped in C++ (a part from forcefully via <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const_cast</phrase></computeroutput>) but <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
is always automatically added by this library in order to enforce contract
|
|
constant-correctness (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
That said, it would be too stringent for this library to also automatically
|
|
add <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput> and require all
|
|
functions to check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput> (not just <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>)
|
|
invariants because only <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>
|
|
members can be accessed from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariants so there could
|
|
be many <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> (but not <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>)
|
|
members that are accessible from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
invariants but not from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput> invariants. To avoid this confusion,
|
|
this library has chosen to draw a clear dichotomy between <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariants so that only
|
|
volatile public functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariants and only non-volatile
|
|
public functions check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
(but not <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>)
|
|
invariants. This is a clear distinction and it should serve most cases.
|
|
If programmers need non-volatile public functions to also check <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
invariants, they can explicitly do so by calling the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase>
|
|
<phrase role="keyword">volatile</phrase></computeroutput> invariant function from
|
|
the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> invariant function
|
|
as shown in this documentation.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> <phrase role="comment">// Volatile invariants.</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">auto</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase><phrase role="special">*</phrase> <phrase role="identifier">cv</phrase> <phrase role="special">=</phrase> <phrase role="keyword">this</phrase><phrase role="special">;</phrase> <phrase role="identifier">cv</phrase><phrase role="special">-></phrase><phrase role="identifier">invariant</phrase><phrase role="special">();</phrase> <phrase role="comment">// Call `const volatile` invariant function above.</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Other non-volatile invariants.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
<para>
|
|
(As usual, private and protected functions do not check any invariant, not
|
|
even when they are <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>,
|
|
see <link linkend="boost_contract.advanced.private_and_protected_functions">Private
|
|
and Protected Functions</link>).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.move_operations">
|
|
<title><link linkend="boost_contract.extras.move_operations">Move Operations</link></title>
|
|
<para>
|
|
As with all public operations of a class, also public move operations should
|
|
maintain class invariants (see <link linkend="Stroustrup13_anchor">[Stroustrup13]</link>,
|
|
p. 520). Specifically, at a minimum C++ requires the following:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
The moved-from object can be copy assigned.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
The moved-from object can be move assigned.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
The moved-from object can be destroyed (if not for any other reason,
|
|
this requires that class invariants are maintained by move operations
|
|
because the destructor of the moved-from object requires class invariants
|
|
to be satisfied at its entry, as always with destructors see <link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor
|
|
Calls</link>).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
Therefore, both the move constructor and the move assignment operator need
|
|
to maintain the class invariants of the moved-from object so their contracts
|
|
can be programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
as usual. For example (see <ulink url="../../example/features/move.cpp"><literal moreinfo="none">move.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">circular_buffer</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">moved</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Do not check (some) invariants for moved-from objects.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="comment">// More invariants here that hold also for moved-from objects (e.g.,</phrase>
|
|
<phrase role="comment">// all assertions necessary to successfully destroy moved-from objects).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Move constructor.</phrase>
|
|
<phrase role="identifier">circular_buffer</phrase><phrase role="special">(</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">&&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">moved</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">moved</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">moved</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special"><</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">>(</phrase><phrase role="identifier">other</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Move assignment.</phrase>
|
|
<phrase role="identifier">circular_buffer</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">&&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Moved-from can be (move) assigned (so no pre `!moved()` here).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">moved</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">moved</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">moved</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">move</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">forward</phrase><phrase role="special"><</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">>(</phrase><phrase role="identifier">other</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">~</phrase><phrase role="identifier">circular_buffer</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Moved-from can always be destroyed (in fact no preconditions).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">moved</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">moved_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">moved_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
This example assumes that it is possible to call the public function <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">moved</phrase><phrase role="special">()</phrase></computeroutput>
|
|
on the moved-from object. <footnote id="boost_contract.extras.move_operations.f0">
|
|
<para>
|
|
In this example, the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">moved</phrase><phrase role="special">()</phrase></computeroutput> function is simple enough that programmers
|
|
could decide to not even call <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
from it for optimization reasons. However, calling <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">moved</phrase><phrase role="special">()</phrase></computeroutput>
|
|
has no negative impact, a part from run-time overhead, because this library
|
|
automatically disables contract checking while checking other contracts
|
|
(so this call will not cause infinite recursion).
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
The default move constructor and move assignment operator generated by
|
|
C++ will not automatically check contracts. Therefore, unless the move
|
|
operations are not public or they have no preconditions, no postconditions,
|
|
and their class has no invariants, programmers should manually define them
|
|
using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>
|
|
instead of relying on their default implementations generated by C++. (Same
|
|
as for all other operations automatically implemented by C++.)
|
|
</para>
|
|
</note>
|
|
<para>
|
|
As always, programmers can decide to not program contracts for a given type.
|
|
Specifically, they might decide to not program contracts for a class that
|
|
needs to be moved in order to avoid the run-time overhead of checking contract
|
|
assertions and to maximize performance (see <link linkend="boost_contract.contract_programming_overview.benefits_and_costs">Benefits
|
|
and Costs</link>).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.unions">
|
|
<title><link linkend="boost_contract.extras.unions">Unions</link></title>
|
|
<para>
|
|
A C++ <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">union</phrase></computeroutput> cannot have virtual
|
|
functions, base classes, and cannot be used as a base class thus subcontracting
|
|
(<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>,
|
|
etc.) do not apply to unions. Also a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">union</phrase></computeroutput>
|
|
cannot inherit from <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
(because it cannot have base classes), instead <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
is used to declare a local object that checks constructor preconditions (at
|
|
the very beginning of the constructor before old value copies and other contracts,
|
|
see declaration of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">pre</phrase></computeroutput> in
|
|
the example below). A part from that, this library is used as usual to program
|
|
contracts for unions. For example (see <ulink url="../../example/features/union.cpp"><literal moreinfo="none">union.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">union</phrase> <phrase role="identifier">positive</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Static class invariants (as usual).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Class invariants (as usual).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">i_</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">d_</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Contracts for constructor, as usual but...</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">positive</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">d_</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// ...unions cannot have bases so constructor preconditions here.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">positive</phrase><phrase role="special">></phrase> <phrase role="identifier">pre</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">});</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_instances</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">y</phrase><phrase role="special">;</phrase> <phrase role="identifier">get</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase><phrase role="special">);</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase> <phrase role="special">==</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="special">}</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_instances</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">i_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">++</phrase><phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Contracts for destructor (as usual).</phrase>
|
|
<phrase role="special">~</phrase><phrase role="identifier">positive</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_instances</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_instances</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">--</phrase><phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Contracts for public function (as usual, but no virtual or override).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">get</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">i_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Contracts for static public function (as usual).</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">positive</phrase><phrase role="special">>();</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">i_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">d_</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.assertion_levels">
|
|
<title><link linkend="boost_contract.extras.assertion_levels">Assertion Levels</link></title>
|
|
<para>
|
|
This library provides three predefined <emphasis>assertion levels</emphasis>
|
|
that can be used to selectively disable assertions depending on their computational
|
|
complexity: <footnote id="boost_contract.extras.assertion_levels.f0">
|
|
<para>
|
|
The assertion levels predefined by this library are similar to the default,
|
|
audit, and axiom levels from <link linkend="P0380_anchor">[P0380]</link>.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
is used to assert conditions that are not computationally expensive,
|
|
at least compared to the cost of executing the function body. These assertions
|
|
are the ones we have seen so far, they are always checked at run-time
|
|
and they cannot be disabled.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>
|
|
is used to assert conditions that are computationally expensive compared
|
|
to the cost of executing the function body. These assertions are not
|
|
checked at run-time unless programmers explicitly define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_AUDITS">BOOST_CONTRACT_AUDITS</link></computeroutput>
|
|
(undefined by default), but the asserted conditions are always compiled
|
|
and therefore validated syntactically (even when they are not actually
|
|
evaluated and checked at run-time).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput>
|
|
is used to assert conditions that are computationally prohibitive, at
|
|
least compared to the cost of executing the function body. These assertions
|
|
are never evaluated or checked at run-time, but the asserted conditions
|
|
are always compiled and therefore validated syntactically (so these assertions
|
|
can serve as formal comments to the code).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
In addition, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_CHECK_AUDIT">BOOST_CONTRACT_CHECK_AUDIT</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_CHECK_AXIOM">BOOST_CONTRACT_CHECK_AXIOM</link></computeroutput>
|
|
are similar to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput>
|
|
but they are used to program audit and axiom levels for implementation checks
|
|
instead of assertions (see <link linkend="boost_contract.advanced.implementation_checks">Implementation
|
|
Checks</link>).
|
|
</para>
|
|
<para>
|
|
For example, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>
|
|
can be used to program computationally expensive assertions (see <ulink url="../../example/features/assertion_level.cpp"><literal moreinfo="none">assertion_level.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">RandomIter</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">RandomIter</phrase> <phrase role="identifier">random_binary_search</phrase><phrase role="special">(</phrase><phrase role="identifier">RandomIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">RandomIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">RandomIter</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase> <phrase role="comment">// Default, not expensive.</phrase>
|
|
<phrase role="comment">// Expensive O(n) assertion (use AXIOM if prohibitive instead).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">is_sorted</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Similarly, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_AUDITS">BOOST_CONTRACT_AUDITS</link></computeroutput>
|
|
can be used to disable expensive old value copies and related assertions
|
|
that use them (see <ulink url="../../example/features/assertion_level.cpp"><literal moreinfo="none">assertion_level.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">vector</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase> <phrase role="identifier">old_me</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_other</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="identifier">old_me</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">old_other</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// Else, skip old value copies...</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// ...and also skip related assertions.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_me</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The condition passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput>
|
|
is compiled but not actually evaluated at run-time so this macro can be used
|
|
to program computationally prohibitive assertions but also assertions that
|
|
cannot actually be programmed in C++ using functions that are declared but
|
|
left undefined. For example, (see <ulink url="../../example/features/assertion_level.cpp"><literal moreinfo="none">assertion_level.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// If valid iterator range (cannot implement in C++ but OK to use in AXIOM).</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">Iter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iter</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase> <phrase role="comment">// Only declared, not actually defined.</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_capacity</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"> <phrase role="comment">/* ... */</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
In addition to these assertion levels that are predefined by this library,
|
|
programmers are free to define their own. For example, the following macro
|
|
could be used to program and selectively disable assertions that have exponential
|
|
computational complexity <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">O</phrase><phrase role="special">(</phrase><phrase role="identifier">e</phrase><phrase role="special">^</phrase><phrase role="identifier">n</phrase><phrase role="special">)</phrase></computeroutput>:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">EXPONENTIALLY_COMPLEX_ASSERTIONS</phrase>
|
|
<phrase role="comment">// Following will compile and also evaluate `cond`.</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">ASSERT_EXP</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#else</phrase>
|
|
<phrase role="comment">// Following will compile but never actually evaluate `cond`.</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">ASSERT_EXP</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">))</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
|
|
<phrase role="identifier">ASSERT_EXP</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>some-exponentially-complex-boolean-condition</emphasis></literal><phrase role="special">);</phrase>
|
|
</programlisting>
|
|
</section>
|
|
<section id="boost_contract.extras.disable_contract_checking">
|
|
<title><link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link></title>
|
|
<para>
|
|
Checking contracts adds run-time overhead and can slow down program execution
|
|
(see <link linkend="boost_contract.contract_programming_overview.benefits_and_costs">Benefits
|
|
and Costs</link>). Therefore, programmers can define any combination of the
|
|
following macros (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">-</phrase><phrase role="identifier">D</phrase></computeroutput>
|
|
option in Clang and GCC, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">/</phrase><phrase role="identifier">D</phrase></computeroutput>
|
|
option in MSVC, etc.) to instruct this library to not check specific groups
|
|
of contract conditions at run-time:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>
|
|
to not check preconditions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>
|
|
to not check postconditions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>
|
|
to not check exception guarantees.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039634896">BOOST_CONTRACT_NO_ENTRY_INVARIANTS</link></computeroutput>
|
|
to not check class invariants at call entry.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>
|
|
to not check class invariants at call exit.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Or, define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>
|
|
to not check class invariants at both call entry and exit. (This is provided
|
|
for convenience, it is equivalent to defining both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039634896">BOOST_CONTRACT_NO_ENTRY_INVARIANTS</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>.)
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>
|
|
to not evaluate implementation checks.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<note>
|
|
<para>
|
|
Old values can be used by both postconditions and exception guarantees
|
|
so it is necessary to define both <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>
|
|
to disable old value copies.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
By default, none of these macros are defined so this library checks all contracts.
|
|
When these macros are defined by the user, the implementation code of this
|
|
library is internally optimized to minimize as much as possible any run-time
|
|
and compile-time overhead associated with checking and compiling contracts
|
|
(see <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable
|
|
Contract Compilation</link> for techniques to completely remove any run-time
|
|
and compile-time overheads associated with contract code).
|
|
</para>
|
|
<para>
|
|
For example, programmers could decide to check all contracts during early
|
|
development builds, but later check only preconditions and maybe entry invariants
|
|
for release builds by defining <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
<title><link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable
|
|
Contract Compilation (Macro Interface)</link></title>
|
|
<para>
|
|
This library provides macros that can be used to completely disable compile-time
|
|
and run-time overhead introduced by contracts but at the cost of manually
|
|
programming <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> statements around contract code:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
This library defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039603040">BOOST_CONTRACT_NO_CONSTRUCTORS</link></computeroutput>
|
|
when contract checking is disabled for constructors.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
This library defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039592176">BOOST_CONTRACT_NO_DESTRUCTORS</link></computeroutput>
|
|
when contract checking is disabled for destructors.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
This library defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039583904">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</link></computeroutput>
|
|
when contract checking is disabled for public functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
This library defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039574704">BOOST_CONTRACT_NO_FUNCTIONS</link></computeroutput>
|
|
when contract checking is disabled for (non-public and non-member) functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
This library defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput>
|
|
when old value copies are disabled.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
This library defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_ALL">BOOST_CONTRACT_NO_ALL</link></computeroutput>
|
|
when all contracts above and also implementation checks (see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>)
|
|
are disabled.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
These macros are not configuration macros and they should not be defined
|
|
directly by programmers (otherwise this library will generate compile-time
|
|
errors). Instead, these macros are automatically defined by this library
|
|
when programmers define <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>
|
|
(or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039634896">BOOST_CONTRACT_NO_ENTRY_INVARIANTS</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>),
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>
|
|
(see <link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link>).
|
|
</para>
|
|
<para>
|
|
Alternatively, this library provides a macro-based interface defined in
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link></computeroutput>
|
|
that can also be used to completely disable compile-time and run-time overheads
|
|
introduced by contracts but without the burden of manually writing the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> statements. For example, the following
|
|
code shows how to use both the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link></computeroutput>
|
|
macro interface and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
statements to completely disable compile-time and run-time overheads for
|
|
non-member function contracts (see <ulink url="../../example/features/ifdef_macro.cpp"><literal moreinfo="none">ifdef_macro.cpp</literal></ulink>
|
|
and <ulink url="../../example/features/ifdef.cpp"><literal moreinfo="none">ifdef.cpp</literal></ulink>):
|
|
</para>
|
|
<informaltable frame="all">
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
Macro Interface
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> Statements
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Use macro interface to completely disable contract code compilation.</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract_macro</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">)(</phrase><phrase role="identifier">old_x</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_FUNCTION</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PRECONDITION</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">++;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Use #ifdef to completely disable contract code compilation.</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">/</phrase><phrase role="identifier">core</phrase><phrase role="special">/</phrase><phrase role="identifier">config</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_ALL</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PRECONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_POSTCONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">++;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<para>
|
|
The same can be done to disable contract code complication for private and
|
|
protected functions. The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038671712">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</link></computeroutput>
|
|
macro is provided to handle non-copyable old value types (similar to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>).
|
|
</para>
|
|
<para>
|
|
For constructors, destructors, and public functions the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link></computeroutput>
|
|
macro interface and the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
statements can be used as follow (see <ulink url="../../example/features/ifdef_macro.cpp"><literal moreinfo="none">ifdef_macro.cpp</literal></ulink>
|
|
and <ulink url="../../example/features/ifdef.cpp"><literal moreinfo="none">ifdef.cpp</literal></ulink>):
|
|
</para>
|
|
<informaltable frame="all">
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
Macro Interface
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> Statements
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">integers</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase>
|
|
<phrase role="comment">// Left in code (almost no overhead).</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">integers</phrase><phrase role="special">>,</phrase>
|
|
<phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="comment">// Followings left in code (almost no overhead).</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_INVARIANT</phrase><phrase role="special">({</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">integers</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">from</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">to</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION</phrase><phrase role="special">(</phrase><phrase role="identifier">integers</phrase><phrase role="special">)([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">to</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">to</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CONSTRUCTOR</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">to</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">from</phrase><phrase role="special">;</phrase> <phrase role="identifier">x</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">to</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">integers</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_DESTRUCTOR</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase> <phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase> <phrase role="comment">// Left in code (almost no overhead).</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">)(</phrase><phrase role="identifier">old_size</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">override_push_back</phrase><phrase role="special">)(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">integers</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PRECONDITION</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">push_back</phrase><phrase role="special">)</phrase> <phrase role="comment">// Left in code (almost no overhead).</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">integers</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PRECONDITIONS</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">integers</phrase><phrase role="special">>,</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="preprocessor">#else</phrase>
|
|
<phrase role="identifier">BASES</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_ALL</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_INVARIANTS</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">integers</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">from</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">to</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PRECONDITIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">integers</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">to</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">to</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_CONSTRUCTORS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_POSTCONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">to</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">from</phrase><phrase role="special">;</phrase> <phrase role="identifier">x</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">to</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">-</phrase> <phrase role="identifier">from</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">integers</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_DESTRUCTORS</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_push_back</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">integers</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PRECONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_POSTCONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_EXCEPTS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">push_back</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<para>
|
|
Static and volatile class invariants can be programmed using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038619104">BOOST_CONTRACT_STATIC_INVARIANT</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038632960">BOOST_CONTRACT_INVARIANT_VOLATILE</link></computeroutput>
|
|
respectively (these macros expand code equivalent to the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</phrase><phrase role="special">()</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_INVARIANT_FUNC</phrase><phrase role="special">()</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase></computeroutput>
|
|
functions).
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link></computeroutput>
|
|
macro interface is usually preferred because more concise and easier to use
|
|
than programming <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
statements by hand. However, C++ macros expand on a single line of code and
|
|
that can make compiler errors less useful when using this macro interface
|
|
plus all contract assertions within a given set of preconditions, postconditions,
|
|
exception guarantees, and class invariants will list the same line number
|
|
in error messages when assertions fail at run-time (but error messages still
|
|
list the assertion code and that should still allow programmers to identify
|
|
the specific assertion that failed). Finally, the macro interface leaves
|
|
a bit of contract decorations in the code but that should add no measurable
|
|
compile-time or run-time overhead (specifically, extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">*</phrase></computeroutput> parameters, calls to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
default constructor which does nothing, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typedef</phrase></computeroutput>s, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.access">boost::contract::access</link></computeroutput>
|
|
friendships are left in user code even when contracts are disabled unless
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> statements are used).
|
|
</para>
|
|
<para>
|
|
Disabling contract as shown in <link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link> leaves the overhead of compiling contract code plus
|
|
some small run-time overhead due to the initialization of old value pointers
|
|
(even if those will be all null and no old value will be actually copied),
|
|
the calls to the contract functions used to initialize <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.check">boost::contract::check</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput>
|
|
(even if those calls will be internally optimized by this library to essentially
|
|
do nothing), etc. For truly performance critical code for which even such
|
|
small run-time overhead might not be acceptable, the macro interface (or
|
|
the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> statements) can be used to completely
|
|
disable compile-time and run-time overheads of contracts. However, for such
|
|
performance critical code even the overhead of checking simple preconditions
|
|
might be too much so it might be best to not program contracts at all.
|
|
</para>
|
|
<para>
|
|
Usually, if the overhead of checking preconditions and other assertions is
|
|
already considered acceptable for an application then the compile-time overhead
|
|
of contracts should not represent an issue and it should be sufficient to
|
|
disable contract checking at run-time as indicated in <link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link> (without a real need to use the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link></computeroutput>
|
|
macro interface or the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
statements in most cases).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.separate_body_implementation">
|
|
<title><link linkend="boost_contract.extras.separate_body_implementation">Separate
|
|
Body Implementation</link></title>
|
|
<para>
|
|
Contracts are part of the program specifications and not of its implementation
|
|
(see <link linkend="boost_contract.contract_programming_overview.specifications_vs__implementation">Specifications
|
|
vs. Implementation</link>). However, this library uses function definitions
|
|
to program contracts so contract code appears together with the function
|
|
implementation code. This is not ideal (even if contracts programmed using
|
|
this library will always appear at the very beginning of the function definition
|
|
so programmers will easily be able to distinguish contract code from the
|
|
rest of the function implementation code so this might not be real limitation
|
|
in practise).
|
|
</para>
|
|
<para>
|
|
In some cases, it might be desirable to completely separate the contract
|
|
code from the function implementation code. For example, this could be necessary
|
|
for software that ships only header files and compiled object files to its
|
|
users. If contracts are programmed in function definitions that are compiled
|
|
in the object files, users will not be able to see the contract code to understand
|
|
semantics and usage of the functions (again, this might not be a real problem
|
|
in practice for example if contracts are already somehow extracted from the
|
|
source code by some tool and presented as part of the documentation of the
|
|
shipped software).
|
|
</para>
|
|
<para>
|
|
In any case, when it is truly important to separate contracts from function
|
|
implementation code, function implementations can be programmed in extra
|
|
<emphasis>body functions</emphasis> (here named <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_body</phrase></computeroutput>, but any other naming scheme could
|
|
be used) that are compiled in object files. Function definitions that remain
|
|
in header files instead will contain just contract code followed by calls
|
|
to the extra body functions. This technique allows to keep the contract code
|
|
in header files while separating the implementation code to source and object
|
|
files. However, this adds the overhead of manually programming an extra function
|
|
declaration for each body function (plus the limitation that constructor
|
|
member initialization lists must be programmed in header files because that
|
|
is where constructors need to be defined to list constructor contract code).
|
|
<footnote id="boost_contract.extras.separate_body_implementation.f0">
|
|
<para>
|
|
When used as default parameter values, lambda functions allow to program
|
|
code statements within function declarations. However, these lambadas cannot
|
|
be effectively used to program contracts in function declarations instead
|
|
of definitions. That is because the C++11 standard does not allow lambdas
|
|
in function declarations to capture any variable (for the good reason that
|
|
it is not at all obvious how to correctly define the semantics of such
|
|
captures). For example, the following code is not valid C++ and it does
|
|
not compile:
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Specifications (in declaration).</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase>
|
|
<phrase role="comment">// Error: Lambdas in default parameters cannot capture `this`, `x`, or any other variable.</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">()></phrase> <phrase role="identifier">pre</phrase> <phrase role="special">=</phrase> <phrase role="special">[&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special"><</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">numeric_limits</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>::</phrase><phrase role="identifier">max</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">},</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&)></phrase> <phrase role="identifier">post</phrase>
|
|
<phrase role="special">=</phrase> <phrase role="special">[&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">old_x</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">);</phrase>
|
|
|
|
<phrase role="comment">// Implementation (in definition).</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">inc</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">()></phrase> <phrase role="identifier">pre</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase><phrase role="keyword">void</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&)></phrase> <phrase role="identifier">post</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_x</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="identifier">pre</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">post</phrase><phrase role="special">,</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">),</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_x</phrase><phrase role="special">)))</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">++;</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
In any case, even if the above code compiled, it would require significant
|
|
boiler-plate code to bind return and old values.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<para>
|
|
For example, the following header file only contains function declarations,
|
|
contract code, and constructor member initializations, but it does not contain
|
|
the code implementing the function bodies (see <ulink url="../../example/features/separate_body.hpp"><literal moreinfo="none">separate_body.hpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">iarray</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">iarray</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">iarray</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">max</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="comment">// Still, member initializations must be here.</phrase>
|
|
<phrase role="identifier">values_</phrase><phrase role="special">(</phrase><phrase role="keyword">new</phrase> <phrase role="keyword">int</phrase><phrase role="special">[</phrase><phrase role="identifier">max</phrase><phrase role="special">]),</phrase>
|
|
<phrase role="identifier">capacity_</phrase><phrase role="special">(</phrase><phrase role="identifier">max</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">max</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">constructor_body</phrase><phrase role="special">(</phrase><phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase> <phrase role="comment">// Separate constructor body impl.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">iarray</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase> <phrase role="comment">// Inv.</phrase>
|
|
<phrase role="identifier">destructor_body</phrase><phrase role="special">();</phrase> <phrase role="comment">// Separate destructor body implementation.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">push_back_body</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase> <phrase role="comment">// Separate member function body implementation.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contracts in class declaration (above), but body implementations are not.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">constructor_body</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">destructor_body</phrase><phrase role="special">();</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back_body</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
Instead, the function bodies are implemented in a separate source file (see
|
|
<ulink url="../../example/features/separate_body.cpp"><literal moreinfo="none">separate_body.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">void</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_body</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">values_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="keyword">int</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">size_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor_body</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">values_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back_body</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="identifier">values_</phrase><phrase role="special">[</phrase><phrase role="identifier">size_</phrase><phrase role="special">++]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The same technique can be used for non-member, private, and protected functions,
|
|
etc.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
When contracts are programmed only in <literal moreinfo="none">.cpp</literal> files and
|
|
also all this library headers are <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase></computeroutput>d
|
|
only from <literal moreinfo="none">.cpp</literal> files, then these <literal moreinfo="none">.cpp</literal>
|
|
files can be compiled disabling specific contract checking (for example,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>,
|
|
see <link linkend="boost_contract.extras.disable_contract_checking">Disable
|
|
Contract Checking</link>). Then the code in these <literal moreinfo="none">.cpp</literal>
|
|
files will always have such contract checking disabled even when linked
|
|
to some other user code that might have been compiled with a different
|
|
set of contracts disabled (i.e., a different set of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> macros defined). This technique might
|
|
be useful to ship compiled object files (e.g., for a library) that will
|
|
never check some contracts (e.g., postconditions, exception guarantees,
|
|
and exit invariants) regardless of the definition of the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
macros used to compile code that links against the shipped object files.
|
|
</para>
|
|
<para>
|
|
On the flip side, if contracts are programmed only in header files (e.g.,
|
|
using extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_body</phrase></computeroutput>
|
|
functions as shown in this section) and this library headers are <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase></computeroutput>d only in these header files
|
|
that are being shipped, then end users can enable or disables contract
|
|
checking of the shipped code by defining the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput> macros when they compile the shipped
|
|
header files as part of their code. This technique might be useful in other
|
|
situations when programmers that ship code want to leave it up the their
|
|
end users to decide which contracts of the shipped code should be checked
|
|
at run-time.
|
|
</para>
|
|
</note>
|
|
</section>
|
|
<section id="boost_contract.extras.no_lambda_functions__no_c__11_">
|
|
<title><link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions (No C++11)</link></title>
|
|
<para>
|
|
This section shows how to use this library without C++11 lambda functions.
|
|
This has some advantages:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
It allows to use this library on compilers that do not support C++11
|
|
lambda functions (essentially most C++03 compilers with adequate support
|
|
for SFINAE can be used in that case, see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros</link> to also avoid using variadic macros). <footnote id="boost_contract.extras.no_lambda_functions__no_c__11_.f0">
|
|
<para>
|
|
Alternatively, on compilers that do not support C++11 lambda functions,
|
|
<ulink url="http://www.boost.org/doc/libs/release/libs/local_function/doc/html/index.html">Boost.LocalFunction</ulink>
|
|
could be used to program the contract functors still within the function
|
|
definitions (for example, see <ulink url="../../example/features/no_lambdas_local_func.cpp"><literal moreinfo="none">no_lambda_local_func.cpp</literal></ulink>).
|
|
In general, such a code is less verbose than the example shown in this
|
|
section that uses contract functions programmed outside of the original
|
|
function definitions (about 30% less lines of code) but the contract
|
|
code is hard to read. Other libraries could also be used to program
|
|
the contract functors without C++11 lambda functions (Boost.Lambda,
|
|
Boost.Fusion, etc.) but again all these techniques will result in contract
|
|
code either more verbose, or harder to read and maintain than the code
|
|
that uses C++11 lambda functions.
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Contract functions (i.e., the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_precondition</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_old</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_postcondition</phrase></computeroutput> functions in the example
|
|
below) can be programmed to fully enforce constant-correctness and other
|
|
contract requirements at compile-time (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>).
|
|
<footnote id="boost_contract.extras.no_lambda_functions__no_c__11_.f1">
|
|
<para>
|
|
If C++ allowed lambda functions to capture variables by constant reference
|
|
(for example allowing a syntax like this <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[</phrase><phrase role="keyword">const</phrase><phrase role="special">&]</phrase>
|
|
<phrase role="special">{</phrase> <phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[</phrase><phrase role="keyword">const</phrase><phrase role="special">&</phrase>
|
|
</computeroutput><literal moreinfo="none"><emphasis>variable-name</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase></computeroutput>,
|
|
see <ulink url="https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/0UKQw9eo3N0">https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/0UKQw9eo3N0</ulink>)
|
|
also lambdas could be used to program contract functors that fully
|
|
enforce <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>
|
|
at compile-time. Note that C++11 lambdas allow to capture variables
|
|
by value (using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[=]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="special">}</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">[</phrase></computeroutput><literal moreinfo="none"><emphasis>variable-name</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase></computeroutput>)
|
|
and these value captures are <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
(unless the lambda is explicitly declared <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">mutable</phrase></computeroutput>)
|
|
but they are not suitable to program postconditions and exception guarantees
|
|
using this library (because those require capturing by reference, see
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>
|
|
and <link linkend="boost_contract.tutorial.exception_guarantees">Exception
|
|
Guarantees</link>), plus they introduce a copy of the captured value
|
|
that might be too expensive in general and therefore not suitable for
|
|
preconditions either.
|
|
</para>
|
|
</footnote>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Code of the contract functions is separated from function body implementations
|
|
(see <link linkend="boost_contract.extras.separate_body_implementation">Separate
|
|
Body Implementation</link>).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
However, not using C++11 lambda functions comes at the significant cost of
|
|
having to manually program the extra contract functions and related boiler-plate
|
|
code. For example, the header file (see <ulink url="../../example/features/no_lambdas.hpp"><literal moreinfo="none">no_lambdas.hpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">class</phrase> <phrase role="identifier">iarray</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">iarray</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">constructor_precondition</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">max</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">constructor_old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>&</phrase>
|
|
<phrase role="identifier">old_instances</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_instances</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">constructor_postcondition</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">old_instances</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">max</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_instances</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">iarray</phrase><phrase role="special">();</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">destructor_old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">>&</phrase> <phrase role="identifier">old_instances</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_instances</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">destructor_postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">old_instances</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_instances</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back_precondition</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back_old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">>&</phrase> <phrase role="identifier">old_size</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back_postcondition</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">old_size</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">instances</phrase><phrase role="special">();</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">int</phrase><phrase role="special">*</phrase> <phrase role="identifier">values_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
And, the source file (see <ulink url="../../example/features/no_lambdas.cpp"><literal moreinfo="none">no_lambdas.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">iarray</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">iarray</phrase><phrase role="special">>(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special">,</phrase> <phrase role="identifier">max</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">)),</phrase>
|
|
<phrase role="identifier">values_</phrase><phrase role="special">(</phrase><phrase role="keyword">new</phrase> <phrase role="keyword">int</phrase><phrase role="special">[</phrase><phrase role="identifier">max</phrase><phrase role="special">]),</phrase> <phrase role="comment">// Member initializations can be here.</phrase>
|
|
<phrase role="identifier">capacity_</phrase><phrase role="special">(</phrase><phrase role="identifier">max</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_instances</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_old</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_instances</phrase><phrase role="special">)))</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_postcondition</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">this</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">max</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_instances</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">))</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">values_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="keyword">int</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">size_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">++</phrase><phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">iarray</phrase><phrase role="special">::~</phrase><phrase role="identifier">iarray</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_instances</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor_old</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_instances</phrase><phrase role="special">)))</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor_postcondition</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_instances</phrase><phrase role="special">)))</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">values_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">--</phrase><phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back_precondition</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back_old</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">ref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_size</phrase><phrase role="special">)))</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(&</phrase><phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">push_back_postcondition</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">old_size</phrase><phrase role="special">)))</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">values_</phrase><phrase role="special">[</phrase><phrase role="identifier">size_</phrase><phrase role="special">++]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">instances</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check static invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">iarray</phrase><phrase role="special">>();</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">instances_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">iarray</phrase><phrase role="special">::</phrase><phrase role="identifier">instances_</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
If programmers also want to fully enforce all contract programming constant-correctness
|
|
requirements at compile-time, they should follow these rules when programming
|
|
the contract functions (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">Constant-Correctness</link>):
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
Precondition functions (i.e., the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_precondition</phrase></computeroutput> functions in the example
|
|
above) can take their arguments either by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
value or by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>,
|
|
and when they are member functions they should be either <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput> or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput>
|
|
functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Postcondition functions (i.e., the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_postcondition</phrase></computeroutput> functions in the example
|
|
above) should take their arguments by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>, and when they are member functions
|
|
they should be either <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Similarly, exception guarantee functions (not shown in the example above)
|
|
should take their arguments by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput>, and when they are member functions
|
|
they should be either <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Old value functions (i.e., the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">...</phrase><phrase role="identifier">_old</phrase></computeroutput> functions in the example above)
|
|
should take their arguments by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput> a part from old value pointers that
|
|
should be taken by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">&</phrase></computeroutput>
|
|
(so only old value pointers can be modified), and when they are member
|
|
functions they should be either <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase></computeroutput> functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
For constructors: Precondition, old value, and exception guarantee functions
|
|
should be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput> (because
|
|
there is no valid object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>
|
|
if the constructor body does not run successfully, see <link linkend="boost_contract.contract_programming_overview.constructor_calls">Constructor
|
|
Calls</link>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
For destructors: Postcondition functions should be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
(because there is no valid object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>
|
|
after the destructor body runs successfully, but exception guarantee
|
|
functions do not have to be <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static</phrase></computeroutput>
|
|
since the object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput> is
|
|
still valid because the destructor body did not run successfully, see
|
|
<link linkend="boost_contract.contract_programming_overview.destructor_calls">Destructor
|
|
Calls</link>).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
Note that the extra contract functions also allow to keep the contract code
|
|
in the header file while all function bodies are implemented in a separate
|
|
source file (including the constructor member initialization list, that could
|
|
not be done with the techniques shown in <link linkend="boost_contract.extras.separate_body_implementation">Separate
|
|
Body Implementation</link>). <footnote id="boost_contract.extras.no_lambda_functions__no_c__11_.f2">
|
|
<para>
|
|
In this example, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">bind</phrase></computeroutput> was
|
|
used to generate nullary functors from the contract functions. As always
|
|
with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">bind</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">cref</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">ref</phrase></computeroutput>
|
|
must be used to bind arguments by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">const</phrase><phrase role="special">&</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">&</phrase></computeroutput>
|
|
respectively, plus it might be necessary to explicitly <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static_cast</phrase></computeroutput>
|
|
the function pointer passed to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">bind</phrase></computeroutput>
|
|
for overloaded functions.
|
|
</para>
|
|
</footnote> Also note that the contract functions can always be declared
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">private</phrase></computeroutput> if programmers need
|
|
to exactly control the public members of the class (this was not done in
|
|
this example only for brevity).
|
|
</para>
|
|
<para>
|
|
The authors think this library is most useful when used together with C++11
|
|
lambda functions (because of the large amount of boiler-plate code required
|
|
when C++11 lambdas are not used as also shown by the example above).
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.extras.no_macros__and_no_variadic_macros_">
|
|
<title><link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_">No
|
|
Macros (and No Variadic Macros)</link></title>
|
|
<para>
|
|
It is possible to specify contracts without using most of the macros provided
|
|
by this library and programming the related code manually instead (the only
|
|
macros that cannot be programmed manually are <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>).
|
|
</para>
|
|
<note>
|
|
<para>
|
|
Some of this library macros are variadic macros, others are not (see below).
|
|
Variadic macros were officially added to the language in C++11 but most
|
|
compilers have been supporting them as an extension for a long time, plus
|
|
all compilers that support C++11 lambda functions should also support C++11
|
|
variadic macros (and this library might rarely be used without the convenience
|
|
of C++11 lambda functions, see <link linkend="boost_contract.extras.no_lambda_functions__no_c__11_">No
|
|
Lambda Functions</link>). <footnote id="boost_contract.extras.no_macros__and_no_variadic_macros_.f0">
|
|
<para>
|
|
Compilation times of this library were measured to be comparable between
|
|
compilers that support variadic macros and compilers that do not.
|
|
</para>
|
|
</footnote> Therefore, the rest of this section can be considered mainly
|
|
a curiosity because programmers should seldom, if ever, need to use this
|
|
library without using its macros.
|
|
</para>
|
|
</note>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.no_macros__and_no_variadic_macros_.h0">
|
|
<phrase id="boost_contract.extras.no_macros__and_no_variadic_macros_.overrides"/><link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_.overrides">Overrides</link>
|
|
</bridgehead>
|
|
<para>
|
|
As shown in <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link> and <link linkend="boost_contract.advanced.named_overrides">Named
|
|
Overrides</link>, this library provides the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput>
|
|
macros to program contracts for overriding public functions (see <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_MAX_ARGS">BOOST_CONTRACT_MAX_ARGS</link></computeroutput> for compilers
|
|
that do not support variadic templates). <footnote id="boost_contract.extras.no_macros__and_no_variadic_macros_.f1">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_MAX_ARGS">BOOST_CONTRACT_MAX_ARGS</link></computeroutput>
|
|
macro is named after <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_FUNCTION_MAX_ARGS</phrase></computeroutput>.
|
|
</para>
|
|
</footnote> These macro cannot be programmed manually but they are not variadic
|
|
macros (so programmers should be able to use them on any C++ compiler with
|
|
a sound support for SFINAE). <footnote id="boost_contract.extras.no_macros__and_no_variadic_macros_.f2">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> These macros expand to SFINAE-based
|
|
introspection template code that are too complex to be programmed manually
|
|
by users (that remains the case even if C++14 generic lambdas were to be
|
|
used here). On a related note, in theory using C++14 generic lambdas, the
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
macro could be re-implemented in a way that can be expanded at function
|
|
scope, instead of class scope (but there is not really a need to do that).
|
|
</para>
|
|
</footnote> The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link></computeroutput>
|
|
macro is a variadic macro instead but programmes can manually repeat the
|
|
non-variadic macro <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput>
|
|
for each overriding public function name on compilers that do not support
|
|
variadic macros.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.no_macros__and_no_variadic_macros_.h1">
|
|
<phrase id="boost_contract.extras.no_macros__and_no_variadic_macros_.assertions__not_variadic_"/><link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_.assertions__not_variadic_">Assertions
|
|
(Not Variadic)</link>
|
|
</bridgehead>
|
|
<para>
|
|
As shown in <link linkend="boost_contract.tutorial.preconditions">Preconditions</link>,
|
|
<link linkend="boost_contract.tutorial.postconditions">Postconditions</link>,
|
|
<link linkend="boost_contract.tutorial.exception_guarantees">Exception Guarantees</link>,
|
|
<link linkend="boost_contract.tutorial.class_invariants">Class Invariants</link>,
|
|
etc. this library provides the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
macro to assert contract conditions. This is not a variadic macro and programmers
|
|
should be able to use it on all C++ compilers. In any case, the invocation
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase></computeroutput><literal moreinfo="none"><emphasis>cond</emphasis></literal><computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">)</phrase></computeroutput> simply expands to code equivalent to the
|
|
following: <footnote id="boost_contract.extras.no_macros__and_no_variadic_macros_.f3">
|
|
<para>
|
|
<emphasis role="bold">Rationale:</emphasis> There is no need for the code
|
|
expanded by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
to also use C++11 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">__func__</phrase></computeroutput>.
|
|
That is because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">__func__</phrase></computeroutput>
|
|
will always expand to the name <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">operator</phrase><phrase role="special">()</phrase></computeroutput> of the functor used to program the contract
|
|
assertions (e.g., the internal name the compiler assigns to lambda functions)
|
|
and it will not expand to the name of the actual function enclosing the
|
|
contract declaration.
|
|
</para>
|
|
</footnote>
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase><phrase role="special">(!(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">))</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">throw</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">assertion_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">__FILE__</phrase><phrase role="special">,</phrase> <phrase role="identifier">__LINE__</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">BOOST_PP_STRINGIZE</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
<para>
|
|
In fact, this library considers any exception thrown from within preconditions,
|
|
postconditions, exception guarantees, and class invariants as a contract
|
|
failure and reports it calling the related contract failure handler (<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>,
|
|
etc.). If there is a need for it, programmers can always program contract
|
|
assertions that throw specific user-defined exceptions as follow (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">Throw
|
|
on Failures</link>):
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase><phrase role="special">(!</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">)</phrase> <phrase role="keyword">throw</phrase> <literal moreinfo="none"><emphasis>exception-object</emphasis></literal><phrase role="special">;</phrase>
|
|
</programlisting>
|
|
<para>
|
|
However, using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>
|
|
is convenient because it always allows this library to show an informative
|
|
message in case of assertion failure containing the assertion code, file
|
|
name, line number, etc.
|
|
</para>
|
|
<para>
|
|
As shown in <link linkend="boost_contract.extras.assertion_levels">Assertion
|
|
Levels</link>, this library pre-defines <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput>
|
|
assertion levels. These macros are not variadic macros and programmers should
|
|
be able to use them on all C++ compilers. In any case, their implementations
|
|
are equivalent to the following:
|
|
</para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">)</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#else</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">)</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">))</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">)</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="special">(</phrase><literal moreinfo="none"><emphasis>cond</emphasis></literal><phrase role="special">))</phrase>
|
|
</programlisting>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.no_macros__and_no_variadic_macros_.h2">
|
|
<phrase id="boost_contract.extras.no_macros__and_no_variadic_macros_.base_types__variadic_"/><link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_.base_types__variadic_">Base
|
|
Types (Variadic)</link>
|
|
</bridgehead>
|
|
<para>
|
|
As shown in <link linkend="boost_contract.tutorial.base_classes__subcontracting_">Base
|
|
Classes</link>, this library provides the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
variadic macro to declare the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
member type that will expand to the list of all public bases for a derived
|
|
class. Programmers can also declare <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput>
|
|
without using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
at the cost of writing a bit more code and increase maintenance efforts.
|
|
For example (see <ulink url="../../example/features/base_types_no_macro.cpp"><literal moreinfo="none">base_types_no_macro.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">mpl</phrase><phrase role="special">/</phrase><phrase role="identifier">vector</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">chars</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">chars</phrase><phrase role="special">>,</phrase>
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">unique_chars</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">public</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">>,</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">protected</phrase> <phrase role="identifier">has_size</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">has_empty</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Program `base_types` without macros (list only public bases).</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">mpl</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">unique_chars</phrase><phrase role="special">,</phrase> <phrase role="identifier">pushable</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">></phrase> <phrase role="special">></phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">base_types</phrase></computeroutput> member type
|
|
must be a <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">mpl</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase></computeroutput>
|
|
which must list <emphasis>all and only</emphasis> <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">public</phrase></computeroutput>
|
|
base classes (because only public bases subcontract, see <link linkend="boost_contract.contract_programming_overview.function_calls">Function
|
|
Calls</link>), and in the same order these public base classes appear in
|
|
the derived class inheritance list. If the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
macro is not used, it is the responsibility of the programmers to maintain
|
|
the correct list of bases in the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">mpl</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase></computeroutput> each time the derived class inheritance
|
|
list changes (this might significantly complicate maintenance).
|
|
</para>
|
|
<para>
|
|
In general, it is recommended to use the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>
|
|
macro whenever possible.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.no_macros__and_no_variadic_macros_.h3">
|
|
<phrase id="boost_contract.extras.no_macros__and_no_variadic_macros_.old_values__variadic_"/><link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_.old_values__variadic_">Old
|
|
Values (Variadic)</link>
|
|
</bridgehead>
|
|
<para>
|
|
As shown in <link linkend="boost_contract.tutorial.old_values">Old Values</link>,
|
|
this library provides the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
variadic macro to assign old value copies. Programmers can also assign old
|
|
values without using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
at the cost of writing a bit more code manually. For example (see <ulink url="../../example/features/old_no_macro.cpp"><literal moreinfo="none">old_no_macro.cpp</literal></ulink>):
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Program old value instead of using `OLD(size())` macro.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">unsigned</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">make_old</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">copy_old</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">?</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">null_old</phrase><phrase role="special">())</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* ... */</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
The ternary operator <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">copy_old</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">?</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">null_old</phrase><phrase role="special">()</phrase></computeroutput> must be used here to avoid evaluating and
|
|
copying the old value expression <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">size</phrase><phrase role="special">()</phrase></computeroutput> when <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.copy_old_idm45028038942304">boost::contract::copy_old</link></computeroutput>
|
|
returns <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">false</phrase></computeroutput> (because old values
|
|
are not being copied when postcondition and exception guarantee checking
|
|
is disabled at run-time, an overridden virtual function call is not checking
|
|
postconditions or exception guarantees yet, etc.). The enclosing <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.make_old_idm45028038966352">boost::contract::make_old</link></computeroutput>
|
|
copies the old value expression and creates an old value pointer. Otherwise,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.null_old">boost::contract::null_old</link></computeroutput>
|
|
indicates that a null old value pointer should be created.
|
|
</para>
|
|
<para>
|
|
The <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.make_old_idm45028038966352">boost::contract::make_old</link></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.copy_old_idm45028038942304">boost::contract::copy_old</link></computeroutput>
|
|
functions are used exactly as shown above but without the extra <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">v</phrase></computeroutput> parameter when they are called from within
|
|
non-virtual functions (see <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">Public
|
|
Function Overrides</link>). The old value pointer returned by <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.make_old_idm45028038966352">boost::contract::make_old</link></computeroutput>
|
|
can be assigned to either <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput>
|
|
or <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>
|
|
(see <link linkend="boost_contract.extras.old_value_requirements__templates_">Old
|
|
Value Requirements</link>).
|
|
</para>
|
|
<para>
|
|
In general, it is recommended to use the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>
|
|
macro whenever possible.
|
|
</para>
|
|
<bridgehead renderas="sect4" id="boost_contract.extras.no_macros__and_no_variadic_macros_.h4">
|
|
<phrase id="boost_contract.extras.no_macros__and_no_variadic_macros_.macro_interface__variadic_"/><link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_.macro_interface__variadic_">Macro
|
|
Interface (Variadic)</link>
|
|
</bridgehead>
|
|
<para>
|
|
Almost all macros defined in <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link></computeroutput>
|
|
are variadic macros. On compilers that do not support variadic macros, programmers
|
|
can manually disable contract code compilation using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_NO_</phrase><phrase role="special">...</phrase></computeroutput>
|
|
statements as shown in <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable
|
|
Contract Compilation</link>.
|
|
</para>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.examples">
|
|
<title><link linkend="boost_contract.examples">Examples</link></title>
|
|
<para>
|
|
This section lists some examples taken from different sources discussing contract
|
|
programming and implemented here using this library.
|
|
</para>
|
|
<note>
|
|
<para>
|
|
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++.
|
|
Nevertheless, programmers are encouraged to review these examples to see
|
|
a few diverse uses of this library that might be relevant to their needs.
|
|
</para>
|
|
</note>
|
|
<para>
|
|
The examples in this sections are taken from the following sources:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="N1962_anchor">[N1962]</link>: Examples from a detailed and
|
|
complete proposal to add contract programming to C++11 (unfortunately,
|
|
this proposal was never accepted into the standard).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="Meyer97_anchor">[Meyer97]</link>: Examples from the Eiffel
|
|
programming language (reprogrammed here in C++ using this library).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="Mitchell02_anchor">[Mitchell02]</link>: Additional examples
|
|
from the Eiffel programming language (reprogrammed here in C++ using this
|
|
library).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="Cline90_anchor">[Cline90]</link>: 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).
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
The following are some examples of particular interest:
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="N1962_vector_anchor">[N1962] Vector</link>: Complete set
|
|
of contracts for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase></computeroutput>, plus a comparison with <link linkend="N1962_anchor">[N1962]</link> syntax.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="N1962_square_root_anchor">[N1962] Square Root</link>: Comparison
|
|
with D syntax.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="Mitchell02_counter_anchor">[Mitchell02] Counter</link>:
|
|
Subcontracting.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="Meyer97_stack4_anchor">[Meyer97] Stack4</link>: Comparison
|
|
with Eiffel syntax.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
<link linkend="Cline90_vector_anchor">[Cline90] Vector</link>: Comparison
|
|
with A++ syntax.
|
|
</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para>
|
|
Most of the examples listed here use old values and class invariants which
|
|
are instead not supported by <link linkend="P0380_anchor">[P0380]</link>. Therefore,
|
|
there is not meaningful example here that can be directly implemented and compared
|
|
using <link linkend="P0380_anchor">[P0380]</link> syntax.
|
|
</para>
|
|
<section id="boost_contract.examples.__n1962___vector__contracts_for_stl_vector_and_comparison_with___n1962___proposed_syntax">
|
|
<title><anchor id="N1962_vector_anchor"/>[N1962] Vector: Contracts for STL
|
|
vector and comparison with <link linkend="N1962_anchor">[N1962]</link> proposed
|
|
syntax</title>
|
|
<para>
|
|
On compilers that support C++17 <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">if</phrase>
|
|
<phrase role="keyword">constexpr</phrase></computeroutput>, the following example using
|
|
this library can be simplified removing <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><link linkend="boost.contract.condition_if">boost::contract::condition_if</link></computeroutput>
|
|
and related functor templates such as <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">all_of_equal_to</phrase></computeroutput>,
|
|
etc., making it more similar to the pseudo-code on the right-hand side (see
|
|
<link linkend="boost_contract.extras.assertion_requirements__templates_">Assertion
|
|
Requirements</link>).
|
|
</para>
|
|
<informaltable frame="all">
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
This library
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
[N1962] proposal (not accepted in C++) plus C++17 <literal moreinfo="none">if constexpr</literal>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">bind</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">optional</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">/</phrase><phrase role="identifier">cxx11</phrase><phrase role="special">/</phrase><phrase role="identifier">all_of</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">type_traits</phrase><phrase role="special">/</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">next_prior</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">functional</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">iterator</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">memory</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Could be programmed at call site with C++14 generic lambdas.</phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">all_of_equal_to</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">result_type</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">result_type</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">result_type</phrase> <phrase role="keyword">operator</phrase><phrase role="special">()(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">where</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">j</phrase> <phrase role="special">=</phrase> <phrase role="identifier">where</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">last</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="special">++</phrase><phrase role="identifier">j</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(*</phrase><phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="special">*</phrase><phrase role="identifier">j</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">Iter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iter</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase> <phrase role="comment">// Cannot implement in C++ (for axiom only).</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">Iter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">contained</phrase><phrase role="special">(</phrase><phrase role="identifier">Iter</phrase> <phrase role="identifier">first1</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iter</phrase> <phrase role="identifier">last1</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iter</phrase> <phrase role="identifier">first2</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iter</phrase> <phrase role="identifier">last2</phrase><phrase role="special">);</phrase> <phrase role="comment">// For axiom.</phrase>
|
|
|
|
<phrase role="comment">// STL vector requires T copyable but not equality comparable.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">())</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">rbegin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">rend</phrase><phrase role="special">())</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">allocator_type</phrase> <phrase role="identifier">allocator_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">pointer</phrase> <phrase role="identifier">pointer</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_pointer</phrase> <phrase role="identifier">const_pointer</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">reference</phrase> <phrase role="identifier">reference</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_reference</phrase> <phrase role="identifier">const_reference</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">value_type</phrase> <phrase role="identifier">value_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">iterator</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">const_iterator</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">size_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">difference_type</phrase> <phrase role="identifier">difference_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">reverse_iterator</phrase>
|
|
<phrase role="identifier">reverse_iterator</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_reverse_iterator</phrase>
|
|
<phrase role="identifier">const_reverse_iterator</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">alloc</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">T</phrase><phrase role="special">())</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase>
|
|
<phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase>
|
|
<phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* implicit */</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase><phrase role="special">),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase> <phrase role="special">!=</phrase> <phrase role="special">&</phrase><phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="identifier">vect_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">*</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">vector</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">reserve</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">reserve</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">const_iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">reverse_iterator</phrase> <phrase role="identifier">rbegin</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">rend</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_reverse_iterator</phrase> <phrase role="identifier">rbegin</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">const_reverse_iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">rend</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">reverse_iterator</phrase> <phrase role="identifier">rend</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_reverse_iterator</phrase> <phrase role="identifier">rend</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase> <phrase role="special">=</phrase> <phrase role="identifier">T</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">></phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase> <phrase role="identifier">begin</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">Allocator</phrase> <phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants, no pre (throw out_of_range for invalid index).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants, no pre (throw out_of_range for invalid index).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="identifier">front</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">front</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="identifier">front</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">front</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_capacity</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>(),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">back</phrase><phrase role="special">()),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">pop_back</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">pop_back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">contained</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase>
|
|
<phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_capacity</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>(),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_capacity</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">iterator</phrase><phrase role="special">></phrase> <phrase role="identifier">old_where</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">prior</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_where</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">prior</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_where</phrase><phrase role="special">)</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_capacity</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">iterator</phrase><phrase role="special">></phrase> <phrase role="identifier">old_where</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special"><</phrase>
|
|
<phrase role="identifier">max_size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">contained</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_capacity</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">all_of_equal_to</phrase><phrase role="special">(),</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase>
|
|
<phrase role="special">*</phrase><phrase role="identifier">old_where</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">end</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">iterator</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">size_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_size</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_size</phrase> <phrase role="special">-</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">));</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(!</phrase><phrase role="identifier">valid</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">clear</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">clear</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">vector</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase> <phrase role="identifier">old_me</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_other</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="identifier">old_me</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">old_other</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_other</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">condition_if</phrase><phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase> <phrase role="special">>(</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">bind</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>(),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">),</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">cref</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_me</phrase><phrase role="special">))</phrase>
|
|
<phrase role="special">)</phrase>
|
|
<phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check class invariants for left and right objects.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">left_inv</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(&</phrase><phrase role="identifier">left</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">right_inv</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(&</phrase><phrase role="identifier">right</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase> <phrase role="special">==</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">></phrase> <phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// char type has operator==.</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">></phrase> <phrase role="identifier">v</phrase><phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="char">'\0'</phrase><phrase role="special">));</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">cv</phrase> <phrase role="special">=</phrase> <phrase role="identifier">v</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">cv</phrase> <phrase role="special">==</phrase> <phrase role="identifier">v</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">></phrase> <phrase role="identifier">w</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">w</phrase> <phrase role="special">==</phrase> <phrase role="identifier">v</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">>::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(*</phrase><phrase role="identifier">i</phrase> <phrase role="special">==</phrase> <phrase role="char">'\0'</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">char</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">ci</phrase> <phrase role="special">=</phrase> <phrase role="identifier">cv</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(*</phrase><phrase role="identifier">ci</phrase> <phrase role="special">==</phrase> <phrase role="char">'\0'</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">i</phrase><phrase role="special">,</phrase> <phrase role="number">2</phrase><phrase role="special">,</phrase> <phrase role="char">'a'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">0</phrase><phrase role="special">]</phrase> <phrase role="special">==</phrase> <phrase role="char">'a'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">1</phrase><phrase role="special">]</phrase> <phrase role="special">==</phrase> <phrase role="char">'a'</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="char">'b'</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="char">'b'</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">x</phrase> <phrase role="special">{};</phrase> <phrase role="comment">// x type doest not have operator==.</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">x</phrase><phrase role="special">></phrase> <phrase role="identifier">y</phrase><phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">x</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">cy</phrase> <phrase role="special">=</phrase> <phrase role="identifier">y</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">x</phrase><phrase role="special">></phrase> <phrase role="identifier">z</phrase><phrase role="special">(</phrase><phrase role="identifier">y</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">x</phrase><phrase role="special">>::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">j</phrase> <phrase role="special">=</phrase> <phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">j</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">x</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">cj</phrase> <phrase role="special">=</phrase> <phrase role="identifier">cy</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">cj</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">cy</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">j</phrase><phrase role="special">,</phrase> <phrase role="number">2</phrase><phrase role="special">,</phrase> <phrase role="identifier">x</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">y</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Extra spaces, newlines, etc. for visual alignment with this library code.</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">/</phrase><phrase role="identifier">cxx11</phrase><phrase role="special">/</phrase><phrase role="identifier">all_of</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">type_traits</phrase><phrase role="special">/</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">next_prior</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">iterator</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">memory</phrase><phrase role="special">></phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="special">=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">allocator</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
|
|
|
|
<phrase role="identifier">invariant</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">())</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">rbegin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">rend</phrase><phrase role="special">())</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">allocator_type</phrase> <phrase role="identifier">allocator_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">pointer</phrase> <phrase role="identifier">pointer</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_pointer</phrase> <phrase role="identifier">const_pointer</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">reference</phrase> <phrase role="identifier">reference</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_reference</phrase> <phrase role="identifier">const_reference</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">value_type</phrase> <phrase role="identifier">value_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">iterator</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">const_iterator</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">size_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">difference_type</phrase> <phrase role="identifier">difference_type</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">reverse_iterator</phrase>
|
|
<phrase role="identifier">reverse_iterator</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">>::</phrase><phrase role="identifier">const_reverse_iterator</phrase>
|
|
<phrase role="identifier">const_reverse_iterator</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">alloc</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">T</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">alloc</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="comment">/* implicit */</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">*</phrase><phrase role="keyword">this</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">vector</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">*</phrase><phrase role="keyword">this</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="keyword">this</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase> <phrase role="special">!=</phrase> <phrase role="special">&</phrase><phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="identifier">vect_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*</phrase><phrase role="keyword">this</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">vector</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">reserve</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">count</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">reserve</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">const_iterator</phrase> <phrase role="identifier">begin</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">const_iterator</phrase> <phrase role="identifier">end</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">reverse_iterator</phrase> <phrase role="identifier">rbegin</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">rend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">const_reverse_iterator</phrase> <phrase role="identifier">rbegin</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">rend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">reverse_iterator</phrase> <phrase role="identifier">rend</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">const_reverse_iterator</phrase> <phrase role="identifier">rend</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">rend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase> <phrase role="special">=</phrase> <phrase role="identifier">T</phrase><phrase role="special">())</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">></phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()))</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()),</phrase>
|
|
<phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">size_type</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">Alloctor</phrase> <phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// No precondition (throw out_of_range for invalid index).</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// No precondition (throw out_of_range for invalid index).</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="identifier">front</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">front</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="identifier">front</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">front</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">reference</phrase> <phrase role="identifier">back</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">const_reference</phrase> <phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">())</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">back</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">pop_back</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">pop_back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">InputIter</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">InputIter</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// Precondition: [begin(), end()) does not contain [first, last).</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">vallue</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">count</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">assign</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="comment">// if(capacity() > oldof(capacity()))</phrase>
|
|
<phrase role="comment">// [begin(), end()) is invalid</phrase>
|
|
<phrase role="comment">// else</phrase>
|
|
<phrase role="comment">// [where, end()) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">size_type</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()))</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">prior</phrase><phrase role="special">(</phrase><phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">)),</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">prior</phrase><phrase role="special">(</phrase><phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">))</phrase> <phrase role="special">+</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="comment">// [where, end()) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="comment">// else [begin(), end()) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">InputIter</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iterator</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">Iterator</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase> <phrase role="special"><</phrase> <phrase role="identifier">max_size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="comment">// [first, last) is not contained in [begin(), end())</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">+</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()))</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">::</phrase><phrase role="identifier">all_of_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">,</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="comment">// [where, end()) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="comment">// else [begin(), end()) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">,</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">where</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">!</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">where</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldod</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="comment">// [where, end()) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">where</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">iterator</phrase> <phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">iterator</phrase> <phrase role="identifier">last</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">lasst</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">())</phrase> <phrase role="special">-</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">distance</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">end</phrase><phrase role="special">();</phrase>
|
|
<phrase role="comment">// [first, last) is invalid</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">first</phrase><phrase role="special">,</phrase> <phrase role="identifier">last</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">clear</phrase><phrase role="special">()</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">clear</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">vector</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">precondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">get_allocator</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">get_allocator</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">postcondition</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase> <phrase role="keyword">constexpr</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">has_equal_to</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">*</phrase><phrase role="keyword">this</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">other</phrase> <phrase role="special">==</phrase> <phrase role="identifier">oldof</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">swap</phrase><phrase role="special">(</phrase><phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">vector</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Cannot check class invariants for left and right objects.</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase> <phrase role="special">==</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">,</phrase> <phrase role="identifier">Allocator</phrase><phrase role="special">></phrase> <phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="comment">// End.</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</section>
|
|
<section id="boost_contract.examples.__n1962___circle__subcontracting">
|
|
<title><link linkend="boost_contract.examples.__n1962___circle__subcontracting">[N1962]
|
|
Circle: Subcontracting</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">shape</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">shape</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">compute_area</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">shape</phrase><phrase role="special">::</phrase><phrase role="identifier">compute_area</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">circle</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">shape</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">compute_area</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">pi</phrase> <phrase role="special">=</phrase> <phrase role="number">3</phrase><phrase role="special">;</phrase> <phrase role="comment">// Truncated to int from 3.14...</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">circle</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">a_radius</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">radius_</phrase><phrase role="special">(</phrase><phrase role="identifier">a_radius</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">radius</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">a_radius</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">compute_area</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase>
|
|
<phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_compute_area</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">circle</phrase><phrase role="special">::</phrase><phrase role="identifier">compute_area</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">pi</phrase> <phrase role="special">*</phrase> <phrase role="identifier">radius</phrase><phrase role="special">()</phrase> <phrase role="special">*</phrase> <phrase role="identifier">radius</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">pi</phrase> <phrase role="special">*</phrase> <phrase role="identifier">radius</phrase><phrase role="special">()</phrase> <phrase role="special">*</phrase> <phrase role="identifier">radius</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">radius</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">radius_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="identifier">radius_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">circle</phrase> <phrase role="identifier">c</phrase><phrase role="special">(</phrase><phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">radius</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">compute_area</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__n1962___factorial__recursion">
|
|
<title><anchor id="N1962_factorial_anchor"/><link linkend="boost_contract.examples.__n1962___factorial__recursion">[N1962]
|
|
Factorial: Recursion</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">factorial</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative natural number.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special"><=</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase> <phrase role="comment">// Max function input.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special"><</phrase> <phrase role="number">2</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Select assertion.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Assertions automatically disabled in other assertions.</phrase>
|
|
<phrase role="comment">// Therefore, this postcondition can recursively call the</phrase>
|
|
<phrase role="comment">// function without causing infinite recursion.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">*</phrase> <phrase role="identifier">factorial</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">n</phrase> <phrase role="special"><</phrase> <phrase role="number">2</phrase> <phrase role="special">?</phrase> <phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">n</phrase> <phrase role="special">*</phrase> <phrase role="identifier">factorial</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">factorial</phrase><phrase role="special">(</phrase><phrase role="number">4</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">24</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__n1962___equal__operators">
|
|
<title><link linkend="boost_contract.examples.__n1962___equal__operators">[N1962]
|
|
Equal: Operators</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Forward declaration because == and != contracts use one another's function.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">!=(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">!(</phrase><phrase role="identifier">left</phrase> <phrase role="special">==</phrase> <phrase role="identifier">right</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">!(</phrase><phrase role="identifier">left</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">right</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase> <phrase role="special">==</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">number</phrase> <phrase role="special">{</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase> <phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">number</phrase> <phrase role="identifier">n</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">n</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase> <phrase role="special">=</phrase> <phrase role="number">123</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">assert</phrase><phrase role="special">((</phrase><phrase role="identifier">n</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="keyword">true</phrase><phrase role="special">);</phrase> <phrase role="comment">// Explicitly call operator==.</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">((</phrase><phrase role="identifier">n</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="keyword">false</phrase><phrase role="special">);</phrase> <phrase role="comment">// Explicitly call operator!=.</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__n1962___sum__array_parameter">
|
|
<title><link linkend="boost_contract.examples.__n1962___sum__array_parameter">[N1962]
|
|
Sum: Array parameter</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">sum</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase><phrase role="special">*</phrase> <phrase role="identifier">array</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">%</phrase> <phrase role="number">4</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">result</phrase> <phrase role="special">+=</phrase> <phrase role="identifier">array</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">a</phrase><phrase role="special">[</phrase><phrase role="number">4</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="special">{</phrase><phrase role="number">1</phrase><phrase role="special">,</phrase> <phrase role="number">2</phrase><phrase role="special">,</phrase> <phrase role="number">3</phrase><phrase role="special">,</phrase> <phrase role="number">4</phrase><phrase role="special">};</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">sum</phrase><phrase role="special">(</phrase><phrase role="number">4</phrase><phrase role="special">,</phrase> <phrase role="identifier">a</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__n1962___square_root__default_parameters_and_comparison_with_d_syntax">
|
|
<title><anchor id="N1962_square_root_anchor"/><link linkend="boost_contract.examples.__n1962___square_root__default_parameters_and_comparison_with_d_syntax">[N1962]
|
|
Square Root: Default parameters and comparison with D syntax</link></title>
|
|
<informaltable frame="all">
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
This Library
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
The D Programming Language
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cmath</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">long</phrase> <phrase role="identifier">lsqrt</phrase><phrase role="special">(</phrase><phrase role="keyword">long</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">long</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">*</phrase> <phrase role="identifier">result</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><phrase role="identifier">result</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">*</phrase> <phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">></phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="keyword">long</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">sqrt</phrase><phrase role="special">(</phrase><phrase role="keyword">double</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">)));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">lsqrt</phrase><phrase role="special">(</phrase><phrase role="number">4</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Extra spaces, newlines, etc. for visual alignment with this library code.</phrase>
|
|
|
|
|
|
|
|
<phrase role="keyword">long</phrase> <phrase role="identifier">lsqrt</phrase><phrase role="special">(</phrase><phrase role="keyword">long</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">in</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="identifier">out</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">*</phrase> <phrase role="identifier">result</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">((</phrase><phrase role="identifier">result</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">*</phrase> <phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">></phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">do</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">cast</phrase><phrase role="special">(</phrase><phrase role="keyword">long</phrase><phrase role="special">)</phrase><phrase role="identifier">std</phrase><phrase role="special">.</phrase><phrase role="identifier">math</phrase><phrase role="special">.</phrase><phrase role="identifier">sqrt</phrase><phrase role="special">(</phrase><phrase role="identifier">cast</phrase><phrase role="special">(</phrase><phrase role="identifier">real</phrase><phrase role="special">)</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="comment">// End.</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</section>
|
|
<section id="boost_contract.examples.__meyer97___stack4__comparison_with_eiffel_syntax">
|
|
<title><anchor id="Meyer97_stack4_anchor"/><link linkend="boost_contract.examples.__meyer97___stack4__comparison_with_eiffel_syntax">[Meyer97]
|
|
Stack4: Comparison with Eiffel syntax</link></title>
|
|
<informaltable frame="all">
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
This Library
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
The Eiffel Programming Language
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// File: stack4.hpp</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">STACK4_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">STACK4_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Dispenser with LIFO access policy and fixed max capacity.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">stack4</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">stack4</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count non-negative.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase> <phrase role="comment">// Count bounded.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase> <phrase role="comment">// Empty if no elem.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Initialization */</phrase>
|
|
|
|
<phrase role="comment">// Allocate static from a maximum of n elements.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">stack4</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">stack4</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative capacity.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">);</phrase> <phrase role="comment">// Capacity set.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">capacity_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">n</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">count_</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">array_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">n</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Deep copy via constructor.</phrase>
|
|
<phrase role="comment">/* implicit */</phrase> <phrase role="identifier">stack4</phrase><phrase role="special">(</phrase><phrase role="identifier">stack4</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">capacity_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">count_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">array_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity_</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Deep copy via assignment.</phrase>
|
|
<phrase role="identifier">stack4</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">=(</phrase><phrase role="identifier">stack4</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">other</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(*</phrase><phrase role="keyword">this</phrase> <phrase role="special">==</phrase> <phrase role="identifier">other</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">array_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">capacity_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">count_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">array_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity_</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">other</phrase><phrase role="special">.</phrase><phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*</phrase><phrase role="keyword">this</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy this stack.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">stack4</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">array_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Access */</phrase>
|
|
|
|
<phrase role="comment">// Max number of stack elements.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Number of stack elements.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">count_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Top element.</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not empty (i.e., count > 0).</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">count_</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Status Report */</phrase>
|
|
|
|
<phrase role="comment">// Is stack empty?</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Empty definition.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">count_</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Is stack full?</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">full</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="comment">// Full definition.</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">count_</phrase> <phrase role="special">==</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Element Change */</phrase>
|
|
|
|
<phrase role="comment">// Add x on top.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not full.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not empty.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="comment">// Added to top.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// One more.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">count_</phrase><phrase role="special">++]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Remove top element.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">remove</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not empty (i.e., count > 0).</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not full.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// One less.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">--</phrase><phrase role="identifier">count_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Friend Helpers */</phrase>
|
|
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">bool</phrase> <phrase role="keyword">operator</phrase><phrase role="special">==(</phrase><phrase role="identifier">stack4</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase> <phrase role="identifier">stack4</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">inv1</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(&</phrase><phrase role="identifier">left</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">inv2</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(&</phrase><phrase role="identifier">right</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">count_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">array_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">])</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">*</phrase> <phrase role="identifier">array_</phrase><phrase role="special">;</phrase> <phrase role="comment">// Internally use C-style array.</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">--</phrase> <phrase role="identifier">Extra</phrase> <phrase role="identifier">spaces</phrase><phrase role="special">,</phrase> <phrase role="identifier">newlines</phrase><phrase role="special">,</phrase> <phrase role="identifier">etc</phrase><phrase role="special">.</phrase> <phrase role="keyword">for</phrase> <phrase role="identifier">visual</phrase> <phrase role="identifier">alignment</phrase> <phrase role="identifier">with</phrase> <phrase role="keyword">this</phrase> <phrase role="identifier">library</phrase> <phrase role="identifier">code</phrase><phrase role="special">.</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">indexing</phrase>
|
|
<phrase role="identifier">destription</phrase><phrase role="special">:</phrase> <phrase role="string">"Dispenser with LIFO access policy and a fixed max capacity."</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">interface</phrase> <phrase role="identifier">STACK4</phrase><phrase role="special">[</phrase><phrase role="identifier">G</phrase><phrase role="special">]</phrase> <phrase role="identifier">creation</phrase> <phrase role="identifier">make</phrase> <phrase role="special">--</phrase> <phrase role="identifier">Interface</phrase> <phrase role="identifier">only</phrase> <phrase role="special">(</phrase><phrase role="identifier">no</phrase> <phrase role="identifier">implementation</phrase><phrase role="special">).</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">invariant</phrase>
|
|
<phrase role="identifier">count_non_negative</phrase><phrase role="special">:</phrase> <phrase role="identifier">count</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase>
|
|
<phrase role="identifier">count_bounded</phrase><phrase role="special">:</phrase> <phrase role="identifier">count</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase>
|
|
<phrase role="identifier">empty_if_no_elements</phrase><phrase role="special">:</phrase> <phrase role="identifier">empty</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
|
|
|
|
|
|
<phrase role="identifier">feature</phrase> <phrase role="special">--</phrase> <phrase role="identifier">Initialization</phrase>
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Allocate</phrase> <phrase role="identifier">stack</phrase> <phrase role="keyword">for</phrase> <phrase role="identifier">a</phrase> <phrase role="identifier">maximum</phrase> <phrase role="identifier">of</phrase> <phrase role="identifier">n</phrase> <phrase role="identifier">elements</phrase><phrase role="special">.</phrase>
|
|
<phrase role="identifier">make</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase><phrase role="special">:</phrase> <phrase role="identifier">INTEGER</phrase><phrase role="special">)</phrase> <phrase role="identifier">is</phrase>
|
|
<phrase role="identifier">require</phrase>
|
|
<phrase role="identifier">non_negative_capacity</phrase><phrase role="special">:</phrase> <phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase>
|
|
<phrase role="identifier">ensure</phrase>
|
|
<phrase role="identifier">capacity_set</phrase><phrase role="special">:</phrase> <phrase role="identifier">capacity</phrase> <phrase role="special">=</phrase> <phrase role="identifier">n</phrase>
|
|
<phrase role="identifier">end</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">feature</phrase> <phrase role="special">--</phrase> <phrase role="identifier">Access</phrase>
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Max</phrase> <phrase role="identifier">number</phrase> <phrase role="identifier">of</phrase> <phrase role="identifier">stack</phrase> <phrase role="identifier">elements</phrase><phrase role="special">.</phrase>
|
|
<phrase role="identifier">capacity</phrase><phrase role="special">:</phrase> <phrase role="identifier">INTEGER</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Number</phrase> <phrase role="identifier">of</phrase> <phrase role="identifier">stack</phrase> <phrase role="identifier">elements</phrase><phrase role="special">.</phrase>
|
|
<phrase role="identifier">count</phrase><phrase role="special">:</phrase> <phrase role="identifier">INTEGER</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Top</phrase> <phrase role="identifier">element</phrase><phrase role="special">.</phrase>
|
|
<phrase role="identifier">item</phrase><phrase role="special">:</phrase> <phrase role="identifier">G</phrase> <phrase role="identifier">is</phrase>
|
|
<phrase role="identifier">require</phrase>
|
|
<phrase role="identifier">not_empty</phrase><phrase role="special">:</phrase> <phrase role="keyword">not</phrase> <phrase role="identifier">empty</phrase> <phrase role="special">--</phrase> <phrase role="identifier">i</phrase><phrase role="special">.</phrase><phrase role="identifier">e</phrase><phrase role="special">.,</phrase> <phrase role="identifier">count</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase>
|
|
<phrase role="identifier">end</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">feature</phrase> <phrase role="special">--</phrase> <phrase role="identifier">Status</phrase> <phrase role="identifier">report</phrase>
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Is</phrase> <phrase role="identifier">stack</phrase> <phrase role="identifier">empty</phrase><phrase role="special">?</phrase>
|
|
<phrase role="identifier">empty</phrase><phrase role="special">:</phrase> <phrase role="identifier">BOOLEAN</phrase> <phrase role="identifier">is</phrase>
|
|
<phrase role="identifier">ensure</phrase>
|
|
<phrase role="identifier">empty_definition</phrase><phrase role="special">:</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">end</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Is</phrase> <phrase role="identifier">stack</phrase> <phrase role="identifier">full</phrase><phrase role="special">?</phrase>
|
|
<phrase role="identifier">full</phrase><phrase role="special">:</phrase> <phrase role="identifier">BOOLEAN</phrase> <phrase role="identifier">is</phrase>
|
|
<phrase role="identifier">ensure</phrase>
|
|
<phrase role="identifier">full_definition</phrase><phrase role="special">:</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">end</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">feature</phrase> <phrase role="special">--</phrase> <phrase role="identifier">Element</phrase> <phrase role="identifier">change</phrase>
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Add</phrase> <phrase role="identifier">x</phrase> <phrase role="identifier">on</phrase> <phrase role="identifier">top</phrase><phrase role="special">.</phrase>
|
|
<phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">:</phrase> <phrase role="identifier">G</phrase><phrase role="special">)</phrase> <phrase role="identifier">is</phrase>
|
|
<phrase role="identifier">require</phrase>
|
|
<phrase role="identifier">not_full</phrase><phrase role="special">:</phrase> <phrase role="keyword">not</phrase> <phrase role="identifier">full</phrase>
|
|
<phrase role="identifier">ensure</phrase>
|
|
<phrase role="identifier">not_empty</phrase><phrase role="special">:</phrase> <phrase role="keyword">not</phrase> <phrase role="identifier">empty</phrase>
|
|
<phrase role="identifier">added_to_top</phrase><phrase role="special">:</phrase> <phrase role="identifier">item</phrase> <phrase role="special">=</phrase> <phrase role="identifier">x</phrase>
|
|
<phrase role="identifier">one_more_item</phrase><phrase role="special">:</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">old</phrase> <phrase role="identifier">count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase>
|
|
<phrase role="identifier">end</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">Remove</phrase> <phrase role="identifier">top</phrase> <phrase role="identifier">element</phrase><phrase role="special">.</phrase>
|
|
<phrase role="identifier">remove</phrase> <phrase role="identifier">is</phrase>
|
|
<phrase role="identifier">require</phrase>
|
|
<phrase role="identifier">not_empty</phrase><phrase role="special">:</phrase> <phrase role="keyword">not</phrase> <phrase role="identifier">empty</phrase> <phrase role="special">--</phrase> <phrase role="identifier">i</phrase><phrase role="special">.</phrase><phrase role="identifier">e</phrase><phrase role="special">.,</phrase> <phrase role="identifier">count</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase>
|
|
<phrase role="identifier">ensure</phrase>
|
|
<phrase role="identifier">not_full</phrase><phrase role="special">:</phrase> <phrase role="keyword">not</phrase> <phrase role="identifier">full</phrase>
|
|
<phrase role="identifier">one_fewer_item</phrase><phrase role="special">:</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">old</phrase> <phrase role="identifier">count</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase>
|
|
|
|
<phrase role="identifier">end</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">end</phrase> <phrase role="special">--</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">interface</phrase> <phrase role="identifier">STACK4</phrase>
|
|
|
|
<phrase role="special">--</phrase> <phrase role="identifier">End</phrase><phrase role="special">.</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="string">"stack4.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">stack4</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">s</phrase><phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">remove</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</section>
|
|
<section id="boost_contract.examples.__meyer97___stack3__error_codes_instead_of_preconditions">
|
|
<title><link linkend="boost_contract.examples.__meyer97___stack3__error_codes_instead_of_preconditions">[Meyer97]
|
|
Stack3: Error codes instead of preconditions</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// File: stack3.cpp</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"stack4.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">optional</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Dispenser LIFO with max capacity using error codes.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">stack3</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">error</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count non-negative.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase> <phrase role="comment">// Count bounded.</phrase>
|
|
<phrase role="comment">// Empty if no element.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">enum</phrase> <phrase role="identifier">error_code</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">no_error</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">overflow_error</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">underflow_error</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">size_error</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="comment">/* Initialization */</phrase>
|
|
|
|
<phrase role="comment">// Create stack for max of n elems, if n < 0 set error (no preconditions).</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">stack3</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">n</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">default_value</phrase> <phrase role="special">=</phrase> <phrase role="identifier">T</phrase><phrase role="special">())</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">stack_</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">),</phrase> <phrase role="identifier">error_</phrase><phrase role="special">(</phrase><phrase role="identifier">no_error</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Error if impossible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><phrase role="identifier">n</phrase> <phrase role="special"><</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">error</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">size_error</phrase><phrase role="special">));</phrase>
|
|
<phrase role="comment">// No error if possible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">((</phrase><phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="special">!</phrase><phrase role="identifier">error</phrase><phrase role="special">());</phrase>
|
|
<phrase role="comment">// Created if no error.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">error</phrase><phrase role="special">())</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">n</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="identifier">stack_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">stack4</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>(</phrase><phrase role="identifier">n</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">size_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Access */</phrase>
|
|
|
|
<phrase role="comment">// Max number of stack elements.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Number of stack elements.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Top element if present, otherwise none and set error (no preconditions).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Error if impossible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">error</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">underflow_error</phrase><phrase role="special">));</phrase>
|
|
<phrase role="comment">// No error if possible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">!</phrase><phrase role="identifier">error</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">no_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&>(</phrase><phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">item</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">underflow_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&>();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Status Report */</phrase>
|
|
|
|
<phrase role="comment">// Error indicator set by various operations.</phrase>
|
|
<phrase role="identifier">error_code</phrase> <phrase role="identifier">error</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">error_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">full</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Element Change */</phrase>
|
|
|
|
<phrase role="comment">// Add x to top if capacity allows, otherwise set error (no preconditions).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase><phrase role="special">></phrase> <phrase role="identifier">old_full</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Error if impossible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_full</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">error</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">overflow_error</phrase><phrase role="special">));</phrase>
|
|
<phrase role="comment">// No error if possible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!*</phrase><phrase role="identifier">old_full</phrase> <phrase role="special">==</phrase> <phrase role="special">!</phrase><phrase role="identifier">error</phrase><phrase role="special">());</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">error</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase> <phrase role="comment">// If no error...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// ...not empty.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">x</phrase><phrase role="special">);</phrase> <phrase role="comment">// ...added to top.</phrase>
|
|
<phrase role="comment">// ...one more.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">full</phrase><phrase role="special">())</phrase> <phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">overflow_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">no_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Remove top element if possible, otherwise set error (no preconditions).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">remove</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase><phrase role="special">></phrase> <phrase role="identifier">old_empty</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Error if impossible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_empty</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">error</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase>
|
|
<phrase role="identifier">underflow_error</phrase><phrase role="special">));</phrase>
|
|
<phrase role="comment">// No error if possible.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!*</phrase><phrase role="identifier">old_empty</phrase> <phrase role="special">==</phrase> <phrase role="special">!</phrase><phrase role="identifier">error</phrase><phrase role="special">());</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">error</phrase><phrase role="special">())</phrase> <phrase role="special">{</phrase> <phrase role="comment">// If no error...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase> <phrase role="comment">// ...not full.</phrase>
|
|
<phrase role="comment">// ...one less.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">())</phrase> <phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">underflow_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">stack_</phrase><phrase role="special">.</phrase><phrase role="identifier">remove</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">error_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">no_error</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">stack4</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">stack_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">mutable</phrase> <phrase role="identifier">error_code</phrase> <phrase role="identifier">error_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">stack3</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">s</phrase><phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(*</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">remove</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___name_list__relaxed_subcontracts">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___name_list__relaxed_subcontracts">[Mitchell02]
|
|
Name List: Relaxed subcontracts</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// List of names.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">name_list</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative count.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create an empty list.</phrase>
|
|
<phrase role="identifier">name_list</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Empty list.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy list.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">name_list</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Basic Queries */</phrase>
|
|
|
|
<phrase role="comment">// Number of names in list.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">names_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Is name in list?</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">name</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// If empty, has not.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">result</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">names_</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">()</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">names_</phrase><phrase role="special">.</phrase><phrase role="identifier">cbegin</phrase><phrase role="special">(),</phrase>
|
|
<phrase role="identifier">names_</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">(),</phrase> <phrase role="identifier">name</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Add name to list, if name not already in list.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">name</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase><phrase role="special">></phrase> <phrase role="identifier">old_has_name</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">));</phrase> <phrase role="comment">// Not already in list.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!*</phrase><phrase role="identifier">old_has_name</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// If-guard allows to relax subcontracts.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">));</phrase> <phrase role="comment">// Name in list.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Inc.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">names_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase> <phrase role="identifier">names_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">relaxed_name_list</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">name_list</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Subcontracting.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">put</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Add name to list, or do nothing if name already in list (relaxed).</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">name</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase><phrase role="special">></phrase> <phrase role="identifier">old_has_name</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_put</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">relaxed_name_list</phrase><phrase role="special">::</phrase><phrase role="identifier">put</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">name</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Relax inherited preconditions.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">));</phrase> <phrase role="comment">// Already in list.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Inherited post. not checked given if-guard.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(*</phrase><phrase role="identifier">old_has_name</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Count unchanged if name already in list.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">))</phrase> <phrase role="identifier">name_list</phrase><phrase role="special">::</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">name</phrase><phrase role="special">);</phrase> <phrase role="comment">// Else, do nothing.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">js</phrase> <phrase role="special">=</phrase> <phrase role="string">"John Smith"</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">relaxed_name_list</phrase> <phrase role="identifier">rl</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">rl</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">rl</phrase><phrase role="special">.</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">));</phrase>
|
|
<phrase role="identifier">rl</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">);</phrase> <phrase role="comment">// OK, relaxed contracts allow calling this again (do nothing).</phrase>
|
|
|
|
<phrase role="identifier">name_list</phrase> <phrase role="identifier">nl</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">nl</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">nl</phrase><phrase role="special">.</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">));</phrase>
|
|
<phrase role="comment">// nl.put(js); // Error, contracts do not allow calling this again.</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___dictionary__key_value_map">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___dictionary__key_value_map">[Mitchell02]
|
|
Dictionary: Key-value map</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">utility</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">map</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">K</phrase><phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">dictionary</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative count.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create empty dictionary.</phrase>
|
|
<phrase role="identifier">dictionary</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy dictionary.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">dictionary</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Basic Queries */</phrase>
|
|
|
|
<phrase role="comment">// Number of key entries.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Has entry for key?</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">K</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">key</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Empty has no key.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">result</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">)</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Value for a given key.</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value_for</phrase><phrase role="special">(</phrase><phrase role="identifier">K</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">key</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">));</phrase> <phrase role="comment">// Has key.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Find != end because of precondition (no defensive programming).</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">)-></phrase><phrase role="identifier">second</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Add value of a given key.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">K</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">key</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">));</phrase> <phrase role="comment">// Has not key already.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count inc.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">));</phrase> <phrase role="comment">// Has key.</phrase>
|
|
<phrase role="comment">// Value set for key.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value_for</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_pair</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Remove value for given key.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">remove</phrase><phrase role="special">(</phrase><phrase role="identifier">K</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">key</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">));</phrase> <phrase role="comment">// Has key.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count dec.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">));</phrase> <phrase role="comment">// Has not key.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">key</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">map</phrase><phrase role="special"><</phrase><phrase role="identifier">K</phrase><phrase role="special">,</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">items_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">js</phrase> <phrase role="special">=</phrase> <phrase role="string">"John Smith"</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">dictionary</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase><phrase role="special">,</phrase> <phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">ages</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">ages</phrase><phrase role="special">.</phrase><phrase role="identifier">has</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">));</phrase>
|
|
|
|
<phrase role="identifier">ages</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">,</phrase> <phrase role="number">23</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">ages</phrase><phrase role="special">.</phrase><phrase role="identifier">value_for</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">23</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">ages</phrase><phrase role="special">.</phrase><phrase role="identifier">remove</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">ages</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___courier__subcontracting_and_static_class_invariants">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___courier__subcontracting_and_static_class_invariants">[Mitchell02]
|
|
Courier: Subcontracting and static class invariants</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">package</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">weight_kg</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">location</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">accepted_hour</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">delivered_hour</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">package</phrase><phrase role="special">(</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">_weight_kg</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">_location</phrase> <phrase role="special">=</phrase> <phrase role="string">""</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">_accepted_hour</phrase> <phrase role="special">=</phrase> <phrase role="number">0.0</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">_delivered_hour</phrase> <phrase role="special">=</phrase> <phrase role="number">0.0</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">weight_kg</phrase><phrase role="special">(</phrase><phrase role="identifier">_weight_kg</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">location</phrase><phrase role="special">(</phrase><phrase role="identifier">_location</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">accepted_hour</phrase><phrase role="special">(</phrase><phrase role="identifier">_accepted_hour</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">delivered_hour</phrase><phrase role="special">(</phrase><phrase role="identifier">_delivered_hour</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{}</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="comment">// Courier for package delivery.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">courier</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">courier</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Positive min. insurance.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">min_insurance_usd</phrase> <phrase role="special">>=</phrase> <phrase role="number">0.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Above min. insurance.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">insurance_cover_usd</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">min_insurance_usd</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">double</phrase> <phrase role="identifier">min_insurance_usd</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create courier with specified insurance value.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">courier</phrase><phrase role="special">(</phrase><phrase role="keyword">double</phrase> <phrase role="identifier">_insurance_cover_usd</phrase> <phrase role="special">=</phrase> <phrase role="identifier">min_insurance_usd</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">courier</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Positive insurance.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">_insurance_cover_usd</phrase> <phrase role="special">>=</phrase> <phrase role="number">0.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="identifier">insurance_cover_usd_</phrase><phrase role="special">(</phrase><phrase role="identifier">_insurance_cover_usd</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy courier.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">courier</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Queries */</phrase>
|
|
|
|
<phrase role="comment">// Return insurance cover.</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">insurance_cover_usd</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">insurance_cover_usd_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Deliver package to destination.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">deliver</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">package</phrase><phrase role="special">&</phrase> <phrase role="identifier">package_delivery</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">destination</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Within max weight of this delivery.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">weight_kg</phrase> <phrase role="special"><</phrase> <phrase role="number">5.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Within max delivery type.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">double</phrase><phrase role="special">(</phrase><phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">delivered_hour</phrase> <phrase role="special">-</phrase>
|
|
<phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">accepted_hour</phrase><phrase role="special">)</phrase> <phrase role="special"><=</phrase> <phrase role="number">3.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="comment">// Delivered at destination.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">location</phrase> <phrase role="special">==</phrase> <phrase role="identifier">destination</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">location</phrase> <phrase role="special">=</phrase> <phrase role="identifier">destination</phrase><phrase role="special">;</phrase>
|
|
<phrase role="comment">// Delivery takes 2.5 hours.</phrase>
|
|
<phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">delivered_hour</phrase> <phrase role="special">=</phrase> <phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">accepted_hour</phrase> <phrase role="special">+</phrase> <phrase role="number">2.5</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">insurance_cover_usd_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">courier</phrase><phrase role="special">::</phrase><phrase role="identifier">min_insurance_usd</phrase> <phrase role="special">=</phrase> <phrase role="number">10.0e+6</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Different courier for package delivery.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">different_courier</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">different_courier</phrase><phrase role="special">>,</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">courier</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Subcontracting.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="comment">// Better insurance amount.</phrase>
|
|
<phrase role="identifier">different_insurance_usd</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">courier</phrase><phrase role="special">::</phrase><phrase role="identifier">min_insurance_usd</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Above different insurance value.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">insurance_cover_usd</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="identifier">different_insurance_usd</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">deliver</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">double</phrase> <phrase role="identifier">different_insurance_usd</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create courier with specified insurance value.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">different_courier</phrase><phrase role="special">(</phrase>
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">insurance_cover_usd</phrase> <phrase role="special">=</phrase> <phrase role="identifier">different_insurance_usd</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">different_courier</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Positive insurance value.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">insurance_cover_usd</phrase> <phrase role="special">></phrase> <phrase role="number">0.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="identifier">courier</phrase><phrase role="special">(</phrase><phrase role="identifier">insurance_cover_usd</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy courier.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">different_courier</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">deliver</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">package</phrase><phrase role="special">&</phrase> <phrase role="identifier">package_delivery</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">destination</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_deliver</phrase>
|
|
<phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">different_courier</phrase><phrase role="special">::</phrase><phrase role="identifier">deliver</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">package_delivery</phrase><phrase role="special">,</phrase> <phrase role="identifier">destination</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Package can weight more (weaker precondition).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">weight_kg</phrase> <phrase role="special"><=</phrase> <phrase role="number">8.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Faster delivery (stronger postcondition).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">double</phrase><phrase role="special">(</phrase><phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">delivered_hour</phrase> <phrase role="special">-</phrase>
|
|
<phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">accepted_hour</phrase><phrase role="special">)</phrase> <phrase role="special"><=</phrase> <phrase role="number">2.0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="comment">// Inherited "delivery at destination" postcondition.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">location</phrase> <phrase role="special">=</phrase> <phrase role="identifier">destination</phrase><phrase role="special">;</phrase>
|
|
<phrase role="comment">// Delivery takes 0.5 hours.</phrase>
|
|
<phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">delivered_hour</phrase> <phrase role="special">=</phrase> <phrase role="identifier">package_delivery</phrase><phrase role="special">.</phrase><phrase role="identifier">accepted_hour</phrase> <phrase role="special">+</phrase> <phrase role="number">0.5</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">double</phrase> <phrase role="identifier">different_courier</phrase><phrase role="special">::</phrase><phrase role="identifier">different_insurance_usd</phrase> <phrase role="special">=</phrase> <phrase role="number">20.0e+6</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">package</phrase> <phrase role="identifier">cups</phrase><phrase role="special">(</phrase><phrase role="number">3.6</phrase><phrase role="special">,</phrase> <phrase role="string">"store"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">courier</phrase> <phrase role="identifier">c</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">c</phrase><phrase role="special">.</phrase><phrase role="identifier">deliver</phrase><phrase role="special">(</phrase><phrase role="identifier">cups</phrase><phrase role="special">,</phrase> <phrase role="string">"home"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">cups</phrase><phrase role="special">.</phrase><phrase role="identifier">location</phrase> <phrase role="special">==</phrase> <phrase role="string">"home"</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">package</phrase> <phrase role="identifier">desk</phrase><phrase role="special">(</phrase><phrase role="number">7.2</phrase><phrase role="special">,</phrase> <phrase role="string">"store"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">different_courier</phrase> <phrase role="identifier">dc</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">dc</phrase><phrase role="special">.</phrase><phrase role="identifier">deliver</phrase><phrase role="special">(</phrase><phrase role="identifier">desk</phrase><phrase role="special">,</phrase> <phrase role="string">"office"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">desk</phrase><phrase role="special">.</phrase><phrase role="identifier">location</phrase> <phrase role="special">==</phrase> <phrase role="string">"office"</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___stack__stack_like_container">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___stack__stack_like_container">[Mitchell02]
|
|
Stack: Stack-like container</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">optional</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">stack</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative count.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create empty stack.</phrase>
|
|
<phrase role="identifier">stack</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy stack.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">stack</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Basic Queries */</phrase>
|
|
|
|
<phrase role="comment">// Number of items.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Item at index in [1, count()] (as in Eiffel).</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">item_at</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Positive index.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">count</phrase><phrase role="special">());</phrase> <phrase role="comment">// Index within count.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">items_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Derived Queries */</phrase>
|
|
|
|
<phrase role="comment">// If no items.</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">is_empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Consistent with count.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Top item.</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase> <phrase role="comment">// Avoid extra construction of T.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Not empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Item on top.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">item_at</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">item_at</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Push item to the top.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">new_item</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count inc.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">new_item</phrase><phrase role="special">);</phrase> <phrase role="comment">// Item set.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">new_item</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Pop top item.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">remove</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Not empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count dec.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">pop_back</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">items_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">stack</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">s</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">item</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">remove</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">is_empty</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___simple_queue__queue_like_container_and_disable_old_value_copies_for_audit_assertions">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___simple_queue__queue_like_container_and_disable_old_value_copies_for_audit_assertions">[Mitchell02]
|
|
Simple Queue: Queue-like container and disable old value copies for audit assertions</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">optional</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">simple_queue</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">simple_queue</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative count.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create empty queue.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">simple_queue</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">a_capacity</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">simple_queue</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">a_capacity</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Positive capacity.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Capacity set.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">a_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">is_empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// Empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">reserve</phrase><phrase role="special">(</phrase><phrase role="identifier">a_capacity</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy queue.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">simple_queue</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Basic Queries */</phrase>
|
|
|
|
<phrase role="comment">// Items in queue (in their order).</phrase>
|
|
<phrase role="comment">// (Somewhat exposes implementation but allows to check more contracts.)</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">items</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">items_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Max number of items queue can hold.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Derived Queries */</phrase>
|
|
|
|
<phrase role="comment">// Number of items.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Return items count.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">().</phrase><phrase role="identifier">size</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Item at head.</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">head</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">is_empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Return item on top.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="identifier">items</phrase><phrase role="special">().</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// If queue contains no item.</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">is_empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Consistent with count.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// If queue has no room for another item.</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">is_full</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase> <phrase role="comment">// Consistent with size and capacity.</phrase>
|
|
<phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="keyword">int</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">().</phrase><phrase role="identifier">size</phrase><phrase role="special">())));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Remove head itme and shift all other items.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">remove</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Expensive all_equal postcond. and old_items copy might be skipped.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase> <phrase role="identifier">old_items</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDIITS</phrase>
|
|
<phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">())</phrase>
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// Else, leave old pointer null...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">is_empty</phrase><phrase role="special">());</phrase> <phrase role="comment">// Not empty.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count dec.</phrase>
|
|
<phrase role="comment">// ...following skipped #ifndef AUDITS.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_items</phrase><phrase role="special">)</phrase> <phrase role="identifier">all_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">(),</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_items</phrase><phrase role="special">,</phrase> <phrase role="comment">/* shifted = */</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">erase</phrase><phrase role="special">(</phrase><phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Add item to tail.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">item</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Expensive all_equal postcond. and old_items copy might be skipped.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase> <phrase role="identifier">old_items</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">())</phrase>
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// Else, leave old pointer null...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase> <phrase role="comment">// Room for add.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count inc.</phrase>
|
|
<phrase role="comment">// Second to last item.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">().</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="identifier">item</phrase><phrase role="special">);</phrase>
|
|
<phrase role="comment">// ...following skipped #ifndef AUDITS.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_items</phrase><phrase role="special">)</phrase> <phrase role="identifier">all_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">(),</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_items</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">items_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">item</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contract helper.</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">all_equal</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">left</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">right</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="identifier">offset</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Correct offset.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">+</phrase> <phrase role="identifier">offset</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">offset</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">left</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">i</phrase> <phrase role="special">-</phrase> <phrase role="identifier">offset</phrase><phrase role="special">)</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">right</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="identifier">i</phrase><phrase role="special">))</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">items_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">simple_queue</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">q</phrase><phrase role="special">(</phrase><phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">put</phrase><phrase role="special">(</phrase><phrase role="number">456</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">10</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">head</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">is_empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">is_full</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">items</phrase> <phrase role="special">=</phrase> <phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">items</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">items</phrase><phrase role="special">.</phrase><phrase role="identifier">at</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="number">456</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">remove</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">q</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___customer_manager__contracts_instead_of_defensive_programming">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___customer_manager__contracts_instead_of_defensive_programming">[Mitchell02]
|
|
Customer Manager: Contracts instead of defensive programming</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">string</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">map</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">utility</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Basic customer information.</phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">customer_info</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">customer_manager</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">identifier</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">identifier</phrase> <phrase role="identifier">id</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">customer_info</phrase><phrase role="special">(</phrase><phrase role="identifier">identifier</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">_id</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">id</phrase><phrase role="special">(</phrase><phrase role="identifier">_id</phrase><phrase role="special">),</phrase> <phrase role="identifier">name_</phrase><phrase role="special">(),</phrase> <phrase role="identifier">address_</phrase><phrase role="special">(),</phrase> <phrase role="identifier">birthday_</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">name_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">address_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">birthday_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="comment">// Manage customers.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">customer_manager</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase> <phrase role="comment">// Non-negative count.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="identifier">customer_manager</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">customer_manager</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Basic Queries */</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">customers_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">id_active</phrase><phrase role="special">(</phrase><phrase role="identifier">customer_info</phrase><phrase role="special">::</phrase><phrase role="identifier">identifier</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">customers_</phrase><phrase role="special">.</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">customers_</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Derived Queries */</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">name_for</phrase><phrase role="special">(</phrase><phrase role="identifier">customer_info</phrase><phrase role="special">::</phrase><phrase role="identifier">identifier</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">id_active</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase> <phrase role="comment">// Active.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Find != end because of preconditions (no defensive programming).</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">customers_</phrase><phrase role="special">.</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">)-></phrase><phrase role="identifier">second</phrase><phrase role="special">.</phrase><phrase role="identifier">name_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">add</phrase><phrase role="special">(</phrase><phrase role="identifier">customer_info</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">info</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_count</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Not already active.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">id_active</phrase><phrase role="special">(</phrase><phrase role="identifier">info</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_count</phrase> <phrase role="special">+</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Count inc.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">id_active</phrase><phrase role="special">(</phrase><phrase role="identifier">info</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase> <phrase role="comment">// Activated.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">customers_</phrase><phrase role="special">.</phrase><phrase role="identifier">insert</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">make_pair</phrase><phrase role="special">(</phrase><phrase role="identifier">info</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">,</phrase> <phrase role="identifier">customer</phrase><phrase role="special">(</phrase><phrase role="identifier">info</phrase><phrase role="special">)));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">set_name</phrase><phrase role="special">(</phrase><phrase role="identifier">customer_info</phrase><phrase role="special">::</phrase><phrase role="identifier">identifier</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">id</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">name</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">id_active</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase> <phrase role="comment">// Already active.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">name_for</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="identifier">name</phrase><phrase role="special">);</phrase> <phrase role="comment">// Name set.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Find != end because of precondition (no defensive programming).</phrase>
|
|
<phrase role="identifier">customers_</phrase><phrase role="special">.</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">id</phrase><phrase role="special">)-></phrase><phrase role="identifier">second</phrase><phrase role="special">.</phrase><phrase role="identifier">name_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">name</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">agent</phrase> <phrase role="special">{};</phrase> <phrase role="comment">// Customer agent.</phrase>
|
|
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">customer</phrase> <phrase role="special">:</phrase> <phrase role="identifier">customer_info</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">agent</phrase> <phrase role="identifier">managing_agent</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">string</phrase> <phrase role="identifier">last_contact</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">customer</phrase><phrase role="special">(</phrase><phrase role="identifier">customer_info</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">info</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">customer_info</phrase><phrase role="special">(</phrase><phrase role="identifier">info</phrase><phrase role="special">),</phrase>
|
|
<phrase role="identifier">managing_agent</phrase><phrase role="special">(),</phrase> <phrase role="identifier">last_contact</phrase><phrase role="special">()</phrase> <phrase role="special">{}</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">map</phrase><phrase role="special"><</phrase><phrase role="identifier">customer_info</phrase><phrase role="special">::</phrase><phrase role="identifier">identifier</phrase><phrase role="special">,</phrase> <phrase role="identifier">customer</phrase><phrase role="special">></phrase> <phrase role="identifier">customers_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">customer_manager</phrase> <phrase role="identifier">m</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">customer_info</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">js</phrase><phrase role="special">(</phrase><phrase role="string">"john_smith_123"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">m</phrase><phrase role="special">.</phrase><phrase role="identifier">add</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">m</phrase><phrase role="special">.</phrase><phrase role="identifier">set_name</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">,</phrase> <phrase role="string">"John Smith"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">m</phrase><phrase role="special">.</phrase><phrase role="identifier">name_for</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">)</phrase> <phrase role="special">==</phrase> <phrase role="string">"John Smith"</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">m</phrase><phrase role="special">.</phrase><phrase role="identifier">count</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">m</phrase><phrase role="special">.</phrase><phrase role="identifier">id_active</phrase><phrase role="special">(</phrase><phrase role="identifier">js</phrase><phrase role="special">.</phrase><phrase role="identifier">id</phrase><phrase role="special">));</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___observer__pure_virtual_functions">
|
|
<title><link linkend="boost_contract.examples.__mitchell02___observer__pure_virtual_functions">[Mitchell02]
|
|
Observer: Pure virtual functions</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">OBSERVER_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">OBSERVER_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Observer.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">observer</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">subject</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// No inv and no bases so contracts optional if no pre, post, and override.</phrase>
|
|
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="identifier">observer</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">observer</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// If up-to-date with related subject.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// Update this observer.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">update</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">observer</phrase><phrase role="special">::</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">observer</phrase><phrase role="special">::</phrase><phrase role="identifier">update</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">());</phrase> <phrase role="comment">// Up-to-date.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">SUBJECT_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">SUBJECT_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"observer.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">algorithm</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// Subject for observer design pattern.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">subject</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">all_observers_valid</phrase><phrase role="special">(</phrase><phrase role="identifier">observers</phrase><phrase role="special">()));</phrase> <phrase role="comment">// Valid.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Construct subject with no observer.</phrase>
|
|
<phrase role="identifier">subject</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariant.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy subject.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">subject</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariant.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Queries */</phrase>
|
|
|
|
<phrase role="comment">// If given object is attached.</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">attached</phrase><phrase role="special">(</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*</phrase> <phrase role="identifier">ob</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">ob</phrase><phrase role="special">);</phrase> <phrase role="comment">// Not null.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">find</phrase><phrase role="special">(</phrase><phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">cbegin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">(),</phrase> <phrase role="identifier">ob</phrase><phrase role="special">)</phrase> <phrase role="special">!=</phrase>
|
|
<phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Attach given object as an observer.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">attach</phrase><phrase role="special">(</phrase><phrase role="identifier">observer</phrase><phrase role="special">*</phrase> <phrase role="identifier">ob</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="special">></phrase> <phrase role="identifier">old_observers</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="identifier">old_observers</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">observers</phrase><phrase role="special">());</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">ob</phrase><phrase role="special">);</phrase> <phrase role="comment">// Not null.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">attached</phrase><phrase role="special">(</phrase><phrase role="identifier">ob</phrase><phrase role="special">));</phrase> <phrase role="comment">// Not already attached.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">attached</phrase><phrase role="special">(</phrase><phrase role="identifier">ob</phrase><phrase role="special">));</phrase> <phrase role="comment">// Attached.</phrase>
|
|
<phrase role="comment">// Others not changed (frame rule).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">other_observers_unchanged</phrase><phrase role="special">(</phrase>
|
|
<phrase role="special">*</phrase><phrase role="identifier">old_observers</phrase><phrase role="special">,</phrase> <phrase role="identifier">observers</phrase><phrase role="special">(),</phrase> <phrase role="identifier">ob</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(</phrase><phrase role="identifier">ob</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">protected</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Contracts could have been omitted for protected/private with no pre/post.</phrase>
|
|
|
|
<phrase role="comment">/* Queries */</phrase>
|
|
|
|
<phrase role="comment">// All observers attached to this subject.</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="identifier">observers</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="identifier">obs</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase><phrase role="special">*>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">cbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">obs</phrase><phrase role="special">.</phrase><phrase role="identifier">push_back</phrase><phrase role="special">(*</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">obs</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Update all attached observers.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">notify</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Protected members use `function` (no inv and no subcontracting).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// All updated.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">all_observers_updated</phrase><phrase role="special">(</phrase><phrase role="identifier">observers</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase><phrase role="special">*>::</phrase><phrase role="identifier">iterator</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">observers_</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Class invariants ensure no null pointers in observers but class</phrase>
|
|
<phrase role="comment">// invariants not checked for non-public functions so assert here.</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(*</phrase><phrase role="identifier">i</phrase><phrase role="special">);</phrase> <phrase role="comment">// Pointer not null (defensive programming).</phrase>
|
|
<phrase role="special">(*</phrase><phrase role="identifier">i</phrase><phrase role="special">)-></phrase><phrase role="identifier">update</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Contract Helpers */</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">all_observers_valid</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">obs</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">obs</phrase><phrase role="special">.</phrase><phrase role="identifier">cbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">obs</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!*</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">other_observers_unchanged</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">old_obs</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">new_obs</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*</phrase> <phrase role="identifier">ob</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Private members use `function` (no inv and no subcontracting).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">ob</phrase><phrase role="special">);</phrase> <phrase role="comment">// Not null.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="identifier">remaining</phrase> <phrase role="special">=</phrase> <phrase role="identifier">new_obs</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">remove</phrase><phrase role="special">(</phrase><phrase role="identifier">remaining</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">(),</phrase> <phrase role="identifier">remaining</phrase><phrase role="special">.</phrase><phrase role="identifier">end</phrase><phrase role="special">(),</phrase> <phrase role="identifier">ob</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">remaining_it</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">remaining</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">old_it</phrase> <phrase role="special">=</phrase> <phrase role="identifier">old_obs</phrase><phrase role="special">.</phrase><phrase role="identifier">begin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="keyword">while</phrase><phrase role="special">(</phrase><phrase role="identifier">remaining</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">()</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">remaining_it</phrase> <phrase role="special">&&</phrase> <phrase role="identifier">old_obs</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">()</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">old_it</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(*</phrase><phrase role="identifier">remaining_it</phrase> <phrase role="special">!=</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_it</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">++</phrase><phrase role="identifier">remaining_it</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">++</phrase><phrase role="identifier">old_it</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">all_observers_updated</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">obs</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase> <phrase role="keyword">const</phrase><phrase role="special">*>::</phrase><phrase role="identifier">const_iterator</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="identifier">obs</phrase><phrase role="special">.</phrase><phrase role="identifier">cbegin</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">i</phrase> <phrase role="special">!=</phrase> <phrase role="identifier">obs</phrase><phrase role="special">.</phrase><phrase role="identifier">cend</phrase><phrase role="special">();</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!*</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(!(*</phrase><phrase role="identifier">i</phrase><phrase role="special">)-></phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">())</phrase> <phrase role="keyword">return</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">observer</phrase><phrase role="special">*></phrase> <phrase role="identifier">observers_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="string">"observer/observer.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"observer/subject.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">test_state</phrase><phrase role="special">;</phrase> <phrase role="comment">// For testing only.</phrase>
|
|
|
|
<phrase role="comment">// Implement an actual subject.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">concrete_subject</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">subject</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Subcontracting.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">state</phrase><phrase role="special">;</phrase> <phrase role="comment">// Some state being observed.</phrase>
|
|
|
|
<phrase role="identifier">concrete_subject</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">state_</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">concrete_subject</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">set_state</phrase><phrase role="special">(</phrase><phrase role="identifier">state</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">new_state</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">state_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">new_state</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">state_</phrase> <phrase role="special">==</phrase> <phrase role="identifier">test_state</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">notify</phrase><phrase role="special">();</phrase> <phrase role="comment">// Notify all observers.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">state</phrase> <phrase role="identifier">get_state</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">state_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">state</phrase> <phrase role="identifier">state_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="comment">// Implement an actual observer.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">concrete_observer</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">observer</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Subcontracting.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">,</phrase> <phrase role="identifier">update</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Create concrete observer.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">concrete_observer</phrase><phrase role="special">(</phrase><phrase role="identifier">concrete_subject</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">subj</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">subject_</phrase><phrase role="special">(</phrase><phrase role="identifier">subj</phrase><phrase role="special">),</phrase> <phrase role="identifier">observed_state_</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">concrete_observer</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Implement base virtual functions.</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_up_to_date_with_subject</phrase>
|
|
<phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">concrete_observer</phrase><phrase role="special">::</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="comment">// For simplicity, assume always up-to-date.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">update</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_update</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">concrete_observer</phrase><phrase role="special">::</phrase><phrase role="identifier">update</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">observed_state_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">subject_</phrase><phrase role="special">.</phrase><phrase role="identifier">get_state</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">observed_state_</phrase> <phrase role="special">==</phrase> <phrase role="identifier">test_state</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">concrete_subject</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">subject_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">concrete_subject</phrase><phrase role="special">::</phrase><phrase role="identifier">state</phrase> <phrase role="identifier">observed_state_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">concrete_subject</phrase> <phrase role="identifier">subj</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">concrete_observer</phrase> <phrase role="identifier">ob</phrase><phrase role="special">(</phrase><phrase role="identifier">subj</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">subj</phrase><phrase role="special">.</phrase><phrase role="identifier">attach</phrase><phrase role="special">(&</phrase><phrase role="identifier">ob</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">subj</phrase><phrase role="special">.</phrase><phrase role="identifier">set_state</phrase><phrase role="special">(</phrase><phrase role="identifier">test_state</phrase> <phrase role="special">=</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">subj</phrase><phrase role="special">.</phrase><phrase role="identifier">set_state</phrase><phrase role="special">(</phrase><phrase role="identifier">test_state</phrase> <phrase role="special">=</phrase> <phrase role="number">456</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__mitchell02___counter__subcontracting">
|
|
<title><anchor id="Mitchell02_counter_anchor"/><link linkend="boost_contract.examples.__mitchell02___counter__subcontracting">[Mitchell02]
|
|
Counter: Subcontracting</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">PUSH_BUTTON_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">PUSH_BUTTON_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">push_button</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// No inv and no bases so contracts optional if no pre, post, and override.</phrase>
|
|
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create an enabled button.</phrase>
|
|
<phrase role="identifier">push_button</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">enabled_</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">enabled</phrase><phrase role="special">());</phrase> <phrase role="comment">// Enabled.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy button.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">push_button</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Queries */</phrase>
|
|
|
|
<phrase role="comment">// If button is enabled.</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">enabled</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">enabled_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Enable button.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">enable</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">enabled</phrase><phrase role="special">());</phrase> <phrase role="comment">// Enabled.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">enabled_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Disable button.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">disable</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">enabled</phrase><phrase role="special">());</phrase> <phrase role="comment">// Disabled.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">enabled_</phrase> <phrase role="special">=</phrase> <phrase role="keyword">false</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Invoke externally when button clicked.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">on_bn_clicked</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">enabled_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push_button</phrase><phrase role="special">::</phrase><phrase role="identifier">on_bn_clicked</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">enabled</phrase><phrase role="special">());</phrase> <phrase role="comment">// Enabled.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">DECREMENT_BUTTON_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">DECREMENT_BUTTON_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"push_button.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"counter.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"../observer/observer.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">noncopyable</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">decrement_button</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">push_button</phrase><phrase role="special">,</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">observer</phrase><phrase role="special">,</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">noncopyable</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">on_bn_clicked</phrase><phrase role="special">,</phrase> <phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">,</phrase> <phrase role="identifier">update</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">decrement_button</phrase><phrase role="special">(</phrase><phrase role="identifier">counter</phrase><phrase role="special">&</phrase> <phrase role="identifier">a_counter</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">counter_</phrase><phrase role="special">(</phrase><phrase role="identifier">a_counter</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Enable iff positive value.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">enabled</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">a_counter</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">attach</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy button.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">decrement_button</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">on_bn_clicked</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_value</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_on_bn_clicked</phrase>
|
|
<phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">decrement_button</phrase><phrase role="special">::</phrase><phrase role="identifier">on_bn_clicked</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Counter decremented.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_value</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">decrement</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_up_to_date_with_subject</phrase>
|
|
<phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">decrement_button</phrase><phrase role="special">::</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="comment">// For simplicity, assume always up-to-date.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">update</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_update</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">decrement_button</phrase><phrase role="special">::</phrase><phrase role="identifier">update</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Enabled iff positive value.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">enabled</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">></phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="identifier">disable</phrase><phrase role="special">();</phrase>
|
|
<phrase role="keyword">else</phrase> <phrase role="identifier">enable</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">counter</phrase><phrase role="special">&</phrase> <phrase role="identifier">counter_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">COUNTER_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">COUNTER_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"../observer/subject.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">counter</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">subject</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Construct counter with specified value.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">counter</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">a_value</phrase> <phrase role="special">=</phrase> <phrase role="number">10</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">value_</phrase><phrase role="special">(</phrase><phrase role="identifier">a_value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">a_value</phrase><phrase role="special">);</phrase> <phrase role="comment">// Value set.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy counter.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">counter</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Queries */</phrase>
|
|
|
|
<phrase role="comment">// Current counter value.</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">value_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="comment">// Decrement counter value.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">decrement</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">old_value</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_value</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase> <phrase role="comment">// Decrement.</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">--</phrase><phrase role="identifier">value_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">notify</phrase><phrase role="special">();</phrase> <phrase role="comment">// Notify all attached observers.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">value_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="string">"counter/counter.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"counter/decrement_button.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="string">"observer/observer.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">test_counter</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">view_of_counter</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">observer</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">,</phrase> <phrase role="identifier">update</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">/* Creation */</phrase>
|
|
|
|
<phrase role="comment">// Create view associated with given counter.</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">view_of_counter</phrase><phrase role="special">(</phrase><phrase role="identifier">counter</phrase><phrase role="special">&</phrase> <phrase role="identifier">a_counter</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase> <phrase role="identifier">counter_</phrase><phrase role="special">(</phrase><phrase role="identifier">a_counter</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">attach</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">test_counter</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Destroy view.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">view_of_counter</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Could have omitted contracts here (nothing to check).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">/* Commands */</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">bool</phrase> <phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_up_to_date_with_subject</phrase>
|
|
<phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">view_of_counter</phrase><phrase role="special">::</phrase><phrase role="identifier">up_to_date_with_subject</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">;</phrase> <phrase role="comment">// For simplicity, assume always up-to-date.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">update</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_update</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">view_of_counter</phrase><phrase role="special">::</phrase><phrase role="identifier">update</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">counter_</phrase><phrase role="special">.</phrase><phrase role="identifier">value</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">test_counter</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">counter</phrase><phrase role="special">&</phrase> <phrase role="identifier">counter_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">counter</phrase> <phrase role="identifier">cnt</phrase><phrase role="special">(</phrase><phrase role="identifier">test_counter</phrase> <phrase role="special">=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">view_of_counter</phrase> <phrase role="identifier">view</phrase><phrase role="special">(</phrase><phrase role="identifier">cnt</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">decrement_button</phrase> <phrase role="identifier">dec</phrase><phrase role="special">(</phrase><phrase role="identifier">cnt</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">dec</phrase><phrase role="special">.</phrase><phrase role="identifier">enabled</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="identifier">test_counter</phrase><phrase role="special">--;</phrase>
|
|
<phrase role="identifier">dec</phrase><phrase role="special">.</phrase><phrase role="identifier">on_bn_clicked</phrase><phrase role="special">();</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(!</phrase><phrase role="identifier">dec</phrase><phrase role="special">.</phrase><phrase role="identifier">enabled</phrase><phrase role="special">());</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__cline90___vector__comparison_with_a___proposal_syntax">
|
|
<title><anchor id="Cline90_vector_anchor"/><link linkend="boost_contract.examples.__cline90___vector__comparison_with_a___proposal_syntax">[Cline90]
|
|
Vector: Comparison with A++ proposal syntax</link></title>
|
|
<informaltable frame="all">
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
This Library
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
A++ Proposal (never actually implemented)
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">VECTOR_HPP_</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">VECTOR_HPP_</phrase>
|
|
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// NOTE: Incomplete contract assertions, addressing only `size`.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">10</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">vector</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="identifier">data_</phrase><phrase role="special">(</phrase><phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">count</phrase><phrase role="special">]),</phrase>
|
|
<phrase role="identifier">size_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">vector</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase> <phrase role="comment">// Non-negative result already checked by invariant.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">T</phrase><phrase role="special">*</phrase> <phrase role="identifier">slice</phrase> <phrase role="special">=</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">count</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">count</phrase> <phrase role="special">&&</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">slice</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">data_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">slice</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">size_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">*</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="preprocessor">#endif</phrase> <phrase role="comment">// #include guard</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="comment">// Extra spaces, newlines, etc. for visual alignment with this library code.</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vector</phrase> <phrase role="special">{</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">legal</phrase><phrase role="special">:</phrase> <phrase role="comment">// Class invariants (legal).</phrase>
|
|
<phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">10</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">data_</phrase><phrase role="special">(</phrase><phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">count</phrase><phrase role="special">]),</phrase>
|
|
<phrase role="identifier">size_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">vector</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase> <phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">*</phrase> <phrase role="identifier">slice</phrase> <phrase role="special">=</phrase> <phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">count</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">count</phrase> <phrase role="special">&&</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">slice</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">];</phrase>
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">data_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">slice</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">size_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase> <phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">T</phrase><phrase role="special">&</phrase> <phrase role="keyword">operator</phrase><phrase role="special">[](</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">index</phrase><phrase role="special">];</phrase> <phrase role="special">}</phrase>
|
|
|
|
|
|
|
|
|
|
<phrase role="identifier">axioms</phrase><phrase role="special">:</phrase> <phrase role="comment">// Preconditions (require) and postconditions (promise) for each func.</phrase>
|
|
<phrase role="special">[</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase> <phrase role="identifier">require</phrase> <phrase role="identifier">count</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">promise</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">]</phrase> <phrase role="identifier">vector</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">[</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase><phrase role="special">;</phrase> <phrase role="identifier">require</phrase> <phrase role="identifier">count</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">promise</phrase> <phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">]</phrase> <phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">[</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">;</phrase> <phrase role="identifier">require</phrase> <phrase role="identifier">index</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase> <phrase role="special">&&</phrase> <phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">()]</phrase> <phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)[</phrase><phrase role="identifier">x</phrase><phrase role="special">];</phrase> <phrase role="comment">// Op[].</phrase>
|
|
<phrase role="special">[</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">index</phrase><phrase role="special">;</phrase> <phrase role="identifier">require</phrase> <phrase role="identifier">index</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase> <phrase role="special">&&</phrase> <phrase role="identifier">index</phrase> <phrase role="special"><</phrase> <phrase role="identifier">size</phrase><phrase role="special">()]</phrase> <phrase role="special">(*</phrase><phrase role="keyword">this</phrase><phrase role="special">)[</phrase><phrase role="identifier">x</phrase><phrase role="special">]</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase> <phrase role="comment">// Op[].</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">*</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="comment">// End.</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="string">"vector.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">v</phrase> <phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">0</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="number">123</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">resize</phrase><phrase role="special">(</phrase><phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">[</phrase><phrase role="number">0</phrase><phrase role="special">]</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">2</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</entry>
|
|
<entry>
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</section>
|
|
<section id="boost_contract.examples.__cline90___stack__stack_like_container">
|
|
<title><link linkend="boost_contract.examples.__cline90___stack__stack_like_container">[Cline90]
|
|
Stack: Stack-like container</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// NOTE: Incomplete contract assertions, addressing only `empty` and `full`.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">stack</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">stack</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">stack</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="identifier">data_</phrase><phrase role="special">(</phrase><phrase role="keyword">new</phrase> <phrase role="identifier">T</phrase><phrase role="special">[</phrase><phrase role="identifier">capacity</phrase><phrase role="special">]),</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">),</phrase> <phrase role="identifier">size_</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">full</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">capacity</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">for</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">i</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase> <phrase role="identifier">i</phrase> <phrase role="special"><</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase> <phrase role="special">++</phrase><phrase role="identifier">i</phrase><phrase role="special">)</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">i</phrase><phrase role="special">]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">T</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">stack</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">delete</phrase><phrase role="special">[]</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">size_</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">full</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">size_</phrase> <phrase role="special">==</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">data_</phrase><phrase role="special">[</phrase><phrase role="identifier">size_</phrase><phrase role="special">++]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">T</phrase> <phrase role="identifier">pop</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">data_</phrase><phrase role="special">[--</phrase><phrase role="identifier">size_</phrase><phrase role="special">];</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">T</phrase><phrase role="special">*</phrase> <phrase role="identifier">data_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">capacity_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">size_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">stack</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">s</phrase><phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__cline90___vector_stack__subcontracting">
|
|
<title><link linkend="boost_contract.examples.__cline90___vector_stack__subcontracting">[Cline90]
|
|
Vector-Stack: Subcontracting</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="string">"vector.hpp"</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">optional</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="comment">// NOTE: Incomplete contract assertions, addressing only `empty` and `full`.</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">abstract_stack</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">abstract_stack</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// AXIOM as empty() cannot actually be checked here to avoid</phrase>
|
|
<phrase role="comment">// calling pure virtual function length() during construction).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">abstract_stack</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">full</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">empty</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">));</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">length</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">item</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">clear</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">length</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">capacity</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">item</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*</phrase><phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_item</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">item</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(!</phrase><phrase role="identifier">full</phrase><phrase role="special">());</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(*</phrase><phrase role="identifier">result</phrase> <phrase role="special">==</phrase> <phrase role="special">*</phrase><phrase role="identifier">old_item</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*</phrase><phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">>::</phrase><phrase role="identifier">clear</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">empty</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="keyword">false</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">vstack</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase> <phrase role="special">\</phrase>
|
|
<phrase role="identifier">vstack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="special">>,</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">abstract_stack</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special"><</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">());</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">,</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">,</phrase> <phrase role="identifier">item</phrase><phrase role="special">,</phrase> <phrase role="identifier">push</phrase><phrase role="special">,</phrase> <phrase role="identifier">pop</phrase><phrase role="special">,</phrase> <phrase role="identifier">clear</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">vstack</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">count</phrase> <phrase role="special">=</phrase> <phrase role="number">10</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">vstack</phrase><phrase role="special">>([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase> <phrase role="special">>=</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}),</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">(</phrase><phrase role="identifier">count</phrase><phrase role="special">),</phrase> <phrase role="comment">// OK, executed after precondition so count >= 0.</phrase>
|
|
<phrase role="identifier">len_</phrase><phrase role="special">(</phrase><phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">0</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">count</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">vstack</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Inherited from abstract_stack.</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">length</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_length</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vstack</phrase><phrase role="special">::</phrase><phrase role="identifier">length</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">len_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">capacity</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_capacity</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vstack</phrase><phrase role="special">::</phrase><phrase role="identifier">capacity</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">.</phrase><phrase role="identifier">size</phrase><phrase role="special">();</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">item</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="keyword">const</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_item</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vstack</phrase><phrase role="special">::</phrase><phrase role="identifier">item</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">[</phrase><phrase role="identifier">len_</phrase> <phrase role="special">-</phrase> <phrase role="number">1</phrase><phrase role="special">]);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_push</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vstack</phrase><phrase role="special">::</phrase><phrase role="identifier">push</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">value</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">vect_</phrase><phrase role="special">[</phrase><phrase role="identifier">len_</phrase><phrase role="special">++]</phrase> <phrase role="special">=</phrase> <phrase role="identifier">value</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">pop</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">optional</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&></phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_pop</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vstack</phrase><phrase role="special">::</phrase><phrase role="identifier">pop</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="special">*(</phrase><phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="identifier">vect_</phrase><phrase role="special">[--</phrase><phrase role="identifier">len_</phrase><phrase role="special">]);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">clear</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="comment">/* override */</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_clear</phrase><phrase role="special">>(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">vstack</phrase><phrase role="special">::</phrase><phrase role="identifier">clear</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">len_</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">vector</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">vect_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">len_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">vstack</phrase><phrase role="special"><</phrase><phrase role="keyword">int</phrase><phrase role="special">></phrase> <phrase role="identifier">s</phrase><phrase role="special">(</phrase><phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">capacity</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">3</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">push</phrase><phrase role="special">(</phrase><phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">length</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">s</phrase><phrase role="special">.</phrase><phrase role="identifier">pop</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">123</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.examples.__cline90___calendar__a_very_simple_calendar">
|
|
<title><link linkend="boost_contract.examples.__cline90___calendar__a_very_simple_calendar">[Cline90]
|
|
Calendar: A very simple calendar</link></title>
|
|
<para>
|
|
<programlisting xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">cassert</phrase><phrase role="special">></phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">calendar</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">date</phrase><phrase role="special">()</phrase> <phrase role="special">>=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">date</phrase><phrase role="special">()</phrase> <phrase role="special"><=</phrase> <phrase role="identifier">days_in</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase><phrase role="special">()));</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">calendar</phrase><phrase role="special">()</phrase> <phrase role="special">:</phrase> <phrase role="identifier">month_</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase><phrase role="special">),</phrase> <phrase role="identifier">date_</phrase><phrase role="special">(</phrase><phrase role="number">31</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">date</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">31</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="special">~</phrase><phrase role="identifier">calendar</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">month_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">date</phrase><phrase role="special">()</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// Check invariants.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">);</phrase>
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">date_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">reset</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">new_month</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">new_month</phrase> <phrase role="special">>=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">new_month</phrase> <phrase role="special"><=</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="identifier">new_month</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">month_</phrase> <phrase role="special">=</phrase> <phrase role="identifier">new_month</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">int</phrase> <phrase role="identifier">days_in</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">month</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">()</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase> <phrase role="special">>=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">month</phrase> <phrase role="special"><=</phrase> <phrase role="number">12</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">([&]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special">>=</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">result</phrase> <phrase role="special"><=</phrase> <phrase role="number">31</phrase><phrase role="special">);</phrase>
|
|
<phrase role="special">})</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="identifier">result</phrase> <phrase role="special">=</phrase> <phrase role="number">31</phrase><phrase role="special">;</phrase> <phrase role="comment">// For simplicity, assume all months have 31 days.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">month_</phrase><phrase role="special">,</phrase> <phrase role="identifier">date_</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">};</phrase>
|
|
|
|
<phrase role="keyword">int</phrase> <phrase role="identifier">main</phrase><phrase role="special">()</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">calendar</phrase> <phrase role="identifier">cal</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">cal</phrase><phrase role="special">.</phrase><phrase role="identifier">date</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">31</phrase><phrase role="special">);</phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">cal</phrase><phrase role="special">.</phrase><phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">1</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="identifier">cal</phrase><phrase role="special">.</phrase><phrase role="identifier">reset</phrase><phrase role="special">(</phrase><phrase role="number">8</phrase><phrase role="special">);</phrase> <phrase role="comment">// Set month </phrase>
|
|
<phrase role="identifier">assert</phrase><phrase role="special">(</phrase><phrase role="identifier">cal</phrase><phrase role="special">.</phrase><phrase role="identifier">month</phrase><phrase role="special">()</phrase> <phrase role="special">==</phrase> <phrase role="number">8</phrase><phrase role="special">);</phrase>
|
|
|
|
<phrase role="keyword">return</phrase> <phrase role="number">0</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting>
|
|
</para>
|
|
</section>
|
|
</section>
|
|
<section id="reference"><title>Reference</title><section id="header.boost.contract_hpp"><title>Header <<ulink url="../../../../boost/contract.hpp">boost/contract.hpp</ulink>></title><para>Include all header files required by this library at once (for convenience). </para><para>All header files <computeroutput>boost/contract/*.hpp</computeroutput> are independent from one another and can be included one-by-one to reduce the amount of code to compile from this library in user code (but this was measured to not make an appreciable difference in compile-time so <computeroutput>boost/contract.hpp</computeroutput> can be included directly in most cases). Instead the headers <computeroutput>boost/contract/core/*.hpp</computeroutput> are not independent from other library headers and they are automatically included by the <computeroutput>boost/contract/*.hpp</computeroutput> headers (so the <computeroutput>boost/contract/core/*.hpp</computeroutput> headers are usually not directly included by programmers).</para><para>All files under the <computeroutput>boost/contract/detail/</computeroutput> directory, names within the <computeroutput>boost::contract::detail</computeroutput> namespace, names prefixed by <computeroutput>boost_contract_detail</computeroutput>... and <computeroutput>BOOST_CONTRACT_DETAIL</computeroutput>... (in any namesapce, including user's code) are reserved for internal use of this library and should never be used directly by programmers.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.getting_started"> Getting Started</link> </para>
|
|
</para>
|
|
</para>
|
|
</section>
|
|
<section id="header.boost.contract.assert_hpp"><title>Header <<ulink url="../../../../boost/contract/assert.hpp">boost/contract/assert.hpp</ulink>></title><para>Assert contract conditions. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link>(cond)
|
|
<link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link>(cond)
|
|
<link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link>(cond)</synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_ASSERT"><refmeta><refentrytitle>Macro BOOST_CONTRACT_ASSERT</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_ASSERT</refname><refpurpose>Preferred way to assert contract conditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.assert_hpp">boost/contract/assert.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_ASSERT(cond)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Any exception thrown from within a contract (preconditions, postconditions, exception guarantees, old value copies at body, class invariants, etc.) is interpreted by this library as a contract failure. Therefore, users can program contract assertions manually throwing an exception when an asserted condition is checked to be <computeroutput>false</computeroutput> (this library will then call the appropriate contract failure handler <computeroutput><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>, etc.). However, it is preferred to use this macro because it expands to code that throws <computeroutput><link linkend="boost.contract.assertion_failure">boost::contract::assertion_failure</link></computeroutput> with the correct assertion file name (using <computeroutput>__FILE__</computeroutput>), line number (using <computeroutput>__LINE__</computeroutput>), and asserted condition code so to produce informative error messages (C++11 <computeroutput>__func__</computeroutput> is not used here because in most cases it will simply expand to the internal compiler name of the lambda function used to program the contract conditions adding no specificity to the error message).</para><para> <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput> are the three assertion levels predefined by this library.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exceptions Guarantees</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>cond</computeroutput></term><listitem><para>Boolean contract condition to check. (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_ASSERT((cond))</computeroutput> will always work.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028040814800"><refmeta><refentrytitle>Macro BOOST_CONTRACT_ASSERT_AUDIT</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_ASSERT_AUDIT</refname><refpurpose>Preferred way to assert contract conditions that are computationally expensive, at least compared to the computational cost of executing the function body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.assert_hpp">boost/contract/assert.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_ASSERT_AUDIT(cond)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The asserted condition will always be compiled and validated syntactically, but it will not be checked at run-time unless <computeroutput><link linkend="BOOST_CONTRACT_AUDITS">BOOST_CONTRACT_AUDITS</link></computeroutput> is defined (undefined by default). This macro is defined by code equivalent to:</para><para><programlisting><phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> \
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#else</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> \
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para> <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput> are the three assertion levels predefined by this library. If there is a need, programmers are free to implement their own assertion levels defining macros similar to the one above.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_levels"> Assertion Levels</link>, <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>cond</computeroutput></term><listitem><para>Boolean contract condition to check. (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_ASSERT_AUDIT((cond))</computeroutput> will always work.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028067443360"><refmeta><refentrytitle>Macro BOOST_CONTRACT_ASSERT_AXIOM</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_ASSERT_AXIOM</refname><refpurpose>Preferred way to document in the code contract conditions that are computationally prohibitive, at least compared to the computational cost of executing the function body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.assert_hpp">boost/contract/assert.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_ASSERT_AXIOM(cond)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The asserted condition will always be compiled and validated syntactically, but it will never be checked at run-time. This macro is defined by code equivalent to:</para><para><programlisting><phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_ASSERT_AXIOM</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> \
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para> <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CO_idm45028067443360">BOOST_CONTRACT_ASSERT_AXIOM</link></computeroutput> are the three assertion levels predefined by this library. If there is a need, programmers are free to implement their own assertion levels defining macros similar to the one above.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_levels"> Assertion Levels</link>, <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>cond</computeroutput></term><listitem><para>Boolean contract condition to check. (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_ASSERT_AXIOM((cond))</computeroutput> will always work.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.base_types_hpp"><title>Header <<ulink url="../../../../boost/contract/base_types.hpp">boost/contract/base_types.hpp</ulink>></title><para>Specify inheritance form base classes (for subcontracting). </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link>(...)</synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_BASE_TYPES"><refmeta><refentrytitle>Macro BOOST_CONTRACT_BASE_TYPES</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_BASE_TYPES</refname><refpurpose>Used to program the <computeroutput>typedef</computeroutput> that lists the bases of a derived class. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.base_types_hpp">boost/contract/base_types.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_BASE_TYPES(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>In order to support subcontracting, a derived class that specifies contracts for one or more overriding public functions must declare a <computeroutput>typedef</computeroutput> named <computeroutput>base_types</computeroutput> (or <computeroutput><link linkend="BOOST_CO_idm45028039730032">BOOST_CONTRACT_BASES_TYPEDEF</link></computeroutput>) using this macro:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">protected</phrase> <phrase role="keyword">virtual</phrase> <phrase role="identifier">w1</phrase><phrase role="special">,</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">w2</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">:</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>This <computeroutput>typedef</computeroutput> must be <computeroutput>public</computeroutput> unless <computeroutput><link linkend="boost.contract.access">boost::contract::access</link></computeroutput> is used.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.base_classes__subcontracting_"> Base Classes</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>...</computeroutput></term><listitem><para>Comma separated list of base classes. Each base must explicitly specify its access specifier <computeroutput>public</computeroutput>, <computeroutput>protected</computeroutput>, or <computeroutput>private</computeroutput>, and also <computeroutput>virtual</computeroutput> when present (this not always required in C++ instead). There is a limit of about 20 maximum bases that can be listed (because of similar limits in Boost.MPL internally used by this library). This is a variadic macro parameter, on compilers that do not support variadic macros, the <computeroutput>typedef</computeroutput> for base classes can be programmed manually without using this macro (see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link>). </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.call_if_hpp"><title>Header <<ulink url="../../../../boost/contract/call_if.hpp">boost/contract/call_if.hpp</ulink>></title><para>Statically disable compilation and execution of functor calls. </para><para><note><para>These facilities allow to emulate C++17 <computeroutput>if constexpr</computeroutput> statements when used together with functor templates (and C++14 generic lambdas). Therefore, they are not useful on C++17 compilers where <computeroutput> if constexpr</computeroutput> can be directly used instead. </para>
|
|
</note>
|
|
</para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ThenResult <phrase role="special">=</phrase> <phrase role="identifier">internal_type</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040096080">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">false</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">internal_type</phrase><phrase role="special">></phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase> <phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040056608">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">internal_type</phrase><phrase role="special">></phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ThenResult<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040040480">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">ThenResult</phrase><phrase role="special">></phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase> <phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040000352">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase> <phrase role="special">></phrase> <link linkend="boost.contract.call_if_c"><phrase role="identifier">call_if_c</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase> <phrase role="special">></phrase> <link linkend="boost.contract.call_if"><phrase role="identifier">call_if</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase> <phrase role="keyword">bool</phrase> <link linkend="boost.contract.condition_if_c"><phrase role="identifier">condition_if_c</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <link linkend="boost.contract.condition_if"><phrase role="identifier">condition_if</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if_statement"><refmeta><refentrytitle>Struct template call_if_statement</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if_statement</refname><refpurpose>Select compilation and execution of functor template calls using a static boolean predicate (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ThenResult <phrase role="special">=</phrase> <phrase role="identifier">internal_type</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if_statement">call_if_statement</link> <phrase role="special">{</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This class template has no members because it is never used directly, it is only used via its specializations. Usually this class template is instantiated only via the return value of <computeroutput><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput> and <computeroutput><link linkend="boost.contract.call_if_c">boost::contract::call_if_c</link></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">bool</phrase> Pred</literallayout></para><para><para>Static boolean predicate that selects which functor template call to compile and execute. </para></para></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> Then</literallayout></para><para><para>Type of the functor template to call if the static predicate <computeroutput>Pred</computeroutput> is <computeroutput>true</computeroutput>. </para></para></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> ThenResult <phrase role="special">=</phrase> <phrase role="identifier">internal_type</phrase></literallayout></para><para><para>Return type of then-branch functor template call (this is usually automatically deduced by this library so it is never explicitly specified by the user, and that is why it is often marked as <computeroutput>internal_type</computeroutput> in this documentation). </para></para></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if__idm45028040096080"><refmeta><refentrytitle>Struct template call_if_statement<false, Then, internal_type></refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if_statement<false, Then, internal_type></refname><refpurpose>Template specialization to handle static predicates that are <computeroutput>false</computeroutput> (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040096080">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">false</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">internal_type</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.call_if__idm45028040096080construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028040060640-bb"><phrase role="identifier">call_if_statement</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028040087248-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Else<phrase role="special">></phrase> <phrase role="identifier">result_of</phrase><phrase role="special"><</phrase> <phrase role="identifier">Else</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">></phrase><phrase role="special">::</phrase><phrase role="identifier">type</phrase> <link linkend="idm45028040086688-bb"><phrase role="identifier">else_</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Else</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">ElseIfPred</phrase><phrase role="special">,</phrase> <phrase role="identifier">ElseIfThen</phrase> <phrase role="special">></phrase> <link linkend="idm45028040078032-bb"><phrase role="identifier">else_if_c</phrase></link><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">ElseIfPred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">ElseIfThen</phrase> <phrase role="special">></phrase>
|
|
<link linkend="idm45028040069280-bb"><phrase role="identifier">else_if</phrase></link><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This template specialization handles all else-branch functor template calls (whether they return void or not). Usually this class template is instantiated only via the return value of <computeroutput><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput> and <computeroutput><link linkend="boost.contract.call_if_c">boost::contract::call_if_c</link></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> Then</literallayout></para><para><para>Type of functor template to call when the static predicate is <computeroutput>true</computeroutput> (never the case for this template specialization). </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.call_if__idm45028040096080construct-copy-destruct"/><computeroutput>call_if_statement</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028040060640-bb"/><phrase role="identifier">call_if_statement</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object with the then-branch functor template. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Then-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>false</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the return type of all other functor template calls specified for this call-if object. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028040087248-bb"/><computeroutput>call_if_statement</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Else<phrase role="special">></phrase> <phrase role="identifier">result_of</phrase><phrase role="special"><</phrase> <phrase role="identifier">Else</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">></phrase><phrase role="special">::</phrase><phrase role="identifier">type</phrase> <anchor id="idm45028040086688-bb"/><phrase role="identifier">else_</phrase><phrase role="special">(</phrase><phrase role="identifier">Else</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify the else-branch functor template. <para><note><para>The <computeroutput>result_of<Else()>::type</computeroutput> expression needs be evaluated only when the static predicate is already checked to be <computeroutput>false</computeroutput> (because <computeroutput>Else()</computeroutput> is required to compile only in that case). Thus, this result-of expression is evaluated lazily and only in instantiations of this template specialization.</para>
|
|
</note>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is actually compiled and executed for this template specialization (because the if-statement static predicate is <computeroutput>false</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the return type of all other functor template calls specified for this call-if object.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A copy of the value returned by the call to the else-branch functor template <computeroutput>f()</computeroutput>. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">ElseIfPred</phrase><phrase role="special">,</phrase> <phrase role="identifier">ElseIfThen</phrase> <phrase role="special">></phrase> <anchor id="idm45028040078032-bb"/><phrase role="identifier">else_if_c</phrase><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify an else-if-branch functor template (using a static boolean predicate). <para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-if-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is actually compiled and executed if and only if <computeroutput>ElseIfPred</computeroutput> is <computeroutput>true</computeroutput> (because the if-statement static predicate is already <computeroutput>false</computeroutput> for this template specialization). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the return type of all other functor template calls specified for this call-if object.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>ElseIfPred</computeroutput></term><listitem><para>Static boolean predicate selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so the else statement and additional else-if statements can be specified if needed. Eventually, this will be the return value of the one functor template call being compiled and executed. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">ElseIfPred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">ElseIfThen</phrase> <phrase role="special">></phrase>
|
|
<anchor id="idm45028040069280-bb"/><phrase role="identifier">else_if</phrase><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify an else-if-branch functor template (using a nullary boolen meta-function). <para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-if-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is actually compiled and executed if and only if <computeroutput>ElseIfPred::value</computeroutput> is <computeroutput>true</computeroutput> (because the if-statement static predicate is already <computeroutput>false</computeroutput> for this template specialization). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the return type of all other functor template calls specified for this call-if object.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>ElseIfPred</computeroutput></term><listitem><para>Nullary boolean meta-function selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so the else statement and additional else-if statements can be specified if needed. Eventually, this will be the return value of the one functor template call being compiled and executed. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if__idm45028040056608"><refmeta><refentrytitle>Struct template call_if_statement<true, Then, internal_type></refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if_statement<true, Then, internal_type></refname><refpurpose>Template specialization to dispatch between then-branch functor template calls that return void and the ones that return non-void (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040056608">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">internal_type</phrase><phrase role="special">></phrase> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> boost::contract::call_if_statement< true, Then, result_of< Then()>::type >
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.call_if__idm45028040056608construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028040044496-bb"><phrase role="identifier">call_if_statement</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The base class is a call-if statement so the else and else-if statements can be specified if needed. Usually this class template is instantiated only via the return value of <computeroutput><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput> and <computeroutput><link linkend="boost.contract.call_if_c">boost::contract::call_if_c</link></computeroutput>.</para><para><note><para>The <computeroutput>result_of<Then()>::type</computeroutput> expression needs be evaluated only when the static predicate is already checked to be <computeroutput>true</computeroutput> (because <computeroutput>Then()</computeroutput> is required to compile only in that case). Thus, this template specialization introduces an extra level of indirection necessary for proper lazy evaluation of this result-of expression.</para>
|
|
</note>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> Then</literallayout></para><para><para>Type of functor template to call when the static predicate is <computeroutput>true</computeroutput> (as it is for this template specialization). </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.call_if__idm45028040056608construct-copy-destruct"/><computeroutput>call_if_statement</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028040044496-bb"/><phrase role="identifier">call_if_statement</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object with the then-branch functor template. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Then-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is compiled and called for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the return type of all other functor template calls specified for this call-if object. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if__idm45028040040480"><refmeta><refentrytitle>Struct template call_if_statement<true, Then, ThenResult></refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if_statement<true, Then, ThenResult></refname><refpurpose>Template specialization to handle static predicates that are <computeroutput>true</computeroutput> for then-branch functor template calls that do not return void (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ThenResult<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040040480">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">ThenResult</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.call_if__idm45028040040480construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028040004752-bb"><phrase role="identifier">call_if_statement</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028040030672-bb">public member functions</link></phrase>
|
|
<link linkend="idm45028040030112-bb"><phrase role="keyword">operator</phrase> <phrase role="identifier">ThenResult</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Else<phrase role="special">></phrase> <phrase role="identifier">ThenResult</phrase> <link linkend="idm45028040028608-bb"><phrase role="identifier">else_</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Else</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">ThenResult</phrase> <phrase role="special">></phrase>
|
|
<link linkend="idm45028040022112-bb"><phrase role="identifier">else_if_c</phrase></link><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">ThenResult</phrase> <phrase role="special">></phrase>
|
|
<link linkend="idm45028040013376-bb"><phrase role="identifier">else_if</phrase></link><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Usually this class template is instantiated only via the return value of <computeroutput><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput> and <computeroutput><link linkend="boost.contract.call_if_c">boost::contract::call_if_c</link></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> Then</literallayout></para><para><para>Type of functor template to call when the static predicate is <computeroutput>true</computeroutput> (as it is for this template specialization). </para></para></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> ThenResult</literallayout></para><para><para>Non-void return type of the then-branch functor template call. </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.call_if__idm45028040040480construct-copy-destruct"/><computeroutput>call_if_statement</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028040004752-bb"/><phrase role="identifier">call_if_statement</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object with the then-branch functor template. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Then-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is actually compiled and executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the <computeroutput>ThenResult</computeroutput> type. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028040030672-bb"/><computeroutput>call_if_statement</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028040030112-bb"/><phrase role="keyword">operator</phrase> <phrase role="identifier">ThenResult</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>This implicit type conversion returns a copy of the value returned by the call to the then-branch functor template. </listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Else<phrase role="special">></phrase> <phrase role="identifier">ThenResult</phrase> <anchor id="idm45028040028608-bb"/><phrase role="identifier">else_</phrase><phrase role="special">(</phrase><phrase role="identifier">Else</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify the else-branch functor template. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the <computeroutput>ThenResult</computeroutput> type.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A copy of the value returned by the call to the then-branch functor template (because the else-branch functor template call is not executed). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">ThenResult</phrase> <phrase role="special">></phrase>
|
|
<anchor id="idm45028040022112-bb"/><phrase role="identifier">else_if_c</phrase><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify an else-if-branch functor template (using a static boolean predicate). <para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-if-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the <computeroutput>ThenResult</computeroutput> type.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>ElseIfPred</computeroutput></term><listitem><para>Static boolean predicate selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so the else statement and additional else-if statements can be specified if needed. Eventually, it will be the return value of the then-branch functor template call for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="identifier">ThenResult</phrase> <phrase role="special">></phrase>
|
|
<anchor id="idm45028040013376-bb"/><phrase role="identifier">else_if</phrase><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify an else-if-branch functor template (using a nullary boolean meta-function). <para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-if-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be the same as (or implicitly convertible to) the <computeroutput>ThenResult</computeroutput> type.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>ElseIfPred</computeroutput></term><listitem><para>Nullary boolean meta-function selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so the else statement and additional else-if statements can be specified if needed. Eventually, it will be the return value of the then-branch functor template call for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if__idm45028040000352"><refmeta><refentrytitle>Struct template call_if_statement<true, Then, void></refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if_statement<true, Then, void></refname><refpurpose>Template specialization to handle static predicates that are <computeroutput>true</computeroutput> for then-branch functor template calls that return void (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.call_if__idm45028040000352">call_if_statement</link><phrase role="special"><</phrase><phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.call_if__idm45028040000352construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028039968784-bb"><phrase role="identifier">call_if_statement</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Then</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039991568-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Else<phrase role="special">></phrase> <phrase role="keyword">void</phrase> <link linkend="idm45028039991008-bb"><phrase role="identifier">else_</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Else</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase> <phrase role="special">></phrase> <link linkend="idm45028039985184-bb"><phrase role="identifier">else_if_c</phrase></link><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase> <phrase role="special">></phrase> <link linkend="idm45028039976928-bb"><phrase role="identifier">else_if</phrase></link><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Usually this class template is instantiated only via the return value of <computeroutput><link linkend="boost.contract.call_if">boost::contract::call_if</link></computeroutput> and <computeroutput><link linkend="boost.contract.call_if_c">boost::contract::call_if_c</link></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> Then</literallayout></para><para><para>Type of functor template to call when the static predicate if <computeroutput>true</computeroutput> (as it is for this template specialization). </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.call_if__idm45028040000352construct-copy-destruct"/><computeroutput>call_if_statement</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028039968784-bb"/><phrase role="identifier">call_if_statement</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object with the then-branch functor template. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Then-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is actually compiled and executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be <computeroutput>void</computeroutput> for this template specialization (because the then-branch functor template calls return void). </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039991568-bb"/><computeroutput>call_if_statement</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Else<phrase role="special">></phrase> <phrase role="keyword">void</phrase> <anchor id="idm45028039991008-bb"/><phrase role="identifier">else_</phrase><phrase role="special">(</phrase><phrase role="identifier">Else</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify the else-branch functor template. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be <computeroutput>void</computeroutput> for this template specialization (because the then-branch functor template calls return void). </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase> <phrase role="special">></phrase> <anchor id="idm45028039985184-bb"/><phrase role="identifier">else_if_c</phrase><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify an else-if-branch functor template (using a static boolean predicate). <para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-if-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be <computeroutput>void</computeroutput> for this template specialization (because the then-branch functor template calls return void).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>ElseIfPred</computeroutput></term><listitem><para>Static boolean predicate selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so the else statement and additional else-if statements can be specified if needed. Eventually, it will return void for this template specialization (because the then-branch functor template calls return void). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> ElseIfPred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> ElseIfThen<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="keyword">true</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase><phrase role="special">,</phrase> <phrase role="keyword">void</phrase> <phrase role="special">></phrase> <anchor id="idm45028039976928-bb"/><phrase role="identifier">else_if</phrase><phrase role="special">(</phrase><phrase role="identifier">ElseIfThen</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Specify an else-if-branch functor template (using a nullary boolean meta-function). <para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Else-if-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is never compiled or executed for this template specialization (because the if-statement static predicate is <computeroutput>true</computeroutput>). The return type of <computeroutput>f()</computeroutput> must be <computeroutput>void</computeroutput> for this template specialization (because the then-branch functor template calls return void).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>ElseIfPred</computeroutput></term><listitem><para>Nullary boolean meta-function selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so the else statement and additional else-if statements can be specified if needed. Eventually, it will return void for this template specialization (because the then-branch functor template calls return void). </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if_c"><refmeta><refentrytitle>Function template call_if_c</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if_c</refname><refpurpose>Select compilation and execution of functor template calls using a static boolean predicate (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase> <phrase role="special">></phrase> <phrase role="identifier">call_if_c</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Create a call-if object with the specified then-branch functor template:</para><para><programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">call_if_c</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred1</phrase><phrase role="special">></phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">then_functor_template1</phrase>
|
|
<phrase role="special">)</phrase><phrase role="special">.</phrase><phrase role="keyword">template</phrase> <phrase role="identifier">else_if_c</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred2</phrase><phrase role="special">></phrase><phrase role="special">(</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">then_functor_template2</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="comment">// Optionally, other `else_if_c` or</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// `else_if`.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">else_</phrase><phrase role="special">(</phrase> <phrase role="comment">// Optional for `void` functors,</phrase>
|
|
<phrase role="identifier">else_functor_template</phrase> <phrase role="comment">// but required for non `void`.</phrase>
|
|
<phrase role="special">)</phrase>
|
|
</programlisting></para><para>Optional functor templates for else-if-branches and the else-branch can be specified as needed (the else-branch function template is required if <computeroutput>f</computeroutput> returns non-void).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Then-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is compiled and executed if and only if <computeroutput>Pred</computeroutput> is <computeroutput>true</computeroutput>. The return type of other functor template calls specified for this call-if statement (else-branch, else-if-branches, etc.) must be the same as (or implicitly convertible to) the return type of then-branch functor call <computeroutput>f()</computeroutput>.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Pred</computeroutput></term><listitem><para>Static boolean predicate selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so else and else-if statements can be specified if needed. Eventually, this will be the return value of the one functor template call being compiled and executed (which could also be <computeroutput>void</computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.call_if"><refmeta><refentrytitle>Function template call_if</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::call_if</refname><refpurpose>Select compilation and execution of functor template calls using a nullary boolean meta-function (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.call_if_statement">call_if_statement</link><phrase role="special"><</phrase> <phrase role="identifier">Pred</phrase><phrase role="special">::</phrase><phrase role="identifier">value</phrase><phrase role="special">,</phrase> <phrase role="identifier">Then</phrase> <phrase role="special">></phrase> <phrase role="identifier">call_if</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is equivalent to <computeroutput>boost::contract::call_if_c<Pred::value>(f)</computeroutput>. Create a call-if object with the specified then-branch functor template:</para><para><programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">call_if</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred1</phrase><phrase role="special">></phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">then_functor_template1</phrase>
|
|
<phrase role="special">)</phrase><phrase role="special">.</phrase><phrase role="keyword">template</phrase> <phrase role="identifier">else_if</phrase><phrase role="special"><</phrase><phrase role="identifier">Pred2</phrase><phrase role="special">></phrase><phrase role="special">(</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">then_functor_template2</phrase>
|
|
<phrase role="special">)</phrase> <phrase role="comment">// Optionally, other `else_if` or</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// `else_if_c`.</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">else_</phrase><phrase role="special">(</phrase> <phrase role="comment">// Optional for `void` functors,</phrase>
|
|
<phrase role="identifier">else_functor_template</phrase> <phrase role="comment">// but required for non `void`.</phrase>
|
|
<phrase role="special">)</phrase>
|
|
</programlisting></para><para>Optional functor templates for else-if-branches and the else-branch can be specified as needed (the else-branch functor template is required if <computeroutput>f</computeroutput> returns non-void).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Then-branch nullary functor template. The functor template call <computeroutput>f()</computeroutput> is compiled and executed if and only if <computeroutput>Pred::value</computeroutput> is <computeroutput>true</computeroutput>. The return type of other functor template calls specified for this call-if statement (else-branch, else-if-branches, etc.) must be the same as (or implicitly convertible to) the return type of then-branch functor template call <computeroutput>f()</computeroutput>.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Pred</computeroutput></term><listitem><para>Nullary boolean meta-function selecting which functor template call to compile and execute.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>A call-if statement so else and else-if statements can be specified if needed. Eventually, this will be the return value of the one functor template call being compiled and executed (which could also be <computeroutput>void</computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.condition_if_c"><refmeta><refentrytitle>Function template condition_if_c</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::condition_if_c</refname><refpurpose>Select compilation and execution of a boolean functor template condition using a static boolean predicate (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">bool</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">condition_if_c</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> else_ <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Compile and execute the nullary boolean functor template call <computeroutput>f()</computeroutput> if and only if the specified static boolean predicate <computeroutput>Pred</computeroutput> is <computeroutput>true</computeroutput>, otherwise trivially return <computeroutput>else_</computeroutput> (<computeroutput>true</computeroutput> by default) at run-time.</para><para>A call to <computeroutput>boost::contract::condition_if_c<Pred>(f, else_)</computeroutput> is logically equivalent to <computeroutput>boost::contract::call_if_c<Pred>(f, [] { return else_; })</computeroutput> (but its internal implementation is optimized and it does not actually use <computeroutput>call_if_c</computeroutput>).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>else_</computeroutput></term><listitem><para>Boolean value to return when <computeroutput>Pred</computeroutput> is <computeroutput>false</computeroutput> (instead of compiling and executing the functor template call <computeroutput>f()</computeroutput>).</para></listitem></varlistentry><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary boolean functor template. The functor template call <computeroutput>f()</computeroutput> is compiled and executed if and only if <computeroutput>Pred</computeroutput> is <computeroutput>true</computeroutput>.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Pred</computeroutput></term><listitem><para>Static boolean predicate selecting when the functor template call <computeroutput>f()</computeroutput> should be compiled and executed. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Boolean value returned by <computeroutput>f()</computeroutput> if the static predicate <computeroutput>Pred</computeroutput> is <computeroutput>true</computeroutput>. Otherwise, trivially return <computeroutput>else_</computeroutput>. </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.condition_if"><refmeta><refentrytitle>Function template condition_if</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::condition_if</refname><refpurpose>Select compilation and execution of a boolean functor template condition using a nullary boolean meta-function (not needed on C++17 compilers, use <computeroutput>if constexpr</computeroutput> instead). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.call_if_hpp">boost/contract/call_if.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Pred<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Then<phrase role="special">></phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">condition_if</phrase><phrase role="special">(</phrase><phrase role="identifier">Then</phrase> f<phrase role="special">,</phrase> <phrase role="keyword">bool</phrase> else_ <phrase role="special">=</phrase> <phrase role="keyword">true</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is equivalent to <computeroutput>boost::contract::condition_if_c<Pred::value>(f, else_)</computeroutput>. Compile and execute the nullary boolean functor template call <computeroutput>f()</computeroutput> if and only if the specified nullary boolean meta-function <computeroutput>Pred::value</computeroutput> is <computeroutput>true</computeroutput>, otherwise trivially return <computeroutput>else_</computeroutput> (<computeroutput>true</computeroutput> by default) at run-time.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_requirements__templates_">
|
|
Assertion Requirements</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>else_</computeroutput></term><listitem><para>Boolean value to return when <computeroutput>Pred::value</computeroutput> is <computeroutput>false</computeroutput> (instead of compiling and executing the functor template call <computeroutput>f()</computeroutput>).</para></listitem></varlistentry><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary boolean functor template. The functor template call <computeroutput>f()</computeroutput> is compiled and executed if and only if <computeroutput>Pred::value</computeroutput> is <computeroutput>true</computeroutput>. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Pred</computeroutput></term><listitem><para>Nullary boolean meta-function selecting when the functor template call <computeroutput>f()</computeroutput> should be compiled and executed.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Boolean value returned by <computeroutput>f()</computeroutput> if the static predicate <computeroutput>Pred::value</computeroutput> is <computeroutput>true</computeroutput>. Otherwise, trivially return <computeroutput>else_</computeroutput>. </para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.check_hpp"><title>Header <<ulink url="../../../../boost/contract/check.hpp">boost/contract/check.hpp</ulink>></title><para>RAII object that checks contracts. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.check">check</link><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.check"><refmeta><refentrytitle>Class check</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::check</refname><refpurpose>RAII object that checks the contracts. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.check_hpp">boost/contract/check.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.check">check</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.checkconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="idm45028039885152-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039877776-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.check">check</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">></phrase>
|
|
<link linkend="idm45028039874880-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">></phrase>
|
|
<link linkend="idm45028039865600-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">></phrase>
|
|
<link linkend="idm45028039854560-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039843472-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.specify_except">specify_except</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039834288-bb"><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.specify_nothing">specify_nothing</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039825088-bb"><phrase role="special">~</phrase><phrase role="identifier">check</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>In general, when this object is constructed it checks class invariants at entry, preconditions, and makes old value copies at body. When it is destructed, it checks class invariants at exist, postconditions, and exception guarantees. This object enforces the following (see <link linkend="boost_contract.contract_programming_overview"> Contract Programming Overview</link>):</para><para><itemizedlist>
|
|
<listitem><para>Postconditions are checked only if the body does not throw an exception. </para>
|
|
</listitem>
|
|
<listitem><para>Exceptions guarantees are checked only if the body throws an exception. </para>
|
|
</listitem>
|
|
<listitem><para>Constructor entry never checks class invariants. </para>
|
|
</listitem>
|
|
<listitem><para>Destructor exit checks class invariants only if the body throws an exception (even if destructors should usually not be programmed to throw exceptions in C++ and they are implicitly declared <computeroutput>noexcept</computeroutput> since C++11). </para>
|
|
</listitem>
|
|
<listitem><para>Static invariants are always checked at entry and exit (and regardless of the body throwing exceptions or not).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
When used this way, this object is constructed and initialized to the return value of one of the contract functions <computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput>, <computeroutput><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput>, <computeroutput><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput>, or <computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>. In addition to that, this object can be constructed from a nullary functor when it is used to program implementation checks.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial"> Tutorial</link>, <link linkend="boost_contract.advanced.implementation_checks"> Implementation Checks</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.checkconstruct-copy-destruct"/><computeroutput>check</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <anchor id="idm45028039885152-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object for implementation checks. <para>This can be used to program checks within implementation code (body, etc.). This constructor is not declared <computeroutput>explicit</computeroutput> so initializations can use assignment syntax <computeroutput>=</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating the program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor that asserts implementation checks. <computeroutput>f()</computeroutput> will be called as soon as this object is constructed at the point it is declared within the implementation code (see <link linkend="boost_contract.advanced.implementation_checks">
|
|
Implementation Checks</link>). </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039877776-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><link linkend="boost.contract.check">check</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> other<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object copying it from the specified one. <para>This object will check the contract, the copied-from object will not (i.e., contract check ownership is transferred from the copied object to the new object being created by this constructor).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>other</computeroutput></term><listitem><para>Copied-from object. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">></phrase>
|
|
<anchor id="idm45028039874880-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> contract<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object to check the specified contract. <para>This checks class invariants at entry (if those were specified for the given contract). This constructor is not declared <computeroutput>explicit</computeroutput> so initializations can use assignment syntax <computeroutput>=</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating the program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>contract</computeroutput></term><listitem><para>Contract to be checked (usually the return value of <computeroutput><computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput></computeroutput> or <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>VirtualResult</computeroutput></term><listitem><para>Return type of the enclosing function declaring the contract if that is either a virtual or an overriding public function, otherwise this is always <computeroutput>void</computeroutput>. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">></phrase>
|
|
<anchor id="idm45028039865600-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> contract<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object to check the specified contract. <para>This checks class invariants at entry and preconditions (if any of those were specified for the given contract). This constructor is not declared <computeroutput>explicit</computeroutput> so initializations can use assignment syntax <computeroutput>=</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating the program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>contract</computeroutput></term><listitem><para>Contract to be checked (usually the return value of <computeroutput><computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput></computeroutput>, or <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>VirtualResult</computeroutput></term><listitem><para>Return type of the enclosing function declaring the contract if that is either a virtual or an overriding public function, otherwise this is always <computeroutput>void</computeroutput>. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">></phrase>
|
|
<anchor id="idm45028039854560-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> contract<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object to check the specified contract. <para>This checks class invariants at entry and preconditions then it makes old value copies at body (if any of those were specified for the given contract). This constructor is not declared <computeroutput>explicit</computeroutput> so initializations can use assignment syntax <computeroutput>=</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating te program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>contract</computeroutput></term><listitem><para>Contract to be checked (usually the return value of <computeroutput><computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput></computeroutput>, or <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>VirtualResult</computeroutput></term><listitem><para>Return type of the enclosing function declaring the contract if that is either a virtual or an overriding public function, otherwise this is always <computeroutput>void</computeroutput>. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039843472-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><link linkend="boost.contract.specify_except">specify_except</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> contract<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object to check the specified contract. <para>This checks class invariants at entry and preconditions then it makes old value copies at body, plus the destructor of this object will also check postconditions in this case (if any of those were specified for the given contract). This constructor is not declared <computeroutput>explicit</computeroutput> so initializations can use assignment syntax <computeroutput>=</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating the program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>contract</computeroutput></term><listitem><para>Contract to be checked (usually the return value of <computeroutput><computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput></computeroutput>, or <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput>).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039834288-bb"/><phrase role="identifier">check</phrase><phrase role="special">(</phrase><link linkend="boost.contract.specify_nothing">specify_nothing</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> contract<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object to check the specified contract. <para>This checks class invariants at entry and preconditions then it makes old value copies at body, plus the destructor of this object will also check postconditions and exception guarantees in this case (if any of those were specified for the given contract). This constructor is not declared <computeroutput>explicit</computeroutput> so initializations can use assignment syntax <computeroutput>=</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating the program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>contract</computeroutput></term><listitem><para>Contract to be checked (usually the return value of <computeroutput><computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput></computeroutput>, <computeroutput><computeroutput><link linkend="boost.contract.destructor">boost::contract::destructor</link></computeroutput></computeroutput>, or <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput>).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039825088-bb"/><phrase role="special">~</phrase><phrase role="identifier">check</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para>This checks class invariants at exit and either postconditions when the enclosing function body did not throw an exception, or exception guarantees when the function body threw an exception (if class invariants, postconditions, and exception guarantees respectively were specified for the enclosing class and the contract parameter given when constructing this object).</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify contract failure handlers that throw exceptions instead of terminating the program (see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>). (This is declared <computeroutput>noexcept(false)</computeroutput> since C++11.) </para></listitem></orderedlist></refsect2></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.constructor_hpp"><title>Header <<ulink url="../../../../boost/contract/constructor.hpp">boost/contract/constructor.hpp</ulink>></title><para>Program contracts for constructors. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link> <link linkend="boost.contract.constructor"><phrase role="identifier">constructor</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.constructor"><refmeta><refentrytitle>Function template constructor</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::constructor</refname><refpurpose>Program contracts for constructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.constructor_hpp">boost/contract/constructor.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link> <phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify postconditions, exception guarantees, old value copies at body, and check class invariants for constructors (see <computeroutput><computeroutput><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput></computeroutput> to specify preconditions for constructors):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">:</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">u</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// No `.precondition` (use `constructor_precondition` instead).</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Constructor body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for constructors that do not have postconditions and exception guarantees, within classes that have no invariants.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.constructors"> Constructors</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing constructor declaring the contract. (Constructors check all class invariants, including static and volatile invariants, see <link linkend="boost_contract.tutorial.class_invariants">
|
|
Class Invariants</link> and <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the constructor declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the constructor body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.access_hpp"><title>Header <<ulink url="../../../../boost/contract/core/access.hpp">boost/contract/core/access.hpp</ulink>></title><para>Allow to declare invariants, base types, etc all as private members. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.access">access</link><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.access"><refmeta><refentrytitle>Class access</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::access</refname><refpurpose>Declare this class as friend to program invariants and base types as private members. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.access_hpp">boost/contract/core/access.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.access">access</link> <phrase role="special">{</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Declare this class a friend of the user-defined class specifying the contracts and then invariant functions and the base types <computeroutput>typedef</computeroutput> can be declared as non-public members:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">w</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase> <phrase role="comment">// Private.</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="special">...</phrase> <phrase role="special">}</phrase> <phrase role="comment">// Private (same for static and volatile).</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>In real code, programmers will likely chose to declare this class as friend so to fully control public interfaces of their user-defined classes (this is not extensively done in the examples of this documentation only for brevity). This class is not intended to be directly used by programmers a part from being declared as <computeroutput>friend</computeroutput> (and that is why this class does not have any public member and it is not copyable).</para><para><warning><para>Not declaring this class friend of user-defined classes will cause compiler errors on some compilers (e.g., MSVC) because the private members needed to check the contracts will not be accessible. On other compilers (e.g., GCC and CLang), the private access will instead fail SFINAE and no compiler error will be reported while invariants and subcontracting will be silently skipped at run-time. Therefore, programmers must make sure to either declare this class as friend or to always declare invariant functions and base types <computeroutput>typedef</computeroutput> as public members.</para>
|
|
</warning>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.access_specifiers"> Access Specifiers</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.check_macro_hpp"><title>Header <<ulink url="../../../../boost/contract/core/check_macro.hpp">boost/contract/core/check_macro.hpp</ulink>></title><para>Macros for implementation checks. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link>(cond)
|
|
<link linkend="BOOST_CONTRACT_CHECK_AUDIT">BOOST_CONTRACT_CHECK_AUDIT</link>(cond)
|
|
<link linkend="BOOST_CONTRACT_CHECK_AXIOM">BOOST_CONTRACT_CHECK_AXIOM</link>(cond)</synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_CHECK"><refmeta><refentrytitle>Macro BOOST_CONTRACT_CHECK</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_CHECK</refname><refpurpose>Preferred way to assert implementation check conditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.check_macro_hpp">boost/contract/core/check_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_CHECK(cond)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>It is preferred to use this macro instead of programming implementation checks in a nullary functor passed to <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> constructor because this macro will completely remove implementation checks from the code when <computeroutput><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput> is defined:</para><para><programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting></para><para> <computeroutput><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AUDIT">BOOST_CONTRACT_CHECK_AUDIT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AXIOM">BOOST_CONTRACT_CHECK_AXIOM</link></computeroutput> are the three assertion levels predefined by this library for implementation checks.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.implementation_checks"> Implementation Checks</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>cond</computeroutput></term><listitem><para>Boolean condition to check within implementation code (function body, etc.). (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_CHECK((cond))</computeroutput> will always work.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_CHECK_AUDIT"><refmeta><refentrytitle>Macro BOOST_CONTRACT_CHECK_AUDIT</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_CHECK_AUDIT</refname><refpurpose>Preferred way to assert implementation check conditions that are computationally expensive, at least compared to the computational cost of executing the function body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.check_macro_hpp">boost/contract/core/check_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_CHECK_AUDIT(cond)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The specified condition will always be compiled and validated syntactically, but it will not be checked at run-time unless <computeroutput><link linkend="BOOST_CONTRACT_AUDITS">BOOST_CONTRACT_AUDITS</link></computeroutput> is defined (undefined by default). This macro is defined by code equivalent to:</para><para><programlisting><phrase role="preprocessor">#ifdef</phrase> <phrase role="identifier">BOOST_CONTRACT_AUDITS</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_CHECK_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> \
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#else</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_CHECK_AUDIT</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> \
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para> <computeroutput><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AUDIT">BOOST_CONTRACT_CHECK_AUDIT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AXIOM">BOOST_CONTRACT_CHECK_AXIOM</link></computeroutput> are the three assertion levels predefined by this library for implementation checks. If there is a need, programmers are free to implement their own assertion levels defining macros similar to the one above.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_levels"> Assertion Levels</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>cond</computeroutput></term><listitem><para>Boolean condition to check within implementation code (function body, etc.). (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_CHECK_AUDIT((cond))</computeroutput> will always work.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_CHECK_AXIOM"><refmeta><refentrytitle>Macro BOOST_CONTRACT_CHECK_AXIOM</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_CHECK_AXIOM</refname><refpurpose>Preferred way to document in the code implementation check conditions that are computationally prohibitive, at least compared to the computational cost of executing the function body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.check_macro_hpp">boost/contract/core/check_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_CHECK_AXIOM(cond)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The specified condition will always be compiled and validated syntactically, but it will never be checked at run-time. This macro is defined by code equivalent to:</para><para><programlisting><phrase role="preprocessor">#define</phrase> <phrase role="identifier">BOOST_CONTRACT_CHECK_AXIOM</phrase><phrase role="special">(</phrase><phrase role="identifier">cond</phrase><phrase role="special">)</phrase> \
|
|
<phrase role="identifier">BOOST_CONTRACT_CHECK</phrase><phrase role="special">(</phrase><phrase role="keyword">true</phrase> <phrase role="special">||</phrase> <phrase role="identifier">cond</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para> <computeroutput><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AUDIT">BOOST_CONTRACT_CHECK_AUDIT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AXIOM">BOOST_CONTRACT_CHECK_AXIOM</link></computeroutput> are the three assertion levels predefined by this library for implementation checks. If there is a need, programmers are free to implement their own assertion levels defining macros similar to the one above.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_levels"> Assertion Levels</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>cond</computeroutput></term><listitem><para>Boolean condition to check within implementation code (function body, etc.). (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_CHECK_AXIOM((cond))</computeroutput> will always work.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.config_hpp"><title>Header <<ulink url="../../../../boost/contract/core/config.hpp">boost/contract/core/config.hpp</ulink>></title><para>Configure this library compile-time and run-time behaviours. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CONTRACT_DYN_LINK">BOOST_CONTRACT_DYN_LINK</link>
|
|
<link linkend="BOOST_CONTRACT_STATIC_LINK">BOOST_CONTRACT_STATIC_LINK</link>
|
|
<link linkend="BOOST_CONTRACT_HEADER_ONLY">BOOST_CONTRACT_HEADER_ONLY</link>
|
|
<link linkend="BOOST_CO_idm45028039741520">BOOST_CONTRACT_DISABLE_THREADS</link>
|
|
<link linkend="BOOST_CONTRACT_MAX_ARGS">BOOST_CONTRACT_MAX_ARGS</link>
|
|
<link linkend="BOOST_CO_idm45028039730032">BOOST_CONTRACT_BASES_TYPEDEF</link>
|
|
<link linkend="BOOST_CO_idm45028039722128">BOOST_CONTRACT_INVARIANT_FUNC</link>
|
|
<link linkend="BOOST_CO_idm45028039712400">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</link>
|
|
<link linkend="BOOST_CONTRACT_PERMISSIVE">BOOST_CONTRACT_PERMISSIVE</link>
|
|
<link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link>
|
|
<link linkend="BOOST_CO_idm45028039682816">BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</link>
|
|
<link linkend="BOOST_CO_idm45028039677008">BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION</link>
|
|
<link linkend="BOOST_CONTRACT_AUDITS">BOOST_CONTRACT_AUDITS</link>
|
|
<link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link>
|
|
<link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link>
|
|
<link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link>
|
|
<link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link>
|
|
<link linkend="BOOST_CO_idm45028039634896">BOOST_CONTRACT_NO_ENTRY_INVARIANTS</link>
|
|
<link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link>
|
|
<link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link>
|
|
<link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link>
|
|
<link linkend="BOOST_CO_idm45028039603040">BOOST_CONTRACT_NO_CONSTRUCTORS</link>
|
|
<link linkend="BOOST_CO_idm45028039592176">BOOST_CONTRACT_NO_DESTRUCTORS</link>
|
|
<link linkend="BOOST_CO_idm45028039583904">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</link>
|
|
<link linkend="BOOST_CO_idm45028039574704">BOOST_CONTRACT_NO_FUNCTIONS</link>
|
|
<link linkend="BOOST_CO_idm45028039564368">BOOST_CONTRACT_NO_CONDITIONS</link>
|
|
<link linkend="BOOST_CONTRACT_NO_ALL">BOOST_CONTRACT_NO_ALL</link></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_DYN_LINK"><refmeta><refentrytitle>Macro BOOST_CONTRACT_DYN_LINK</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_DYN_LINK</refname><refpurpose>Define this macro to compile this library as a shared library (recommended). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_DYN_LINK</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library is compiled so it can be linked as a shared library (a.k.a., Dynamically Linked Library or DLL) to user code. This library will automatically define this macro when Boost libraries are built as shared libraries (e.g., defining <computeroutput>BOOST_ALL_DYN_LINK</computeroutput> or using <computeroutput>bjam link=shared ...</computeroutput>).</para><para><warning><para>In general this library will correctly check contracts at run-time only when compiled as a shared library, unless user code checks contracts in a single program unit (e.g., a single program with only statically linked libraries). Therefore, it is recommended to build and use this library as a shared library by defining this macro (or equivalently by building all Boost libraries as shared libraries).</para>
|
|
</warning>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.getting_started"> Getting Started</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_STATIC_LINK"><refmeta><refentrytitle>Macro BOOST_CONTRACT_STATIC_LINK</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_STATIC_LINK</refname><refpurpose>Define this macro to compile this library as a static library (not recommended). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_STATIC_LINK</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library is compiled so it can be linked statically to user code. This library will automatically define this macro when Boost libraries are built as static libraries.</para><para><warning><para>This library is not guaranteed to always work correctly at run-time when this macro is defined (define <computeroutput><link linkend="BOOST_CONTRACT_DYN_LINK">BOOST_CONTRACT_DYN_LINK</link></computeroutput> or <computeroutput>BOOST_ALL_DYN_LINK</computeroutput> instead). However, this macro can be defined and this library can be safely used as a static library for user code that checks contracts in a single program unit (e.g., a single program with only statically linked libraries).</para>
|
|
</warning>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.getting_started"> Getting Started</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_HEADER_ONLY"><refmeta><refentrytitle>Macro BOOST_CONTRACT_HEADER_ONLY</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_HEADER_ONLY</refname><refpurpose>Automatically defined by this library when it is being used as a header-only library (not recommended). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_HEADER_ONLY</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users do not define <computeroutput><link linkend="BOOST_CONTRACT_DYN_LINK">BOOST_CONTRACT_DYN_LINK</link></computeroutput> (or <computeroutput>BOOST_ALL_DYN_LINK</computeroutput>) and <computeroutput><link linkend="BOOST_CONTRACT_STATIC_LINK">BOOST_CONTRACT_STATIC_LINK</link></computeroutput>. When used as a header-only library, this library code does not have to be compiled separately from user code, this library headers are simply included and compiled as part of the user program.</para><para><warning><para>This library is not guaranteed to always work correctly at run-time when this macro is defined (define <computeroutput><link linkend="BOOST_CONTRACT_DYN_LINK">BOOST_CONTRACT_DYN_LINK</link></computeroutput> or <computeroutput>BOOST_ALL_DYN_LINK</computeroutput> instead). However, this macro can be defined and this library can be safely used as a header-only library for user code that checks contracts in a single program unit (e.g., a single program with only statically linked libraries).</para>
|
|
</warning>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.getting_started"> Getting Started</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039741520"><refmeta><refentrytitle>Macro BOOST_CONTRACT_DISABLE_THREADS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_DISABLE_THREADS</refname><refpurpose>Define this macro to not lock internal library data for thread safety (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_DISABLE_THREADS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Defining this macro will make the library implementation code not thread safe so this macro should not be defined unless the library is being used by single-threaded applications only. This library will automatically define this macro when Boost libraries are built without threads (e.g., defining <computeroutput>BOOST_DISABLE_THREADS</computeroutput>).</para><para><note><para>When this macro is left undefined this library needs to internally use some sort of global lock (to ensure contract checking is globally disabled when other contracts are being checked and also to safely access failure handler functors). That could introduce an undesired amount of synchronization in some multi-threaded applications.</para>
|
|
</note>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.contract_programming_overview.assertions"> Assertions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_MAX_ARGS"><refmeta><refentrytitle>Macro BOOST_CONTRACT_MAX_ARGS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_MAX_ARGS</refname><refpurpose>Maximum number of arguments for public function overrides on compilers that do not support variadic templates (default to <computeroutput>10</computeroutput>). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_MAX_ARGS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>On compilers that do not support C++11 variadic templates, this macro is defined to the maximum number of arguments that public function overrides can have and pass to <computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput> (users can redefine this macro to a different value). On compilers that support variadic templates, this macro has no effect.</para><para><note><para>Regardless of the value of this macro and of compiler support for variadic templates, there might be an intrinsic limit of about 18 arguments for public function overrides (because of similar limits in Boost.MPL and Boost.FunctionTypes internally used by this library).</para>
|
|
</note>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039730032"><refmeta><refentrytitle>Macro BOOST_CONTRACT_BASES_TYPEDEF</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_BASES_TYPEDEF</refname><refpurpose>Define the name of the base type <computeroutput>typedef</computeroutput> (<computeroutput>base_types</computeroutput> by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_BASES_TYPEDEF</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro expands to the name of the <computeroutput>typedef</computeroutput> that lists the base classes for subcontracting via <computeroutput><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput>:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">w</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">:</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">BOOST_CONTRACT_TYPEDEF</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>When used this way, users can redefine this macro if the <computeroutput>typedef</computeroutput> must have a name different from <computeroutput>base_types</computeroutput> (because of name clashes in user code, etc.).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.base_classes__subcontracting_"> Base Classes</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039722128"><refmeta><refentrytitle>Macro BOOST_CONTRACT_INVARIANT_FUNC</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_INVARIANT_FUNC</refname><refpurpose>Define the name of the class invariant member function (<computeroutput>invariant</computeroutput> by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_INVARIANT_FUNC</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro expands to the name of the <computeroutput>const</computeroutput> and <computeroutput>const volatile</computeroutput> member functions that check class invariants and volatile class invariants respectively:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_INVARIANT_FUNC</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_INVARIANT_FUNC</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>When used this way, users can redefine this macro if the invariant functions must have a name different from <computeroutput>invariant</computeroutput> (because of name clashes in user code, etc.).</para><para><note><para>C++ does not allow to overload member functions based on the <computeroutput>static</computeroutput> classifier, so this macro must always be defined to be different than the function name defined for <computeroutput><link linkend="BOOST_CO_idm45028039712400">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</link></computeroutput>.</para>
|
|
</note>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039712400"><refmeta><refentrytitle>Macro BOOST_CONTRACT_STATIC_INVARIANT_FUNC</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_STATIC_INVARIANT_FUNC</refname><refpurpose>Define the name of the static invariant member function (<computeroutput>static_invariant</computeroutput> by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_STATIC_INVARIANT_FUNC</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro expands to the name of the <computeroutput>static</computeroutput> member function that checks static class invariants:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>When used this way, users can redefine this macro if the static invariant function must have a name different from <computeroutput>static_invariant</computeroutput> (because of name clashes in user code, etc.).</para><para><note><para>C++ does not allow to overload member functions based on the <computeroutput>static</computeroutput> classifier, so this macro must always be defined to be different than the function name defined for <computeroutput><link linkend="BOOST_CO_idm45028039722128">BOOST_CONTRACT_INVARIANT_FUNC</link></computeroutput>.</para>
|
|
</note>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_PERMISSIVE"><refmeta><refentrytitle>Macro BOOST_CONTRACT_PERMISSIVE</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_PERMISSIVE</refname><refpurpose>Disable some compile-time errors generated by this library (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_PERMISSIVE</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Defining this macro disables a number of static checks and related compile-time errors generated by this library, for example:</para><para><itemizedlist>
|
|
<listitem><para>The static invariant member function named as <computeroutput>BOOST_CONTRACT_STATIC_INVARIANT_FUNC</computeroutput> must be declared <computeroutput>static</computeroutput>. </para>
|
|
</listitem>
|
|
<listitem><para>Non-static invariant member functions named as <computeroutput>BOOST_CONTRACT_INVARIANT_FUNC</computeroutput> must be declared either <computeroutput>const</computeroutput>, <computeroutput>const volatile</computeroutput>, or <computeroutput>volatile const</computeroutput>. </para>
|
|
</listitem>
|
|
<listitem><para>Derived classes that program contracts for one or more public function overrides via <computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput> must also define the <computeroutput><link linkend="BOOST_CONTRACT_BASE_TYPES">BOOST_CONTRACT_BASE_TYPES</link></computeroutput> <computeroutput>typedef</computeroutput>.</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
In general, it is not recommended to define this macro because these compile-time checks can guard against misuses of this library.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.tutorial.base_classes__subcontracting_"> Base Classes</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039691696"><refmeta><refentrytitle>Macro BOOST_CONTRACT_ON_MISSING_CHECK_DECL</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_ON_MISSING_CHECK_DECL</refname><refpurpose>Code block to execute if contracts are not assigned to a <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> variable (undefined and executes <computeroutput>BOOST_ASSERT(false)</computeroutput> by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_ON_MISSING_CHECK_DECL</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>In general, there is a logic error in the program when contracts are not explicitly assigned to a local variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> and without using C++11 <computeroutput>auto</computeroutput> declarations (because that is a misuse of this library). Therefore, by default (i.e., when this macro is not defined) this library calls <computeroutput>BOOST_ASSERT(false)</computeroutput> in those cases. If this macro is defined, this library will execute the code expanded by this macro instead of calling <computeroutput>BOOST_ASSERT(false)</computeroutput> (if programmers prefer to throw an exception, etc.).</para><para>This macro can also be defined to be any block of code (and use empty curly brackets <computeroutput>{}</computeroutput> to generate no error, not recommended), for example (on GCC): <programlisting><phrase role="identifier">gcc</phrase> <phrase role="special">-</phrase><phrase role="identifier">DBOOST_CONTRACT_ON_MISSING_CHECK_DECL</phrase><phrase role="special">=</phrase><phrase role="char">'{ throw std::logic_error("missing contract check declaration"); }'</phrase> <phrase role="special">...</phrase>
|
|
</programlisting></para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial"> Tutorial</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039682816"><refmeta><refentrytitle>Macro BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</refname><refpurpose>Define this macro to not disable other assertions while checking preconditions (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Not disabling other assertions while checking preconditions can lead to infinite recursion in user code so by default this macro is not defined.</para><para>However, the <link linkend="boost_contract.bibliography"> [N1962]</link> proposal does not disable assertions while checking preconditions because arguments can reach the function body unchecked if assertions are disabled while checking preconditions (e.g., when these same functions bodies are called to check the preconditions in question). This macro can be defined to obtain the behaviour specified in <link linkend="boost_contract.bibliography"> [N1962]</link> (at the risk of infinite recursion).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.contract_programming_overview.feature_summary">
|
|
Feature Summary</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039677008"><refmeta><refentrytitle>Macro BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION</refname><refpurpose>Define this macro to not disable any assertion while checking other assertions (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Not disabling assertions while checking other assertions can lead to infinite recursion in user code so by default this macro is not defined. (Defining this macro automatically implies that other assertion checking is disabled while checking preconditions as if <computeroutput><link linkend="BOOST_CO_idm45028039682816">BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION</link></computeroutput> was also defined.)</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.contract_programming_overview.feature_summary">
|
|
Feature Summary</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_AUDITS"><refmeta><refentrytitle>Macro BOOST_CONTRACT_AUDITS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_AUDITS</refname><refpurpose>Define this macro to evaluate and check audit assertions at run-time (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_AUDITS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Audit assertions and implementation checks programmed via <computeroutput><link linkend="BOOST_CO_idm45028040814800">BOOST_CONTRACT_ASSERT_AUDIT</link></computeroutput> and <computeroutput><link linkend="BOOST_CONTRACT_CHECK_AUDIT">BOOST_CONTRACT_CHECK_AUDIT</link></computeroutput> are always compiled and validated syntactically. However, they are not evaluated and checked at run-time unless this macro is defined (because these conditions can be computationally expensive, at least compared to the computational cost of executing the function body).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.assertion_levels"> Assertion Levels</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_NO_CHECKS"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_CHECKS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_CHECKS</refname><refpurpose>If defined, this library disables implementation checks (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_CHECKS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with implementation checks. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of implementation checks or use <computeroutput><link linkend="BOOST_CONTRACT_CHECK">BOOST_CONTRACT_CHECK</link></computeroutput> (recommended).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.implementation_checks">
|
|
Implementation Checks</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039660048"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_PRECONDITIONS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_PRECONDITIONS</refname><refpurpose>If defined, this library does not check preconditions (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_PRECONDITIONS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with checking preconditions. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of preconditions or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039653664"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_POSTCONDITIONS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_POSTCONDITIONS</refname><refpurpose>If defined, this library does not check postconditions (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_POSTCONDITIONS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with checking postconditions. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of postconditions or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para>It is necessary to disable both postconditions and exception guarantees defining <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput> and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput> in order to disable old value copies (see <computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput>).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_NO_EXCEPTS"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_EXCEPTS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_EXCEPTS</refname><refpurpose>If defined, this library does not check exception guarantees (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_EXCEPTS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with checking exception guarantees. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of exception guarantees or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para>It is necessary to disable both postconditions and exception guarantees defining <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput> and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput> in order to disable old value copies (see <computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput>).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039634896"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_ENTRY_INVARIANTS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_ENTRY_INVARIANTS</refname><refpurpose>If defined, this library does not check class invariants at entry (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_ENTRY_INVARIANTS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with checking class invariants at entry. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of entry class invariants or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para>This macro is automatically defined when <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput> is defined.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039627280"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_EXIT_INVARIANTS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_EXIT_INVARIANTS</refname><refpurpose>If defined, this library does not check class invariants at exit (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_EXIT_INVARIANTS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with checking class invariants at exit. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of exit class invariants or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para>This macro is automatically defined when <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput> is defined.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039619664"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_INVARIANTS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_INVARIANTS</refname><refpurpose>If defined, this library does not check class invariants (undefined by default). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_INVARIANTS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>If this macro is defined, this library internal code is also optimized to reduce compile-time (not just run-time) overhead associated with checking class invariants. In addition, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of class invariants or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para>Defining this macro is equivalent to defining both <computeroutput><link linkend="BOOST_CO_idm45028039634896">BOOST_CONTRACT_NO_ENTRY_INVARIANTS</link></computeroutput> and <computeroutput><link linkend="BOOST_CO_idm45028039627280">BOOST_CONTRACT_NO_EXIT_INVARIANTS</link></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.disable_contract_checking">
|
|
Disable Contract Checking</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_NO_OLDS"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_OLDS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_OLDS</refname><refpurpose>Automatically defined by this library when old value copies are not to be performed. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_OLDS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define both <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput> and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>. Users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of old value copies or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.old_values"> Old Values</link>, <link linkend="boost_contract.advanced.old_values_copied_at_body">
|
|
Old Values Copied at Body</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039603040"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_CONSTRUCTORS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_CONSTRUCTORS</refname><refpurpose>Automatically defined by this library when contracts are not checked for constructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_CONSTRUCTORS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define all <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>. Users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of contracts for constructors or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><note><para>Constructor preconditions are checked separately by <computeroutput><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput> so they are disabled by <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput> instead.</para>
|
|
</note>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.constructors"> Constructors</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039592176"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_DESTRUCTORS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_DESTRUCTORS</refname><refpurpose>Automatically defined by this library when contracts are not checked for destructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_DESTRUCTORS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define all <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>. Users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of contracts for destructors or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.destructors"> Destructors</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039583904"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</refname><refpurpose>Automatically defined by this library when contracts are not checked for public functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define all <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>. Users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of contracts for public functions or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_functions"> Public Functions</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039574704"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_FUNCTIONS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_FUNCTIONS</refname><refpurpose>Automatically defined by this library when contracts are not checked for non-member, private, or protected functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_FUNCTIONS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define all <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>. Users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of contracts for non-member, private and protected functions, or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para>This macro is also used when contracts are not checked for private or protected functions, lambda functions, code blocks, loops, etc.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.non_member_functions"> Non-Member Functions</link>, <link linkend="boost_contract.advanced.private_and_protected_functions">
|
|
Private and Protected Functions</link>, <link linkend="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__">
|
|
Lambdas, Loops, Code Blocks</link>, <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028039564368"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_CONDITIONS</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_CONDITIONS</refname><refpurpose>Automatically defined by this library when contracts are not checked for preconditions, postconditions, exceptions guarantees, and class invariants (excluding implementation checks). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_CONDITIONS</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define all <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>, and <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>. Users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to completely disable compilation of contracts within specifications (so excluding implementation checks which are contracts within implementations instead), or use the macros defined in <computeroutput>boost/contract_macro.hpp</computeroutput> (recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_NO_ALL"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NO_ALL</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NO_ALL</refname><refpurpose>Automatically defined by this library when contracts are not checked at all (neither for specifications nor for implementations). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.config_hpp">boost/contract/core/config.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NO_ALL</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This macro is not a configuration macro and this library will generate a compile-time error if users try to define it directly. This library will automatically define this macro when users define all <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_NO_CHECKS">BOOST_CONTRACT_NO_CHECKS</link></computeroutput>. For example, users can manually program <computeroutput>#ifndef</computeroutput> statements in their code using this macro to avoid including the <computeroutput>boost/contract.hpp</computeroutput> header all together:</para><para><programlisting><phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">/</phrase><phrase role="identifier">core</phrase><phrase role="special">/</phrase><phrase role="identifier">config</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_ALL</phrase>
|
|
<phrase role="preprocessor">#include</phrase> <phrase role="special"><</phrase><phrase role="identifier">boost</phrase><phrase role="special">/</phrase><phrase role="identifier">contract</phrase><phrase role="special">.</phrase><phrase role="identifier">hpp</phrase><phrase role="special">></phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Or, use the <computeroutput>boost/contract_macro.hpp</computeroutput> header and related macros instead (because the <computeroutput>boost/contract_macro.hpp</computeroutput> header is already optimized to not include other headers from this library when contracts are not checked, but recommended only for applications where it is truly necessary to completely remove contract code compilation from production code).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.constructor_precondition_hpp"><title>Header <<ulink url="../../../../boost/contract/core/constructor_precondition.hpp">boost/contract/core/constructor_precondition.hpp</ulink>></title><para>Program preconditions for constructors. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase> <phrase role="keyword">class</phrase> <link linkend="boost.contract.constructor_precondition">constructor_precondition</link><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.constructor_precondition"><refmeta><refentrytitle>Class template constructor_precondition</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::constructor_precondition</refname><refpurpose>Program preconditions for constructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.constructor_precondition_hpp">boost/contract/core/constructor_precondition.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.constructor_precondition">constructor_precondition</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.constructor_preconditionconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039532016-bb"><phrase role="identifier">constructor_precondition</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <phrase role="keyword">explicit</phrase> <link linkend="idm45028039529824-bb"><phrase role="identifier">constructor_precondition</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This class must be the very first base of the class declaring the constructor for which preconditions are programmed (that way constructor arguments can be checked by preconditions even before they are used to initialize other base classes):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">,</phrase> \
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">u</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">!=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">b</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase><phrase role="special">.</phrase><phrase role="number">0</phrase> <phrase role="special">/</phrase> <phrase role="keyword">float</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase><phrase role="special">)</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>User-defined classes should inherit privately from this class (to not alter the public interface of user-defined classes). In addition, this class should never be declared as a virtual base (because virtual bases are initialized only once across the entire inheritance hierarchy preventing preconditions of other base classes from being checked).</para><para>This class cannot be used this way in a <computeroutput>union</computeroutput> because unions cannot have base classes in C++. Instead, this class is used in a <computeroutput>union</computeroutput> to declare a local object within the constructor definition just before <computeroutput><link linkend="boost.contract.constructor">boost::contract::constructor</link></computeroutput> is used (see <link linkend="boost_contract.extras.unions"> Unions</link>).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.constructors"> Constructors</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> Class</literallayout></para><para><para>The class type of the constructor for which preconditions are being programmed. </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.constructor_preconditionconstruct-copy-destruct"/><computeroutput>constructor_precondition</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039532016-bb"/><phrase role="identifier">constructor_precondition</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object without specifying constructor preconditions. <para>This is implicitly called for those constructors of the contracted class that do not specify preconditions.</para><para><note><para>The implementation of this library is optimized so that calling this default constructor should amount to negligible compile-time and run-time overheads (likely to be optimized away completely by most compilers). </para>
|
|
</note>
|
|
</para></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <phrase role="keyword">explicit</phrase> <anchor id="idm45028039529824-bb"/><phrase role="identifier">constructor_precondition</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object specifying constructor preconditions. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library to check constructor preconditions <computeroutput>f()</computeroutput>. Assertions within this functor call are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) value, or better by (constant) reference to avoid extra copies. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.exception_hpp"><title>Header <<ulink url="../../../../boost/contract/core/exception.hpp">boost/contract/core/exception.hpp</ulink>></title><para>Handle contract assertion failures. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.assertion_failure">assertion_failure</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.bad_virtual_result_cast">bad_virtual_result_cast</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.exception">exception</link><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">enum</phrase> <link linkend="boost.contract.from">from</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase> <phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">></phrase> <link linkend="boost.contract.from_failure_handler"><phrase role="identifier">from_failure_handler</phrase></link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase> <phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">></phrase> <link linkend="boost.contract.failure_handler"><phrase role="identifier">failure_handler</phrase></link><phrase role="special">;</phrase>
|
|
<phrase role="identifier">failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <link linkend="boost.contract.set_check_failure"><phrase role="identifier">set_check_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">failure_handler</phrase> <link linkend="boost.contract.get_check_failure"><phrase role="identifier">get_check_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.check_failure"><phrase role="identifier">check_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_precondition_failure"><phrase role="identifier">set_precondition_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <link linkend="boost.contract.get_precondition_failure"><phrase role="identifier">get_precondition_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.precondition_failure"><phrase role="identifier">precondition_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_postcondition_failure"><phrase role="identifier">set_postcondition_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <link linkend="boost.contract.get_postcondition_failure"><phrase role="identifier">get_postcondition_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.postcondition_failure"><phrase role="identifier">postcondition_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_except_failure"><phrase role="identifier">set_except_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <link linkend="boost.contract.get_except_failure"><phrase role="identifier">get_except_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.except_failure"><phrase role="identifier">except_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_old_failure"><phrase role="identifier">set_old_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <link linkend="boost.contract.get_old_failure"><phrase role="identifier">get_old_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.old_failure"><phrase role="identifier">old_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_entr_idm45028039331504"><phrase role="identifier">set_entry_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <link linkend="boost.contract.get_entr_idm45028039322016"><phrase role="identifier">get_entry_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.entry_invariant_failure"><phrase role="identifier">entry_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_exit_invariant_failure"><phrase role="identifier">set_exit_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <link linkend="boost.contract.get_exit_invariant_failure"><phrase role="identifier">get_exit_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">void</phrase> <link linkend="boost.contract.exit_invariant_failure"><phrase role="identifier">exit_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<link linkend="boost.contract.set_invariant_failure"><phrase role="identifier">set_invariant_failure</phrase></link><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.assertion_failure"><refmeta><refentrytitle>Class assertion_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::assertion_failure</refname><refpurpose>Exception typically used to report a contract assertion failure. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.assertion_failure">assertion_failure</link> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">exception</phrase>, <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">exception</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.assertion_failureconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028039498256-bb"><phrase role="identifier">assertion_failure</phrase></link><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="string">""</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="keyword">long</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="string">""</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028039491216-bb"><phrase role="identifier">assertion_failure</phrase></link><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039488736-bb"><phrase role="special">~</phrase><phrase role="identifier">assertion_failure</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039512912-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <link linkend="idm45028039512352-bb"><phrase role="identifier">what</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <link linkend="idm45028039507248-bb"><phrase role="identifier">file</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">unsigned</phrase> <phrase role="keyword">long</phrase> <link linkend="idm45028039504320-bb"><phrase role="identifier">line</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <link linkend="idm45028039501392-bb"><phrase role="identifier">code</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This exception is thrown by code expanded by <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput> (but it can also be thrown by user code programmed manually without that macro). This exception is typically used to report contract assertion failures because it contains detailed information about the file name, line number, and source code of the asserted condition (so it can be used by this library to provide detailed error messages when handling contract assertion failures).</para><para>However, any other exception can be used to report a contract assertion failure (including user-defined exceptions). This library will call the appropriate contract failure handler function ( <computeroutput><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>, etc.) when this or any other exception is thrown while checking contracts (by default, these failure handler functions print an error message to <computeroutput>std::cerr</computeroutput> and terminate the program, but they can be customized to take any other action).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.assertion_failureconstruct-copy-destruct"/><computeroutput>assertion_failure</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028039498256-bb"/><phrase role="identifier">assertion_failure</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> file <phrase role="special">=</phrase> <phrase role="string">""</phrase><phrase role="special">,</phrase> <phrase role="keyword">unsigned</phrase> <phrase role="keyword">long</phrase> line <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> code <phrase role="special">=</phrase> <phrase role="string">""</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object with file name, line number, and source code text of an assertion condition (all optional). <para>This constructor can also be used to specify no information (default constructor), or to specify only file name and line number but not source code text (because of the parameter default values).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>code</computeroutput></term><listitem><para>Text listing the source code of the assertion condition. </para></listitem></varlistentry><varlistentry><term><computeroutput>file</computeroutput></term><listitem><para>Name of the file containing the assertion (usually set using <computeroutput>__FILE__</computeroutput>). </para></listitem></varlistentry><varlistentry><term><computeroutput>line</computeroutput></term><listitem><para>Number of the line containing the assertion (usually set using <computeroutput>__LINE__</computeroutput>). </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028039491216-bb"/><phrase role="identifier">assertion_failure</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> code<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object only with the source code text of the assertion condition. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>code</computeroutput></term><listitem><para>Text listing the source code of the assertion condition. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039488736-bb"/><phrase role="special">~</phrase><phrase role="identifier">assertion_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11). </para></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039512912-bb"/><computeroutput>assertion_failure</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">virtual</phrase> <phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <anchor id="idm45028039512352-bb"/><phrase role="identifier">what</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>String describing the failed assertion. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A string formatted similarly to the following: <computeroutput>assertion "`code()`" failed: file "`file()`", line `line()`</computeroutput> (where `` indicate execution quotes). File, line, and code will be omitted from this string if they were not specified when constructing this object. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <anchor id="idm45028039507248-bb"/><phrase role="identifier">file</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Name of the file containing the assertion. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>File name as specified at construction (or <computeroutput>""</computeroutput> if no file was specified). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">unsigned</phrase> <phrase role="keyword">long</phrase> <anchor id="idm45028039504320-bb"/><phrase role="identifier">line</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Number of the line containing the assertion. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>Line number as specified at construction (or <computeroutput>0</computeroutput> if no line number was specified). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <anchor id="idm45028039501392-bb"/><phrase role="identifier">code</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Text listing the source code of the assertion condition. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>Assertion condition source code as specified at construction (or <computeroutput>""</computeroutput> if no source code text was specified). </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.bad_virtual_result_cast"><refmeta><refentrytitle>Class bad_virtual_result_cast</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::bad_virtual_result_cast</refname><refpurpose>Exception thrown when inconsistent return values are passed to overridden virtual public functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.bad_virtual_result_cast">bad_virtual_result_cast</link> <phrase role="special">:</phrase>
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">bad_cast</phrase>, <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">exception</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.bad_virtual_result_castconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028039476720-bb"><phrase role="identifier">bad_virtual_result_cast</phrase></link><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039473136-bb"><phrase role="special">~</phrase><phrase role="identifier">bad_virtual_result_cast</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039481040-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <link linkend="idm45028039480480-bb"><phrase role="identifier">what</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This exception is thrown when programmers pass to this library return value parameters for public function overrides in derived classes that are not consistent with the return type parameter passed for the virtual public function being overridden from the base classes. This allows this library to give more descriptive error messages in such cases of misuse.</para><para>This exception is internally thrown by this library and programmers should not need to throw it from user code.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.bad_virtual_result_castconstruct-copy-destruct"/><computeroutput>bad_virtual_result_cast</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028039476720-bb"/><phrase role="identifier">bad_virtual_result_cast</phrase><phrase role="special">(</phrase><phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> from_type_name<phrase role="special">,</phrase>
|
|
<phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> to_type_name<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object with the name of the from- and to- result types. <para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>from_type_name</computeroutput></term><listitem><para>Name of the from-type (source of the cast). </para></listitem></varlistentry><varlistentry><term><computeroutput>to_type_name</computeroutput></term><listitem><para>Name of the to-type (destination of the cast). </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039473136-bb"/><phrase role="special">~</phrase><phrase role="identifier">bad_virtual_result_cast</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11). </para></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039481040-bb"/><computeroutput>bad_virtual_result_cast</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">virtual</phrase> <phrase role="keyword">char</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <anchor id="idm45028039480480-bb"/><phrase role="identifier">what</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Description for this error (containing both from- and to- type names). <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11). </para></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.exception"><refmeta><refentrytitle>Class exception</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::exception</refname><refpurpose>Public base class for all exceptions directly thrown by this library. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.exception">exception</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.exceptionconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039464176-bb"><phrase role="special">~</phrase><phrase role="identifier">exception</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This class does not inherit from <computeroutput>std::exception</computeroutput> because exceptions deriving from this class will do that (inheriting from <computeroutput>std::exception</computeroutput>, <computeroutput>std::bad_cast</computeroutput>, etc.).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <computeroutput><link linkend="boost.contract.assertion_failure">boost::contract::assertion_failure</link></computeroutput>, <computeroutput><link linkend="boost.contract.bad_virtual_result_cast">boost::contract::bad_virtual_result_cast</link></computeroutput>, etc. </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.exceptionconstruct-copy-destruct"/><computeroutput>exception</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039464176-bb"/><phrase role="special">~</phrase><phrase role="identifier">exception</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11). </para></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.from"><refmeta><refentrytitle>Type from</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::from</refname><refpurpose>Indicate the kind of operation where the contract assertion failed. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
|
|
<phrase role="keyword">enum</phrase> <phrase role="identifier">from</phrase> <phrase role="special">{</phrase> <link linkend="boost.contract.from.from_constructor">from_constructor</link>, <link linkend="boost.contract.from.from_destructor">from_destructor</link>, <link linkend="boost.contract.from.from_function">from_function</link> <phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is passed as a parameter to the assertion failure handler functions. For example, it might be necessary to know in which operation an assertion failed to make sure exceptions are never thrown from destructors, not even when contract failure handlers are programmed by users to throw exceptions instead of terminating the program.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><varlistentry><term><computeroutput>from_constructor</computeroutput><anchor id="boost.contract.from.from_constructor"/></term><listitem>Assertion failed when checking contracts for constructors. </listitem></varlistentry><varlistentry><term><computeroutput>from_destructor</computeroutput><anchor id="boost.contract.from.from_destructor"/></term><listitem>Assertion failed when checking contracts for destructors . </listitem></varlistentry><varlistentry><term><computeroutput>from_function</computeroutput><anchor id="boost.contract.from.from_function"/></term><listitem>Assertion failed when checking contracts for functions (members or not, public or not). </listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.from_failure_handler"><refmeta><refentrytitle>Type definition from_failure_handler</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>from_failure_handler</refname><refpurpose>Type of assertion failure handler functions (with <computeroutput>from</computeroutput> parameter). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase> <phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase><phrase role="special">)</phrase><phrase role="special">></phrase> <phrase role="identifier">from_failure_handler</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Assertion failure handler functions specified by this type must be functors returning <computeroutput>void</computeroutput> and taking a single parameter of type <computeroutput><link linkend="boost.contract.from">boost::contract::from</link></computeroutput>. For example, this is used to specify contract failure handlers for class invariants, preconditions, postconditions, and exception guarantees.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.failure_handler"><refmeta><refentrytitle>Type definition failure_handler</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>failure_handler</refname><refpurpose>Type of assertion failure handler functions (without <computeroutput>from</computeroutput> parameter). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special"><</phrase> <phrase role="keyword">void</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">></phrase> <phrase role="identifier">failure_handler</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Assertion failure handler functions specified by this type must be nullary functors returning <computeroutput>void</computeroutput>. For example, this is used to specify contract failure handlers for implementation checks.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_check_failure"><refmeta><refentrytitle>Function set_check_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_check_failure</refname><refpurpose>Set failure handler for implementation checks. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <phrase role="identifier">set_check_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.advanced.implementation_checks"> Implementation Checks</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_check_failure"><refmeta><refentrytitle>Function get_check_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_check_failure</refname><refpurpose>Return failure handler currently set for implementation checks. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">failure_handler</phrase> <phrase role="identifier">get_check_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.advanced.implementation_checks"> Implementation Checks</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.check_failure"><refmeta><refentrytitle>Function check_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::check_failure</refname><refpurpose>Call failure handler for implementation checks. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">check_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on implementation check failures (not the default).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.advanced.implementation_checks"> Implementation Checks</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_precondition_failure"><refmeta><refentrytitle>Function set_precondition_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_precondition_failure</refname><refpurpose>Set failure handler for preconditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<phrase role="identifier">set_precondition_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_precondition_failure"><refmeta><refentrytitle>Function get_precondition_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_precondition_failure</refname><refpurpose>Return failure handler currently set for preconditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="identifier">get_precondition_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.precondition_failure"><refmeta><refentrytitle>Function precondition_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::precondition_failure</refname><refpurpose>Call failure handler for preconditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">precondition_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> where<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on contract assertion failures (not the default).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>where</computeroutput></term><listitem><para>Operation that failed the contract assertion (when this function is called by this library, this parameter will never be <computeroutput>from_destructor</computeroutput> because destructors do not have preconditions).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_postcondition_failure"><refmeta><refentrytitle>Function set_postcondition_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_postcondition_failure</refname><refpurpose>Set failure handler for postconditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<phrase role="identifier">set_postcondition_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_postcondition_failure"><refmeta><refentrytitle>Function get_postcondition_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_postcondition_failure</refname><refpurpose>Return failure handler currently set for postconditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="identifier">get_postcondition_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.postcondition_failure"><refmeta><refentrytitle>Function postcondition_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::postcondition_failure</refname><refpurpose>Call failure handler for postconditions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">postcondition_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> where<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on contract assertion failures (not the default).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>where</computeroutput></term><listitem><para>Operation that failed the contract assertion (e.g., this might be useful to program failure handler functors that never throw from destructors, not even when they are programmed by users to throw exceptions instead of terminating the program).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_except_failure"><refmeta><refentrytitle>Function set_except_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_except_failure</refname><refpurpose>Set failure handler for exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<phrase role="identifier">set_except_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_except_failure"><refmeta><refentrytitle>Function get_except_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_except_failure</refname><refpurpose>Return failure handler currently set for exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="identifier">get_except_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.except_failure"><refmeta><refentrytitle>Function except_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::except_failure</refname><refpurpose>Call failure handler for exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">except_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> where<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on contract assertion failures (not the default), however:</para><para><warning><para>When this failure handler is called there is already an active exception (the one that caused the exception guarantees to be checked in the first place). Therefore, programming this failure handler to throw yet another exception will force C++ to automatically terminate the program.</para>
|
|
</warning>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>where</computeroutput></term><listitem><para>Operation that failed the contract assertion.</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_old_failure"><refmeta><refentrytitle>Function set_old_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_old_failure</refname><refpurpose>Set failure handler for old values copied at body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <phrase role="identifier">set_old_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_old_failure"><refmeta><refentrytitle>Function get_old_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_old_failure</refname><refpurpose>Return failure handler currently set for old values copied at body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="identifier">get_old_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.old_failure"><refmeta><refentrytitle>Function old_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::old_failure</refname><refpurpose>Call failure handler for old values copied at body. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">old_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> where<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on contract assertion failures (not the default).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>where</computeroutput></term><listitem><para>Operation that failed the old value copy (e.g., this might be useful to program failure handler functors that never throw from destructors, not even when they are programmed by users to throw exceptions instead of terminating the program).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_entr_idm45028039331504"><refmeta><refentrytitle>Function set_entry_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_entry_invariant_failure</refname><refpurpose>Set failure handler for class invariants at entry. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<phrase role="identifier">set_entry_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_entr_idm45028039322016"><refmeta><refentrytitle>Function get_entry_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_entry_invariant_failure</refname><refpurpose>Return failure handler currently set for class invariants at entry. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="identifier">get_entry_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.entry_invariant_failure"><refmeta><refentrytitle>Function entry_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::entry_invariant_failure</refname><refpurpose>Call failure handler for class invariants at entry. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">entry_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> where<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on contract assertion failures (not the default).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>where</computeroutput></term><listitem><para>Operation that failed the contract assertion (e.g., this might be useful to program failure handler functors that never throw from destructors, not even when they are programmed by users to throw exceptions instead of terminating the program).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_exit_invariant_failure"><refmeta><refentrytitle>Function set_exit_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_exit_invariant_failure</refname><refpurpose>Set failure handler for class invariants at exit. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<phrase role="identifier">set_exit_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Set a new failure handler and returns it.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.get_exit_invariant_failure"><refmeta><refentrytitle>Function get_exit_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::get_exit_invariant_failure</refname><refpurpose>Return failure handler currently set for class invariants at exit. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="identifier">get_exit_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A copy of the failure handler currently set.</para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.exit_invariant_failure"><refmeta><refentrytitle>Function exit_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::exit_invariant_failure</refname><refpurpose>Call failure handler for class invariants at exit. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">exit_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from</phrase> where<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is often called only internally by this library.</para><para><emphasis role="bold">Throws:</emphasis> This can throw in case programmers specify a failure handler that throws exceptions on contract assertion failures (not the default).</para><para>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>where</computeroutput></term><listitem><para>Operation that failed the contract assertion (e.g., this might be useful to program failure handler functors that never throw from destructors, not even when they are programmed by users to throw exceptions instead of terminating the program).</para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.set_invariant_failure"><refmeta><refentrytitle>Function set_invariant_failure</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::set_invariant_failure</refname><refpurpose>Set failure handler for class invariants (at both entry and exit). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.exception_hpp">boost/contract/core/exception.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase>
|
|
<phrase role="identifier">set_invariant_failure</phrase><phrase role="special">(</phrase><phrase role="identifier">from_failure_handler</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is provided for convenience and it is equivalent to call both <computeroutput><computeroutput><link linkend="boost.contract.set_entr_idm45028039331504">boost::contract::set_entry_invariant_failure</link></computeroutput></computeroutput> and <computeroutput><computeroutput><link linkend="boost.contract.set_exit_invariant_failure">boost::contract::set_exit_invariant_failure</link></computeroutput></computeroutput> with the same functor parameter <computeroutput>f</computeroutput>.</para><para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept</computeroutput> (or <computeroutput>throw()</computeroutput> before C++11).</para><para>
|
|
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__"> Throw on Failure</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>New failure handler functor to set for both entry and exit invariants.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Same failure handler functor <computeroutput>f</computeroutput> passed as parameter (e.g., for concatenating function calls).</para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.specify_hpp"><title>Header <<ulink url="../../../../boost/contract/core/specify.hpp">boost/contract/core/specify.hpp</ulink>></title><para>Specify preconditions, old values copied at body, postconditions, and exception guarantees. </para><para>Preconditions, old values copied at body, postconditions, and exception guarantees are all optionals but, when they are specified, they need to be specified in that order. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify_except">specify_except</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase> <phrase role="keyword">class</phrase> <link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.specify_except"><refmeta><refentrytitle>Class specify_except</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::specify_except</refname><refpurpose>Allow to specify exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.specify_hpp">boost/contract/core/specify.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify_except">specify_except</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.specify_exceptconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039254448-bb"><phrase role="special">~</phrase><phrase role="identifier">specify_except</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039262016-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <link linkend="idm45028039261456-bb"><phrase role="identifier">except</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Allow to specify the functor this library will call to check exception guarantees. This object is internally constructed by the library when users specify contracts calling <computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput> and similar functions (that is why this class does not have a public constructor).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.specify_exceptconstruct-copy-destruct"/><computeroutput>specify_except</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039254448-bb"/><phrase role="special">~</phrase><phrase role="identifier">specify_except</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept(false)</computeroutput> since C++11 to allow users to program failure handlers that throw exceptions on contract assertion failures (not the default, see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>). </para></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039262016-bb"/><computeroutput>specify_except</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <anchor id="idm45028039261456-bb"/><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify exception guarantees. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library to check exception guarantees <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After exception guarantees have been specified, the object returned by this function does not allow to specify any additional contract. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.specify_nothing"><refmeta><refentrytitle>Class specify_nothing</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::specify_nothing</refname><refpurpose>Used to prevent setting other contract conditions after exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.specify_hpp">boost/contract/core/specify.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.specify_nothingconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039246384-bb"><phrase role="special">~</phrase><phrase role="identifier">specify_nothing</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This class has no member function so it is used to prevent specifying additional functors to check any other contract. This object is internally constructed by the library when users specify contracts calling <computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput> and similar functions (that is why this class does not have a public constructor).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial"> Tutorial</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.specify_nothingconstruct-copy-destruct"/><computeroutput>specify_nothing</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039246384-bb"/><phrase role="special">~</phrase><phrase role="identifier">specify_nothing</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept(false)</computeroutput> since C++11 to allow users to program failure handlers that throw exceptions on contract assertion failures (not the default, see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>). </para></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.specify__idm45028039243488"><refmeta><refentrytitle>Class template specify_old_postcondition_except</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::specify_old_postcondition_except</refname><refpurpose>Allow to specify old values copied at body, postconditions, and exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.specify_hpp">boost/contract/core/specify.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.specify__idm45028039243488construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039208320-bb"><phrase role="special">~</phrase><phrase role="identifier">specify_old_postcondition_except</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039235392-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <link linkend="idm45028039234832-bb"><phrase role="identifier">old</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_except">specify_except</link> <link linkend="idm45028039226336-bb"><phrase role="identifier">postcondition</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <link linkend="idm45028039215328-bb"><phrase role="identifier">except</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Allow to specify functors this library will call to copy old values at body, check postconditions, and check exception guarantees. This object is internally constructed by the library when users specify contracts calling <computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput> and similar functions (that is why this class does not have a public constructor).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase></literallayout></para><para><para>Return type of the enclosing function declaring the contract if that is either a virtual or an overriding public function, otherwise this is always <computeroutput>void</computeroutput>. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.specify__idm45028039243488construct-copy-destruct"/><computeroutput>specify_old_postcondition_except</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039208320-bb"/><phrase role="special">~</phrase><phrase role="identifier">specify_old_postcondition_except</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept(false)</computeroutput> since C++11 to allow users to program failure handlers that throw exceptions on contract assertion failures (not the default, see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>). </para></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039235392-bb"/><computeroutput>specify_old_postcondition_except</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <anchor id="idm45028039234832-bb"/><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify old values copied at body. <para>It should often be sufficient to initialize old value pointers as soon as they are declared, without using this function (see <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library <computeroutput>f()</computeroutput> to assign old value copies just before the body is executed but after entry invariants (when they apply) and preconditions are checked. Old value pointers within this functor call are usually assigned using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput></computeroutput>. Any exception thrown by a call to this functor will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput></computeroutput> (because old values could not be copied to check postconditions and exception guarantees). This functor should capture old value pointers by references so they can be assigned (all other variables needed to evaluate old value expressions can be captured by (constant) value, or better by (constant) reference to avoid extra copies).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After old values copied at body have been specified, the object returned by this function allows to optionally specify postconditions and exception guarantees. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_except">specify_except</link> <anchor id="idm45028039226336-bb"/><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify postconditions. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Functor called by this library to check postconditions <computeroutput>f()</computeroutput> or <computeroutput>f(result)</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit). This functor must be a nullary functor <computeroutput>f()</computeroutput> if <computeroutput>VirtualResult</computeroutput> is <computeroutput>void</computeroutput>, otherwise it must be a unary functor <computeroutput>f(result)</computeroutput> accepting the return value <computeroutput>result</computeroutput> as a parameter of type <computeroutput>VirtualResult const&</computeroutput> (to avoid extra copies of the return value, or of type <computeroutput>VirtualResult</computeroutput> or <computeroutput>VirtualResult const</computeroutput> if extra copies of the return value are irrelevant).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After postconditions have been specified, the object returned by this function allows to optionally specify exception guarantees. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <anchor id="idm45028039215328-bb"/><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify exception guarantees. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library to check exception guarantees <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After exception guarantees have been specified, the object returned by this function does not allow to specify any additional contract. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.specify__idm45028039205424"><refmeta><refentrytitle>Class template specify_postcondition_except</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::specify_postcondition_except</refname><refpurpose>Allow to specify postconditions or exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.specify_hpp">boost/contract/core/specify.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.specify__idm45028039205424construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039179568-bb"><phrase role="special">~</phrase><phrase role="identifier">specify_postcondition_except</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039198144-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_except">specify_except</link> <link linkend="idm45028039197584-bb"><phrase role="identifier">postcondition</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <link linkend="idm45028039186576-bb"><phrase role="identifier">except</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Allow to specify functors this library will call to check postconditions or exception guarantees. This object is internally constructed by the library when users specify contracts calling <computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput> and similar functions (that is why this class does not have a public constructor).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase></literallayout></para><para><para>Return type of the enclosing function declaring the contract if that is either a virtual or an overriding public function, otherwise this is always <computeroutput>void</computeroutput>. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.specify__idm45028039205424construct-copy-destruct"/><computeroutput>specify_postcondition_except</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039179568-bb"/><phrase role="special">~</phrase><phrase role="identifier">specify_postcondition_except</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept(false)</computeroutput> since C++11 to allow users to program failure handlers that throw exceptions on contract assertion failures (not the default, see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>). </para></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039198144-bb"/><computeroutput>specify_postcondition_except</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_except">specify_except</link> <anchor id="idm45028039197584-bb"/><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify postconditions. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Functor called by this library to check postconditions <computeroutput>f()</computeroutput> or <computeroutput>f(result)</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit). This functor must be a nullary functor <computeroutput>f()</computeroutput> if <computeroutput>VirtualResult</computeroutput> is <computeroutput>void</computeroutput>, otherwise it must be a unary functor <computeroutput>f(result)</computeroutput> accepting the return value <computeroutput>result</computeroutput> as a parameter of type <computeroutput>VirtualResult const&</computeroutput> (to avoid extra copies of the return value, or of type <computeroutput>VirtualResult</computeroutput> or <computeroutput>VirtualResult const</computeroutput> if extra copies of the return value are irrelevant).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After postconditions have been specified, the object returned by this function allows to optionally specify exception guarantees. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <anchor id="idm45028039186576-bb"/><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify exception guarantees. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library to check exception guarantees <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After exception guarantees have been specified, the object returned by this function does not allow to specify any additional contract. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.specify__idm45028039176672"><refmeta><refentrytitle>Class template specify_precondition_old_postcondition_except</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::specify_precondition_old_postcondition_except</refname><refpurpose>Allow to specify preconditions, old values copied at body, postconditions, and exception guarantees. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.specify_hpp">boost/contract/core/specify.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.specify__idm45028039176672construct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039133664-bb"><phrase role="special">~</phrase><phrase role="identifier">specify_precondition_old_postcondition_except</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039167792-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <link linkend="idm45028039167232-bb"><phrase role="identifier">precondition</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <link linkend="idm45028039160176-bb"><phrase role="identifier">old</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_except">specify_except</link> <link linkend="idm45028039151680-bb"><phrase role="identifier">postcondition</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <link linkend="idm45028039140672-bb"><phrase role="identifier">except</phrase></link><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Allow to specify functors this library will call to check preconditions, copy old values at body, check postconditions, and check exception guarantees. This object is internally constructed by the library when users specify contracts calling <computeroutput><link linkend="boost.contract.function">boost::contract::function</link></computeroutput> and similar functions (that is why this class does not have a public constructor).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link>, <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> VirtualResult <phrase role="special">=</phrase> <phrase role="keyword">void</phrase></literallayout></para><para><para>Return type of the enclosing function declaring the contract if that is either a virtual or an overriding public function, otherwise this is always <computeroutput>void</computeroutput>. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.specify__idm45028039176672construct-copy-destruct"/><computeroutput>specify_precondition_old_postcondition_except</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039133664-bb"/><phrase role="special">~</phrase><phrase role="identifier">specify_precondition_old_postcondition_except</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Destruct this object. <para><emphasis role="bold">Throws:</emphasis> This is declared <computeroutput>noexcept(false)</computeroutput> since C++11 to allow users to program failure handlers that throw exceptions on contract assertion failures (not the default, see <link linkend="boost_contract.advanced.throw_on_failures__and__noexcept__">
|
|
Throw on Failure</link>). </para></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039167792-bb"/><computeroutput>specify_precondition_old_postcondition_except</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <anchor id="idm45028039167232-bb"/><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify preconditions. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library to check preconditions <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) value, or better by (constant) reference (to avoid extra copies).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After preconditions have been specified, the object returned by this function allows to optionally specify old values copied at body, postconditions, and exception guarantees. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039205424">specify_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase> <anchor id="idm45028039160176-bb"/><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify old values copied at body. <para>It should often be sufficient to initialize old value pointers as soon as they are declared, without using this function (see <link linkend="boost_contract.advanced.old_values_copied_at_body"> Old Values Copied at Body</link>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library <computeroutput>f()</computeroutput> to assign old value copies just before the body is executed but after entry invariants (when they apply) and preconditions are checked. Old value pointers within this functor call are usually assigned using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput></computeroutput>. Any exception thrown by a call to this functor will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput></computeroutput> (because old values could not be copied to check postconditions and exception guarantees). This functor should capture old value pointers by references so they can be assigned (all other variables needed to evaluate old value expressions can be captured by (constant) value, or better by (constant) reference to avoid extra copies).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After old values copied at body have been specified, the object returned by this functions allows to optionally specify postconditions and exception guarantees. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_except">specify_except</link> <anchor id="idm45028039151680-bb"/><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify postconditions. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Functor called by this library to check postconditions <computeroutput>f()</computeroutput> or <computeroutput>f(result)</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit). This functor must be a nullary functor <computeroutput>f()</computeroutput> if <computeroutput>VirtualResult</computeroutput> is <computeroutput>void</computeroutput>, otherwise it must be a unary functor <computeroutput>f(result)</computeroutput> accepting the return value <computeroutput>result</computeroutput> as a parameter of type <computeroutput>VirtualResult const&</computeroutput> (to avoid extra copies of the return value, or of type <computeroutput>VirtualResult</computeroutput> or <computeroutput>VirtualResult const</computeroutput> if extra copies of the return value are irrelevant).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After postconditions have been specified, the object returned by this function allows to optionally specify exception guarantees. </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> F<phrase role="special">></phrase> <link linkend="boost.contract.specify_nothing">specify_nothing</link> <anchor id="idm45028039140672-bb"/><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="identifier">F</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> f<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Allow to specify exception guarantees. <para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>Nullary functor called by this library to check exception guarantees <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><computeroutput><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>After exception guarantees have been specified, the object returned by this function does not allow to specify any additional contract. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.core.virtual_hpp"><title>Header <<ulink url="../../../../boost/contract/core/virtual.hpp">boost/contract/core/virtual.hpp</ulink>></title><para>Handle virtual public functions with contracts (for subcontracting). </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.virtual_">virtual_</link><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.virtual_"><refmeta><refentrytitle>Class virtual_</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::virtual_</refname><refpurpose>Type of extra function parameter to handle contracts for virtual public functions (for subcontracting). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.core.virtual_hpp">boost/contract/core/virtual.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">{</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Virtual public functions (and therefore also public function overrides) declaring contracts using this library must specify an extra function parameter at the very end of their parameter list. This parameter must be a pointer to this class and it must have default value <computeroutput>0</computeroutput> or <computeroutput>nullptr</computeroutput> (this extra parameter is often named <computeroutput>v</computeroutput> in this documentation, but any name can be used):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="keyword">int</phrase> <phrase role="identifier">x</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Declare `v`.</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Contract declaration (which will use `v`) and function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>In practice this extra parameter does not alter the calling interface of the enclosing function declaring the contract because it is always the very last parameter and it has a default value (so it can always be omitted when users call the function). This extra parameter must be passed to <computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>, and all other operations of this library that accept a pointer to <computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput>. A part from that, this class is not intended to be directly used by programmers (and that is why this class does not have any public member and it is not copyable).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.virtual_public_functions"> Virtual Public Functions</link>, <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.destructor_hpp"><title>Header <<ulink url="../../../../boost/contract/destructor.hpp">boost/contract/destructor.hpp</ulink>></title><para>Program contracts for destructors. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link> <link linkend="boost.contract.destructor"><phrase role="identifier">destructor</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.destructor"><refmeta><refentrytitle>Function template destructor</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::destructor</refname><refpurpose>Program contracts for destructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.destructor_hpp">boost/contract/destructor.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039243488">specify_old_postcondition_except</link> <phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify postconditions, exception guarantees, old value copies at body, and check class invariants for destructors (destructors cannot have preconditions, see <link linkend="boost_contract.contract_programming_overview.destructor_calls"> Destructor Calls</link>):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="special">~</phrase><phrase role="identifier">u</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// No `.precondition` (destructors have no preconditions).</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Destructor body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for destructors that do not have postconditions and exception guarantees, within classes that have no invariants.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.destructors"> Destructors</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing destructor declaring the contract. (Destructors check all class invariants, including static and volatile invariants, see <link linkend="boost_contract.tutorial.class_invariants">
|
|
Class Invariants</link> and <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the destructor declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the destructor body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.function_hpp"><title>Header <<ulink url="../../../../boost/contract/function.hpp">boost/contract/function.hpp</ulink>></title><para>Program contracts for (non-public) functions. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <link linkend="boost.contract.function"><phrase role="identifier">function</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.function"><refmeta><refentrytitle>Function function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::function</refname><refpurpose>Program contracts for non-member, private and protected functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.function_hpp">boost/contract/function.hpp</link>>
|
|
|
|
</phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <phrase role="identifier">function</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, and old value copies at body for non-member, private and protected functions (these functions never check class invariants, see <link linkend="boost_contract.contract_programming_overview.function_calls"> Function Calls</link>):</para><para><programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting></para><para>This can be used also to program contracts in implementation code for lambda functions, loops, and arbitrary blocks of code. For optimization, this can be omitted for code that does not have preconditions, postconditions, and exception guarantees.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.non_member_functions"> Non-Member Functions</link>, <link linkend="boost_contract.advanced.private_and_protected_functions">
|
|
Private and Protected Functions</link>, <link linkend="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__">
|
|
Lambdas, Loops, Code Blocks</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.old_hpp"><title>Header <<ulink url="../../../../boost/contract/old.hpp">boost/contract/old.hpp</ulink>></title><para>Handle old values. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link>(...)</synopsis><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <phrase role="keyword">struct</phrase> <link linkend="boost.contract.is_old_value_copyable">is_old_value_copyable</link><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.old_pointer">old_pointer</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <phrase role="keyword">class</phrase> <link linkend="boost.contract.old_ptr">old_ptr</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <phrase role="keyword">class</phrase> <link linkend="boost.contract.old_ptr_if_copyable">old_ptr_if_copyable</link><phrase role="special">;</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.old_value">old_value</link><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <phrase role="keyword">struct</phrase> <link linkend="boost.contract.old_value_copy">old_value_copy</link><phrase role="special">;</phrase>
|
|
<link linkend="boost.contract.old_value">old_value</link> <link linkend="boost.contract.null_old"><phrase role="identifier">null_old</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="boost.contract.old_pointer">old_pointer</link> <link linkend="boost.contract.make_old_idm45028038966352"><phrase role="identifier">make_old</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.old_value">old_value</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="boost.contract.old_pointer">old_pointer</link> <link linkend="boost.contract.make_old_idm45028038955872"><phrase role="identifier">make_old</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase><phrase role="special">,</phrase> <link linkend="boost.contract.old_value">old_value</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">bool</phrase> <link linkend="boost.contract.copy_old_idm45028038942304"><phrase role="identifier">copy_old</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">bool</phrase> <link linkend="boost.contract.copy_old_idm45028038936160"><phrase role="identifier">copy_old</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.is_old_value_copyable"><refmeta><refentrytitle>Struct template is_old_value_copyable</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::is_old_value_copyable</refname><refpurpose>Trait to check if an old value type can be copied or not. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.is_old_value_copyable">is_old_value_copyable</link> <phrase role="special">:</phrase> <phrase role="keyword">public</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">is_copy_constructible</phrase><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>By default, this unary boolean meta-function is equivalent to <computeroutput>boost::is_copy_constructible<T></computeroutput> but programmers can chose to specialize it for user-defined types (in general some kind of specialization is also needed on compilers that do not support C++11, see <ulink url="http://www.boost.org/doc/libs/release/libs/type_traits/doc/html/boost_typetraits/reference/is_copy_constructible.html"><computeroutput>boost::is_copy_constructible</computeroutput></ulink>):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase><phrase role="special">;</phrase> <phrase role="comment">// Some user-defined type for which old values shall not be copied.</phrase>
|
|
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase> <phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="special">></phrase> <phrase role="comment">// Specialization to not copy old values of type `u`.</phrase>
|
|
<phrase role="keyword">struct</phrase> <phrase role="identifier">is_old_value_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase> <phrase role="special">:</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">false_type</phrase> <phrase role="special">{</phrase><phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase> <phrase role="special">}</phrase> <phrase role="comment">// namespace</phrase>
|
|
</programlisting></para><para>A given old value type <computeroutput>T</computeroutput> is copied only if <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>true</computeroutput>. A copyable old value type <computeroutput>V</computeroutput> is always copied using <computeroutput>boost::contract::old_value_copy<V></computeroutput>. A non-copyable old value type <computeroutput>W</computeroutput> generates a compile-time error when <computeroutput>boost::contract::old_ptr<W></computeroutput> is dereferenced, but instead leaves <computeroutput>boost::contract::old_ptr_if_copyable<W></computeroutput> always null (without generating compile-time errors).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.old_value_requirements__templates_">
|
|
Old Value Requirements</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.old_pointer"><refmeta><refentrytitle>Class old_pointer</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::old_pointer</refname><refpurpose>Convert old value copies into old value pointers. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.old_pointer">old_pointer</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039072464-bb">public member functions</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <link linkend="idm45028039071904-bb"><phrase role="keyword">operator</phrase> <phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <link linkend="idm45028039066272-bb"><phrase role="keyword">operator</phrase> <phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This class is usually only implicitly used by this library and it does not explicitly appear in user code (that is why this class does not have public constructors, etc.). </para><refsect2><title><anchor id="idm45028039072464-bb"/><computeroutput>old_pointer</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <anchor id="idm45028039071904-bb"/><phrase role="keyword">operator</phrase> <phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Convert this object to an actual old value pointer for which the old value type <computeroutput>T</computeroutput> might or not be copyable. <para>For example, this is implicitly called when assigning or initializing old value pointers of type <computeroutput><computeroutput><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput></computeroutput>.</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>T</computeroutput></term><listitem><para>Type of the pointed old value. The old value pointer will always be null if this type is not copyable (see <computeroutput><computeroutput><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput></computeroutput>), but this library will not generate a compile-time error. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase> <anchor id="idm45028039066272-bb"/><phrase role="keyword">operator</phrase> <phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Convert this object to an actual old value pointer for which the old value type <computeroutput>T</computeroutput> must be copyable. <para>For example, this is implicitly called when assigning or initializing old value pointers of type <computeroutput><computeroutput><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput></computeroutput>.</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>T</computeroutput></term><listitem><para>Type of the pointed old value. This type must be copyable (see <computeroutput><computeroutput><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput></computeroutput>), otherwise this library will generate a compile-time error when the old value pointer is dereferenced. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.old_ptr"><refmeta><refentrytitle>Class template old_ptr</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::old_ptr</refname><refpurpose>Old value pointer that requires the pointed old value type to be copyable. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.old_ptr">old_ptr</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// types</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">T</phrase> <anchor id="boost.contract.old_ptr.element_type"/><phrase role="identifier">element_type</phrase><phrase role="special">;</phrase> <phrase role="comment">// Pointed old value type. </phrase>
|
|
|
|
<phrase role="comment">// <link linkend="boost.contract.old_ptrconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039036032-bb"><phrase role="identifier">old_ptr</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039051072-bb">public member functions</link></phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <link linkend="idm45028039050512-bb"><phrase role="keyword">operator</phrase> <phrase role="special">*</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <link linkend="idm45028039044928-bb"><phrase role="keyword">operator</phrase><phrase role="special">-></phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028039039344-bb"><phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This pointer can be set to point an actual old value copy using either <computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput> or <computeroutput><link linkend="boost.contract.make_old_idm45028038966352">boost::contract::make_old</link></computeroutput> (that is why this class does not have public non-default constructors):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="comment">// For copyable `old_type`.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.old_values"> Old Values</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> T</literallayout></para><para><para>Type of the pointed old value. This type must be copyable (i.e., <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> must be <computeroutput>true</computeroutput>), otherwise this pointer will always be null and this library will generate a compile-time error when the pointer is dereferenced. </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.old_ptrconstruct-copy-destruct"/><computeroutput>old_ptr</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039036032-bb"/><phrase role="identifier">old_ptr</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this old value pointer as null. </listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039051072-bb"/><computeroutput>old_ptr</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <anchor id="idm45028039050512-bb"/><phrase role="keyword">operator</phrase> <phrase role="special">*</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Dereference this old value pointer. <para>This will generate a run-time error if this pointer is null and a compile-time error if the pointed type <computeroutput>T</computeroutput> is not copyable (i.e., if <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>false</computeroutput>).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>The pointed old value. Contract assertions should not change the state of the program so this member function is <computeroutput>const</computeroutput> and it returns the old value as a reference to a constant object (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">
|
|
Constant Correctness</link>). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <anchor id="idm45028039044928-bb"/><phrase role="keyword">operator</phrase><phrase role="special">-></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Structure-dereference this old value pointer. <para>This will generate a compile-time error if the pointed type <computeroutput>T</computeroutput> is not copyable (i.e., if <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>false</computeroutput>).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A pointer to the old value (null if this old value pointer is null). Contract assertions should not change the state of the program so this member function is <computeroutput>const</computeroutput> and it returns the old value as a pointer to a constant object (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">
|
|
Constant Correctness</link>). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028039039344-bb"/><phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Query if this old value pointer is null or not (safe-bool operator). <para>(This is implemented using safe-bool emulation on compilers that do not support C++11 explicit type conversion operators.)</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>True if this pointer is not null, false otherwise. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.old_ptr_if_copyable"><refmeta><refentrytitle>Class template old_ptr_if_copyable</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::old_ptr_if_copyable</refname><refpurpose>Old value pointer that does not require the pointed old value type to be copyable. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.old_ptr_if_copyable">old_ptr_if_copyable</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// types</phrase>
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">T</phrase> <anchor id="boost.contract.old_ptr_if_copyable.element_type"/><phrase role="identifier">element_type</phrase><phrase role="special">;</phrase> <phrase role="comment">// Pointed old value type. </phrase>
|
|
|
|
<phrase role="comment">// <link linkend="boost.contract.old_ptr_if_copyableconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<link linkend="idm45028039010816-bb"><phrase role="identifier">old_ptr_if_copyable</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<link linkend="idm45028039010208-bb"><phrase role="identifier">old_ptr_if_copyable</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.old_ptr">old_ptr</link><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028039025888-bb">public member functions</link></phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <link linkend="idm45028039025328-bb"><phrase role="keyword">operator</phrase> <phrase role="special">*</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <link linkend="idm45028039019728-bb"><phrase role="keyword">operator</phrase><phrase role="special">-></phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028039014128-bb"><phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This pointer can be set to point to an actual old value copy using either <computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput> or <computeroutput><link linkend="boost.contract.make_old_idm45028038966352">boost::contract::make_old</link></computeroutput>:</para><para><programlisting><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="comment">// Type `T` might or not be copyable.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="comment">// Always null for non-copyable types.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.old_value_requirements__templates_">
|
|
Old Value Requirements</link></para>
|
|
</para>
|
|
|
|
</para><refsect2><title>Template Parameters</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">typename</phrase> T</literallayout></para><para><para>Type of the pointed old value. If this type is not copyable (i.e., <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>false</computeroutput>), this pointer will always be null (but this library will not generate a compile-time error when this pointer is dereferenced). </para></para></listitem></orderedlist></refsect2><refsect2><title><anchor id="boost.contract.old_ptr_if_copyableconstruct-copy-destruct"/><computeroutput>old_ptr_if_copyable</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><anchor id="idm45028039010816-bb"/><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this old value pointer as null. </listitem><listitem><para><literallayout class="monospaced"><anchor id="idm45028039010208-bb"/><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special">(</phrase><link linkend="boost.contract.old_ptr">old_ptr</link><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> other<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this old value pointer from an old value pointer that requires the old value type to be copyable. <para>Ownership of the pointed value object is transferred to this pointer. This constructor is implicitly called by this library when assigning an object of this type using <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput></computeroutput> (this constructor is usually not explicitly called by user code).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>other</computeroutput></term><listitem><para>Old value pointer that requires the old value type to be copyable. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028039025888-bb"/><computeroutput>old_ptr_if_copyable</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <anchor id="idm45028039025328-bb"/><phrase role="keyword">operator</phrase> <phrase role="special">*</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Dereference this old value pointer. <para>This will generate a run-time error if this pointer is null, but no compile-time error is generated if the pointed type <computeroutput>T</computeroutput> is not copyable (i.e., if <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>false</computeroutput>).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>The pointed old value. Contract assertions should not change the state of the program so this member function is <computeroutput>const</computeroutput> and it returns the old value as a reference to a constant object (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">
|
|
Constant Correctness</link>). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">*</phrase> <anchor id="idm45028039019728-bb"/><phrase role="keyword">operator</phrase><phrase role="special">-></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Structure-dereference this old value pointer. <para>This will return null but will not generate a compile-time error if the pointed type <computeroutput>T</computeroutput> is not copyable (i.e., if <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>false</computeroutput>).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>A pointer to the old value (null if this old value pointer is null). Contract assertions should not change the state of the program so this member function is <computeroutput>const</computeroutput> and it returns the old value as a pointer to a constant object (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">
|
|
Constant Correctness</link>). </para></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028039014128-bb"/><phrase role="keyword">operator</phrase> <phrase role="keyword">bool</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Query if this old value pointer is null or not (safe-bool operator). <para>(This is implemented using safe-bool emulation on compilers that do not support C++11 explicit type conversion operators.)</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>True if this pointer is not null, false otherwise. </para></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.old_value"><refmeta><refentrytitle>Class old_value</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::old_value</refname><refpurpose>Convert user-specified expressions to old values. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">class</phrase> <link linkend="boost.contract.old_value">old_value</link> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.old_valueconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<link linkend="idm45028039000816-bb"><phrase role="identifier">old_value</phrase></link><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">enable_if</phrase><phrase role="special"><</phrase> <link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="special">></phrase><phrase role="special">::</phrase><phrase role="identifier">type</phrase> <phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<link linkend="idm45028038993280-bb"><phrase role="identifier">old_value</phrase></link><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">,</phrase>
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">disable_if</phrase><phrase role="special"><</phrase> <link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="special">></phrase><phrase role="special">::</phrase><phrase role="identifier">type</phrase> <phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This class is usually only implicitly used by this library and it does not explicitly appear in user code.</para><para>On older compilers that cannot correctly deduce the <computeroutput><link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link></computeroutput> trait used in the declaration of this class, programmers can manually specialize that trait to make sure that only old value types that are copyable are actually copied.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.old_value_requirements__templates_">
|
|
Old Value Requirements</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.old_valueconstruct-copy-destruct"/><computeroutput>old_value</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<anchor id="idm45028039000816-bb"/><phrase role="identifier">old_value</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> old<phrase role="special">,</phrase>
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">enable_if</phrase><phrase role="special"><</phrase> <link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="special">></phrase><phrase role="special">::</phrase><phrase role="identifier">type</phrase> <phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object from the specified old value when the old value type is copy constructible. <para>The specified old value <computeroutput>old</computeroutput> is copied (one time only) using <computeroutput><computeroutput><link linkend="boost.contract.old_value_copy">boost::contract::old_value_copy</link></computeroutput></computeroutput>, in which case the related old value pointer will not be null (but no copy is made if postconditions and exception guarantees are not being checked, see <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput></computeroutput>).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>old</computeroutput></term><listitem><para>Old value to be copied.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>T</computeroutput></term><listitem><para>Old value type. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem><listitem><para><literallayout class="monospaced"><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<anchor id="idm45028038993280-bb"/><phrase role="identifier">old_value</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> old<phrase role="special">,</phrase>
|
|
<phrase role="keyword">typename</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">disable_if</phrase><phrase role="special"><</phrase> <link linkend="boost.contract.is_old_value_copyable">boost::contract::is_old_value_copyable</link><phrase role="special"><</phrase> <phrase role="identifier">T</phrase> <phrase role="special">></phrase> <phrase role="special">></phrase><phrase role="special">::</phrase><phrase role="identifier">type</phrase> <phrase role="special">*</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object from the specified old value when the old value type is not copyable. <para>The specified old value <computeroutput>old</computeroutput> cannot be copied in this case so it is not copied and the related old value pointer will always be null (thus calls to this constructor have no effect and they will likely be optimized away by most compilers).</para><para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>old</computeroutput></term><listitem><para>Old value (that will not be copied in this case).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>T</computeroutput></term><listitem><para>Old value type. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.old_value_copy"><refmeta><refentrytitle>Struct template old_value_copy</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::old_value_copy</refname><refpurpose>Trait to copy an old value. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> T<phrase role="special">></phrase>
|
|
<phrase role="keyword">struct</phrase> <link linkend="boost.contract.old_value_copy">old_value_copy</link> <phrase role="special">{</phrase>
|
|
<phrase role="comment">// <link linkend="boost.contract.old_value_copyconstruct-copy-destruct">construct/copy/destruct</link></phrase>
|
|
<phrase role="keyword">explicit</phrase> <link linkend="idm45028038975504-bb"><phrase role="identifier">old_value_copy</phrase></link><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="comment">// <link linkend="idm45028038979392-bb">public member functions</link></phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <link linkend="idm45028038978832-bb"><phrase role="identifier">old</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>By default, the implementation of this trait uses <computeroutput>T's</computeroutput> copy constructor to make one single copy of the specified value. However, programmers can specialize this trait to copy old values using user-specific operations different from <computeroutput>T's</computeroutput> copy constructor. The default implementation of this trait is equivalent to:</para><para><programlisting><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">old_value_copy</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">old_value_copy</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">old</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">old_</phrase><phrase role="special">(</phrase><phrase role="identifier">old</phrase><phrase role="special">)</phrase> <phrase role="comment">// One single copy of value using T's copy constructor.</phrase>
|
|
<phrase role="special">{</phrase><phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="keyword">return</phrase> <phrase role="identifier">old_</phrase><phrase role="special">;</phrase> <phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">private</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="identifier">old_</phrase><phrase role="special">;</phrase> <phrase role="comment">// The old value copy.</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>This library will instantiate and use this trait only on old value types <computeroutput>T</computeroutput> that are copyable (i.e., for which <computeroutput>boost::contract::is_old_value_copyable<T>::value</computeroutput> is <computeroutput>true</computeroutput>).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.old_value_requirements__templates_">
|
|
Old Value Requirements</link> </para>
|
|
</para>
|
|
</para><refsect2><title><anchor id="boost.contract.old_value_copyconstruct-copy-destruct"/><computeroutput>old_value_copy</computeroutput>
|
|
public
|
|
construct/copy/destruct</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="keyword">explicit</phrase> <anchor id="idm45028038975504-bb"/><phrase role="identifier">old_value_copy</phrase><phrase role="special">(</phrase><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> old<phrase role="special">)</phrase><phrase role="special">;</phrase></literallayout></para>Construct this object by making one single copy of the specified old value. <para>This is the only operation within this library that actually copies old values. This ensures this library makes one and only one copy of an old value (if they actually need to be copied, see <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput></computeroutput>).</para><para>
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>old</computeroutput></term><listitem><para>The old value to copy. </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></listitem></orderedlist></refsect2><refsect2><title><anchor id="idm45028038979392-bb"/><computeroutput>old_value_copy</computeroutput> public member functions</title><orderedlist><listitem><para><literallayout class="monospaced"><phrase role="identifier">T</phrase> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> <anchor id="idm45028038978832-bb"/><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase><phrase role="special">;</phrase></literallayout></para>Return a (constant) reference to the old value that was copied. <para>Contract assertions should not change the state of the program so the old value copy is returned as <computeroutput>const</computeroutput> (see <link linkend="boost_contract.contract_programming_overview.constant_correctness">
|
|
Constant Correctness</link>). </para></listitem></orderedlist></refsect2></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.null_old"><refmeta><refentrytitle>Function null_old</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::null_old</refname><refpurpose>Return a "null" old value. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<link linkend="boost.contract.old_value">old_value</link> <phrase role="identifier">null_old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The related old value pointer will also be null. This function is usually only called by the code expanded by <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>Null old value. </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.make_old_idm45028038966352"><refmeta><refentrytitle>Function make_old</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::make_old</refname><refpurpose>Make an old value pointer (but not for virtual public functions and public functions overrides). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<link linkend="boost.contract.old_pointer">old_pointer</link> <phrase role="identifier">make_old</phrase><phrase role="special">(</phrase><link linkend="boost.contract.old_value">old_value</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> old<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The related old value pointer will not be null if the specified old value was actually copied. This function is usually only called by code expanded by <computeroutput>BOOST_CONTRACT_OLDOF(old_expr)</computeroutput> as in:</para><para><programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">make_old</phrase><phrase role="special">(</phrase><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">copy_old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">?</phrase> <phrase role="identifier">old_expr</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">null_old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>old</computeroutput></term><listitem><para>Old value which is usually implicitly constructed from the user old value expression to be copied (use the ternary operator <computeroutput>?:</computeroutput> to completely avoid to evaluate the old value expression when <computeroutput>boost::contract::copy_old()</computeroutput> is <computeroutput>false</computeroutput>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Old value pointer (usually implicitly converted to either <computeroutput><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput> or <computeroutput><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput> in user code). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.make_old_idm45028038955872"><refmeta><refentrytitle>Function make_old</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::make_old</refname><refpurpose>Make an old value pointer (for virtual public functions and public functions overrides). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<link linkend="boost.contract.old_pointer">old_pointer</link> <phrase role="identifier">make_old</phrase><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase> v<phrase role="special">,</phrase> <link linkend="boost.contract.old_value">old_value</link> <phrase role="keyword">const</phrase> <phrase role="special">&</phrase> old<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The related old value pointer will not be null if the specified old value was actually copied. This function is usually only called by code expanded by <computeroutput>BOOST_CONTRACT_OLDOF(v, old_expr)</computeroutput> as in:</para><para><programlisting><phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">make_old</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">copy_old</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">)</phrase> <phrase role="special">?</phrase> <phrase role="identifier">old_expr</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">null_old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>old</computeroutput></term><listitem><para>Old value which is usually implicitly constructed from the user old value expression to be copied (use the ternary operator <computeroutput>?:</computeroutput> to completely avoid to evaluate the old value expression when <computeroutput>boost::contract::copy_old(v)</computeroutput> is <computeroutput>false</computeroutput>).</para></listitem></varlistentry><varlistentry><term><computeroutput>v</computeroutput></term><listitem><para>The trailing parameter of type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual or overriding public function declaring the contract. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>Old value pointer (usually implicitly converted to either <computeroutput><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput> or <computeroutput><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput> in user code). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.copy_old_idm45028038942304"><refmeta><refentrytitle>Function copy_old</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::copy_old</refname><refpurpose>Query if old values need to be copied (but not for virtual public functions and public function overrides). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">copy_old</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>For example, this function always returns false when both postconditions and exception guarantees are not being checked (see <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput></computeroutput>). This function is usually only called by the code expanded by <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Returns:</term><listitem><para>True if old values need to be copied, false otherwise. </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.copy_old_idm45028038936160"><refmeta><refentrytitle>Function copy_old</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::copy_old</refname><refpurpose>Query if old values need to be copied (for virtual public functions and public function overrides). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">bool</phrase> <phrase role="identifier">copy_old</phrase><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase> v<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>For example, this function always returns false when both postconditions and exception guarantees are not being checked (see <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput></computeroutput>). In addition, this function returns false when overridden functions are being called subsequent times by this library to support subcontracting. This function is usually only called by the code expanded by <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput></computeroutput>.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link></para>
|
|
</para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>v</computeroutput></term><listitem><para>The trailing parameter of type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual or overriding public function declaring the contract.</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>True if old values need to be copied, false otherwise. </para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_OLDOF"><refmeta><refentrytitle>Macro BOOST_CONTRACT_OLDOF</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_OLDOF</refname><refpurpose>Macro typically used to copy an old value expression and assign it to an old value pointer. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.old_hpp">boost/contract/old.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_OLDOF(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>The expression expanded by this macro should be assigned to an old value pointer of type <computeroutput><link linkend="boost.contract.old_ptr">boost::contract::old_ptr</link></computeroutput> or <computeroutput><link linkend="boost.contract.old_ptr_if_copyable">boost::contract::old_ptr_if_copyable</link></computeroutput>. This is an overloaded variadic macro and it can be used in the following different ways.</para><para>1. From within virtual public functions and public functions overrides:</para><para><programlisting><phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para>2. From all other operations:</para><para><programlisting><phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">v</emphasis></computeroutput> is the extra parameter of type <computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function or public function overrides declaring the contract. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_expr</emphasis></computeroutput> is the expression to be evaluated and copied into the old value pointer. (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_OLDOF(v, (old_expr))</computeroutput> will always work.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
On compilers that do not support variadic macros, programmers can manually copy old value expressions without using this macro (see <link linkend="boost_contract.extras.no_macros__and_no_variadic_macros_"> No Macros</link>).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.old_values"> Old Values</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.override_hpp"><title>Header <<ulink url="../../../../boost/contract/override.hpp">boost/contract/override.hpp</ulink>></title><para>Handle public function overrides (for subcontracting). </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link>(type_name, func_name)
|
|
<link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link>(func_name)
|
|
<link linkend="BOOST_CONTRACT_OVERRIDES">BOOST_CONTRACT_OVERRIDES</link>(...)</synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038910320"><refmeta><refentrytitle>Macro BOOST_CONTRACT_NAMED_OVERRIDE</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_NAMED_OVERRIDE</refname><refpurpose>Declare an override type trait with an arbitrary name. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.override_hpp">boost/contract/override.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_NAMED_OVERRIDE(type_name, func_name)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Declare the override type trait named <computeroutput>type_name</computeroutput> to pass as an explicit template parameter to <computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput> for public function overrides.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.advanced.named_overrides"> Named Overrides</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>func_name</computeroutput></term><listitem><para>Function name of the public function override. This macro is called just once even if the function name is overloaded (the same override type trait is used for all overloaded functions with the same name, see <link linkend="boost_contract.advanced.function_overloads">
|
|
Function Overloads</link>). (This is not a variadic macro parameter but it should never contain commas because it is an identifier.) </para></listitem></varlistentry><varlistentry><term><computeroutput>type_name</computeroutput></term><listitem><para>Name of the override type trait this macro will declare. (This is not a variadic macro parameter but it should never contain commas because it is an identifier.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_OVERRIDE"><refmeta><refentrytitle>Macro BOOST_CONTRACT_OVERRIDE</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_OVERRIDE</refname><refpurpose>Declare an override type trait named <computeroutput>override_<emphasis>func_name</emphasis></computeroutput>. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.override_hpp">boost/contract/override.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_OVERRIDE(func_name)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>Declare the override type trait named <computeroutput>override_<emphasis>func_name</emphasis></computeroutput> to pass as an explicit template parameter to <computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput> for public function overrides. Use <computeroutput><link linkend="BOOST_CO_idm45028038910320">BOOST_CONTRACT_NAMED_OVERRIDE</link></computeroutput> to generate an override type trait with a name different than <computeroutput>override_<emphasis>func_name</emphasis></computeroutput> (usually not needed).</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>func_name</computeroutput></term><listitem><para>Function name of the public function override. This macro is called just once even if the function name is overloaded (the same override type trait is used for all overloaded functions with the same name, see <link linkend="boost_contract.advanced.function_overloads"> Function Overloads</link>). (This is not a variadic macro parameter but it should never contain any comma because it is an identifier.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_OVERRIDES"><refmeta><refentrytitle>Macro BOOST_CONTRACT_OVERRIDES</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_OVERRIDES</refname><refpurpose>Declare multiple override type traits at once naming them <computeroutput>override_...</computeroutput> (for convenience). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.override_hpp">boost/contract/override.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_OVERRIDES(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This variadic macro is provided for convenience as <computeroutput>BOOST_CONTRACT_OVERRIDES(f_1, f_2, ..., f_n)</computeroutput> expands to code equivalent to:</para><para><programlisting><phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">f_1</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">f_2</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">f_n</phrase><phrase role="special">)</phrase>
|
|
</programlisting></para><para>On compilers that do not support variadic macros, the override type traits can be equivalently programmed one-by-one calling <computeroutput><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput> for each function name as shown above.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link></para>
|
|
</para>
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>...</computeroutput></term><listitem><para>A comma separated list of one or more function names of public function overrides. (Each function name should never contain commas because it is an identifier.) </para></listitem></varlistentry></variablelist></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract.public_function_hpp"><title>Header <<ulink url="../../../../boost/contract/public_function.hpp">boost/contract/public_function.hpp</ulink>></title><para>Program contracts for public functions (including subcontracting). </para><para>The different overloads handle public functions that are static, virtual void, virtual non-void, overriding void, and overriding non-void. </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">namespace</phrase> <phrase role="identifier">boost</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">namespace</phrase> <phrase role="identifier">contract</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <link linkend="boost.contract.public_f_idm45028038882752"><phrase role="identifier">public_function</phrase></link><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <link linkend="boost.contract.public_f_idm45028038871856"><phrase role="identifier">public_function</phrase></link><phrase role="special">(</phrase><phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link>
|
|
<link linkend="boost.contract.public_f_idm45028038857568"><phrase role="identifier">public_function</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase>
|
|
<link linkend="boost.contract.public_f_idm45028038838928"><phrase role="identifier">public_function</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">&</phrase><phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Override<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> F<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Class<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase><phrase role="special">...</phrase> Args<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link>
|
|
<link linkend="boost.contract.public_f_idm45028038815120"><phrase role="identifier">public_function</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="identifier">F</phrase><phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Override<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> F<phrase role="special">,</phrase>
|
|
<phrase role="keyword">typename</phrase> Class<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase><phrase role="special">...</phrase> Args<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase>
|
|
<link linkend="boost.contract.public_f_idm45028038784992"><phrase role="identifier">public_function</phrase></link><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">&</phrase><phrase role="special">,</phrase> <phrase role="identifier">F</phrase><phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase><phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="special">}</phrase></synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.public_f_idm45028038882752"><refmeta><refentrytitle>Function template public_function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::public_function</refname><refpurpose>Program contracts for static public functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.public_function_hpp">boost/contract/public_function.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check static class invariants for static public functions:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">static_invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for non-static).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for static public functions that do not have preconditions, postconditions and exception guarantees, within classes that have no static invariants.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.static_public_functions"> Static Public Functions</link></para>
|
|
</para>
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the static public function declaring the contract. This template parameter must be explicitly specified for static public functions (because they have no object <computeroutput>this</computeroutput> so there is no function argument from which this type template parameter can be automatically deduced by C++).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the static public function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.public_f_idm45028038871856"><refmeta><refentrytitle>Function template public_function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::public_function</refname><refpurpose>Program contracts for public functions that are not static, not virtual, and do not not override. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.public_function_hpp">boost/contract/public_function.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link> <phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check class invariants for public functions that are not static, not virtual, and do not override:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for public functions that do not have preconditions, postconditions and exception guarantees, within classes that have no invariants.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_functions"> Public Functions</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing public function declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>).</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the public function declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the public function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.public_f_idm45028038857568"><refmeta><refentrytitle>Function template public_function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::public_function</refname><refpurpose>Program contracts for void virtual public functions that do not override. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.public_function_hpp">boost/contract/public_function.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link>
|
|
<phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase> v<phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check class invariants for public functions that are virtual, do not override, and return <computeroutput>void:</computeroutput> </para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>A virtual public function should always call <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput> otherwise this library will not be able to correctly use it for subcontracting.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.virtual_public_functions"> Virtual Public Functions</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing virtual public function declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>).</para></listitem></varlistentry><varlistentry><term><computeroutput>v</computeroutput></term><listitem><para>The trailing parameter of type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the virtual public function declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)</para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the public function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.public_f_idm45028038838928"><refmeta><refentrytitle>Function template public_function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::public_function</refname><refpurpose>Program contracts for non-void virtual public functions that do not override. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.public_function_hpp">boost/contract/public_function.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Class<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase>
|
|
<phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase> v<phrase role="special">,</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">&</phrase> r<phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check class invariants for public functions that are virtual, do not override, and do not return <computeroutput>void:</computeroutput> </para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">(</phrase><phrase role="identifier">t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body (use `return result = return_expr`).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>A virtual public function should always call <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput> otherwise this library will not be able to correctly use it for subcontracting.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.virtual_public_functions"> Virtual Public Functions</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing virtual public function declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>).</para></listitem></varlistentry><varlistentry><term><computeroutput>r</computeroutput></term><listitem><para>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 <computeroutput>return</computeroutput> statement. </para></listitem></varlistentry><varlistentry><term><computeroutput>v</computeroutput></term><listitem><para>The trailing parameter of type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the virtual public function declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.)</para></listitem></varlistentry><varlistentry><term><computeroutput>VirtualResult</computeroutput></term><listitem><para>This type must be the same as, or compatible with, the return type of the enclosing virtual public function declaring the contract (this library might not be able to generate a compile-time error if these types mismatch, but in general that will cause run-time errors or undefined behaviour). Alternatively, <computeroutput>boost::optional<<emphasis>return-type</emphasis>></computeroutput> can also be used (see <link linkend="boost_contract.advanced.optional_return_values">
|
|
Optional Return Values</link>). (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the public function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.public_f_idm45028038815120"><refmeta><refentrytitle>Function template public_function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::public_function</refname><refpurpose>Program contracts for void public functions overrides (virtual or not). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.public_function_hpp">boost/contract/public_function.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Override<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> F<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> Class<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase><phrase role="special">...</phrase> Args<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link>
|
|
<phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase> v<phrase role="special">,</phrase> <phrase role="identifier">F</phrase> f<phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">,</phrase> <phrase role="identifier">Args</phrase> <phrase role="special">&</phrase><phrase role="special">...</phrase> args<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check class invariants for public function overrides (virtual or not) that return <computeroutput>void:</computeroutput> </para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">,</phrase> \
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">w</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Override from `b::f`.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_f</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">u</phrase><phrase role="special">::</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>A public function override should always call <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput> otherwise this library will not be able to correctly use it for subcontracting.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>args</computeroutput></term><listitem><para>All 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 <computeroutput>v</computeroutput>.</para></listitem></varlistentry><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>A pointer to the enclosing public function override declaring the contract (but see <link linkend="boost_contract.advanced.function_overloads">
|
|
Function Overloads</link>). </para></listitem></varlistentry><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing public function override declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>). </para></listitem></varlistentry><varlistentry><term><computeroutput>v</computeroutput></term><listitem><para>The trailing parameter of type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing public function override. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Args</computeroutput></term><listitem><para>The types of all parameters passed to the enclosing public function override declaring the contract, but excluding the trailing parameter type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput>*</computeroutput>. On compilers that do not support variadic templates, this library internally implements this function using preprocessor meta-programming (in this case, the maximum number of supported arguments is defined by <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_MAX_ARGS">BOOST_CONTRACT_MAX_ARGS</link></computeroutput></computeroutput>). (Usually these template parameters are automatically deduced by C++ and they do not need to be explicitly specified by programmers.)</para></listitem></varlistentry><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the virtual public function declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry><varlistentry><term><computeroutput>F</computeroutput></term><listitem><para>The function pointer type of the enclosing public function override declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry><varlistentry><term><computeroutput>Override</computeroutput></term><listitem><para>The type trait <computeroutput>override_<emphasis>function-name</emphasis></computeroutput> declared using the <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput></computeroutput> or related macros. This template parameter must be explicitly specified (because there is no function argument from which it can be automatically deduced by C++). </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the public function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry><refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="boost.contract.public_f_idm45028038784992"><refmeta><refentrytitle>Function template public_function</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>boost::contract::public_function</refname><refpurpose>Program contracts for non-void public functions overrides (virtual or not). </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract.public_function_hpp">boost/contract/public_function.hpp</link>>
|
|
|
|
</phrase>
|
|
<phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> Override<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> VirtualResult<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase> F<phrase role="special">,</phrase>
|
|
<phrase role="keyword">typename</phrase> Class<phrase role="special">,</phrase> <phrase role="keyword">typename</phrase><phrase role="special">...</phrase> Args<phrase role="special">></phrase>
|
|
<link linkend="boost.contract.specify__idm45028039176672">specify_precondition_old_postcondition_except</link><phrase role="special"><</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">></phrase>
|
|
<phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><link linkend="boost.contract.virtual_">virtual_</link> <phrase role="special">*</phrase> v<phrase role="special">,</phrase> <phrase role="identifier">VirtualResult</phrase> <phrase role="special">&</phrase> r<phrase role="special">,</phrase> <phrase role="identifier">F</phrase> f<phrase role="special">,</phrase> <phrase role="identifier">Class</phrase> <phrase role="special">*</phrase> obj<phrase role="special">,</phrase>
|
|
<phrase role="identifier">Args</phrase> <phrase role="special">&</phrase><phrase role="special">...</phrase> args<phrase role="special">)</phrase><phrase role="special">;</phrase></synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to specify preconditions, postconditions, exception guarantees, old value copies at body, and check class invariants for public function overrides (virtual or not) that do not return <computeroutput>void:</computeroutput> </para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">,</phrase> \
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">w</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">invariant</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Override from `b::f`.</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">c</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase>
|
|
<phrase role="identifier">override_f</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">u</phrase><phrase role="special">::</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">(</phrase><phrase role="identifier">t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body (use `return result = return_expr`).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>A public function override should always call <computeroutput><computeroutput><link linkend="boost.contract.public_f_idm45028038882752">boost::contract::public_function</link></computeroutput></computeroutput> otherwise this library will not be able to correctly use it for subcontracting.</para><para><para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link></para>
|
|
</para>
|
|
|
|
|
|
|
|
</para><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term>Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>args</computeroutput></term><listitem><para>All 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 <computeroutput>v</computeroutput>.</para></listitem></varlistentry><varlistentry><term><computeroutput>f</computeroutput></term><listitem><para>A pointer to the enclosing public function override declaring the contract (but see <link linkend="boost_contract.advanced.function_overloads">
|
|
Function Overloads</link>). </para></listitem></varlistentry><varlistentry><term><computeroutput>obj</computeroutput></term><listitem><para>The object <computeroutput>this</computeroutput> from the scope of the enclosing public function override declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>). </para></listitem></varlistentry><varlistentry><term><computeroutput>r</computeroutput></term><listitem><para>A reference to the return value of the enclosing public function override declaring the contract. This is usually a local variable declared by the enclosing public function override just before the contract, but programmers must set it to the actual value being returned by the function at each <computeroutput>return</computeroutput> statement. </para></listitem></varlistentry><varlistentry><term><computeroutput>v</computeroutput></term><listitem><para>The trailing parameter of type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing public function override. </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Template Parameters:</term><listitem><variablelist spacing="compact"><?dbhtml
|
|
list-presentation="table"
|
|
?><varlistentry><term><computeroutput>Args</computeroutput></term><listitem><para>The types of all parameters passed to the enclosing public function override declaring the contract, but excluding the trailing parameter type <computeroutput><computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput>*</computeroutput>. On compilers that do not support variadic templates, this library internally implements this function using preprocessor meta-programming (in this case, the maximum number of supported arguments is defined by <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_MAX_ARGS">BOOST_CONTRACT_MAX_ARGS</link></computeroutput></computeroutput>). (Usually these template parameters are automatically deduced by C++ and they do not need to be explicitly specified by programmers.)</para></listitem></varlistentry><varlistentry><term><computeroutput>Class</computeroutput></term><listitem><para>The type of the class containing the virtual public function declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry><varlistentry><term><computeroutput>F</computeroutput></term><listitem><para>The function pointer type of the enclosing public function override declaring the contract. (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry><varlistentry><term><computeroutput>Override</computeroutput></term><listitem><para>The type trait <computeroutput>override_<emphasis>function-name</emphasis></computeroutput> declared using the <computeroutput><computeroutput><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput></computeroutput> or related macros. This template parameter must be explicitly specified (because there is no function argument from which it can be automatically deduced by C++). </para></listitem></varlistentry><varlistentry><term><computeroutput>VirtualResult</computeroutput></term><listitem><para>This type must be the same as, or compatible with, the return type of the enclosing public function override declaring the contract (this library might not be able to generate a compile-time error if these types mismatch, but in general that will cause run-time errors or undefined behaviour). Alternatively, <computeroutput>boost::optional<<emphasis>return-type</emphasis>></computeroutput> can also be used (see <link linkend="boost_contract.advanced.optional_return_values">
|
|
Optional Return Values</link>). (Usually this template parameter is automatically deduced by C++ and it does not need to be explicitly specified by programmers.) </para></listitem></varlistentry></variablelist></listitem></varlistentry><varlistentry><term>Returns:</term><listitem><para>The result of this function must be assigned to a variable of type <computeroutput><link linkend="boost.contract.check">boost::contract::check</link></computeroutput> declared explicitly (i.e., without using C++11 <computeroutput>auto</computeroutput> declarations) and locally just before the code of the public function body (otherwise this library will generate a run-time error, see <computeroutput><link linkend="BOOST_CO_idm45028039691696">BOOST_CONTRACT_ON_MISSING_CHECK_DECL</link></computeroutput>). </para></listitem></varlistentry></variablelist></refsect1></refentry>
|
|
</section>
|
|
<section id="header.boost.contract_macro_hpp"><title>Header <<ulink url="../../../../boost/contract_macro.hpp">boost/contract_macro.hpp</ulink>></title><para>Allow to disable contracts to completely remove their compile-time and run-time overhead. </para><para>This header automatically includes all header files <computeroutput>boost/contract/*.hpp</computeroutput> necessary to use its macros.</para><para>Almost all the macros defined in this header file are variadic macros. On compilers that do not support variadic macros, programmers can manually code <computeroutput>#ifndef BOOST_CONTRACT_NO_...</computeroutput> statements instead (see <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>). </para><synopsis xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
|
<link linkend="BOOST_CO_idm45028038745840">BOOST_CONTRACT_PRECONDITION</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link>(...)
|
|
<link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link>(...)
|
|
<link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link>(...)
|
|
<link linkend="BOOST_CONTRACT_OLD_PTR">BOOST_CONTRACT_OLD_PTR</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038671712">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</link>(...)
|
|
<link linkend="BOOST_CONTRACT_INVARIANT">BOOST_CONTRACT_INVARIANT</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038632960">BOOST_CONTRACT_INVARIANT_VOLATILE</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038619104">BOOST_CONTRACT_STATIC_INVARIANT</link>(...)
|
|
<link linkend="BOOST_CONTRACT_CONSTRUCTOR">BOOST_CONTRACT_CONSTRUCTOR</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038585312">BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION</link>(...)
|
|
<link linkend="BOOST_CONTRACT_DESTRUCTOR">BOOST_CONTRACT_DESTRUCTOR</link>(...)
|
|
<link linkend="BOOST_CONTRACT_FUNCTION">BOOST_CONTRACT_FUNCTION</link>()
|
|
<link linkend="BOOST_CO_idm45028038532624">BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038515616">BOOST_CONTRACT_PUBLIC_FUNCTION</link>(...)
|
|
<link linkend="BOOST_CO_idm45028038483696">BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</link>(...)</synopsis>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038745840"><refmeta><refentrytitle>Macro BOOST_CONTRACT_PRECONDITION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_PRECONDITION</refname><refpurpose>Program preconditions that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_PRECONDITION(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_PRECONDITION(f)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PRECONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">precondition</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">f</emphasis></computeroutput> is the nullay functor called by this library to check preconditions <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>). This functor should capture variables by (constant) value, or better by (constant) reference (to avoid extra copies). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.preconditions"> Preconditions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038734160"><refmeta><refentrytitle>Macro BOOST_CONTRACT_POSTCONDITION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_POSTCONDITION</refname><refpurpose>Program postconditions that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_POSTCONDITION(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_POSTCONDITION(f)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039653664">BOOST_CONTRACT_NO_POSTCONDITIONS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_POSTCONDITIONS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">f</emphasis></computeroutput> is the functor called by this library to check postconditions <computeroutput>f()</computeroutput> or <computeroutput>f(result)</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><link linkend="boost.contract.postcondition_failure">boost::contract::postcondition_failure</link></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit). This functor takes the return value (preferably by <computeroutput>const&</computeroutput>) <computeroutput>result</computeroutput> as its one single parameter <computeroutput>f(result)</computeroutput> but only for virtual public functions and public functions overrides, otherwise it takes no parameter <computeroutput>f()</computeroutput>. (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.postconditions"> Postconditions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_EXCEPT"><refmeta><refentrytitle>Macro BOOST_CONTRACT_EXCEPT</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_EXCEPT</refname><refpurpose>Program exception guarantees that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_EXCEPT(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_EXCEPT(f)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CONTRACT_NO_EXCEPTS">BOOST_CONTRACT_NO_EXCEPTS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_EXCEPTS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">f</emphasis></computeroutput> is the nullary functor called by this library to check exception guarantees <computeroutput>f()</computeroutput>. Assertions within this functor are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this functor indicates a contract assertion failure (and will result in this library calling <computeroutput><link linkend="boost.contract.except_failure">boost::contract::except_failure</link></computeroutput>). This functor should capture variables by (constant) references (to access the values they will have at function exit). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.exception_guarantees"> Exception Guarantees</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_OLD"><refmeta><refentrytitle>Macro BOOST_CONTRACT_OLD</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_OLD</refname><refpurpose>Program old value copies at body that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_OLD(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_OLD(f)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="special">.</phrase><phrase role="identifier">old</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">f</emphasis></computeroutput> is the nullary functor called by this library <computeroutput>f()</computeroutput> to assign old value copies just before the body is execute but after entry invariants (when they apply) and preconditions are checked. Old value pointers within this functor call are usually assigned using <computeroutput><link linkend="BOOST_CONTRACT_OLDOF">BOOST_CONTRACT_OLDOF</link></computeroutput>. Any exception thrown by a call to this functor will result in this library calling <computeroutput><link linkend="boost.contract.old_failure">boost::contract::old_failure</link></computeroutput> (because old values could not be copied to check postconditions and exception guarantees). This functor should capture old value pointers by references so they can be assigned (all other variables needed to evaluate old value expressions can be captured by (constant) value, or better by (constant) reference to avoid extra copies). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.advanced.old_values_copied_at_body">
|
|
Old Values Copied at Body</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_OLD_PTR"><refmeta><refentrytitle>Macro BOOST_CONTRACT_OLD_PTR</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_OLD_PTR</refname><refpurpose>Program old values that can be completely disabled at compile-time for old value types that are required to be copyable. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_OLD_PTR(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to program old value copies for copyable types:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_a</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase> <phrase role="comment">// Null...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_b</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr_b</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase> <phrase role="comment">// Set.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_var_a</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase> <phrase role="comment">// ...set.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">g</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_a</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase> <phrase role="comment">// No `v`</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_b</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_var_b</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr_b</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase> <phrase role="comment">// `v`</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_var_a</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase> <phrase role="comment">// `v`</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>This is an overloaded variadic macro and it can be used in the following different ways (note that no code is generated when <computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput> is defined).</para><para>1. <computeroutput>BOOST_CONTRACT_OLD_PTR(old_type)(old_var)</computeroutput> expands to code equivalent to the following (this leaves the old value pointer null):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="comment">// This declaration does not need to use `v`.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>2. <computeroutput>BOOST_CONTRACT_OLD_PTR(old_type)(old_var, old_expr)</computeroutput> expands to code equivalent to the following (this initializes the pointer to the old value copy, but not to be used for virtual public functions and public function overrides):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>3. <computeroutput>BOOST_CONTRACT_OLD_PTR(old_type)(v, old_var, old_expr)</computeroutput> expands to code equivalent to the following (this initializes the pointer to the old value copy for virtual public functions and public function overrides):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_type</emphasis></computeroutput> is the type of the pointed old value. This type must be copyable (i.e., <computeroutput>boost::contract::is_old_value_copyable<old_type>::value</computeroutput> is <computeroutput>true</computeroutput>), otherwise this pointer will always be null and this library will generate a compile-time error when the pointer is dereferenced (see <computeroutput><link linkend="BOOST_CO_idm45028038671712">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</link></computeroutput>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.) (Rationale: Template parameters like this one are specified to this library macro interface using their own set of parenthesis <computeroutput>SOME_MACRO(template_params)(other_params)</computeroutput>.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">v</emphasis></computeroutput> is the extra training parameter of type <computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function or public function override declaring the contract. (This is not a variadic macro parameter but it should never contain commas because it is an identifier.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_var</emphasis></computeroutput> is the name of the old value pointer variable. (This is not a variadic macro parameter but it should never contain commas because it is an identifier.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_expr</emphasis></computeroutput> is the expression to be evaluated and copied in the old value pointer. (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_OLD_PTR(old_type)(v, old_var, (old_expr))</computeroutput> will always work.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.old_values"> Old Values</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038671712"><refmeta><refentrytitle>Macro BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</refname><refpurpose>Program old values that can be completely disabled at compile-time for old value types that are not required to be copyable. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used to program old value copies for types that might or might not be copyable:</para><para><programlisting><phrase role="keyword">template</phrase><phrase role="special"><</phrase><phrase role="keyword">typename</phrase> <phrase role="identifier">T</phrase><phrase role="special">></phrase> <phrase role="comment">// Type `T` might or not be copyable.</phrase>
|
|
<phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_a</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_b</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_b</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">old_expr_b</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_var_a</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// In postconditions or exception guarantees:</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_a</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="comment">// Always null for non-copyable types.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_b</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="comment">// Always null for non-copyable types.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">g</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_a</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR_IF_COPYABLE</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type_b</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_var_b</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">old_expr_b</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">old_var_a</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr_a</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase> <phrase role="comment">// In postconditions or exception guarantees:</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_a</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="comment">// Always null for non-copyable types.</phrase>
|
|
<phrase role="keyword">if</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var_b</phrase><phrase role="special">)</phrase> <phrase role="special">...</phrase> <phrase role="comment">// Always null for non-copyable types.</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>This is an overloaded variadic macro and it can be used in the following different ways (note that no code is generated when <computeroutput><link linkend="BOOST_CONTRACT_NO_OLDS">BOOST_CONTRACT_NO_OLDS</link></computeroutput> is defined).</para><para>1. <computeroutput>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(old_var)</computeroutput> expands to code equivalent to the following (this leaves the old value pointer null):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="comment">// This declaration does not need to use `v`.</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>2. <computeroutput>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(old_var, old_expr)</computeroutput> expands to code equivalent to the following (this initializes the pointer to the old value copy, but not to be used for virtual public functions and public function overrides):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>3. <computeroutput>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(v, old_var, old_expr)</computeroutput> expands to code equivalent to the following (this initializes the pointer to the old value copy for virtual public functions and public function overrides):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_OLDS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">old_ptr_if_copyable</phrase><phrase role="special"><</phrase><phrase role="identifier">old_type</phrase><phrase role="special">></phrase> <phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_type</emphasis></computeroutput> is the type of the pointed old value. If this type is not copyable (i.e., <computeroutput>boost::contract::is_old_value_copyable<old_type>::value</computeroutput> is <computeroutput>false</computeroutput>), this pointer will always be null, but this library will not generate a compile-time error when this pointer is dereferenced (see <computeroutput><link linkend="BOOST_CONTRACT_OLD_PTR">BOOST_CONTRACT_OLD_PTR</link></computeroutput>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">v</emphasis></computeroutput> is the extra trailing parameter of type <computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function or public function override declaring the contract. (This is not a variadic macro parameter but it should never contain commas because it is an identifier.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_var</emphasis></computeroutput> is the name of the old value pointer variable. (This is not a variadic macro parameter but it should never contain commas because it is an identifier.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">old_expr</emphasis></computeroutput> is the expression to be evaluated and copied in the old value pointer. (This is not a variadic macro parameter so any comma it might contain must be protected by round parenthesis and <computeroutput>BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(old_type)(v, old_var, (old_expr))</computeroutput> will always work.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.extras.old_value_requirements__templates_">
|
|
Old Value Requirements</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_INVARIANT"><refmeta><refentrytitle>Macro BOOST_CONTRACT_INVARIANT</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_INVARIANT</refname><refpurpose>Program (constant) class invariants that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_INVARIANT(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_INVARIANT</computeroutput>({ ... }) expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_INVARIANTS</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_INVARIANT_FUNC</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><emphasis role="bold">{ ... }</emphasis> is the definition of the function that checks class invariants for public functions that are not static and not volatile (see <computeroutput><link linkend="BOOST_CO_idm45028038619104">BOOST_CONTRACT_STATIC_INVARIANT</link></computeroutput> and <computeroutput><link linkend="BOOST_CO_idm45028038632960">BOOST_CONTRACT_INVARIANT_VOLATILE</link></computeroutput>). The curly parenthesis are mandatory (rationale: this is so the syntax of this macro resembles mote the syntax of the lambda functions usually used to specify preconditions, etc.). Assertions within this function are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this function indicates a contract assertion failure (and will result in this library calling either <computeroutput><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput> or <computeroutput><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038632960"><refmeta><refentrytitle>Macro BOOST_CONTRACT_INVARIANT_VOLATILE</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_INVARIANT_VOLATILE</refname><refpurpose>Program volatile class invariants that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_INVARIANT_VOLATILE(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_INVARIANT_VOLATILE</computeroutput>({ ... }) expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_INVARIANTS</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_INVARIANT_FUNC</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="keyword">const</phrase> <phrase role="keyword">volatile</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><emphasis role="bold">{ ... }</emphasis> is the definition of the function that checks class invariants for volatile public functions (see <computeroutput><link linkend="BOOST_CONTRACT_INVARIANT">BOOST_CONTRACT_INVARIANT</link></computeroutput> and <computeroutput><link linkend="BOOST_CO_idm45028038619104">BOOST_CONTRACT_STATIC_INVARIANT</link></computeroutput>). The curly parenthesis are mandatory. Assertions within this function are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this function indicates a contract assertion failure (and will result in this library calling either <computeroutput><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput> or <computeroutput><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038619104"><refmeta><refentrytitle>Macro BOOST_CONTRACT_STATIC_INVARIANT</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_STATIC_INVARIANT</refname><refpurpose>Program static class invariants that can be completely disabled at compile-time. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_STATIC_INVARIANT(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para><computeroutput>BOOST_CONTRACT_STATIC_INVARIANT</computeroutput>({ ... }) expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039619664">BOOST_CONTRACT_NO_INVARIANTS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_INVARIANTS</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">BOOST_CONTRACT_STATIC_INVARIANT_FUNC</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><emphasis role="bold">{ ... }</emphasis> is the definition of the function that checks class invariants for static public functions (see <computeroutput><link linkend="BOOST_CONTRACT_INVARIANT">BOOST_CONTRACT_INVARIANT</link></computeroutput> and <computeroutput><link linkend="BOOST_CO_idm45028038632960">BOOST_CONTRACT_INVARIANT_VOLATILE</link></computeroutput>). The curly parenthesis are mandatory. Assertions within this function are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this function indicates a contract assertion failure (and will result in this library calling either <computeroutput><link linkend="boost.contract.entry_invariant_failure">boost::contract::entry_invariant_failure</link></computeroutput> or <computeroutput><link linkend="boost.contract.exit_invariant_failure">boost::contract::exit_invariant_failure</link></computeroutput>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.class_invariants"> Class Invariants</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_CONSTRUCTOR"><refmeta><refentrytitle>Macro BOOST_CONTRACT_CONSTRUCTOR</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_CONSTRUCTOR</refname><refpurpose>Program contracts that can be completely disabled at compile-time for constructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_CONSTRUCTOR(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link></computeroutput> to specify postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for constructors (see <computeroutput><link linkend="BOOST_CO_idm45028038585312">BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION</link></computeroutput> to specify preconditions for constructors):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_INVARIANT</phrase><phrase role="special">(</phrase><phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="identifier">u</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CONSTRUCTOR</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// No `PRECONDITION` (use `CONSTRUCTOR_PRECONDITION` if needed).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_epxr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Constructor body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for constructors that do not have postconditions and exception guarantees, within classes that have no invariants.</para><para><computeroutput>BOOST_CONTRACT_CONSTRUCTOR(obj)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039603040">BOOST_CONTRACT_NO_CONSTRUCTORS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_CONSTRUCTORS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor</phrase><phrase role="special">(</phrase><phrase role="identifier">obj</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">obj</emphasis></computeroutput> is the object <computeroutput>this</computeroutput> from the scope of the enclosing constructor declaring the contract. Constructors check all class invariants, including static and volatile invariants (see <computeroutput><link linkend="BOOST_CONTRACT_INVARIANT">BOOST_CONTRACT_INVARIANT</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028038619104">BOOST_CONTRACT_STATIC_INVARIANT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CO_idm45028038632960">BOOST_CONTRACT_INVARIANT_VOLATILE</link></computeroutput>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">internal_var</emphasis></computeroutput> 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).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.constructors"> Constructors</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038585312"><refmeta><refentrytitle>Macro BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION</refname><refpurpose>Program preconditions that can be disabled at compile-time for constructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CONTRACT_CONSTRUCTOR">BOOST_CONTRACT_CONSTRUCTOR</link></computeroutput> to specify contracts for constructors. Constructors that do not have preconditions do not use this macro. When at least one of the class constructors uses this macro, <computeroutput><link linkend="boost.contract.constructor_precondition">boost::contract::constructor_precondition</link></computeroutput> must be the first and private base of the class declaring the constructor for which preconditions are programmed:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">,</phrase> \
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">explicit</phrase> <phrase role="identifier">u</phrase><phrase role="special">(</phrase><phrase role="keyword">unsigned</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase> <phrase role="special">:</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION</phrase><phrase role="special">(</phrase><phrase role="identifier">u</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="identifier">x</phrase> <phrase role="special">!=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase><phrase role="special">,</phrase>
|
|
<phrase role="identifier">b</phrase><phrase role="special">(</phrase><phrase role="number">1</phrase> <phrase role="special">/</phrase> <phrase role="identifier">x</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para><computeroutput>BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(class_type)(f)</computeroutput> expands to code equivalent to the following (note that when <computeroutput><link linkend="BOOST_CO_idm45028039660048">BOOST_CONTRACT_NO_PRECONDITIONS</link></computeroutput> is defined, this macro trivially expands to a default constructor call that is internally implemented to do nothing so this should have minimal to no overhead):</para><para><programlisting><phrase role="comment">// Guarded only by NO_PRECONDITIONS (and not also by NO_CONSTRUCTORS)</phrase>
|
|
<phrase role="comment">// because for constructor's preconditions (not for postconditions, etc.).</phrase>
|
|
<phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PRECONDITIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">class_type</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#else</phrase> <phrase role="comment">// No-op call (likely optimized away, minimal to no overhead).</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">class_type</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">class_type</emphasis></computeroutput> is the type of the class containing the constructor for which preconditions are being programmed. (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">f</emphasis></computeroutput> is the nullary functor called by this library to check constructor preconditions <computeroutput>f()</computeroutput>. Assertions within this functor call are usually programmed using <computeroutput><link linkend="BOOST_CONTRACT_ASSERT">BOOST_CONTRACT_ASSERT</link></computeroutput>, but any exception thrown by a call to this functor indicates a contract failure (and will result in this library calling <computeroutput><link linkend="boost.contract.precondition_failure">boost::contract::precondition_failure</link></computeroutput>). This functor should capture variables by (constant) value, or better by (constant) reference to avoid extra copies. (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.)</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.constructors"> Constructors</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_DESTRUCTOR"><refmeta><refentrytitle>Macro BOOST_CONTRACT_DESTRUCTOR</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_DESTRUCTOR</refname><refpurpose>Program contracts that can be completely disabled at compile-time for destructors. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_DESTRUCTOR(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link></computeroutput> to specify postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for destructors (destructors cannot have preconditions, see <link linkend="boost_contract.contract_programming_overview.destructor_calls"> Destructor Calls</link>):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_INVARIANT</phrase><phrase role="special">(</phrase><phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="special">~</phrase><phrase role="identifier">u</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_DESTRUCTOR</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="comment">// No `PRECONDITION` (destructors have no preconditions).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Destructor body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for destructors that do not have postconditions and exception guarantees, within classes that have no invariants.</para><para><computeroutput>BOOST_CONTRACT_DESTRUCTOR(obj)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039592176">BOOST_CONTRACT_NO_DESTRUCTORS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_DESTRUCTORS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">destructor</phrase><phrase role="special">(</phrase><phrase role="identifier">obj</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">obj</emphasis></computeroutput> is the object <computeroutput>this</computeroutput> from the scope of the enclosing destructor declaring the contract. Destructors check all class invariants, including static and volatile invariants (see <link linkend="boost_contract.tutorial.class_invariants">
|
|
Class Invariants</link> and <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>). (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">internal_var</emphasis></computeroutput> 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).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.destructors"> Destructors</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CONTRACT_FUNCTION"><refmeta><refentrytitle>Macro BOOST_CONTRACT_FUNCTION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_FUNCTION</refname><refpurpose>Program contracts that can be completely disabled at compile-time for (non-public) functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_FUNCTION()</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CO_idm45028038745840">BOOST_CONTRACT_PRECONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link></computeroutput> to specify preconditions, postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for (non-public) functions:</para><para><programlisting><phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PRECONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
</programlisting></para><para>This can be used to program contracts for non-member functions but also for private and protected functions, lambda functions, loops, arbitrary blocks of code, etc. For optimization, this can be omitted for code that does not have preconditions, postconditions, and exception guarantees.</para><para><computeroutput>BOOST_CONTRACT_FUNCTION()</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039574704">BOOST_CONTRACT_NO_FUNCTIONS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">function</phrase><phrase role="special">(</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">internal_var</emphasis></computeroutput> 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).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.non_member_functions"> Non-Member Functions</link>, <link linkend="boost_contract.advanced.private_and_protected_functions">
|
|
Private and Protected Functions</link>, <link linkend="boost_contract.advanced.lambdas__loops__code_blocks__and__constexpr__">
|
|
Lambdas, Loops, Code Blocks</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038532624"><refmeta><refentrytitle>Macro BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION</refname><refpurpose>Program contracts that can be completely disabled at compile-time for static public functions. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CO_idm45028038745840">BOOST_CONTRACT_PRECONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link></computeroutput> to specify preconditions, postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for static public functions:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_STATIC_INVARIANT</phrase><phrase role="special">(</phrase><phrase role="special">{</phrase> <phrase role="comment">// Optional (as for non-static).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="keyword">static</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="identifier">u</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PRECONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for static public functions that do not have preconditions, postconditions and exception guarantees, within classes that have no static invariants.</para><para><computeroutput>BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(class_type)</computeroutput> expands to code equivalent to the following (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039583904">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</link></computeroutput> is defined):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">class_type</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where:</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">class_type</emphasis></computeroutput> is the type of the class containing the static public function declaring the contract. (This is a variadic macro parameter so it can contain commas not protected by round parenthesis.) </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">internal_var</emphasis></computeroutput> 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).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.static_public_functions"> Static Public Functions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038515616"><refmeta><refentrytitle>Macro BOOST_CONTRACT_PUBLIC_FUNCTION</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_PUBLIC_FUNCTION</refname><refpurpose>Program contracts that can be completely disabled at compile-time for non-static public functions that do not override. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_PUBLIC_FUNCTION(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CO_idm45028038745840">BOOST_CONTRACT_PRECONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link></computeroutput> to specify preconditions, postconditions, exception guarantees, and old value copies at body that can be completely disabled at compile-time for non-static public functions (virtual or not, void or not) that do not override:</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_INVARIANT</phrase><phrase role="special">(</phrase><phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Non-virtual (same if void).</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PRECONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body (use `return result = return_expr`).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Virtual and void.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="keyword">void</phrase> <phrase role="identifier">g</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Virtual and non-void.</phrase>
|
|
<phrase role="keyword">virtual</phrase> <phrase role="identifier">t</phrase> <phrase role="identifier">h</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">(</phrase><phrase role="identifier">t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body (use `return result = return_expr`).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>For optimization, this can be omitted for non-virtual public functions that do not have preconditions, postconditions and exception guarantees, within classes that have no invariants. Virtual public functions should always use <computeroutput><link linkend="BOOST_CO_idm45028038515616">BOOST_CONTRACT_PUBLIC_FUNCTION</link></computeroutput> otherwise this library will not be able to correctly use them for subcontracting.</para><para>This is an overloaded variadic macro and it can be used in the following different ways (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039583904">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</link></computeroutput> is defined).</para><para>1. <computeroutput>BOOST_CONTRACT_PUBLIC_FUNCTION(obj)</computeroutput> expands to code equivalent to the following (for non-virtual public functions that are not static and do not override, returning void or not):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">obj</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>2. <computeroutput>BOOST_CONTRACT_PUBLIC_FUNCTION(v, obj)</computeroutput> expands to code equivalent to the following (for virtual public functions that do not override, returning void):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">obj</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>3. <computeroutput>BOOST_CONTRACT_PUBLIC_FUNCTION(v, r, obj)</computeroutput> expands to code equivalent to the following (for virtual public functions that do not override, not returning void):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">public_function</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">r</phrase><phrase role="special">,</phrase> <phrase role="identifier">obj</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where (these are all variadic macro parameters so they can contain commas not protected by round parenthesis):</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">v</emphasis></computeroutput> is the extra parameter of type <computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function declaring the contract. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">r</emphasis></computeroutput> 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 <computeroutput>return</computeroutput> statement. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">obj</emphasis></computeroutput> is the object <computeroutput>this</computeroutput> from the scope of the enclosing public function declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>). </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">internal_var</emphasis></computeroutput> 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).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.public_functions"> Public Functions</link>, <link linkend="boost_contract.tutorial.virtual_public_functions">
|
|
Virtual Public Functions</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
<refentry xmlns:xi="http://www.w3.org/2001/XInclude" id="BOOST_CO_idm45028038483696"><refmeta><refentrytitle>Macro BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</refentrytitle><manvolnum>3</manvolnum></refmeta><refnamediv><refname>BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</refname><refpurpose>Program contracts that can be completely disabled at compile-time for public function overrides. </refpurpose></refnamediv><refsynopsisdiv><synopsis><phrase role="comment">// In header: <<link linkend="header.boost.contract_macro_hpp">boost/contract_macro.hpp</link>>
|
|
|
|
</phrase>BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(...)</synopsis></refsynopsisdiv><refsect1><title>Description</title><para>This is used together with <computeroutput><link linkend="BOOST_CO_idm45028038745840">BOOST_CONTRACT_PRECONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CO_idm45028038734160">BOOST_CONTRACT_POSTCONDITION</link></computeroutput>, <computeroutput><link linkend="BOOST_CONTRACT_EXCEPT">BOOST_CONTRACT_EXCEPT</link></computeroutput>, and <computeroutput><link linkend="BOOST_CONTRACT_OLD">BOOST_CONTRACT_OLD</link></computeroutput> 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):</para><para><programlisting><phrase role="keyword">class</phrase> <phrase role="identifier">u</phrase>
|
|
<phrase role="preprocessor">#define</phrase> <phrase role="identifier">BASES</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">constructor_precondition</phrase><phrase role="special"><</phrase><phrase role="identifier">u</phrase><phrase role="special">></phrase><phrase role="special">,</phrase> \
|
|
<phrase role="keyword">public</phrase> <phrase role="identifier">b</phrase><phrase role="special">,</phrase> <phrase role="keyword">private</phrase> <phrase role="identifier">w</phrase>
|
|
<phrase role="special">:</phrase> <phrase role="identifier">BASES</phrase>
|
|
<phrase role="special">{</phrase>
|
|
<phrase role="keyword">friend</phrase> <phrase role="keyword">class</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">access</phrase><phrase role="special">;</phrase>
|
|
|
|
<phrase role="keyword">typedef</phrase> <phrase role="identifier">BOOST_CONTRACT_BASE_TYPES</phrase><phrase role="special">(</phrase><phrase role="identifier">BASES</phrase><phrase role="special">)</phrase> <phrase role="identifier">base_types</phrase><phrase role="special">;</phrase>
|
|
<phrase role="preprocessor">#undef</phrase> <phrase role="identifier">BASES</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_INVARIANT</phrase><phrase role="special">(</phrase><phrase role="special">{</phrase> <phrase role="comment">// Optional (as for static and volatile).</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="identifier">BOOST_CONTRACT_OVERRIDES</phrase><phrase role="special">(</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="identifier">g</phrase><phrase role="special">)</phrase>
|
|
|
|
<phrase role="keyword">public</phrase><phrase role="special">:</phrase>
|
|
<phrase role="comment">// Override from `b::f`, and void.</phrase>
|
|
<phrase role="keyword">void</phrase> <phrase role="identifier">f</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">override_f</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">u</phrase><phrase role="special">::</phrase><phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PRECONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_EXCEPT</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body.</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="comment">// Override from `b::g`, and void.</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">g</phrase><phrase role="special">(</phrase><phrase role="identifier">t_1</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">t_n</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">,</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">virtual_</phrase><phrase role="special">*</phrase> <phrase role="identifier">v</phrase> <phrase role="special">=</phrase> <phrase role="number">0</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase>
|
|
<phrase role="identifier">t</phrase> <phrase role="identifier">result</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD_PTR</phrase><phrase role="special">(</phrase><phrase role="identifier">old_type</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase><phrase role="identifier">old_var</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</phrase><phrase role="special">(</phrase><phrase role="identifier">override_g</phrase><phrase role="special">)</phrase><phrase role="special">(</phrase>
|
|
<phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">result</phrase><phrase role="special">,</phrase> <phrase role="special">&</phrase><phrase role="identifier">u</phrase><phrase role="special">::</phrase><phrase role="identifier">g</phrase><phrase role="special">,</phrase> <phrase role="keyword">this</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_1</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">,</phrase> <phrase role="identifier">a_n</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_OLD</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional.</phrase>
|
|
<phrase role="identifier">old_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">BOOST_CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">old_expr</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_POSTCONDITION</phrase><phrase role="special">(</phrase><phrase role="special">[</phrase><phrase role="special">&</phrase><phrase role="special">]</phrase> <phrase role="special">(</phrase><phrase role="identifier">t</phrase> <phrase role="keyword">const</phrase><phrase role="special">&</phrase> <phrase role="identifier">result</phrase><phrase role="special">)</phrase> <phrase role="special">{</phrase> <phrase role="comment">// Optional</phrase>
|
|
<phrase role="identifier">BOOST_CONTRACT_ASSERT</phrase><phrase role="special">(</phrase><phrase role="special">...</phrase><phrase role="special">)</phrase><phrase role="special">;</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">;</phrase> <phrase role="comment">// Trailing `;` is required.</phrase>
|
|
|
|
<phrase role="special">...</phrase> <phrase role="comment">// Function body (use `return result = return_expr`).</phrase>
|
|
<phrase role="special">}</phrase>
|
|
|
|
<phrase role="special">...</phrase>
|
|
<phrase role="special">}</phrase><phrase role="special">;</phrase>
|
|
</programlisting></para><para>Public function overrides should always use <computeroutput><link linkend="BOOST_CO_idm45028038483696">BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE</link></computeroutput> otherwise this library will not be able to correctly use it for subcontracting.</para><para>This is an overloaded variadic macro and it can be used in the following different ways (note that no code is generated when <computeroutput><link linkend="BOOST_CO_idm45028039583904">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</link></computeroutput> is defined).</para><para>1. <computeroutput>BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_type)(v, f, obj, ...)</computeroutput> expands to code equivalent to the following (for public function overrides that return void):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase>
|
|
<phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">override_type</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="identifier">obj</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>2. <computeroutput>BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(override_type)(v, r, f, obj, ...)</computeroutput> expands to code equivalent to the following (for public function overrides that do not return void):</para><para><programlisting><phrase role="preprocessor">#ifndef</phrase> <phrase role="identifier">BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS</phrase>
|
|
<phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase><phrase role="identifier">check</phrase> <phrase role="identifier">internal_var</phrase> <phrase role="special">=</phrase> <phrase role="identifier">boost</phrase><phrase role="special">::</phrase><phrase role="identifier">contract</phrase><phrase role="special">::</phrase>
|
|
<phrase role="identifier">public_function</phrase><phrase role="special"><</phrase><phrase role="identifier">override_type</phrase><phrase role="special">></phrase><phrase role="special">(</phrase><phrase role="identifier">v</phrase><phrase role="special">,</phrase> <phrase role="identifier">r</phrase><phrase role="special">,</phrase> <phrase role="identifier">f</phrase><phrase role="special">,</phrase> <phrase role="identifier">obj</phrase><phrase role="special">,</phrase> <phrase role="special">...</phrase><phrase role="special">)</phrase>
|
|
<phrase role="preprocessor">#endif</phrase>
|
|
</programlisting></para><para>Where (these are all variadic macro parameters so they can contain commas not protected by round parenthesis):</para><para><itemizedlist>
|
|
<listitem><para><computeroutput><emphasis role="bold">override_type</emphasis></computeroutput> is the type <computeroutput>override_<emphasis>function-name</emphasis></computeroutput> declared using the <computeroutput><link linkend="BOOST_CONTRACT_OVERRIDE">BOOST_CONTRACT_OVERRIDE</link></computeroutput> or related macros. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">v</emphasis></computeroutput> is the extra parameter of type <computeroutput><link linkend="boost.contract.virtual_">boost::contract::virtual_</link></computeroutput><computeroutput>*</computeroutput> and default value <computeroutput>0</computeroutput> from the enclosing virtual public function declaring the contract. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">r</emphasis></computeroutput> 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 <computeroutput>return</computeroutput> statement. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">f</emphasis></computeroutput> is a pointer to the enclosing public function override declaring the contract. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">obj</emphasis></computeroutput> is the object <computeroutput>this</computeroutput> from the scope of the enclosing public function declaring the contract. This object might be mutable, <computeroutput>const</computeroutput>, <computeroutput>volatile</computeroutput>, or <computeroutput>const volatile</computeroutput> depending on the cv-qualifier of the enclosing function (volatile public functions will check volatile class invariants, see <link linkend="boost_contract.extras.volatile_public_functions">
|
|
Volatile Public Functions</link>). </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">...</emphasis></computeroutput> 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 <computeroutput>v</computeroutput>. </para>
|
|
</listitem>
|
|
<listitem><para><computeroutput><emphasis role="bold">internal_var</emphasis></computeroutput> 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).</para>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<para><emphasis role="bold">See Also:</emphasis><para> <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">
|
|
Disable Contract Compilation</link>, <link linkend="boost_contract.tutorial.public_function_overrides__subcontracting_">
|
|
Public Function Overrides</link> </para>
|
|
</para>
|
|
</para></refsect1></refentry>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.release_notes">
|
|
<title><link linkend="boost_contract.release_notes">Release Notes</link></title>
|
|
<para>
|
|
This section contains notes on all releases of this library (from the latest
|
|
to the oldest).
|
|
</para>
|
|
<section id="boost_contract.release_notes.release_1_0_1">
|
|
<title><link linkend="boost_contract.release_notes.release_1_0_1">Release 1.0.1</link></title>
|
|
<para>
|
|
September 12, 2019
|
|
</para>
|
|
<para>
|
|
Cleanups and small fixes:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uncaught_exceptions</phrase></computeroutput> on C++17 compilers
|
|
onward (instead of the now obsolete <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">uncaught_exception</phrase></computeroutput>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Fixed a few warnings.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed linking to Boost.System (Boost.System is now a header-only library).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added this library to Boost's Continuous Integration (CI).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Fixed documentation typos.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released files are part of <ulink url="https://www.boost.org/users/history/">Boost
|
|
1.72.0</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_1_0_0">
|
|
<title><link linkend="boost_contract.release_notes.release_1_0_0">Release 1.0.0</link></title>
|
|
<para>
|
|
January 6, 2018 (Il Giorno della Befana)
|
|
</para>
|
|
<para>
|
|
First Boost release:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Fixed all tests to pass as Boost regression tests (and marked those that
|
|
are expected to fail on some specific compiler/platform versions).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Adjusted build scripts to work within Boost libraries.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Updated documentation to fit as a Boost library.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released files are part of <ulink url="https://www.boost.org/users/history/">Boost
|
|
1.67.0</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_5_0">
|
|
<title><link linkend="boost_contract.release_notes.release_0_5_0">Release 0.5.0</link></title>
|
|
<para>
|
|
September 2, 2017
|
|
</para>
|
|
<para>
|
|
Contracts without the macros:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Using plain C++ code instead of macros to program contracts.
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
The removed macros very hard to use because they required programmers
|
|
to learn a domain-specific embedded language that replaced the
|
|
usual C++ syntax for declaring functions and classes.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
The removed macros also made the code less readable, increased
|
|
compilation time (because of extensive preprocessor meta-programming),
|
|
and gave cryptic compiler errors.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
However, the removed macros more correctly specified contracts
|
|
in code declarations instead of definitions, and they completely
|
|
removed extra code when contracts were disabled (both of those
|
|
can be done by the current version of this library but at the cost
|
|
of manually writing some boiler-plate code which was previous automatically
|
|
handled by the macros instead, see <link linkend="boost_contract.extras.separate_body_implementation">Separate
|
|
Body Implementation</link> and <link linkend="boost_contract.extras.disable_contract_compilation__macro_interface_">Disable
|
|
Contract Compilation</link>).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Simplified the library by removing some extra features that were not
|
|
directly related to contract programming, specifically:
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Removed loop variants (because these are rarely if ever used).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed named and deduced parameters (because these can be programmed
|
|
directly using <ulink url="http://www.boost.org/doc/libs/release/libs/parameter/doc/html/index.html">Boost.Parameter</ulink>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed concepts (because these can be programmed directly using
|
|
<ulink url="http://www.boost.org/doc/libs/1_65_0/libs/concept_check/concept_check.htm">Boost.ConceptCheck</ulink>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed emulation of virtual specifiers <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">final</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">new</phrase></computeroutput> (because <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">override</phrase></computeroutput>
|
|
can be programmed directly using C++11, while <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">final</phrase></computeroutput>
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">new</phrase></computeroutput> are rarely
|
|
if ever used).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">static_assert</phrase></computeroutput>
|
|
emulation (because this can be programmed directly using C++11,
|
|
or using <ulink url="http://www.boost.org/doc/libs/1_65_0/doc/html/boost_staticassert.html">Boost.StaticAssert</ulink>).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Ensuring that old values and return values are copied only once even
|
|
when subcontracting is used with multiple inheritance.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Improved template meta-programming algorithm that searches the inheritance
|
|
tree for subcontracting when multiple inheritance is used.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Exception specifications and function-try blocks apply also to exceptions
|
|
thrown by the contracts, and not just to exceptions thrown by the body.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added contracts for exception guarantees (using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">.</phrase><phrase role="identifier">except</phrase><phrase role="special">(...)</phrase></computeroutput>,
|
|
etc.).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added predefined assertion levels for "audit" and "axiom".
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">call_if</phrase></computeroutput> and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">condition_if</phrase></computeroutput> (assertions requirements
|
|
were supported also by previous revisions of this library but they were
|
|
handled by internal code generated by the contract macros).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.5.0">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_4_1">
|
|
<title><link linkend="boost_contract.release_notes.release_0_4_1">Release 0.4.1</link></title>
|
|
<note>
|
|
<para>
|
|
This revision of the library passed Boost formal review and it was accepted
|
|
into the Boost libraries (see <ulink url="https://groups.google.com/forum/?fromgroups=#!topic/boost-list/jQ7OjAmos_Y">https://groups.google.com/forum/?fromgroups=#!topic/boost-list/jQ7OjAmos_Y</ulink>).
|
|
</para>
|
|
</note>
|
|
<para>
|
|
August 20, 2012
|
|
</para>
|
|
<para>
|
|
Accepted into Boost:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Using non-fix-spaced font in Full Table of Contents section.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added a couple of notes to the documentation.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Changed <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">CONTRACT_MEMBER_BODY</phrase><phrase role="special">(</phrase><phrase role="identifier">class_type</phrase><phrase role="special">,</phrase> <phrase role="identifier">function_name</phrase><phrase role="special">)</phrase></computeroutput> to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">class_type</phrase><phrase role="special">::</phrase><phrase role="identifier">CONTRACT_MEMBER_BODY</phrase><phrase role="special">(</phrase><phrase role="identifier">function_name</phrase><phrase role="special">)</phrase></computeroutput> so the macro can also be used to declare
|
|
derived classes avoiding using the library syntax even when the base
|
|
class has contracts.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.4.1">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_4_0">
|
|
<title><link linkend="boost_contract.release_notes.release_0_4_0">Release 0.4.0</link></title>
|
|
<para>
|
|
June 4, 2012
|
|
</para>
|
|
<para>
|
|
Simplified syntax, added named parameters and concepts:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Simplified syntax by reducing extra parenthesis to the bare necessary
|
|
minimum (using some of the preprocessor parsing techniques originally
|
|
introduced by Boost.LocalFunction).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Postcondition old values only copy the old-of expression (e.g., copy
|
|
just vector size instead of entire vector). This improves performance
|
|
and introduces the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">ConstantCopyConstructible</phrase></computeroutput>
|
|
requirement just for the old value expression type (e.g., a vector might
|
|
not be copyable while its size always is because it is an integral type).
|
|
Removed the <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">copyable</phrase></computeroutput>
|
|
tag.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Body defined outside the macros (so compiler-errors for definitions retain
|
|
their usual meaning).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">CONTRACT_CLASS</phrase></computeroutput>
|
|
macro and removed the need to duplicate declaration elements (do not
|
|
repeat function declaration, do not repeat class name in function declaration,
|
|
etc).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Using <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">_TPL</phrase></computeroutput> macros so
|
|
to reduce compile-time (instead of internally making all templates contract
|
|
functions so to use <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">typename</phrase></computeroutput>
|
|
freely).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Overloading no longer requires unique parameter names.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added C++11-like virtual specifiers.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added constant assertions plus constant-expressions for select assertion
|
|
if-conditions and for loop variants.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added named and deduced parameters.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added concept checking.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed the interface to use the library without the macro (programmers
|
|
were required to write too much boiler-plate code for the non-macro interface
|
|
to be actually usable, plus supporting both the macro and non-macro interfaces
|
|
limited what the macros could do).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.4.0">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_3_490">
|
|
<title><link linkend="boost_contract.release_notes.release_0_3_490">Release
|
|
0.3.490</link></title>
|
|
<para>
|
|
March 7, 2010
|
|
</para>
|
|
<para>
|
|
Support for most/all C++ constructs:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Added support and examples for <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">volatile</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">auto</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">explicit</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">export</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">extern</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">friend</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">inline</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">struct</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">throw</phrase></computeroutput> (for exception specifications).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Documented that <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">union</phrase></computeroutput> cannot
|
|
be contracted.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.3.490">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_3_469">
|
|
<title><link linkend="boost_contract.release_notes.release_0_3_469">Release
|
|
0.3.469</link></title>
|
|
<para>
|
|
February 21, 2010
|
|
</para>
|
|
<para>
|
|
Support for most/all contract programming features:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Removed use of <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">self</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">variable</phrase><phrase role="special">.</phrase><phrase role="identifier">now</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">variable</phrase><phrase role="special">.</phrase><phrase role="identifier">old</phrase></computeroutput>
|
|
in writing contracts. Object <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="keyword">this</phrase></computeroutput>
|
|
and variables are now accessed as usual in member functions. <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">CONTRACT_OLDOF</phrase><phrase role="special">(</phrase><phrase role="identifier">variable</phrase><phrase role="special">)</phrase></computeroutput>
|
|
is used to access old values in postconditions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">precondition</phrase><phrase role="special">)</phrase></computeroutput>, <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">)</phrase></computeroutput>,
|
|
and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">body</phrase><phrase role="special">)</phrase></computeroutput> to specify contracts within the function
|
|
signature sequence. If no preconditions then <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">precondition</phrase><phrase role="special">)</phrase>
|
|
<phrase role="special">({...})</phrase></computeroutput> is simply omitted from
|
|
the sequence (same for postconditions, body is mandatory instead). For
|
|
non-void functions, users can name the result argument with <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">)</phrase> <phrase role="special">(</phrase><phrase role="identifier">result</phrase><phrase role="special">-</phrase><phrase role="identifier">name</phrase><phrase role="special">)</phrase> <phrase role="special">({...})</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Changed contract class template to use same syntax as Boost.Function
|
|
(i.e., <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">F</phrase></computeroutput> function type).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added support for free functions and static member functions.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added support for subcontracting with multiple inheritance.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added static class invariants which are always checked (also at constructors
|
|
entry, destructor exit, and by static member functions).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added block invariants and Eiffel-like loop variants.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added handlers to customize action on contract failure (default to <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="identifier">std</phrase><phrase role="special">::</phrase><phrase role="identifier">terminate</phrase><phrase role="special">()</phrase></computeroutput>).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Removed feature for automatic contract documentation using Doxygen (this
|
|
is not compatible with added <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">precondition</phrase><phrase role="special">)</phrase></computeroutput>,
|
|
<computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">postcondition</phrase><phrase role="special">)</phrase></computeroutput>, and <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">(</phrase><phrase role="identifier">body</phrase><phrase role="special">)</phrase></computeroutput>
|
|
because Doxygen preprocessor is not capable to handle Boost.Preprocessor
|
|
sequences).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Rewritten entire documentation (now using Boost.QuickBook instead of
|
|
Doxygen).
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.3.469">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_2_190">
|
|
<title><link linkend="boost_contract.release_notes.release_0_2_190">Release
|
|
0.2.190</link></title>
|
|
<para>
|
|
November 21, 2009
|
|
</para>
|
|
<para>
|
|
Compiled on both GCC and MSVC:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Compiled using both GCC (Linux and Cygwin) and MSVC (Windows XP).
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Required to use void to specify empty function argument list. This is
|
|
to comply with C++03 standard that does not allow to pass empty macro
|
|
parameters so it does not support empty preprocessor sequences <computeroutput xmlns:xi="http://www.w3.org/2001/XInclude"><phrase role="special">()</phrase></computeroutput>.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.2.190">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_1_126">
|
|
<title><link linkend="boost_contract.release_notes.release_0_1_126">Release
|
|
0.1.126</link></title>
|
|
<para>
|
|
June 17, 2009
|
|
</para>
|
|
<para>
|
|
Added documentation:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Completed first documentation draft.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.1.126">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_1_55">
|
|
<title><link linkend="boost_contract.release_notes.release_0_1_55">Release
|
|
0.1.55</link></title>
|
|
<para>
|
|
April 19, 2009
|
|
</para>
|
|
<para>
|
|
Cleaned-up first release:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
Reorganized files to cleanup root directory.
|
|
</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>
|
|
Added installation program.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.1.55">files</ulink>.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.release_notes.release_0_1_50">
|
|
<title><link linkend="boost_contract.release_notes.release_0_1_50">Release
|
|
0.1.50</link></title>
|
|
<para>
|
|
April 19, 2009
|
|
</para>
|
|
<para>
|
|
First release:
|
|
</para>
|
|
<orderedlist inheritnum="ignore" continuation="restarts">
|
|
<listitem>
|
|
<simpara>
|
|
First public release.
|
|
</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<para>
|
|
Released <ulink url="https://github.com/boostorg/contract/releases/tag/v0.1.50">files</ulink>.
|
|
</para>
|
|
</section>
|
|
</section>
|
|
<section id="boost_contract.bibliography">
|
|
<title><link linkend="boost_contract.bibliography">Bibliography</link></title>
|
|
<para>
|
|
This section lists all references consulted while designing and developing
|
|
this library.
|
|
</para>
|
|
<para>
|
|
<anchor id="Andrzej13_anchor"/>[Andrzej13] A. Krzemienski. <ulink url="https://akrzemi1.wordpress.com/2013/01/04/preconditions-part-i/"><emphasis>Andrzej's
|
|
C++ blog: Preconditions</emphasis></ulink>. 2013.
|
|
</para>
|
|
<para>
|
|
<anchor id="Bright04_anchor"/>[Bright04] W. Bright. <ulink url="https://dlang.org/spec/contracts.html"><emphasis>Contract
|
|
Programming for the D Programming Language</emphasis></ulink>. 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="Bright04b_anchor"/>[Bright04b] W. Bright. <ulink url="http://www.digitalmars.com/ctg/contract.html"><emphasis>Contract
|
|
Programming for the Digital Mars C++ Compiler</emphasis></ulink>. 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="C2_anchor"/>[C2] Aechmea. <ulink url="http://www.programmersheaven.com/app/news/DisplayNews.aspx?NewsID=3843"><emphasis>C^2
|
|
Contract Programming add-on for C++</emphasis></ulink>. 2005.
|
|
</para>
|
|
<para>
|
|
<anchor id="Chrome_anchor"/>[Chrome] RemObjects. <ulink url="http://blogs.remobjects.com/blogs/mh/2008/05/01/p216"><emphasis>Chrome:
|
|
Contract Programming for Object Pascal in .NET</emphasis></ulink>. 2002.
|
|
</para>
|
|
<para>
|
|
<anchor id="Clarke06_anchor"/>[Clarke06] L. A. Clarke and D. S. Rosenblum.
|
|
<ulink url="http://discovery.ucl.ac.uk/4991/1/4991.pdf"><emphasis>A Historical
|
|
Perspective on Runtime Assertion Checking in Software Development</emphasis></ulink>.
|
|
Newsletter ACM SIGSOFT Software Engineering Notes, 2006.
|
|
</para>
|
|
<para>
|
|
<anchor id="Cline90_anchor"/>[Cline90] M. Cline and D. Lea. <emphasis>The Behaviour
|
|
of C++ Classes</emphasis> and <ulink url="http://surface.syr.edu/cgi/viewcontent.cgi?article=1116&context=eecs"><emphasis>Using
|
|
Annotated C++</emphasis></ulink>. Proc. of the Symposium on Object Oriented
|
|
Programming Emphasizing Practical Applications, Maris College, 1990.
|
|
</para>
|
|
<para>
|
|
<anchor id="Ellis90_anchor"/>[Ellis90] M. A. Ellis and B. Stroustrup. <emphasis>The
|
|
Annotated C++ Reference Manual</emphasis>. ANSI Base Document, Addison Wesley,
|
|
1990.
|
|
</para>
|
|
<para>
|
|
<anchor id="Gautron92_anchor"/>[Gautron92] P. Gautron. <emphasis>An Assertion
|
|
Mechanism Based on Exceptions</emphasis>. Fourth C++ Technical Conference,
|
|
1992.
|
|
</para>
|
|
<para>
|
|
<anchor id="Hoare73_anchor"/>[Hoare73] C. A. R. Hoare. <emphasis>Hints on Programming
|
|
Language Design</emphasis>. Stanford University Artificial Intelligence memo
|
|
AIM-224/STAN-CS-73-403, pages 193-216, 1973.
|
|
</para>
|
|
<para>
|
|
<anchor id="CodeContracts_anchor"/>[CodeContracts] Microsoft Research. <ulink url="http://research.microsoft.com/en-us/projects/contracts/"><emphasis>Code
|
|
Contracts: Design-By-Contract Programming for All .NET Programming Languages</emphasis></ulink>.
|
|
2012.
|
|
</para>
|
|
<para>
|
|
<anchor id="iContract_anchor"/>[iContract] O. Enseling. <ulink url="http://www.javaworld.com/javaworld/jw-02-2001/jw-0216-cooltools.html"><emphasis>iContract:
|
|
Contract Programming for Java</emphasis></ulink>. 2001.
|
|
</para>
|
|
<para>
|
|
<anchor id="Jcontract_anchor"/>[Jcontract] Parasoft. <ulink url="http://www.parasoft.com/jsp/products/article.jsp?label=product_info_Jcontract"><emphasis>Jcontract:
|
|
Contract Programming for Java</emphasis></ulink>.
|
|
</para>
|
|
<para>
|
|
<anchor id="Lindrud04_anchor"/>[Lindrud04] J. Lindrud. <ulink url="http://www.codeproject.com/Articles/8293/Design-by-Contract-in-C"><emphasis>Design
|
|
by Contract in C++</emphasis></ulink>. 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="Maley99_anchor"/>[Maley99] D. Maley and I. Spence. <ulink url="http://www.computer.org/portal/web/csdl/doi/10.1109/TOOLS.1999.779000"><emphasis>Emulating
|
|
Design by Contract in C++</emphasis></ulink>. Proceedings of TOOLS, IEEE Computer
|
|
Society, 1999.
|
|
</para>
|
|
<para>
|
|
<anchor id="Meyer97_anchor"/>[Meyer97] B. Meyer. <emphasis>Object Oriented
|
|
Software Construction</emphasis>. Prentice-Hall, 2nd edition, 1997.
|
|
</para>
|
|
<para>
|
|
<anchor id="Mitchell02_anchor"/>[Mitchell02] R. Mitchell and J. McKim. <emphasis>Design
|
|
by Contract, by Example</emphasis>. Addison-Wesley, 2002.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1613_anchor"/>[N1613] T. Ottosen. <ulink url="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1613.pdf"><emphasis>Proposal
|
|
to add Design by Contract to C++</emphasis></ulink>. The C++ Standards Committee,
|
|
N1613, 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1653_anchor"/>[N1653] C. Nelson. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm"><emphasis>Working
|
|
draft changes for C99 preprocessor synchronization</emphasis></ulink>. C++
|
|
Standards Committee, N1653, 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1669_anchor"/>[N1669] T. Ottosen. <ulink url="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1669.html"><emphasis>Proposal
|
|
to add Contract Programming to C++ (revision 1)</emphasis></ulink>. The C++
|
|
Standards Committee, N1669, 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1773_anchor"/>[N1773] D. Abrahams, L. Crowl, T. Ottosen, and J.
|
|
Widman. <ulink url="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1773.html"><emphasis>Proposal
|
|
to add Contract Programming to C++ (revision 2)</emphasis></ulink>. The C++
|
|
Standards Committee, N1773, 2005.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1866_anchor"/>[N1866] L. Crowl and T. Ottosen. <ulink url="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1866.html"><emphasis>Proposal
|
|
to add Contract Programming to C++ (revision 3)</emphasis></ulink>. The C++
|
|
Standards Committee, N1866, 2005.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1895_anchor"/>[N1895] H. Sutter and F. Glassborow. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1895.pdf"><emphasis>Delegating
|
|
Constructors (revision 2)</emphasis></ulink>. C++ Standards Committee, N1895,
|
|
2005.
|
|
</para>
|
|
<para>
|
|
<anchor id="N1962_anchor"/>[N1962] L. Crowl and T. Ottosen. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1962.html"><emphasis>Proposal
|
|
to add Contract Programming to C++ (revision 4)</emphasis></ulink>. The C++
|
|
Standards Committee, N1962, 2006.
|
|
</para>
|
|
<para>
|
|
<anchor id="N2081_anchor"/>[N2081] D. Gregor and B. Stroustrup. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2081.pdf"><emphasis>Concepts
|
|
(revision 1)</emphasis></ulink>. The C++ Standards Committee, N2081, 2006.
|
|
</para>
|
|
<para>
|
|
<anchor id="N2887_anchor"/>[N2887] G. Dos Reis, B. Stroustrup, and A. Meredith.
|
|
<ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2887.pdf"><emphasis>Axioms:
|
|
Semantics Aspects of C++ Concepts</emphasis></ulink>. The C++ Standards Committee,
|
|
N2887, 2009.
|
|
</para>
|
|
<para>
|
|
<anchor id="N2914_anchor"/>[N2914] P. Becker. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2914.pdf"><emphasis>Working
|
|
Draft, Standard for Programming Language C++</emphasis></ulink>. The C++ Standards
|
|
Committee, N2914, 2009.
|
|
</para>
|
|
<para>
|
|
<anchor id="N2906_anchor"/>[N2906] B. Stroustrup. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2906.pdf"><emphasis>Simplifying
|
|
the sue of concepts</emphasis></ulink>. The C++ Standards Committee, N2906,
|
|
2009.
|
|
</para>
|
|
<para>
|
|
<anchor id="N3248_anchor"/>[N3248] J. Lakos. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3248.pdf"><emphasis><literal moreinfo="none">noexcept</literal>
|
|
Prevents Library Validation</emphasis></ulink>. The C++ Standards Committee,
|
|
N3248, 2011.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4154_anchor"/>[N4154] D. Krauss. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4154.pdf"><emphasis>Operator
|
|
<literal moreinfo="none">assert</literal></emphasis></ulink>. The C++ Standards Committee,
|
|
N4154, 2014.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4160_anchor"/>[N4160] A. Krzemienski. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4160.html"><emphasis>Value
|
|
constraints</emphasis></ulink>. The C++ Standards Committee, N4160, 2014.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4248_anchor"/>[N4248] A. Meredith. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4248.html"><emphasis>Library
|
|
Preconditions are a Language Feature</emphasis></ulink>. The C++ Standards
|
|
Committee, N4248, 2014.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4293_anchor"/>[N4293] J. D. Garcia. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4293.pdf"><emphasis>C++
|
|
language support for contract programming</emphasis></ulink>. The C++ Standards
|
|
Committee, N4293, 2014.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4378_anchor"/>[N4378] J. Lakos, N. Myers, A. Zakharov, and A.
|
|
Beels. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4378.pdf"><emphasis>Language
|
|
Support for Contract Assertions (Revision 10)</emphasis></ulink>. The C++ Standards
|
|
Committee, N4378, 2015.
|
|
</para>
|
|
<para>
|
|
<anchor id="Nana_anchor"/>[Nana] P. J. Maker. <ulink url="https://github.com/pjmaker/nana"><emphasis>GNU
|
|
Nana</emphasis></ulink>. 2014.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4379_anchor"/>[N4378] J. Lakos and N. Myers. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4379.pdf"><emphasis>FAQ
|
|
about Contract Assertions</emphasis></ulink>. The C++ Standards Committee,
|
|
N4379, 2015.
|
|
</para>
|
|
<para>
|
|
<anchor id="N4435_anchor"/>[N4435] W. E. Brown. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4435.pdf"><emphasis>Proposing
|
|
Contract Attributes</emphasis></ulink>. The C++ Standards Committee, N4435,
|
|
2015.
|
|
</para>
|
|
<para>
|
|
<anchor id="P0147_anchor"/>[P0147] L. Crowl. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0147r0.html"><emphasis>The
|
|
Use and Implementation of Contracts</emphasis></ulink>. The C++ Standards Committee,
|
|
P0147R0, 2015.
|
|
</para>
|
|
<para>
|
|
<anchor id="P0166_anchor"/>[P0166] J. D. Garcia. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0166r0.pdf"><emphasis>Three
|
|
interesting questions about contracts</emphasis></ulink>. The C++ Standards
|
|
Committee, P0166R0, 2015.
|
|
</para>
|
|
<para>
|
|
<anchor id="P0246_anchor"/>[P0246] N. Myers. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0246r0.pdf"><emphasis>Criteria
|
|
for Contract Support Merged Proposal</emphasis></ulink>. The C++ Standards
|
|
Committee, P0246R0, 2016.
|
|
</para>
|
|
<para>
|
|
<anchor id="P0287_anchor"/>[P0287] G. Dos Reis, J.D. Garcia, F. Logozzo, M.
|
|
Fahndrich, S. Lahiri. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0287r0.pdf"><emphasis>Simple
|
|
Contracts for C++ (R1)</emphasis></ulink>. The C++ Standards Committee, P0287R0,
|
|
2016.
|
|
</para>
|
|
<para>
|
|
<anchor id="P0380_anchor"/>[P0380] G. Dos Reis, J.D. Garcia, J. Lakos, A. Meredith,
|
|
N. Myers, and B. Stroustrup. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0380r1.pdf"><emphasis>A
|
|
Contract Design</emphasis></ulink>. The C++ Standards Committee, P0380R1, 2016.
|
|
</para>
|
|
<para>
|
|
<anchor id="P0542_anchor"/>[P0542] G. Dos Reis, J.D. Garcia, J. Lakos, A. Meredith,
|
|
N. Myers, and B. Stroustrup. <ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0542r0.html"><emphasis>Support
|
|
for contract based programming in C++</emphasis></ulink>. The C++ Standards
|
|
Committee, P0542R0, 2017.
|
|
</para>
|
|
<para>
|
|
<anchor id="Rosenblum95_anchor"/>[Rosenblum95] D. S. Rosenblum. <ulink url="http://www.cs.toronto.edu/~chechik/courses06/csc410/rosenblum_assert95.pdf"><emphasis>A
|
|
practical Approach to Programming With Assertions</emphasis></ulink>. IEEE
|
|
Transactions on Software Engineering, 1995.
|
|
</para>
|
|
<para>
|
|
<anchor id="SPARKAda_anchor"/>[SPARKAda] Praxis. <ulink url="http://www.praxis-his.com/sparkada/language.asp"><emphasis>SPARKAda
|
|
(Ada-like Language with Contract Programming)</emphasis></ulink>.
|
|
</para>
|
|
<para>
|
|
<anchor id="SpecSharp_anchor"/>[SpecSharp] Microsoft. <ulink url="http://research.microsoft.com/en-us/projects/specsharp/"><emphasis>Spec#
|
|
(C# Extension)</emphasis></ulink>.
|
|
</para>
|
|
<para>
|
|
<anchor id="Stroustrup94_anchor"/>[Stroustrup94] B. Stroustrup. <emphasis>The
|
|
Design and Evolution of C++</emphasis>. Addison Wesley, 1994.
|
|
</para>
|
|
<para>
|
|
<anchor id="Stroustrup13_anchor"/>[Stroustrup13] B. Stroustrup. <emphasis>The
|
|
C++ Programming Language</emphasis>. Addison Wesley, 4th Edition, 2013.
|
|
</para>
|
|
<para>
|
|
<anchor id="Tandin04_anchor"/>[Tandin04] A. Tandin. <ulink url="http://www.codeproject.com/KB/macros/DbC_and_Doxygen.aspx"><emphasis>Design
|
|
by Contract macros for C++ and link to Doxygen</emphasis></ulink>. 2004.
|
|
</para>
|
|
<para>
|
|
<anchor id="Wilson06_anchor"/>[Wilson06] M. Wilson. <ulink url="http://www.artima.com/cppsource/deepspace.html"><emphasis>Contract
|
|
Programming 101 - The Nuclear Reactor and the Deep Space Probe</emphasis></ulink>.
|
|
The C++ Source, 2006.
|
|
</para>
|
|
</section>
|
|
<section id="boost_contract.acknowledgments">
|
|
<title><link linkend="boost_contract.acknowledgments">Acknowledgments</link></title>
|
|
<para>
|
|
This section tries to recognize the contributions of all the different people
|
|
that participated directly or indirectly to the design and development of this
|
|
library.
|
|
</para>
|
|
<para>
|
|
Sincere thanks to my parents for their support with my education and my studies
|
|
in computer science.
|
|
</para>
|
|
<para>
|
|
Many thanks to Andrzej Krzemienski for reviewing early versions of this library
|
|
providing valuable insights and exchanging early ideas on assertion requirements.
|
|
</para>
|
|
<para>
|
|
Many thanks to Vicente J. Botet Escriba for reviewing earlier versions of this
|
|
library providing valuable insights and for suggesting to use a dedicated trait
|
|
to copy old values.
|
|
</para>
|
|
<para>
|
|
Thanks to Steven Watanabe for providing valuable insights on C++, SFINAE, and
|
|
introspection.
|
|
</para>
|
|
<para>
|
|
Thanks to Dave Abrahams for moderating the Boost review of this library.
|
|
</para>
|
|
<para>
|
|
Thanks to Daniel James for his help with incorporating this library files into
|
|
the Boost Git repository.
|
|
</para>
|
|
<para>
|
|
Thanks to James E. King III for integrating this library with Boost's Continuous
|
|
Integration (CI).
|
|
</para>
|
|
<para>
|
|
Thanks to David Maley for sharing source code form his inspiring work on emulating
|
|
contract programming and subcontracting in C++ in <link linkend="Maley99_anchor">[Maley99]</link>.
|
|
</para>
|
|
<para>
|
|
Many thanks to Thorsten Ottosen for his work on the <link linkend="N1962_anchor">[N1962]</link>
|
|
proposal (and its previous revisions) and for clarifying the proposal requirements
|
|
directly with the library authors when needed.
|
|
</para>
|
|
<para>
|
|
Many thanks to Bertrand Meyer for his pioneering and thorough work on contract
|
|
programming in <link linkend="Meyer97_anchor">[Meyer97]</link>.
|
|
</para>
|
|
<para>
|
|
Finally, many thanks to the entire Boost community and <ulink url="http://lists.boost.org">mailing
|
|
list</ulink> for providing valuable comments on this library and great insights
|
|
on the C++ programming language.
|
|
</para>
|
|
</section>
|
|
</chapter>
|