Boost logo

Boost :

From: Daniel Wallin (dalwan01_at_[hidden])
Date: 2004-01-07 17:54:35


Dan W. wrote:
> Major clean-up. Interest? Bug reports? Thoughts? Criticism?
>
> All welcome.

I find this overly complicated. Why do we need first_verify() and
last_verify() for instance? I don't get it. I also don't think adding
members and virtual functions to classes depending on whether or not you
are compiling with debug information is necessarily a good idea.

 From a quick look I also noticed a problem with your code:

   # define _INVARIANTS_CHECKED_ invariants< type >::trigobj TRGBJ(this);
      might be dependent type if in a class template ^^^^^^^

I'm attaching a simplified invariant-guard-thing.

-- 
Daniel Wallin

// Copyright Daniel Wallin 2004. Use, modification and distribution is
// subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <iostream>
#include <cassert>

class invariant_access
{
public:
    template<class T>
    static void check_invariant(T const& self)
    {
        self.invariant();
    }
};

template<class T>
void check_invariant(T const& x)
{
    invariant_access::check_invariant(x);
}

struct invariant_checker {};

template<class T>
struct invariant_checker_impl : invariant_checker
{
    invariant_checker_impl(T const& self_)
        : self(self_)
    {
        try
        {
            check_invariant(self);
        }
        catch (...)
        {
            assert(false);
        }
    }

    ~invariant_checker_impl()
    {
        try
        {
            check_invariant(self);
        }
        catch (...)
        {
            assert(false);
        }
    }

    T const& self;
};

template<class T>
invariant_checker_impl<T> make_invariant_checker(T const& x)
{
    return invariant_checker_impl<T>(x);
}

#ifndef NDEBUG
  #define INVARIANT_CHECK \
      invariant_checker const& _invariant_check = make_invariant_checker(*this)
#else
  #define INVARIANT_CHECK do {} while (false)
#endif

class test
{
public:
    test()
       : a(3)
       , b(3)
    {
        INVARIANT_CHECK;
    }

    void f()
    {
        INVARIANT_CHECK;

        a -= 3;
    }

    void g()
    {
        INVARIANT_CHECK;

        a = 3;
        b = 6;

        f();
    }

private:
    friend void check_invariant(test const& x)
    {
        assert(x.a + x.b > 5);
    }

    int a, b;
};

int main()
{
    test a;

    a.g();
    a.f();
}


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