Changed names to be more descriptive. Updated comments.
git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1171 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
parent
4cc104cf36
commit
e972640ae3
1 changed files with 51 additions and 45 deletions
|
@ -60,13 +60,13 @@ namespace Loki
|
||||||
- and determining which exception safety level a function provides.
|
- and determining which exception safety level a function provides.
|
||||||
|
|
||||||
@par Class & Data Invariants
|
@par Class & Data Invariants
|
||||||
ContractChecker and StaticChecker define invariants as "expressions that are
|
ContractChecker and StaticChecker define invariants as "conditions that are
|
||||||
true for particular data". They call a function which returns true if all data
|
true for particular objects". They call a function which returns true if all
|
||||||
are valid, and returns false if any datum is invalid. This is called the
|
conditions are valid, and returns false if any condition is invalid. This is
|
||||||
validator function, and the host class or function provides a pointer to it.
|
called the validator function, and the host class or function provides a
|
||||||
The validator could also assert for any invariant which fails rather than
|
pointer to it. The validator may assert for any invariant which fails rather
|
||||||
return false. If the validator is a static member function, you can use it
|
than return false. If the validator is a static member function, you can use
|
||||||
with checkers in any function, but especially standalone functions and class
|
it with checkers in any function, but especially standalone functions and class
|
||||||
static functions. If the validator is a non-static member function, you can
|
static functions. If the validator is a non-static member function, you can
|
||||||
use it only within non-static member functions.
|
use it only within non-static member functions.
|
||||||
|
|
||||||
|
@ -92,22 +92,27 @@ namespace Loki
|
||||||
|
|
||||||
@par Provided Exception Policies
|
@par Provided Exception Policies
|
||||||
Loki provides several exception policies for use with ContractChecker.
|
Loki provides several exception policies for use with ContractChecker.
|
||||||
- CheckForNoChangeOrThrow
|
- CheckForNoThrowOrChange
|
||||||
- CheckForNoThrow
|
- CheckForNoThrow
|
||||||
|
- CheckForNoChangeOnThrow
|
||||||
- CheckForNoChange
|
- CheckForNoChange
|
||||||
- CheckForEquality
|
|
||||||
- CheckForNothing
|
- CheckForNothing
|
||||||
Loki also provides these two policies for StaticChecker.
|
Loki also provides these two policies for StaticChecker.
|
||||||
- CheckStaticForNoThrow
|
- CheckStaticForNoThrow
|
||||||
- CheckStaticForNothing
|
- CheckStaticForNothing
|
||||||
|
|
||||||
@par Writing Your Own Policies for ContractChecker
|
@par Reasons to Write Your Own Policies for ContractChecker
|
||||||
Loki provides several exception policies for ContractChecker. These policies
|
Loki provides several exception policies for ContractChecker that assert if an
|
||||||
assert if an object changed or a function threw an exception. If you prefer
|
object changed or a function threw an exception. These are good reasons to
|
||||||
policies that log failures to a file, send an email, file a bug report, pop-up
|
write your own policies:
|
||||||
a message box, notify your unit-test framework, or whatever else, you can
|
- you want to log failures to a file,
|
||||||
easily write your own policies. Please follow these guidelines when writing
|
- you want to create a bug report,
|
||||||
your own policies:
|
- you want to stop your unit-test framework,
|
||||||
|
- your classes don't have copy-constructors or equality operators,
|
||||||
|
- or whatever else.
|
||||||
|
|
||||||
|
@par Guidelines for Writing Your Own Policies for ContractChecker
|
||||||
|
Please follow these guidelines when writing your own policies:
|
||||||
- Each policy class must provide three public functions, a constructor, a destructor, and a Check function.
|
- Each policy class must provide three public functions, a constructor, a destructor, and a Check function.
|
||||||
- The destructor could be implied. (Not actually written, and provided by the compiler.)
|
- The destructor could be implied. (Not actually written, and provided by the compiler.)
|
||||||
- The constructor and Check functions accept a pointer to const instance of the host class.
|
- The constructor and Check functions accept a pointer to const instance of the host class.
|
||||||
|
@ -238,15 +243,16 @@ public:
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
template < class Host, class Memento > class CheckForNoChange;
|
template < class Host, class Memento > class CheckForNoChangeOnThrow;
|
||||||
|
|
||||||
/** @class CheckForNoChange
|
/** @class CheckForNoChangeOnThrow
|
||||||
|
|
||||||
@par Exception Safety Level:
|
@par Exception Safety Level:
|
||||||
This exception-checking policy class for ContractChecker asserts only if a
|
This exception-checking policy class for ContractChecker asserts only if a
|
||||||
copy of the host differs from the host object when an exception occurs. Host
|
copy of the host differs from the host object when an exception occurs. Host
|
||||||
classes can use this policy to show which member functions provide the strong
|
classes can use this policy to show which member functions provide the strong
|
||||||
exception guarantee.
|
exception guarantee. Such functions provide transaction semantics - either the
|
||||||
|
action succeeds or the object rollsback to its previous state.
|
||||||
|
|
||||||
@par Requirements:
|
@par Requirements:
|
||||||
This policy requires hosts to provide both the copy-constructor and the
|
This policy requires hosts to provide both the copy-constructor and the
|
||||||
|
@ -254,11 +260,11 @@ template < class Host, class Memento > class CheckForNoChange;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template < class Host >
|
template < class Host >
|
||||||
class CheckForNoChange< Host, void >
|
class CheckForNoChangeOnThrow< Host, void >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline explicit CheckForNoChange( const Host * host ) :
|
inline explicit CheckForNoChangeOnThrow( const Host * host ) :
|
||||||
m_compare( *host ) {}
|
m_compare( *host ) {}
|
||||||
|
|
||||||
inline bool Check( const Host * host ) const
|
inline bool Check( const Host * host ) const
|
||||||
|
@ -274,11 +280,11 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template < class Host, class Memento >
|
template < class Host, class Memento >
|
||||||
class CheckForNoChange
|
class CheckForNoChangeOnThrow
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline explicit CheckForNoChange( const Host * host ) :
|
inline explicit CheckForNoChangeOnThrow( const Host * host ) :
|
||||||
m_compare( *host ) {}
|
m_compare( *host ) {}
|
||||||
|
|
||||||
inline bool Check( const Host * host ) const
|
inline bool Check( const Host * host ) const
|
||||||
|
@ -295,9 +301,9 @@ private:
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
template < class Host, class Memento > class CheckForNoChangeOrThrow;
|
template < class Host, class Memento > class CheckForNoThrowOrChange;
|
||||||
|
|
||||||
/** @class CheckForNoChangeOrThrow
|
/** @class CheckForNoThrowOrChange
|
||||||
This policy comes in two forms - one uses a memento, and one does not. The
|
This policy comes in two forms - one uses a memento, and one does not. The
|
||||||
memento form does not copy the host object, but stores info about the host in
|
memento form does not copy the host object, but stores info about the host in
|
||||||
a memento for later comparison with the host. The other form copies the host
|
a memento for later comparison with the host. The other form copies the host
|
||||||
|
@ -315,11 +321,11 @@ template < class Host, class Memento > class CheckForNoChangeOrThrow;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template < class Host, class Memento >
|
template < class Host, class Memento >
|
||||||
class CheckForNoChangeOrThrow
|
class CheckForNoThrowOrChange
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline explicit CheckForNoChangeOrThrow( const Host * host ) :
|
inline explicit CheckForNoThrowOrChange( const Host * host ) :
|
||||||
m_compare( *host ) {}
|
m_compare( *host ) {}
|
||||||
|
|
||||||
inline bool Check( const Host * host ) const
|
inline bool Check( const Host * host ) const
|
||||||
|
@ -336,11 +342,11 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template < class Host >
|
template < class Host >
|
||||||
class CheckForNoChangeOrThrow< Host, void >
|
class CheckForNoThrowOrChange< Host, void >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline explicit CheckForNoChangeOrThrow( const Host * host ) :
|
inline explicit CheckForNoThrowOrChange( const Host * host ) :
|
||||||
m_compare( *host ) {}
|
m_compare( *host ) {}
|
||||||
|
|
||||||
inline bool Check( const Host * host ) const
|
inline bool Check( const Host * host ) const
|
||||||
|
@ -358,9 +364,9 @@ private:
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
template < class Host, class Memento > class CheckForEquality;
|
template < class Host, class Memento > class CheckForNoChange;
|
||||||
|
|
||||||
/** @class CheckForEquality
|
/** @class CheckForNoChange
|
||||||
|
|
||||||
@par Exception Safety Level:
|
@par Exception Safety Level:
|
||||||
This exception-checking policy class for ContractChecker asserts if a copy of
|
This exception-checking policy class for ContractChecker asserts if a copy of
|
||||||
|
@ -378,11 +384,11 @@ template < class Host, class Memento > class CheckForEquality;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template < class Host, class Memento >
|
template < class Host, class Memento >
|
||||||
class CheckForEquality
|
class CheckForNoChange
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline explicit CheckForEquality( const Host * host ) :
|
inline explicit CheckForNoChange( const Host * host ) :
|
||||||
m_compare( *host ) {}
|
m_compare( *host ) {}
|
||||||
|
|
||||||
inline bool Check( const Host * host ) const
|
inline bool Check( const Host * host ) const
|
||||||
|
@ -397,11 +403,11 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template < class Host >
|
template < class Host >
|
||||||
class CheckForEquality< Host, void >
|
class CheckForNoChange< Host, void >
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
inline explicit CheckForEquality( const Host * host ) :
|
inline explicit CheckForNoChange( const Host * host ) :
|
||||||
m_compare( *host ) {}
|
m_compare( *host ) {}
|
||||||
|
|
||||||
inline bool Check( const Host * host ) const
|
inline bool Check( const Host * host ) const
|
||||||
|
@ -468,18 +474,18 @@ public:
|
||||||
-# Add one of these lines at the top of various class member functions to
|
-# Add one of these lines at the top of various class member functions to
|
||||||
construct a checker near the top of each public function. You may also pass
|
construct a checker near the top of each public function. You may also pass
|
||||||
in pointers to functions which check pre- and post-conditions.
|
in pointers to functions which check pre- and post-conditions.
|
||||||
- CheckFor::NoChangeOrThrow checker( this, &Host::IsValid );
|
- CheckFor::NoThrowOrChange checker( this, &Host::IsValid );
|
||||||
|
- CheckFor::NoChangeOnThrow checker( this, &Host::IsValid );
|
||||||
- CheckFor::NoThrow checker( this, &Host::IsValid );
|
- CheckFor::NoThrow checker( this, &Host::IsValid );
|
||||||
- CheckFor::NoChange checker( this, &Host::IsValid );
|
- CheckFor::NoChange checker( this, &Host::IsValid );
|
||||||
- CheckFor::Equality checker( this, &Host::IsValid );
|
|
||||||
- CheckFor::Invariants checker( this, &Host::IsValid );
|
- CheckFor::Invariants checker( this, &Host::IsValid );
|
||||||
-# Use these guidelines to decide which policy to use inside which function:
|
-# Use these guidelines to decide which policy to use inside which function:
|
||||||
- If the function never throws, then use the CheckForNoThrow policy.
|
- If the function never throws, then use the CheckForNoThrow policy.
|
||||||
- If the function never changes any data members, then use CheckForEquality
|
- If the function never changes any data members, then use CheckForNoChange
|
||||||
policy.
|
policy.
|
||||||
- If the function's normal execution flow changes data, but must make sure
|
- If the function's normal execution flow changes data, but must make sure
|
||||||
data remains unchanged when any exceptions occur, then use the
|
data remains unchanged when any exceptions occur, then use the
|
||||||
CheckForNoChange policy.
|
CheckForNoChangeOnThrow policy.
|
||||||
- Otherwise use the CheckInvariants policy.
|
- Otherwise use the CheckInvariants policy.
|
||||||
-# Recompile a debug version of your program, run the program and all the unit
|
-# Recompile a debug version of your program, run the program and all the unit
|
||||||
tests, and look for which assertions failed.
|
tests, and look for which assertions failed.
|
||||||
|
@ -569,10 +575,10 @@ private:
|
||||||
/// Pointer to member function that checks Host object's invariants.
|
/// Pointer to member function that checks Host object's invariants.
|
||||||
Validator m_validator;
|
Validator m_validator;
|
||||||
|
|
||||||
/// Pointer to member function that checks Host object's pre-conditions.
|
/// Pointer to member function that checks Host function's pre-conditions.
|
||||||
Validator m_pre;
|
Validator m_pre;
|
||||||
|
|
||||||
/// Pointer to member function that checks Host object's post-conditions.
|
/// Pointer to member function that checks Host function's post-conditions.
|
||||||
Validator m_post;
|
Validator m_post;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -590,10 +596,10 @@ template < class Host, class Memento = void >
|
||||||
struct CheckFor
|
struct CheckFor
|
||||||
{
|
{
|
||||||
// These lines declare checkers for non-static functions in a host class.
|
// These lines declare checkers for non-static functions in a host class.
|
||||||
typedef ContractChecker< Host, CheckForNoChangeOrThrow, Memento > NoChangeOrThrow;
|
typedef ContractChecker< Host, CheckForNoThrowOrChange, Memento > NoThrowOrChange;
|
||||||
|
typedef ContractChecker< Host, CheckForNoChangeOnThrow, Memento > NoChangeOnThrow;
|
||||||
typedef ContractChecker< Host, CheckForNoThrow, Memento > NoThrow;
|
typedef ContractChecker< Host, CheckForNoThrow, Memento > NoThrow;
|
||||||
typedef ContractChecker< Host, CheckForNoChange, Memento > NoChange;
|
typedef ContractChecker< Host, CheckForNoChange, Memento > NoChange;
|
||||||
typedef ContractChecker< Host, CheckForEquality, Memento > Equality;
|
|
||||||
typedef ContractChecker< Host, CheckForNothing, Memento > Invariants;
|
typedef ContractChecker< Host, CheckForNothing, Memento > Invariants;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -745,10 +751,10 @@ private:
|
||||||
/// Pointer to member function that checks Host object's invariants.
|
/// Pointer to member function that checks Host object's invariants.
|
||||||
Validator m_validator;
|
Validator m_validator;
|
||||||
|
|
||||||
/// Pointer to member function that checks Host object's pre-conditions.
|
/// Pointer to member function that checks Host function's pre-conditions.
|
||||||
Validator m_pre;
|
Validator m_pre;
|
||||||
|
|
||||||
/// Pointer to member function that checks Host object's post-conditions.
|
/// Pointer to member function that checks Host function's post-conditions.
|
||||||
Validator m_post;
|
Validator m_post;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue