Loki: Loki::ContractChecker< Host, ExceptionPolicy > Class Template Reference

# Loki::ContractChecker< Host, ExceptionPolicy > Class Template Reference

`#include <Checker.h>`

List of all members.

## Public Types
typedef bool(Host::* | Validator )(void) const
| Signature for the validation function.
## Public Member Functions
| ContractChecker (const Host *host, Validator validator, Validator pre=0, Validator post=0)
| ~ContractChecker (void)
bool | Check (void) const

## Detailed Description

### template<class Host, template< class > class ExceptionPolicy>
class Loki::ContractChecker< Host, ExceptionPolicy >

This class determines if a function violated any class invariant, but it also determines if a function fulfills its contract with client code. In the "Design by Contract" paradigm, each function has certain pre-conditions and post-conditions which may differ from the class invariants. This asserts if a check for an invariant fails as well as if any pre- or post-condition fails. It also demonstrate which exception safety level a function provides.

Usage
  1. Implement a function that checks each class invariant. The function must have the signature similar to the Validator type. Something like: "bool Host::IsValid( void ) const;"
     - The function should return true if everything is okay, but false if something is wrong.
     - Or it could assert if anything is wrong.
     - Ideally, it should be private.
  2. Implement similar functions to check for pre-conditions and post-conditions. Functions which verify pre-conditions and post-conditions do not need to check all class invariants, just conditions specific to certain public functions in the host class.
  3. Declare some typedef's inside the class declaration like these. Make one typedef for each exception policy you use. I typedef'ed the CheckForNothing policy as CheckInvariants because even if a function can't provide either the no-throw nor the no-change policies, it should still make sure the object remains in a valid state.
     - typedef Loki::ContractChecker< Host, ::Loki::CheckForNoThrow > CheckForNoThrow;
     - typedef Loki::ContractChecker< Host, ::Loki::CheckForNoChange > CheckForNoChange;
     - typedef Loki::ContractChecker< Host, ::Loki::CheckForEquality > CheckForEquality;
     - typedef Loki::ContractChecker< Host, ::Loki::CheckForNothing > CheckInvariants;
  4. Construct a checker near the top of each member function - except in the validator member function. Pass the this pointer and the address of your validator function into the checker's constructor. You may also pass in pointers to function which check pre- and post-conditions.
     - If the function never throws, then use the CheckForNoThrow policy.
     - If the function never changes any data members, then use CheckForEquality policy.
     - If the function's normal execution flow changes data, but must make sure data remains unchanged when any exceptions occur, then use the CheckForNoChange policy.
     - Otherwise use the CheckInvariants policy.
  5. Recompile a debug version of your program, run the program and all the unit tests, and look for which assertions failed.

## Constructor & Destructor Documentation

template<class Host , template< class > class ExceptionPolicy>

Loki::ContractChecker< Host, ExceptionPolicy >::ContractChecker | ( | const Host * | host,
|  | Validator | validator,
|  | Validator | pre = `0`,
|  | Validator | post = `0` |
| ) |  |  | `[inline]`

The constructor makes sure the host is valid at the time the checker was created, thus insuring the host object was not corrupt from the start.

host Pointer to host object.

validator Pointer to function that checks class invariants.

pre Optional pointer to function that checks pre-conditions.

post Optional pointer to function that checks post-conditions.

References Loki::ContractChecker< Host, ExceptionPolicy >::Check().

Here is the call graph for this function:

template<class Host , template< class > class ExceptionPolicy>

Loki::ContractChecker< Host, ExceptionPolicy >::~ContractChecker | ( | void |  | ) | `[inline]`

The destructor checks if any Host invariants failed, and then calls the ExceptionPolicy's Check function to determine what to do in case of an exception.

References Loki::ContractChecker< Host, ExceptionPolicy >::Check().

Here is the call graph for this function:

## Member Function Documentation

template<class Host , template< class > class ExceptionPolicy>

bool Loki::ContractChecker< Host, ExceptionPolicy >::Check | ( | void |  | ) | const`[inline]`

This first checks the invariants for ContractChecker, and then calls the validator function for the host to make sure no class invariants were broken by the host within the Host's member function body. The host member function can call Check directly to verify the object remains valid at any time. This does not care if the pre- and post-condition validator pointers are null since a host class may pass in NULL pointers for either to indicate the pre-conditions or post-conditions are the same as the overall class invariants.

Referenced by Loki::ContractChecker< Host, ExceptionPolicy >::ContractChecker(), and Loki::ContractChecker< Host, ExceptionPolicy >::~ContractChecker().

The documentation for this class was generated from the following file:

- Checker.h