Source: contracts.h


Annotated List
Files
Globals
Hierarchy
Index
/***************************************************************************
 *
 * File:    contracts.h
 * Created: Sat Dec 4 1999
 * (C) 1999 by David M. <captjay@superlink.net>
 *
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#ifndef CONTRACTS_H
#define CONTRACTS_H

#include <qstring.h>
#include "CustomException.h"

#ifdef USE_CONTRACTS
  #define USE_PRECONDITIONS
  #define USE_POSTCONDITIONS
  #define USE_INVARIANTS
  #define USE_ASSERTIONS
#endif

#ifdef USE_PRECONDITIONS
  void PRECONDFAIL(char*, char*, const int);
#define REQUIRE(x) if (!(x)) PRECONDFAIL(#x, __FILE__, __LINE__);
#else
  #define REQUIRE(x)
#endif

#ifdef USE_POSTCONDITIONS
  void POSTCONDFAIL(char*, char*, const int );
#define ENSURE(x) if (!(x)) POSTCONDFAIL(#x, __FILE__, __LINE__);
#else
  #define ENSURE(x)
#endif

#ifdef USE_ASSERTIONS
  void ASSERTFAIL(char*, char*, const int);

  #define ASSERTION(x) if (!(x)) ASSERTFAIL(#x, __FILE__, __LINE__);
#else
  #define ASSERTION(x)
#endif

#ifdef USE_INVARIANTS
  void INVARIANTFAIL(char*, char*, const int);

  #define INVARIANT(x) if (!(x)) INVARIANTFAIL(#x, __FILE__, __LINE__);
  #define INVARIANTS() this->checkInvariants()
#else
  #define INVARIANT(x)
  #define INVARIANTS()
#endif

/* Definition des exceptions levees par les methodes de contrat */

/**
* @author David M.
* @short Base class for exceptions related to contract theory.
*
* This is the base class for exceptions related to Meyer's contract theory.
* EContractError provides a constructor that allows more precise debugging info,
* such as the line number and the filename from where the error was raised.
*/
class EContractError: public CustomException {
Q_OBJECT
public:
  /**
  * Default constructor. Creates an EContractError object with the message
  * "Contract violation error".
  */
  EContractError();
  /**
  * Construct a new EContractError object from environment data on the location of the
  * contract violation. This constructor is typically used with __FILE__ and __LINE__
  * compiler defines. The parameters must not be 0.
  *
  * @param fileName Name of the file where the error occured.
  * @param line Line where the error occured.
  * @param condition condition that failed (in a string format).
  * @param contract Either of the following constants: PreCondition, PostCondition,
  *                 Invariant or Assertion.
  *
  */
  EContractError(const char* fileName, int line, const char* condition,int contract);
  /**
  * Get the error message of the EContractError object. This method is overloaded to provide
  * meaningful error messages from the values obtained at construction time (file and line of error,
  * condition that was violated, type of contract that was violated).
  *
  * @return QString An error message representing data in the EContractError object.
  */
  virtual QString message() const;

  static const int PreCondition = 1;
  static const int PostCondition = 2;
  static const int Invariant = 4;
  static const int Assertion = 8;

private:
  QString _fileName;
  int _line;
  QString _condition;
  QString _contract;
};

/**
* @author David M.
* @short Exception class representing PreCondition contract errors.
*
* This class is used to represent PreCondition errors in applications.
* It provides constructors that simply call EContractError's constructor.
*
* This class is provided only to provide more specific exception object types.
*
* @see EContractError
*/
class EPreConditionError: public EContractError {
Q_OBJECT
public:
  EPreConditionError();
  EPreConditionError(const char* fileName, int line, const char* condition);
};

/**
* @author David M.
* @short Exception class representing PostCondition contract errors.
*
* This class is used to represent PostCondition errors in applications.
* It provides constructors that simply call EContractError's constructor.
*
* This class is provided only to provide more specific exception object types.
*
* @see EContractError
*/
class EPostConditionError: public EContractError {
Q_OBJECT
public:
  EPostConditionError();
  EPostConditionError(const char* fileName, int line, const char* condition);
};


/**
* @author David M.
* @short Exception class representing Invariant contract errors.
*
* This class is used to represent class Invariant errors in applications.
* It provides constructors that simply call EContractError's constructor.
*
* This class is provided only to provide more specific exception object types.
*
* @see EContractError
*/
class EInvariantError: public EContractError {
Q_OBJECT
public:
  EInvariantError();
  EInvariantError(const char* fileName, int line, const char* condition);
};

/**
* @author David M.
* @short Exception class representing Assertion contract errors.
*
* This class is used to represent Assertion errors in applications.
* It provides constructors that simply call EContractError's constructor.
*
* This class is provided only to provide more specific exception object types.
*
* @see EContractError
*/
class EAssertionError: public EContractError {
Q_OBJECT
public:
  EAssertionError();
  EAssertionError(const char* fileName, int line, const char* condition);
};

#endif



















Generated by: nightsky@centauri on Sat Jan 15 23:06:10 2000, using kdoc 2.0a30.