Boost logo

Boost :

Subject: Re: [boost] [contract] syntax redesign
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2011-09-02 13:32:19


On Tue, May 31, 2011 at 2:49 PM, Lorenzo Caminiti <lorcaminiti_at_[hidden]> wrote:
> I can eliminate a large number of extra parenthesis based on a few pp
> tricks I've learned in processing Boost.Local's `const bind(type)&
> var`. Plus, I can provide (optional) support for variadics macros so
> to use comma-separated pp tuplets instead of parenthesized pp
> sequences on preprocessors with variadics.
>
> For example, I _think_ (but I've not implemented this yet) that I can
> simplify the syntax like this:
>
> #include <contract.hpp>
> #include <vector>
> #include "myvector/pushable.hpp"
>
> // Wrapper class that adds contracts to std::vector.
> CONTRACT_CLASS(
> template( typename T )
> class (myvector) extends( public pushable<T> ) // Subcontracting.
> ) {
>    CONTRACT_CLASS_INVARIANT(
>        empty() == (size() == 0)
>        // More invariants here (comma separated)...
>    )
>
>    CONTRACT_FUNCTION(
>    public void (push_back)( (const T&) element )
>        precondition(
>            size() < max_size()
>            // More preconditions here (comma separated)...
>        )
>        postcondition(
>            auto old_size = CONTRACT_OLDOF(size()), // Old value.
>            size() == old_size + 1
>            // More postconditions here (comma separated)...
>        )
>    ) {
>        vector_.push_back(element); // Implementation.
>    }
>
>    // Rest of the class here (with more contracts if needed)...
> public:
>    typedef typename std::vector<T>::size_type size_type;
>    size_type size(void) const { return vector_.size(); }
>    size_type max_size(void) const { return vector_.max_size(); }
>    bool empty(void) const { return vector_.empty(); }
>    const T& back(void) const { return vector_.back(); }
> private:
>    std::vector<T> vector_;
> };

For whomever is curious, I have implemented the new Boost.Contract
syntax. Here's large number of examples that now compile with the new
syntax:
    http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html2/contract__/examples.html

Highlights are:
* Side-by-side comparison with N1962 (contracts) and N2081 (concepts) syntax:
    http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html2/contract__/examples.html#contract__.examples.n1962_vector
* I have also added C++0x-like virtual specifiers (final, override,
and new) to Boost.Contract:
    http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html2/contract__/examples.html#contract__.examples.mitchell02_counter
* Side-by-side comparison with Eiffel syntax:
    http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html2/contract__/examples.html#contract__.examples.meyer97_stack4

Comments are always welcome :)

(I still have to implement named parameter support (using
Boost.Parameter behind the scene) but that shouldn't be too hard. The
macros already parse the named parameter syntax using in/out/inout
"keywords".)

--Lorenzo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk