Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2003-12-30 09:45:06


Has there been any discussion regarding an invariants library (advanced
assert), or is there one already implemented in boost?

Pseudocode-style implementation:

void boost::invariant( c, err )
{
# if (USE_INVARIANTS)
      if( !c ) throw( std::exception( err );
# endif
}
void boost::precondition( c, err )
{
   boost::invariant( c, err );
}
void boost::postcondition( c, err )
{
   boost::invariant( c, err );
}

bool boost::between( val, lower, upper )
{
   // helper for the mathematical test: lower <= val < upper
   return(( lower <= val ) && ( val < upper ));
}

Example usage:

class mystring
{
   private:
      const char * str;
      int len;
   public:
      const char * getstr() const
      {
         boost::invariant( str != 0, "string is corrupted" );
         return( str );
      }
      void setstr( const char * s, int l )
      {
         boost::precondition( s != 0, "invalid string" );
         boost::precondition( boost::between( l, 0, ::strlen( s )), "invalid
length" );
         str = s;
         len = l;
      }
   public:
      mystring( const char * s, int l ): str( s ), len( l )
      {
         boost::postcondition( str != 0, "invalid string" );
         boost::postcondition( boost::between( len, 0, ::strlen( s )),
"invalid length" );
      }
};

Regards,
Reece

_________________________________________________________________
Tired of 56k? Get a FREE BT Broadband connection
http://www.msn.co.uk/specials/btbroadband


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