Boost logo

Boost :

Subject: Re: [boost] [contract] Macro syntax?
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-04-07 20:19:44


On Tue, Apr 6, 2010 at 2:12 PM, vicente.botet <vicente.botet_at_[hidden]> wrote:
> Lorenzo, As the () syntax is quite ugly, I was wondering if adding a contract preprocessing phase CP_PP could be a option to consider.
> Each contract programming line could be prefixed by a comment such as //~//.
>
>    template<typename T>
>    class myvector: public pushable<T> {
>        //~// invariant {
>        //~//     (size() == 0) == empty();
>        //~// }
>
>    public:
>        void push_back(const T& element)
>          //~// precondition {
>          //~//   size() < max_size();
>          //~// }
>          //~// postcondition {
>          //~//   size() == oldof(this)->size() + 1;
>          //~// }
>        {
>           vector_.push_back(element);
>        }
>        ....
>    private:
>        std::vector<T> vector_;
>    };
>
> As any CP sentence is commented with a C++ comment this code could work without CP preprocessing.
>
> The CP_PP could generate #line directives so the original lines are preserved when warning or errors message are reported by the compiler.
>
> The generated file could be generated in a specific contract directory so these files can be sees by the compiler before the original ones. The user will just need to add -Icontract directives.

JContract uses Javadoc comments. Doxygen provides \pre, \post, and
\invariant tags so if a preprocessor was to extract contracts from
code comments it might make sense to use these tags... There is
already a preprocessing-based CP tool for C++ called "C^2" but I
_think_ is not maintained anymore.

If I have some free time (but not likely in the short term), I would
like look into what can be done to generate Boost.Contract code using
Boost.Wave/Spirit. However, while I recognize the ugliness of the
contract macros syntax, my goal and interest were and remain to see
how CP can be implemented within the C++ language itself (without
external preprocessing)...

Thanks,
Lorenzo


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