Boost logo

Boost :

Subject: Re: [boost] [contract] syntax redesign
From: lcaminiti (lorcaminiti_at_[hidden])
Date: 2011-06-15 12:46:17


lcaminiti wrote:
>
> 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 a side-by-side comparison with N1962 syntax:
>
> http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html/index.html#contract__.introduction.an_example
> For a comparison of the entire std::vector (plus Boost.ConceptCheck
> concepts):
>
> http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html/contract__/examples.html
> For a comparison for all Boost.Contract features (plus
> Boost.ConceptCheck concepts and Boost.Parameter named parameters):
>
> http://svn.boost.org/svn/boost/sandbox/contract/libs/contract/doc/html/contract__/tutorial.html#contract__.tutorial.a_fully_working_example
> (Don't pay attention to the rest of the docs that are way out of date.)
>

I was able to implement this syntax. If anyone is curious, I have attached
the complete grammar of the syntax and the macro APIs to inspect the
syntax's meta-traits:

http://boost.2283326.n4.nabble.com/file/n3599946/grammar03.cpp grammar03.cpp

For example, the macro APIs allow to program the following:

#include &lt;contract/detail/preprocessor/traits/func.hpp&gt;
#include &lt;boost/preprocessor/facilities/is_empty.hpp&gt;
#include &lt;boost/preprocessor/logical/compl.hpp&gt;

#define TRAITS_add CONTRACT_DETAIL_PP_FUNC_TRAITS( \
    int (add) ( int x, int y ) \
)

#define TRAITS_push_back CONTRACT_DETAIL_PP_FUNC_TRAITS( \
    public virtual void (push_back) ( (T&) value ) \
)

#define IS_VIRTUAL(func_traits) \
    BOOST_PP_COMPL(BOOST_PP_IS_EMPTY(CONTRACT_DETAIL_PP_FUNC_TRAITS_VIRTUAL(
\
            func_traits)))

IS_VIRTUAL(TRAITS_add) // expand to 0 (not declared virtual)
IS_VIRTUAL(TRAITS_push_back) // expand to (declared virtual)

#undef TRAITS_add
#undef TRAITS_push_back

Traits macros source code at:
http://contractpp.svn.sourceforge.net/viewvc/contractpp/branches/syn/src/contract/detail/preprocessor/traits/

--Lorenzo

--
View this message in context: http://boost.2283326.n4.nabble.com/contract-syntax-redesign-tp3563993p3599946.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

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